[kit] Work on the dropdown style menu

This commit is contained in:
Eryn Wells 2017-08-16 21:35:48 -07:00
parent e89c858ab7
commit 6a0ca3799f

View file

@ -10,13 +10,22 @@ import Cocoa
internal let PreferencesDidChange_Color = Notification.Name("PreferencesDidChange_Color") internal let PreferencesDidChange_Color = Notification.Name("PreferencesDidChange_Color")
private struct StyleItem {
let name: String
let tag: Int
let colorNames: [String]
}
class PreferencesViewController: NSViewController { class PreferencesViewController: NSViewController {
private static var styleItems: [(name: String, tag: Int)] { private static var styleItems: [StyleItem] {
return [ return [
(name: NSLocalizedString("Single Color", comment: "single color menu item"), StyleItem(name: NSLocalizedString("Single Color", comment: "single color menu item"),
tag: Int(ColorStyle.singleColor.rawValue)), tag: Int(ColorStyle.singleColor.rawValue),
(name: NSLocalizedString("Two Color Gradient — Horizontal", comment: "two color horizontal gradient menu item"), colorNames: [NSLocalizedString("Color", comment: "single color name")]),
tag: Int(ColorStyle.gradient2Horizontal.rawValue)), StyleItem(name: NSLocalizedString("Two Color Gradient — Horizontal", comment: "two color horizontal gradient menu item"),
tag: Int(ColorStyle.gradient2Horizontal.rawValue),
colorNames: [NSLocalizedString("Right", comment: "two color horizontal gradient, color 1"),
NSLocalizedString("Left", comment: "two color horizontal gradient, color 2")]),
] ]
} }
@ -32,7 +41,7 @@ class PreferencesViewController: NSViewController {
let menu = NSMenu() let menu = NSMenu()
for item in PreferencesViewController.styleItems { for item in PreferencesViewController.styleItems {
// TODO: Set action here. // TODO: Set action here.
let menuItem = NSMenuItem(title: item.name, action: nil, keyEquivalent: "") let menuItem = NSMenuItem(title: item.name, action: #selector(PreferencesViewController.styleDidUpdate(sender:)), keyEquivalent: "")
menuItem.target = self menuItem.target = self
menuItem.tag = item.tag menuItem.tag = item.tag
menu.addItem(menuItem) menu.addItem(menuItem)
@ -106,9 +115,9 @@ class PreferencesViewController: NSViewController {
colorPanel.setAction(#selector(PreferencesViewController.colorPanelDidUpdateValue)) colorPanel.setAction(#selector(PreferencesViewController.colorPanelDidUpdateValue))
} }
// MARK: - Actions
func colorPanelDidUpdateValue(_ colorPanel: NSColorPanel) { func colorPanelDidUpdateValue(_ colorPanel: NSColorPanel) {
// TODO: Post a notification about color change.
// print("color panel did update: \(colorPanel.color)")
var info = [String:NSColor]() var info = [String:NSColor]()
for (idx, cv) in colorViews.enumerated() { for (idx, cv) in colorViews.enumerated() {
if cv.colorWell.isActive { if cv.colorWell.isActive {
@ -119,6 +128,21 @@ class PreferencesViewController: NSViewController {
} }
NotificationCenter.default.post(name: PreferencesDidChange_Color, object: nil, userInfo: info) NotificationCenter.default.post(name: PreferencesDidChange_Color, object: nil, userInfo: info)
} }
func styleDidUpdate(sender: NSMenuItem) {
guard let menu = styleMenu.menu else { return }
let idx = menu.index(of: sender)
guard idx != -1 && idx < PreferencesViewController.styleItems.count else { return }
let styleItem = PreferencesViewController.styleItems[idx]
for (idx, colorView) in colorViews.enumerated() {
if idx < styleItem.colorNames.count {
colorView.isHidden = false
colorView.label.stringValue = styleItem.colorNames[idx]
} else {
colorView.isHidden = true
}
}
}
} }
class ColorView: NSView { class ColorView: NSView {