[kit] Implement the notification infrastructure
This commit is contained in:
parent
0aa82ce59e
commit
587e616d76
3 changed files with 55 additions and 16 deletions
|
@ -117,6 +117,7 @@ public class Field {
|
|||
|
||||
private(set) var balls = [Ball]()
|
||||
|
||||
public var defaults = UserDefaults.standard
|
||||
private var parameters: Parameters
|
||||
|
||||
internal var bounds: CGRect {
|
||||
|
@ -125,6 +126,11 @@ public class Field {
|
|||
|
||||
public init(parameters p: Parameters) {
|
||||
parameters = p
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(Field.preferencesDidChange(note:)), name: PreferencesDidChange_Color, object: nil)
|
||||
}
|
||||
|
||||
deinit {
|
||||
NotificationCenter.default.removeObserver(self, name: PreferencesDidChange_Color, object: nil)
|
||||
}
|
||||
|
||||
public func update() {
|
||||
|
@ -246,4 +252,40 @@ public class Field {
|
|||
populateParametersBuffer()
|
||||
populateBallBuffer()
|
||||
}
|
||||
|
||||
// MARK: - Notifications
|
||||
|
||||
@objc
|
||||
func preferencesDidChange(note: Notification) {
|
||||
guard let userInfo = note.userInfo else { return }
|
||||
var didChange = false
|
||||
if let color = userInfo["color0"] as? NSColor {
|
||||
let cf = Float4(color: color)
|
||||
parameters.color0 = cf
|
||||
defaults.color0 = cf
|
||||
didChange = true
|
||||
}
|
||||
if let color = userInfo["color1"] as? NSColor {
|
||||
let cf = Float4(color: color)
|
||||
parameters.color1 = cf
|
||||
defaults.color1 = cf
|
||||
didChange = true
|
||||
}
|
||||
if let color = userInfo["color2"] as? NSColor {
|
||||
let cf = Float4(color: color)
|
||||
parameters.color2 = cf
|
||||
defaults.color2 = cf
|
||||
didChange = true
|
||||
}
|
||||
if let color = userInfo["color3"] as? NSColor {
|
||||
let cf = Float4(color: color)
|
||||
parameters.color3 = cf
|
||||
defaults.color3 = cf
|
||||
didChange = true
|
||||
}
|
||||
|
||||
if didChange {
|
||||
populateParametersBuffer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,7 +52,11 @@ extension UserDefaults {
|
|||
return Float4(values[0], values[1], values[2], values[3])
|
||||
}
|
||||
|
||||
func set(value: Float4, forKey key: String) {
|
||||
set([Float](float4: value), forKey: key)
|
||||
func set(_ value: Float4?, forKey key: String) {
|
||||
if let value = value {
|
||||
set([Float](float4: value), forKey: key)
|
||||
} else {
|
||||
set(nil as Any?, forKey: key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,12 +91,9 @@ class PreferencesViewController: NSViewController {
|
|||
}
|
||||
|
||||
private func prepareColorViews() {
|
||||
guard let colors = defaults.array(forKey: "colors") else { return }
|
||||
for (idx, cv) in colorViews.enumerated() {
|
||||
if idx >= colors.count {
|
||||
continue
|
||||
}
|
||||
if let color = colors[idx] as? NSColor {
|
||||
if let fColor = defaults.float4(forKey: "color\(idx)") {
|
||||
let color = NSColor(float4: fColor)
|
||||
cv.colorWell.color = color
|
||||
}
|
||||
}
|
||||
|
@ -111,20 +108,16 @@ class PreferencesViewController: NSViewController {
|
|||
|
||||
func colorPanelDidUpdateValue(_ colorPanel: NSColorPanel) {
|
||||
// TODO: Post a notification about color change.
|
||||
print("color panel did update: \(colorPanel.color)")
|
||||
var info = [String:Any]()
|
||||
if let currentStyleItem = styleMenu.selectedItem {
|
||||
info["style"] = ColorStyle(rawValue: UInt16(currentStyleItem.tag))!
|
||||
}
|
||||
// print("color panel did update: \(colorPanel.color)")
|
||||
var info = [String:NSColor]()
|
||||
for (idx, cv) in colorViews.enumerated() {
|
||||
let key = "color\(idx)"
|
||||
if cv.colorWell.isActive {
|
||||
info[key] = colorPanel.color
|
||||
info["color\(idx)"] = colorPanel.color
|
||||
} else {
|
||||
info[key] = cv.colorWell.color
|
||||
info["color\(idx)"] = cv.colorWell.color
|
||||
}
|
||||
}
|
||||
NotificationCenter.default.post(name: PreferencesDidChange_Color, object: self, userInfo: info)
|
||||
NotificationCenter.default.post(name: PreferencesDidChange_Color, object: nil, userInfo: info)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue