mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
feat: added monochrome (grayscaled) color to the Tachometer widget
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user