feat: added monochrome (grayscaled) color to the Tachometer widget

This commit is contained in:
Serhiy Mytrovtsiy
2021-10-14 18:29:50 +02:00
parent 4168862287
commit 585014cba1
4 changed files with 41 additions and 3 deletions

View File

@@ -13,6 +13,7 @@ import Cocoa
public class Tachometer: WidgetWrapper {
private var labelState: Bool = false
private var monochromeState: Bool = false
private var chart: TachometerGraphView = TachometerGraphView(
frame: NSRect(
@@ -45,6 +46,7 @@ public class Tachometer: WidgetWrapper {
])
} else {
self.labelState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_label", defaultValue: self.labelState)
self.monochromeState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_monochrome", defaultValue: self.monochromeState)
}
self.draw()
@@ -69,7 +71,15 @@ public class Tachometer: WidgetWrapper {
self.setWidth(self.size + x)
}
public func setValue(_ segments: [circle_segment]) {
public func setValue(_ list: [circle_segment]) {
var segments = list
if self.monochromeState {
for i in 0..<segments.count {
segments[i].color = segments[i].color.grayscaled()
}
}
DispatchQueue.main.async(execute: {
self.chart.setSegments(segments)
})
@@ -87,6 +97,13 @@ public class Tachometer: WidgetWrapper {
state: self.labelState
))
view.addArrangedSubview(toggleTitleRow(
frame: NSRect(x: 0, y: 0, width: view.frame.width, height: Constants.Settings.row),
title: localizedString("Monochrome accent"),
action: #selector(toggleMonochrome),
state: self.monochromeState
))
return view
}
@@ -106,4 +123,16 @@ public class Tachometer: WidgetWrapper {
self.chart.setFrameOrigin(NSPoint(x: x, y: 0))
self.setWidth(self.labelState ? self.size+x : self.size)
}
@objc private func toggleMonochrome(_ sender: NSControl) {
var state: NSControl.StateValue? = nil
if #available(OSX 10.15, *) {
state = sender is NSSwitch ? (sender as! NSSwitch).state: nil
} else {
state = sender is NSButton ? (sender as! NSButton).state: nil
}
self.monochromeState = state! == .on ? true : false
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_monochrome", value: self.monochromeState)
}
}

View File

@@ -413,6 +413,15 @@ public extension NSColor {
let blue = CGFloat(b) / 255.0
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
func grayscaled() -> NSColor {
guard let space = CGColorSpace(name: CGColorSpace.extendedGray),
let cg = self.cgColor.converted(to: space, intent: .perceptual, options: nil),
let color = NSColor.init(cgColor: cg) else {
return self
}
return color
}
}
public extension CATransaction {

View File

@@ -22,7 +22,7 @@ public enum chart_t: Int {
public struct circle_segment {
public let value: Double
public let color: NSColor
public var color: NSColor
public init(value: Double, color: NSColor) {
self.value = value

View File

@@ -258,7 +258,7 @@
"Based on pressure" = "Na podstawie obciążenia";
"System accent" = "Akcent systemowy";
"Monochrome accent" = "Akcent monochromatyczny";
"Clear" = "Wyczyść";
"Clear" = "Przezroczysty";
"White" = "Biały";
"Black" = "Czarny";
"Gray" = "Szary";