NetNewsWire/Evergreen/MainWindow/Sidebar/Renaming/RenameWindowController.swift

79 lines
2.0 KiB
Swift
Raw Normal View History

//
// RenameWindowController.swift
// Evergreen
//
// Created by Brent Simmons on 1/28/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import AppKit
2018-02-04 06:04:28 +01:00
protocol RenameWindowControllerDelegate {
func renameWindowController(_ windowController: RenameWindowController, didRenameObject: Any, withNewName: String)
}
2018-02-03 08:00:09 +01:00
final class RenameWindowController: NSWindowController {
2018-02-03 08:00:09 +01:00
@IBOutlet var renamePrompt: NSTextField!
@IBOutlet var newTitleTextField: NSTextField!
@IBOutlet var renameButton: NSButton!
2018-02-04 06:04:28 +01:00
private var originalTitle: String?
private var representedObject: Any?
private var delegate: RenameWindowControllerDelegate?
2018-02-03 08:00:09 +01:00
2018-02-04 06:04:28 +01:00
convenience init(originalTitle: String, representedObject: Any, delegate: RenameWindowControllerDelegate) {
2018-02-03 08:00:09 +01:00
self.init(windowNibName: NSNib.Name(rawValue: "RenameSheet"))
self.originalTitle = originalTitle
2018-02-04 06:04:28 +01:00
self.representedObject = representedObject
self.delegate = delegate
2018-02-03 08:00:09 +01:00
}
override func windowDidLoad() {
2018-02-04 06:04:28 +01:00
newTitleTextField.stringValue = originalTitle!
2018-02-04 03:49:29 +01:00
let prompt = NSLocalizedString("Rename %@ to:", comment: "Rename sheet")
2018-02-04 06:04:28 +01:00
let localizedPrompt = NSString.localizedStringWithFormat(prompt as NSString, originalTitle!)
2018-02-04 03:49:29 +01:00
renamePrompt.stringValue = localizedPrompt as String
updateUI()
}
2018-02-04 03:49:29 +01:00
// MARK: Actions
@IBAction func cancel(_ sender: AnyObject) {
2018-02-04 06:04:28 +01:00
window?.sheetParent?.endSheet(window!, returnCode: .cancel)
2018-02-04 03:49:29 +01:00
}
@IBAction func rename(_ sender: AnyObject) {
2018-02-04 06:04:28 +01:00
guard let representedObject = representedObject else {
return
}
delegate?.renameWindowController(self, didRenameObject: representedObject, withNewName: newTitleTextField.stringValue)
window?.sheetParent?.endSheet(window!, returnCode: .OK)
2018-02-04 03:49:29 +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
}