2 // PreferencePanelController.swift
5 // Created by Hori,Masaki on 2016/12/19.
6 // Copyright © 2016年 Hori,Masaki. All rights reserved.
11 enum PreferencesPaneType: Int {
17 enum ScreenshotSaveDirectoryPopupMenuItemTag: Int {
19 case saveDirectory = 1000
20 case selectDiretory = 2000
23 private extension Selector {
25 static let didChangeSelection = #selector(PreferencePanelController.didChangeSelection(_:))
28 final class PreferencePanelController: NSWindowController {
30 @IBOutlet var screenShotSaveDirectoryPopUp: NSPopUpButton!
31 @IBOutlet var generalPane: NSView!
32 @IBOutlet var notificationPane: NSView!
34 override var windowNibName: String! {
36 return "PreferencePanelController"
39 private(set) var screenShotSaveDirectory: String {
41 get { return AppDelegate.shared.screenShotSaveDirectory }
43 AppDelegate.shared.screenShotSaveDirectory = newValue
45 let index = screenShotSaveDirectoryPopUp
46 .indexOfItem(withTag: ScreenshotSaveDirectoryPopupMenuItemTag.saveDirectory.rawValue)
48 guard let item = screenShotSaveDirectoryPopUp.item(at: index)
51 let icon = NSWorkspace.shared().icon(forFile: newValue)
52 let iconSize = icon.size
53 let height: Double = 16
54 icon.size = NSSize(width: Double(iconSize.width) * height / Double(iconSize.height), height: height)
57 item.title = FileManager.default.displayName(atPath: newValue)
61 override func windowDidLoad() {
65 screenShotSaveDirectory = AppDelegate.shared.screenShotSaveDirectory
67 guard let window = window,
68 let items = window.toolbar?.items,
69 let item = items.first
72 window.toolbar?.selectedItemIdentifier = item.itemIdentifier
73 NSApplication.shared().sendAction(.didChangeSelection,
80 @IBAction func selectScreenShotSaveDirectoryPopUp(_ sender: AnyObject?) {
82 guard let window = window,
83 let tag = sender?.tag,
84 let itemTag = ScreenshotSaveDirectoryPopupMenuItemTag(rawValue: tag),
85 itemTag == .selectDiretory
88 let panel = NSOpenPanel()
89 panel.canChooseDirectories = true
90 panel.canChooseFiles = false
91 panel.beginSheetModal(for: window) {
93 self.screenShotSaveDirectoryPopUp
94 .selectItem(withTag: ScreenshotSaveDirectoryPopupMenuItemTag.saveDirectory.rawValue)
96 guard $0 != NSModalResponseCancel,
97 let path = panel.url?.path
100 self.screenShotSaveDirectory = path
104 @IBAction func didChangeSelection(_ sender: AnyObject?) {
106 guard let tag = sender?.tag,
107 let paneType = PreferencesPaneType(rawValue: tag)
113 case .general: return generalPane
114 case .notification: return notificationPane
118 guard let item = sender as? NSToolbarItem,
119 let window = self.window
122 window.title = item.label
123 window.contentView?.subviews.forEach { $0.removeFromSuperview() }
125 let windowRect = window.frame
126 var newWindowRect = window.frameRect(forContentRect: pane.frame)
127 newWindowRect.origin.x = windowRect.origin.x
128 newWindowRect.origin.y = windowRect.origin.y + windowRect.size.height - newWindowRect.size.height
129 window.setFrame(newWindowRect, display: true, animate: true)
130 window.contentView?.addSubview(pane)