diff --git a/MTMR/AppleScriptTouchBarItem.swift b/MTMR/AppleScriptTouchBarItem.swift index a379a45..ac66db7 100644 --- a/MTMR/AppleScriptTouchBarItem.swift +++ b/MTMR/AppleScriptTouchBarItem.swift @@ -14,6 +14,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem { return } self.script = script + button.bezelColor = .clear DispatchQueue.main.async { var error: NSDictionary? guard script.compileAndReturnError(&error) else { diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 300e8fe..a56a44c 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -21,14 +21,11 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat self.longTapClosure = callbackLong super.init(identifier: identifier) - button = NSButton(title: title, target: self, action: nil) - - button.cell = CustomButtonCell(backgroundColor: bezelColor!) - button.cell?.title = title - button.title = title - + button = NSButton(title: title, target: nil, action: nil) + button.cell = CustomButtonCell() + button.isBordered = true button.bezelStyle = .rounded - button.bezelColor = bezelColor + button.title = title self.view = button longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong)) @@ -88,42 +85,37 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat } class CustomButtonCell: NSButtonCell { - init(backgroundColor: NSColor) { + + init() { super.init(textCell: "") - if backgroundColor != .clear { - self.isBordered = true - self.backgroundColor = backgroundColor - } else { - self.isBordered = false - } } override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) { - if flag { - self.isBordered = true - } else { - self.isBordered = false - } super.highlight(flag, withFrame: cellFrame, in: controlView) + if !self.isBordered { + self.setTitle(self.title, withColor: flag ? .lightGray : .white) + } } required init(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } -} -extension NSButton { - var title: String { + override var title: String! { get { - return ""// (self.cell?.title)! + return self.attributedTitle.string } - + set (newTitle) { - let attrTitle = NSMutableAttributedString(string: newTitle as String, attributes: [NSAttributedStringKey.foregroundColor: NSColor.white, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 15, weight: .regular), NSAttributedStringKey.baselineOffset: 1]) - attrTitle.setAlignment(.center, range: NSRange(location: 0, length: newTitle.count)) - - self.attributedTitle = attrTitle + setTitle(newTitle, withColor: .white) } } + + func setTitle(_ title: String, withColor color: NSColor) { + let attrTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1]) + attrTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count)) + + self.attributedTitle = attrTitle + } } diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index b564cd3..c6115a5 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -388,6 +388,8 @@ enum GeneralParameter { case width(_: CGFloat) case image(source: SourceProtocol) case align(_: Align) + case bordered(_: Bool) + case background(_:NSColor) } struct GeneralParameters: Decodable { @@ -397,6 +399,8 @@ struct GeneralParameters: Decodable { case width case image case align + case bordered + case background } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) @@ -409,6 +413,12 @@ struct GeneralParameters: Decodable { } let align = try container.decodeIfPresent(Align.self, forKey: .align) ?? .center result[.align] = .align(align) + 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 89ff230..95a300c 100644 --- a/MTMR/SupportHelpers.swift +++ b/MTMR/SupportHelpers.swift @@ -17,6 +17,24 @@ extension String { // ((\s|,)\/\*[\s\S]*?\*\/)|(( |, ")\/\/.*) return self.replacingOccurrences(of: "((\\s|,)\\/\\*[\\s\\S]*?\\*\\/)|(( |, \\\")\\/\\/.*)", with: "", options: .regularExpression) } + + 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.count { + case 3: // RGB (12-bit) + (r, g, b, a) = ((int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17, 255) + case 6: // RGB (24-bit) + (r, g, b, a) = (int >> 16, int >> 8 & 0xFF, int & 0xFF, 255) + case 8: // ARGB (32-bit) + (r, g, b, a) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF) + default: + return nil + } + return NSColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255) + } } extension NSImage { diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 3a5744a..ca012ba 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -211,6 +211,20 @@ class TouchBarController: NSObject, NSTouchBarDelegate { if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth { widthBarItem.setWidth(value: value) } + if case .bordered(let bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem { + item.button.isBordered = bordered + item.button.bezelStyle = bordered ? .rounded : .inline + } + if case .background(let color)? = item.additionalParameters[.background], let item = barItem as? CustomButtonTouchBarItem { + if item.button.cell?.isBordered == false { + let newCell = NSButtonCell() + newCell.title = item.button.title + item.button.cell = newCell + item.button.cell?.isBordered = true + } + item.button.bezelColor = color + (item.button.cell as? NSButtonCell)?.backgroundColor = color + } if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem { let button = item.button! button.imageScaling = .scaleProportionallyDown @@ -221,8 +235,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } button.imageHugsTitle = true - button.cell?.image = source.image - button.bezelColor = .clear + button.image = source.image } return barItem } diff --git a/MTMR/Widgets/TimeTouchBarItem.swift b/MTMR/Widgets/TimeTouchBarItem.swift index 7178dd6..21d98d3 100644 --- a/MTMR/Widgets/TimeTouchBarItem.swift +++ b/MTMR/Widgets/TimeTouchBarItem.swift @@ -10,6 +10,7 @@ class TimeTouchBarItem: CustomButtonTouchBarItem { super.init(identifier: identifier, title: " ", onTap: onTap, onLongTap: onLongTap) timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true) self.view = button + button.bezelColor = .clear updateTime() }