2018-02-03 06:56:17 +01:00
|
|
|
//
|
|
|
|
// RenameWindowController.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 1/28/18.
|
|
|
|
// Copyright © 2018 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-02-03 07:51:32 +01:00
|
|
|
import AppKit
|
|
|
|
|
2018-02-03 08:00:09 +01:00
|
|
|
final class RenameWindowController: NSWindowController {
|
2018-02-03 07:51:32 +01:00
|
|
|
|
2018-02-03 08:00:09 +01:00
|
|
|
@IBOutlet var renamePrompt: NSTextField!
|
|
|
|
@IBOutlet var newTitleTextField: NSTextField!
|
|
|
|
@IBOutlet var renameButton: NSButton!
|
|
|
|
|
|
|
|
private var originalTitle: String!
|
2018-02-04 03:49:29 +01:00
|
|
|
private var hostWindow: NSWindow!
|
|
|
|
private var callback: ((String?) -> Void)?
|
2018-02-03 08:00:09 +01:00
|
|
|
|
2018-02-04 03:49:29 +01:00
|
|
|
convenience init(originalTitle: String, callback: @escaping ((String?) -> Void)) {
|
2018-02-03 08:00:09 +01:00
|
|
|
|
|
|
|
self.init(windowNibName: NSNib.Name(rawValue: "RenameSheet"))
|
|
|
|
self.originalTitle = originalTitle
|
2018-02-04 03:49:29 +01:00
|
|
|
self.callback = callback
|
2018-02-03 08:00:09 +01:00
|
|
|
}
|
2018-02-04 01:05:59 +01:00
|
|
|
|
|
|
|
override func windowDidLoad() {
|
|
|
|
|
|
|
|
newTitleTextField.stringValue = originalTitle
|
2018-02-04 03:49:29 +01:00
|
|
|
|
|
|
|
let prompt = NSLocalizedString("Rename %@ to:", comment: "Rename sheet")
|
|
|
|
let localizedPrompt = NSString.localizedStringWithFormat(prompt as NSString, originalTitle)
|
|
|
|
renamePrompt.stringValue = localizedPrompt as String
|
|
|
|
|
2018-02-04 01:05:59 +01:00
|
|
|
updateUI()
|
|
|
|
}
|
2018-02-04 03:49:29 +01:00
|
|
|
|
|
|
|
func runSheetOnWindow(_ w: NSWindow) {
|
|
|
|
|
|
|
|
guard let window = window else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hostWindow = w
|
|
|
|
hostWindow.beginSheet(window) { (returnCode: NSApplication.ModalResponse) -> Void in
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction func cancel(_ sender: AnyObject) {
|
|
|
|
|
|
|
|
callback?(nil)
|
|
|
|
hostWindow!.endSheet(window!, returnCode: .cancel)
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func rename(_ sender: AnyObject) {
|
|
|
|
|
|
|
|
callback?(newTitleTextField.stringValue)
|
|
|
|
hostWindow!.endSheet(window!, returnCode: .OK)
|
|
|
|
}
|
|
|
|
|
2018-02-04 01:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension RenameWindowController: NSTextFieldDelegate {
|
|
|
|
|
|
|
|
override func controlTextDidChange(_ obj: Notification) {
|
|
|
|
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension RenameWindowController {
|
|
|
|
|
|
|
|
func updateUI() {
|
|
|
|
|
|
|
|
let newTitle = newTitleTextField.stringValue
|
|
|
|
renameButton.isEnabled = !newTitle.isEmpty && newTitle != originalTitle
|
|
|
|
}
|
2018-02-03 08:00:09 +01:00
|
|
|
}
|