NetNewsWire/Mac/MainWindow/AddFolder/AddFolderWindowController.s...

102 lines
2.3 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// AddFolderWindowController.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
2017-05-27 19:43:27 +02:00
//
// Created by Brent Simmons on 8/1/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import AppKit
import Articles
2017-09-17 21:54:08 +02:00
import Account
2017-05-27 19:43:27 +02:00
class AddFolderWindowController : NSWindowController {
2017-05-27 19:43:27 +02:00
@IBOutlet var folderNameTextField: NSTextField!
@IBOutlet var accountPopupButton: NSPopUpButton!
@IBOutlet var addFolderButton: NSButton!
private var hostWindow: NSWindow?
2017-05-27 19:43:27 +02:00
convenience init() {
2018-12-09 21:32:33 +01:00
self.init(windowNibName: NSNib.Name("AddFolderSheet"))
2017-05-27 19:43:27 +02:00
}
// MARK: - API
2017-05-27 19:43:27 +02:00
func runSheetOnWindow(_ w: NSWindow) {
hostWindow = w
2017-09-17 21:54:08 +02:00
hostWindow!.beginSheet(window!) { (returnCode: NSApplication.ModalResponse) -> Void in
2017-05-27 19:43:27 +02:00
2017-09-17 21:54:08 +02:00
if returnCode == NSApplication.ModalResponse.OK {
2017-05-27 19:43:27 +02:00
self.addFolderIfNeeded()
}
}
}
// MARK: - NSViewController
2017-05-27 19:43:27 +02:00
override func windowDidLoad() {
let preferredAccountID = AppDefaults.addFolderAccountID
2017-05-27 19:43:27 +02:00
accountPopupButton.removeAllItems()
2017-05-27 19:43:27 +02:00
let menu = NSMenu()
accountPopupButton.menu = menu
for oneAccount in AccountManager.shared.sortedActiveAccounts {
2017-05-27 19:43:27 +02:00
let oneMenuItem = NSMenuItem()
oneMenuItem.title = oneAccount.nameForDisplay
oneMenuItem.representedObject = oneAccount
menu.addItem(oneMenuItem)
if oneAccount.accountID == preferredAccountID {
accountPopupButton.select(oneMenuItem)
}
2017-05-27 19:43:27 +02:00
}
}
// MARK: - Actions
2017-05-27 19:43:27 +02:00
@IBAction func cancel(_ sender: Any?) {
hostWindow!.endSheet(window!, returnCode: .cancel)
2017-05-27 19:43:27 +02:00
}
@IBAction func addFolder(_ sender: Any?) {
hostWindow!.endSheet(window!, returnCode: .OK)
2017-05-27 19:43:27 +02:00
}
}
// MARK: - Text Field Delegate
extension AddFolderWindowController: NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {
guard let folderName = (obj.object as? NSTextField)?.stringValue else {
addFolderButton.isEnabled = false
return
}
addFolderButton.isEnabled = !folderName.isEmpty
}
2017-05-27 19:43:27 +02:00
}
// MARK: - Private
private extension AddFolderWindowController {
private func addFolderIfNeeded() {
guard let menuItem = accountPopupButton.selectedItem else {
return
}
let account = menuItem.representedObject as! Account
AppDefaults.addFolderAccountID = account.accountID
let folderName = self.folderNameTextField.stringValue
if folderName.isEmpty {
return
}
account.ensureFolder(with: folderName)
}
}