[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]()
|
private(set) var balls = [Ball]()
|
||||||
|
|
||||||
|
public var defaults = UserDefaults.standard
|
||||||
private var parameters: Parameters
|
private var parameters: Parameters
|
||||||
|
|
||||||
internal var bounds: CGRect {
|
internal var bounds: CGRect {
|
||||||
|
@ -125,6 +126,11 @@ public class Field {
|
||||||
|
|
||||||
public init(parameters p: Parameters) {
|
public init(parameters p: Parameters) {
|
||||||
parameters = p
|
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() {
|
public func update() {
|
||||||
|
@ -246,4 +252,40 @@ public class Field {
|
||||||
populateParametersBuffer()
|
populateParametersBuffer()
|
||||||
populateBallBuffer()
|
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])
|
return Float4(values[0], values[1], values[2], values[3])
|
||||||
}
|
}
|
||||||
|
|
||||||
func set(value: Float4, forKey key: String) {
|
func set(_ value: Float4?, forKey key: String) {
|
||||||
set([Float](float4: value), forKey: key)
|
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() {
|
private func prepareColorViews() {
|
||||||
guard let colors = defaults.array(forKey: "colors") else { return }
|
|
||||||
for (idx, cv) in colorViews.enumerated() {
|
for (idx, cv) in colorViews.enumerated() {
|
||||||
if idx >= colors.count {
|
if let fColor = defaults.float4(forKey: "color\(idx)") {
|
||||||
continue
|
let color = NSColor(float4: fColor)
|
||||||
}
|
|
||||||
if let color = colors[idx] as? NSColor {
|
|
||||||
cv.colorWell.color = color
|
cv.colorWell.color = color
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,20 +108,16 @@ class PreferencesViewController: NSViewController {
|
||||||
|
|
||||||
func colorPanelDidUpdateValue(_ colorPanel: NSColorPanel) {
|
func colorPanelDidUpdateValue(_ colorPanel: NSColorPanel) {
|
||||||
// TODO: Post a notification about color change.
|
// TODO: Post a notification about color change.
|
||||||
print("color panel did update: \(colorPanel.color)")
|
// print("color panel did update: \(colorPanel.color)")
|
||||||
var info = [String:Any]()
|
var info = [String:NSColor]()
|
||||||
if let currentStyleItem = styleMenu.selectedItem {
|
|
||||||
info["style"] = ColorStyle(rawValue: UInt16(currentStyleItem.tag))!
|
|
||||||
}
|
|
||||||
for (idx, cv) in colorViews.enumerated() {
|
for (idx, cv) in colorViews.enumerated() {
|
||||||
let key = "color\(idx)"
|
|
||||||
if cv.colorWell.isActive {
|
if cv.colorWell.isActive {
|
||||||
info[key] = colorPanel.color
|
info["color\(idx)"] = colorPanel.color
|
||||||
} else {
|
} 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