From 4a03ba79ecc78b9adf68beed7aab9e8ce06748af Mon Sep 17 00:00:00 2001 From: ad Date: Mon, 30 Apr 2018 11:15:23 +0300 Subject: [PATCH] + background option for button # Conflicts: # MTMR/CustomButtonTouchBarItem.swift # MTMR/ItemsParsing.swift --- MTMR/ItemsParsing.swift | 5 +++++ MTMR/SupportHelpers.swift | 12 ++++++------ MTMR/TouchBarController.swift | 4 ++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index dec4a91..c6115a5 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -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 } } diff --git a/MTMR/SupportHelpers.swift b/MTMR/SupportHelpers.swift index abc8a31..95a300c 100644 --- a/MTMR/SupportHelpers.swift +++ b/MTMR/SupportHelpers.swift @@ -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) } diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index f356397..6b78bde 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -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 }