NetNewsWire/Evergreen/MainWindow/AddFolder/AddFolderWindowController.swift

82 lines
1.8 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// AddFolderWindowController.swift
// Evergreen
//
// Created by Brent Simmons on 8/1/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import AppKit
import Data
2017-09-17 21:54:08 +02:00
import Account
2017-05-27 19:43:27 +02:00
class AddFolderWindowController : NSWindowController {
@IBOutlet var folderNameTextField: NSTextField!
@IBOutlet var accountPopupButton: NSPopUpButton!
var hostWindow: NSWindow?
convenience init() {
2017-09-17 21:54:08 +02:00
self.init(windowNibName: NSNib.Name(rawValue: "AddFolderSheet"))
2017-05-27 19:43:27 +02:00
}
// MARK: API
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
override func windowDidLoad() {
accountPopupButton.removeAllItems()
let menu = NSMenu()
2017-09-24 21:24:44 +02:00
for oneAccount in AccountManager.shared.sortedAccounts {
2017-05-27 19:43:27 +02:00
let oneMenuItem = NSMenuItem()
oneMenuItem.title = oneAccount.nameForDisplay
oneMenuItem.representedObject = oneAccount
menu.addItem(oneMenuItem)
}
accountPopupButton.menu = menu
}
// MARK: Private
private func addFolderIfNeeded() {
guard let menuItem = accountPopupButton.selectedItem else {
return
}
let account = menuItem.representedObject as! Account
let folderName = self.folderNameTextField.stringValue
if folderName.isEmpty {
return
}
account.ensureFolder(with: folderName)
2017-05-27 19:43:27 +02:00
}
// MARK: Actions
@IBAction func cancel(_ sender: AnyObject) {
2017-09-17 21:54:08 +02:00
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.cancel)
2017-05-27 19:43:27 +02:00
}
@IBAction func addFolder(_ sender: AnyObject) {
2017-09-17 21:54:08 +02:00
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.OK)
2017-05-27 19:43:27 +02:00
}
}