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

82 lines
1.8 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-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!
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
}
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
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)
}
}
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
}