From 11c8d167c06a1abc63e90bf1a569d5462c5e0d49 Mon Sep 17 00:00:00 2001 From: Serg Date: Wed, 11 Apr 2018 00:56:14 +0700 Subject: [PATCH] custom width --- MTMR/ItemsParsing.swift | 28 +++++++++++++++++++++++++--- MTMR/TouchBarController.swift | 27 +++++++++++++++++++++------ MTMR/defaultPreset.json | 4 ++-- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index 2ddffd3..ca1f5d8 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -11,25 +11,28 @@ extension Data { struct BarItemDefinition: Decodable { let type: ItemType let action: ActionType + let additionalParameters: [GeneralParameter] private enum CodingKeys: String, CodingKey { case type } - init(type: ItemType, action: ActionType) { + init(type: ItemType, action: ActionType, additionalParameters: [GeneralParameter]) { self.type = type self.action = action + self.additionalParameters = additionalParameters } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) let type = try container.decode(String.self, forKey: .type) let parametersDecoder = SupportedTypesHolder.sharedInstance.lookup(by: type) + let additionalParameters = try GeneralParameters(from: decoder).parameters if let result = try? parametersDecoder(decoder), case let (itemType, action) = result { - self.init(type: itemType, action: action) + self.init(type: itemType, action: action, additionalParameters: additionalParameters) } else { - self.init(type: .staticButton(title: "unknown"), action: .none) + self.init(type: .staticButton(title: "unknown"), action: .none, additionalParameters: additionalParameters) } } @@ -197,3 +200,22 @@ func ==(lhs: ActionType, rhs: ActionType) -> Bool { } } +enum GeneralParameter { + case width(_: CGFloat) +} + +struct GeneralParameters: Decodable { + let parameters: [GeneralParameter] + + fileprivate enum CodingKeys: String, CodingKey { + case width + } + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + var result: [GeneralParameter] = [] + if let value = try container.decodeIfPresent(CGFloat.self, forKey: .width) { + result.append(.width(value)) + } + parameters = result + } +} diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 9c5d092..53d0546 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -57,7 +57,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath) } let jsonData = try? Data(contentsOf: URL(fileURLWithPath: presetPath)) - let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none)] + let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, additionalParameters: [])] for item in jsonItems { let identifierString = item.type.identifierBase.appending(UUID().uuidString) @@ -92,16 +92,21 @@ class TouchBarController: NSObject, NSTouchBarDelegate { return nil } let action = self.action(forItem: item) - + var barItem: NSTouchBarItem! switch item.type { case .staticButton(title: let title): - return CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action) + barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action) case .appleScriptTitledButton(source: let source, refreshInterval: let interval): - return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval) + barItem = AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval) case .timeButton(formatTemplate: let template): - return TimeTouchBarItem(identifier: identifier, formatTemplate: template) + barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template) } - + for parameter in item.additionalParameters { + if case .width(let value) = parameter, let widthBarItem = barItem as? CanSetWidth { + widthBarItem.setWidth(value: value) + } + } + return barItem } @@ -139,3 +144,13 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } +protocol CanSetWidth { + func setWidth(value: CGFloat) +} + +extension NSCustomTouchBarItem: CanSetWidth { + func setWidth(value: CGFloat) { + self.view.widthAnchor.constraint(equalToConstant: value).isActive = true + } +} + diff --git a/MTMR/defaultPreset.json b/MTMR/defaultPreset.json index 480d5a7..b4c87e5 100644 --- a/MTMR/defaultPreset.json +++ b/MTMR/defaultPreset.json @@ -3,8 +3,8 @@ { "type": "exitTouchbar" }, { "type": "brightnessDown" }, { "type": "brightnessUp" }, - { "type": "volumeDown" }, - { "type": "volumeUp" }, + { "type": "volumeDown", "width": 44 }, + { "type": "volumeUp", "width": 44 }, { "type": "previous" }, { "type": "play" }, { "type": "next" },