Get rename sheet working, finally.

This commit is contained in:
Brent Simmons 2018-02-03 21:04:28 -08:00
parent ae840b31bc
commit 68d0885b0d
3 changed files with 36 additions and 35 deletions

View File

@ -15,7 +15,7 @@
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Rename" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g">
<window title="Rename" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="424" height="116"/>

View File

@ -67,20 +67,24 @@ extension MainWindowController {
return
}
renameWindowController = RenameWindowController(originalTitle: object.nameForDisplay) { (newTitle) in
guard let newTitle = newTitle else {
return
}
if let feed = object as? Feed {
feed.editedName = newTitle
}
else if let folder = object as? Folder {
folder.name = newTitle
}
self.renameWindowController = nil
renameWindowController = RenameWindowController(originalTitle: object.nameForDisplay, representedObject: object, delegate: self)
guard let renameSheet = renameWindowController?.window else {
return
}
window.beginSheet(renameSheet)
}
}
renameWindowController?.runSheetOnWindow(window)
extension MainWindowController: RenameWindowControllerDelegate {
func renameWindowController(_ windowController: RenameWindowController, didRenameObject object: Any, withNewName name: String) {
if let feed = object as? Feed {
feed.editedName = name
}
else if let folder = object as? Folder {
folder.name = name
}
}
}

View File

@ -8,57 +8,54 @@
import AppKit
protocol RenameWindowControllerDelegate {
func renameWindowController(_ windowController: RenameWindowController, didRenameObject: Any, withNewName: String)
}
final class RenameWindowController: NSWindowController {
@IBOutlet var renamePrompt: NSTextField!
@IBOutlet var newTitleTextField: NSTextField!
@IBOutlet var renameButton: NSButton!
private var originalTitle: String!
private var hostWindow: NSWindow!
private var callback: ((String?) -> Void)?
private var originalTitle: String?
private var representedObject: Any?
private var delegate: RenameWindowControllerDelegate?
convenience init(originalTitle: String, callback: @escaping ((String?) -> Void)) {
convenience init(originalTitle: String, representedObject: Any, delegate: RenameWindowControllerDelegate) {
self.init(windowNibName: NSNib.Name(rawValue: "RenameSheet"))
self.originalTitle = originalTitle
self.callback = callback
self.representedObject = representedObject
self.delegate = delegate
}
override func windowDidLoad() {
newTitleTextField.stringValue = originalTitle
newTitleTextField.stringValue = originalTitle!
let prompt = NSLocalizedString("Rename %@ to:", comment: "Rename sheet")
let localizedPrompt = NSString.localizedStringWithFormat(prompt as NSString, originalTitle)
let localizedPrompt = NSString.localizedStringWithFormat(prompt as NSString, originalTitle!)
renamePrompt.stringValue = localizedPrompt as String
updateUI()
}
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)
window?.sheetParent?.endSheet(window!, returnCode: .cancel)
}
@IBAction func rename(_ sender: AnyObject) {
callback?(newTitleTextField.stringValue)
hostWindow!.endSheet(window!, returnCode: .OK)
guard let representedObject = representedObject else {
return
}
delegate?.renameWindowController(self, didRenameObject: representedObject, withNewName: newTitleTextField.stringValue)
window?.sheetParent?.endSheet(window!, returnCode: .OK)
}
}