2018-02-03 06:56:17 +01:00
|
|
|
//
|
|
|
|
// RenameWindowController.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2018-02-03 06:56:17 +01:00
|
|
|
//
|
|
|
|
// 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-04 06:04:28 +01:00
|
|
|
protocol RenameWindowControllerDelegate {
|
|
|
|
|
2024-03-21 04:49:15 +01:00
|
|
|
@MainActor func renameWindowController(_ windowController: RenameWindowController, didRenameObject: Any, withNewName: String)
|
2018-02-04 06:04:28 +01:00
|
|
|
}
|
|
|
|
|
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!
|
|
|
|
|
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
|
|
|
|
2018-12-09 21:32:33 +01:00
|
|
|
self.init(windowNibName: NSNib.Name("RenameSheet"))
|
2018-02-03 08:00:09 +01:00
|
|
|
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
|
|
|
}
|
2018-02-04 01:05:59 +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
|
|
|
|
|
2018-02-04 01:05:59 +01:00
|
|
|
updateUI()
|
|
|
|
}
|
2018-02-04 03:49:29 +01:00
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
2018-02-12 03:58:50 +01:00
|
|
|
@IBAction func cancel(_ sender: Any?) {
|
2018-02-04 03:49:29 +01:00
|
|
|
|
2018-02-04 06:04:28 +01:00
|
|
|
window?.sheetParent?.endSheet(window!, returnCode: .cancel)
|
2018-02-04 03:49:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 03:58:50 +01:00
|
|
|
@IBAction func rename(_ sender: Any?) {
|
2018-02-04 03:49:29 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-04 01:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension RenameWindowController: NSTextFieldDelegate {
|
|
|
|
|
2018-12-09 21:32:33 +01:00
|
|
|
func controlTextDidChange(_ obj: Notification) {
|
2018-02-04 01:05:59 +01:00
|
|
|
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension RenameWindowController {
|
|
|
|
|
|
|
|
func updateUI() {
|
|
|
|
|
|
|
|
let newTitle = newTitleTextField.stringValue
|
|
|
|
renameButton.isEnabled = !newTitle.isEmpty && newTitle != originalTitle
|
|
|
|
}
|
2018-02-03 08:00:09 +01:00
|
|
|
}
|