2017-08-12 10:53:13 -07:00
//
// P r e f e r e n c e s V i e w C o n t r o l l e r . s w i f t
// M e t a b a l l s
//
// C r e a t e d b y E r y n W e l l s o n 8 / 1 2 / 1 7 .
// C o p y r i g h t © 2 0 1 7 E r y n W e l l s . A l l r i g h t s r e s e r v e d .
//
import Cocoa
2017-08-13 09:43:09 -07:00
internal let PreferencesDidChange_Color = Notification . Name ( " PreferencesDidChange_Color " )
2017-08-12 10:53:13 -07:00
class PreferencesViewController : NSViewController {
public var defaults = UserDefaults . standard
private var colorStackView = NSStackView ( )
private var colorViews = [ ColorView ] ( )
private lazy var styleMenu : NSPopUpButton = {
let button = NSPopUpButton ( )
button . translatesAutoresizingMaskIntoConstraints = false
let menu = NSMenu ( )
2017-08-16 21:12:12 -07:00
menu . addItem ( withTitle : NSLocalizedString ( " Single Color " , comment : " single color menu item " ) , action : nil , keyEquivalent : " " )
menu . addItem ( withTitle : NSLocalizedString ( " Two Color Gradient — Horizontal " , comment : " two color horizontal gradient menu item " ) , action : nil , keyEquivalent : " " )
2017-08-12 10:53:13 -07:00
button . menu = menu
return button
} ( )
override func loadView ( ) {
let view = NSView ( )
view . translatesAutoresizingMaskIntoConstraints = false
colorStackView . setAccessibilityIdentifier ( " colorStackView " )
colorStackView . translatesAutoresizingMaskIntoConstraints = false
colorStackView . orientation = . vertical
colorStackView . alignment = . left
colorStackView . distribution = . fillProportionally
colorStackView . spacing = 8
view . addSubview ( colorStackView )
let centerX = colorStackView . centerXAnchor . constraint ( equalTo : view . centerXAnchor )
centerX . priority = 999
let centerY = colorStackView . centerYAnchor . constraint ( equalTo : view . centerYAnchor )
centerY . priority = 999
NSLayoutConstraint . activate ( [
centerX , centerY ,
colorStackView . topAnchor . constraint ( greaterThanOrEqualTo : view . topAnchor , constant : 8 ) ,
colorStackView . leftAnchor . constraint ( greaterThanOrEqualTo : view . leftAnchor , constant : 8 ) ,
colorStackView . bottomAnchor . constraint ( lessThanOrEqualTo : view . bottomAnchor , constant : - 8 ) ,
colorStackView . rightAnchor . constraint ( lessThanOrEqualTo : view . rightAnchor , constant : - 8 ) ,
] )
colorStackView . addArrangedSubview ( styleMenu )
for i in 0. . < 4 {
let colorView = ColorView ( )
colorView . translatesAutoresizingMaskIntoConstraints = false
colorView . label . stringValue = " Color \( i + 1 ) "
colorStackView . addArrangedSubview ( colorView )
colorViews . append ( colorView )
}
self . view = view
}
override func viewWillAppear ( ) {
super . viewWillAppear ( )
prepareColorViews ( )
2017-08-13 09:43:09 -07:00
prepareColorPanel ( )
}
override func viewWillDisappear ( ) {
super . viewWillDisappear ( )
NSColorPanel . shared ( ) . close ( )
2017-08-12 10:53:13 -07:00
}
private func prepareColorViews ( ) {
for ( idx , cv ) in colorViews . enumerated ( ) {
2017-08-16 21:12:00 -07:00
if let fColor = defaults . float4 ( forKey : " color \( idx ) " ) {
let color = NSColor ( float4 : fColor )
2017-08-12 10:53:13 -07:00
cv . colorWell . color = color
}
}
}
2017-08-13 09:43:09 -07:00
private func prepareColorPanel ( ) {
let colorPanel = NSColorPanel . shared ( )
colorPanel . isContinuous = true
colorPanel . setTarget ( self )
colorPanel . setAction ( #selector ( PreferencesViewController . colorPanelDidUpdateValue ) )
}
func colorPanelDidUpdateValue ( _ colorPanel : NSColorPanel ) {
// TODO: P o s t a n o t i f i c a t i o n a b o u t c o l o r c h a n g e .
2017-08-16 21:12:00 -07:00
// p r i n t ( " c o l o r p a n e l d i d u p d a t e : \ ( c o l o r P a n e l . c o l o r ) " )
var info = [ String : NSColor ] ( )
2017-08-13 09:43:09 -07:00
for ( idx , cv ) in colorViews . enumerated ( ) {
if cv . colorWell . isActive {
2017-08-16 21:12:00 -07:00
info [ " color \( idx ) " ] = colorPanel . color
2017-08-13 09:43:09 -07:00
} else {
2017-08-16 21:12:00 -07:00
info [ " color \( idx ) " ] = cv . colorWell . color
2017-08-13 09:43:09 -07:00
}
}
2017-08-16 21:12:00 -07:00
NotificationCenter . default . post ( name : PreferencesDidChange_Color , object : nil , userInfo : info )
2017-08-13 09:43:09 -07:00
}
2017-08-12 10:53:13 -07:00
}
class ColorView : NSView {
private let stackView = NSStackView ( )
internal let colorWell = NSColorWell ( )
internal let label = NSTextField ( labelWithString : " Hello " )
override init ( frame frameRect : NSRect ) {
super . init ( frame : frameRect )
commonInit ( )
}
required init ? ( coder : NSCoder ) {
super . init ( coder : coder )
commonInit ( )
}
private func commonInit ( ) {
stackView . translatesAutoresizingMaskIntoConstraints = false
stackView . orientation = . horizontal
stackView . spacing = 8
stackView . alignment = . centerY
stackView . distribution = . equalSpacing
colorWell . translatesAutoresizingMaskIntoConstraints = false
colorWell . setContentHuggingPriority ( 251 , for : . horizontal )
stackView . addArrangedSubview ( colorWell )
label . translatesAutoresizingMaskIntoConstraints = false
stackView . addArrangedSubview ( label )
addSubview ( stackView )
NSLayoutConstraint . activate ( [
stackView . topAnchor . constraint ( equalTo : topAnchor ) ,
stackView . leftAnchor . constraint ( equalTo : leftAnchor ) ,
stackView . bottomAnchor . constraint ( equalTo : bottomAnchor ) ,
stackView . rightAnchor . constraint ( equalTo : rightAnchor ) ,
] )
}
}