From fbec803d472a223f3109c3b258e76d0168bb2ae0 Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 30 Apr 2018 15:07:47 +0700 Subject: [PATCH 1/7] introduce bordered parameter, restore 0.13 borders --- MTMR/AppleScriptTouchBarItem.swift | 1 + MTMR/CustomButtonTouchBarItem.swift | 31 ----------------------------- MTMR/ItemsParsing.swift | 5 +++++ MTMR/TouchBarController.swift | 4 ++++ MTMR/Widgets/TimeTouchBarItem.swift | 1 + 5 files changed, 11 insertions(+), 31 deletions(-) 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..c090e8f 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -22,13 +22,7 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat 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.bezelStyle = .rounded - button.bezelColor = bezelColor self.view = button longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong)) @@ -87,31 +81,6 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat } } -class CustomButtonCell: NSButtonCell { - init(backgroundColor: NSColor) { - 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) - } - - required init(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } -} - extension NSButton { var title: String { get { diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index b564cd3..dec4a91 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -388,6 +388,7 @@ enum GeneralParameter { case width(_: CGFloat) case image(source: SourceProtocol) case align(_: Align) + case bordered(_: Bool) } struct GeneralParameters: Decodable { @@ -397,6 +398,7 @@ struct GeneralParameters: Decodable { case width case image case align + case bordered } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) @@ -409,6 +411,9 @@ 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) + } parameters = result } } diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index a3b28ca..f356397 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -219,6 +219,10 @@ class TouchBarController: NSObject, NSTouchBarDelegate { button.cell?.image = source.image button.bezelColor = .clear } + if case .bordered(let bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem { + item.button.isBordered = bordered + item.button.bezelStyle = bordered ? .rounded : .inline + } 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() } From 907b79965d183eda77889c38819a8c98dee16994 Mon Sep 17 00:00:00 2001 From: ad Date: Mon, 30 Apr 2018 10:49:53 +0300 Subject: [PATCH 2/7] + hexColor --- MTMR/SupportHelpers.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/MTMR/SupportHelpers.swift b/MTMR/SupportHelpers.swift index 89ff230..abc8a31 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.characters.count { + case 3: // RGB (12-bit) + (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17) + case 6: // RGB (24-bit) + (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF) + case 8: // ARGB (32-bit) + (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF) + default: + return .clear + } + return NSColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255) + } } extension NSImage { From 4a03ba79ecc78b9adf68beed7aab9e8ce06748af Mon Sep 17 00:00:00 2001 From: ad Date: Mon, 30 Apr 2018 11:15:23 +0300 Subject: [PATCH 3/7] + 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 } From 12796d6387cced39c48303e8f405ffb83c0da455 Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 30 Apr 2018 18:40:26 +0700 Subject: [PATCH 4/7] fix for colors in non-bordered buttons --- MTMR/CustomButtonTouchBarItem.swift | 5 ++++- MTMR/TouchBarController.swift | 25 +++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index c090e8f..07ddf67 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -21,7 +21,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat self.longTapClosure = callbackLong super.init(identifier: identifier) - button = NSButton(title: title, target: self, action: nil) + button = NSButton(title: title, target: nil, action: nil) + button.cell = NSButtonCell() + button.isBordered = true + button.bezelStyle = .rounded button.title = title self.view = button diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 6b78bde..f31fd84 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -211,21 +211,26 @@ 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 + if bordered { + item.button.bezelStyle = .rounded + } + } + if case .background(let color)? = item.additionalParameters[.background], let item = barItem as? CustomButtonTouchBarItem { + if item.button.cell?.isBordered == false { + item.button.cell = NSButtonCell() + 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 button.imagePosition = .imageLeading button.imageHugsTitle = true - button.cell?.image = source.image - button.bezelColor = .clear - } - 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 { - item.button.bezelColor = color - (item.button.cell as? NSButtonCell)?.backgroundColor = color + button.image = source.image } return barItem } From 453895f395256d913b0a8980081ef9fc4317a0f3 Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 30 Apr 2018 18:53:32 +0700 Subject: [PATCH 5/7] fix wrong title while first execution --- MTMR/TouchBarController.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index f31fd84..f08336f 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -219,7 +219,9 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } if case .background(let color)? = item.additionalParameters[.background], let item = barItem as? CustomButtonTouchBarItem { if item.button.cell?.isBordered == false { - item.button.cell = NSButtonCell() + let newCell = NSButtonCell() + newCell.title = item.button.title + item.button.cell = newCell item.button.cell?.isBordered = true } item.button.bezelColor = color From cd69bc341c2a2928938d459d6b58c2f7dbe9cfb5 Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 30 Apr 2018 22:54:15 +0700 Subject: [PATCH 6/7] fix margin for some buttons --- MTMR/TouchBarController.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index f08336f..624f41d 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -213,9 +213,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } if case .bordered(let bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem { item.button.isBordered = bordered - if bordered { - item.button.bezelStyle = .rounded - } + 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 { From 49d7333d5e7ed9e74264cb4fc6385a1d1dbd8068 Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 30 Apr 2018 23:18:59 +0700 Subject: [PATCH 7/7] implement highlight for buttons with no border --- MTMR/CustomButtonTouchBarItem.swift | 38 ++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 07ddf67..a56a44c 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -22,7 +22,7 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat super.init(identifier: identifier) button = NSButton(title: title, target: nil, action: nil) - button.cell = NSButtonCell() + button.cell = CustomButtonCell() button.isBordered = true button.bezelStyle = .rounded button.title = title @@ -84,18 +84,38 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat } } -extension NSButton { - var title: String { +class CustomButtonCell: NSButtonCell { + + init() { + super.init(textCell: "") + } + + override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) { + 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") + } + + 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 + } }