mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
+ background option for button
# Conflicts: # MTMR/CustomButtonTouchBarItem.swift # MTMR/ItemsParsing.swift
This commit is contained in:
parent
907b79965d
commit
4a03ba79ec
@ -389,6 +389,7 @@ enum GeneralParameter {
|
||||
case image(source: SourceProtocol)
|
||||
case align(_: Align)
|
||||
case bordered(_: Bool)
|
||||
case background(_:NSColor)
|
||||
}
|
||||
|
||||
struct GeneralParameters: Decodable {
|
||||
@ -399,6 +400,7 @@ struct GeneralParameters: Decodable {
|
||||
case image
|
||||
case align
|
||||
case bordered
|
||||
case background
|
||||
}
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
@ -414,6 +416,9 @@ struct GeneralParameters: Decodable {
|
||||
if let borderedFlag = try container.decodeIfPresent(Bool.self, forKey: .bordered) {
|
||||
result[.bordered] = .bordered(borderedFlag)
|
||||
}
|
||||
if let backgroundColor = try container.decodeIfPresent(String.self, forKey: .background)?.hexColor {
|
||||
result[.background] = .background(backgroundColor)
|
||||
}
|
||||
parameters = result
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,20 +18,20 @@ extension String {
|
||||
return self.replacingOccurrences(of: "((\\s|,)\\/\\*[\\s\\S]*?\\*\\/)|(( |, \\\")\\/\\/.*)", with: "", options: .regularExpression)
|
||||
}
|
||||
|
||||
var hexColor: NSColor {
|
||||
var hexColor: NSColor? {
|
||||
let hex = trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
|
||||
var int = UInt32()
|
||||
Scanner(string: hex).scanHexInt32(&int)
|
||||
let a, r, g, b: UInt32
|
||||
switch hex.characters.count {
|
||||
switch hex.count {
|
||||
case 3: // RGB (12-bit)
|
||||
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
|
||||
(r, g, b, a) = ((int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17, 255)
|
||||
case 6: // RGB (24-bit)
|
||||
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
|
||||
(r, g, b, a) = (int >> 16, int >> 8 & 0xFF, int & 0xFF, 255)
|
||||
case 8: // ARGB (32-bit)
|
||||
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
|
||||
(r, g, b, a) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
|
||||
default:
|
||||
return .clear
|
||||
return nil
|
||||
}
|
||||
return NSColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
|
||||
}
|
||||
|
||||
@ -223,6 +223,10 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
item.button.isBordered = bordered
|
||||
item.button.bezelStyle = bordered ? .rounded : .inline
|
||||
}
|
||||
if case .background(let color)? = item.additionalParameters[.background], let item = barItem as? CustomButtonTouchBarItem {
|
||||
item.button.bezelColor = color
|
||||
(item.button.cell as? NSButtonCell)?.backgroundColor = color
|
||||
}
|
||||
return barItem
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user