mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
feat: add an option to choose text alignment in the Mini widget (#517)
This commit is contained in:
@@ -14,6 +14,7 @@ import Cocoa
|
||||
public class Mini: WidgetWrapper {
|
||||
private var labelState: Bool = true
|
||||
private var colorState: Color = .monochrome
|
||||
private var alignmentState: String = "left"
|
||||
|
||||
private var labelLayer: CATextLayer? = nil
|
||||
private var valueLayer: CATextLayer? = nil
|
||||
@@ -32,6 +33,15 @@ public class Mini: WidgetWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
private var alignment: NSTextAlignment {
|
||||
get {
|
||||
if let alignmentPair = Alignments.first(where: { $0.key == self.alignmentState }) {
|
||||
return alignmentPair.additional as? NSTextAlignment ?? .left
|
||||
}
|
||||
return .left
|
||||
}
|
||||
}
|
||||
|
||||
public init(title: String, config: NSDictionary?, preview: Bool = false) {
|
||||
var widgetTitle: String = title
|
||||
if config != nil {
|
||||
@@ -76,6 +86,7 @@ public class Mini: WidgetWrapper {
|
||||
if !preview {
|
||||
self.colorState = Color.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState.key))
|
||||
self.labelState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_label", defaultValue: self.labelState)
|
||||
self.alignmentState = Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_alignment", defaultValue: self.alignmentState)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,15 +100,18 @@ public class Mini: WidgetWrapper {
|
||||
let valueSize: CGFloat = self.labelState ? 12 : 14
|
||||
var origin: CGPoint = CGPoint(x: Constants.Widget.margin.x, y: (Constants.Widget.height-valueSize)/2)
|
||||
let style = NSMutableParagraphStyle()
|
||||
style.alignment = self.labelState ? .left : .center
|
||||
style.alignment = self.labelState ? self.alignment : .center
|
||||
|
||||
if self.labelState {
|
||||
let style = NSMutableParagraphStyle()
|
||||
style.alignment = self.alignment
|
||||
|
||||
let stringAttributes = [
|
||||
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .light),
|
||||
NSAttributedString.Key.foregroundColor: isDarkMode ? NSColor.white : NSColor.textColor,
|
||||
NSAttributedString.Key.paragraphStyle: NSMutableParagraphStyle()
|
||||
NSAttributedString.Key.paragraphStyle: style
|
||||
]
|
||||
let rect = CGRect(x: origin.x, y: 12, width: 20, height: 7)
|
||||
let rect = CGRect(x: origin.x, y: 12, width: self.width - (Constants.Widget.margin.x*2), height: 7)
|
||||
let str = NSAttributedString.init(string: self.label, attributes: stringAttributes)
|
||||
str.draw(with: rect)
|
||||
|
||||
@@ -118,8 +132,8 @@ public class Mini: WidgetWrapper {
|
||||
NSAttributedString.Key.foregroundColor: color,
|
||||
NSAttributedString.Key.paragraphStyle: style
|
||||
]
|
||||
let rect = CGRect(x: origin.x, y: origin.y, width: width - (Constants.Widget.margin.x*2), height: valueSize+1)
|
||||
let str = NSAttributedString.init(string: "\(Int(self.value.rounded(toPlaces: 2) * 100))%", attributes: stringAttributes)
|
||||
let rect = CGRect(x: origin.x, y: origin.y, width: self.width - (Constants.Widget.margin.x*2), height: valueSize+1)
|
||||
let str = NSAttributedString.init(string: "1%", attributes: stringAttributes)
|
||||
str.draw(with: rect)
|
||||
|
||||
self.setWidth(width)
|
||||
@@ -166,31 +180,31 @@ public class Mini: WidgetWrapper {
|
||||
// MARK: - Settings
|
||||
|
||||
public override func settings(width: CGFloat) -> NSView {
|
||||
let height: CGFloat = 60 + (Constants.Settings.margin*3)
|
||||
let rowHeight: CGFloat = 30
|
||||
let view = SettingsContainerView(width: width)
|
||||
|
||||
let view: NSView = NSView(frame: NSRect(
|
||||
x: Constants.Settings.margin,
|
||||
y: Constants.Settings.margin,
|
||||
width: width - (Constants.Settings.margin*2),
|
||||
height: height
|
||||
))
|
||||
|
||||
view.addSubview(toggleTitleRow(
|
||||
frame: NSRect(x: 0, y: rowHeight + Constants.Settings.margin, width: view.frame.width, height: rowHeight),
|
||||
view.addArrangedSubview(toggleTitleRow(
|
||||
frame: NSRect(x: 0, y: 0, width: view.frame.width, height: Constants.Settings.row),
|
||||
title: localizedString("Label"),
|
||||
action: #selector(toggleLabel),
|
||||
state: self.labelState
|
||||
))
|
||||
|
||||
view.addSubview(selectRow(
|
||||
frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 0, width: view.frame.width, height: rowHeight),
|
||||
view.addArrangedSubview(selectRow(
|
||||
frame: NSRect(x: 0, y: 0, width: view.frame.width, height: Constants.Settings.row),
|
||||
title: localizedString("Color"),
|
||||
action: #selector(toggleColor),
|
||||
items: self.colors,
|
||||
selected: self.colorState.key
|
||||
))
|
||||
|
||||
view.addArrangedSubview(selectRow(
|
||||
frame: NSRect(x: 0, y: 0, width: view.frame.width, height: Constants.Settings.row),
|
||||
title: localizedString("Alignment"),
|
||||
action: #selector(toggleAlignment),
|
||||
items: Alignments,
|
||||
selected: self.alignmentState
|
||||
))
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
@@ -217,4 +231,17 @@ public class Mini: WidgetWrapper {
|
||||
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_label", value: self.labelState)
|
||||
self.display()
|
||||
}
|
||||
|
||||
@objc private func toggleAlignment(_ sender: NSMenuItem) {
|
||||
guard let key = sender.representedObject as? String else {
|
||||
return
|
||||
}
|
||||
|
||||
if let newAlignment = Alignments.first(where: { $0.key == key }) {
|
||||
self.alignmentState = newAlignment.key
|
||||
}
|
||||
|
||||
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_alignment", value: key)
|
||||
self.display()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,6 +84,12 @@ public let NetworkReaders: [KeyValue_t] = [
|
||||
KeyValue_t(key: "process", value: "Processes based")
|
||||
]
|
||||
|
||||
public let Alignments: [KeyValue_t] = [
|
||||
KeyValue_t(key: "left", value: "Left alignment", additional: NSTextAlignment.left),
|
||||
KeyValue_t(key: "center", value: "Center alignment", additional: NSTextAlignment.center),
|
||||
KeyValue_t(key: "right", value: "Right alignment", additional: NSTextAlignment.right)
|
||||
]
|
||||
|
||||
public struct Color: KeyValue_p, Equatable {
|
||||
public let key: String
|
||||
public let value: String
|
||||
|
||||
Reference in New Issue
Block a user