1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 17:38:38 +00:00

custom width

This commit is contained in:
Serg 2018-04-11 00:56:14 +07:00
parent 0ba5984217
commit 11c8d167c0
3 changed files with 48 additions and 11 deletions

View File

@ -11,25 +11,28 @@ extension Data {
struct BarItemDefinition: Decodable { struct BarItemDefinition: Decodable {
let type: ItemType let type: ItemType
let action: ActionType let action: ActionType
let additionalParameters: [GeneralParameter]
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case type case type
} }
init(type: ItemType, action: ActionType) { init(type: ItemType, action: ActionType, additionalParameters: [GeneralParameter]) {
self.type = type self.type = type
self.action = action self.action = action
self.additionalParameters = additionalParameters
} }
init(from decoder: Decoder) throws { init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self) let container = try decoder.container(keyedBy: CodingKeys.self)
let type = try container.decode(String.self, forKey: .type) let type = try container.decode(String.self, forKey: .type)
let parametersDecoder = SupportedTypesHolder.sharedInstance.lookup(by: type) let parametersDecoder = SupportedTypesHolder.sharedInstance.lookup(by: type)
let additionalParameters = try GeneralParameters(from: decoder).parameters
if let result = try? parametersDecoder(decoder), if let result = try? parametersDecoder(decoder),
case let (itemType, action) = result { case let (itemType, action) = result {
self.init(type: itemType, action: action) self.init(type: itemType, action: action, additionalParameters: additionalParameters)
} else { } 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
}
}

View File

@ -57,7 +57,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath) try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath)
} }
let jsonData = try? Data(contentsOf: URL(fileURLWithPath: 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 { for item in jsonItems {
let identifierString = item.type.identifierBase.appending(UUID().uuidString) let identifierString = item.type.identifierBase.appending(UUID().uuidString)
@ -92,16 +92,21 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
return nil return nil
} }
let action = self.action(forItem: item) let action = self.action(forItem: item)
var barItem: NSTouchBarItem!
switch item.type { switch item.type {
case .staticButton(title: let title): 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): 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): 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
}
}

View File

@ -3,8 +3,8 @@
{ "type": "exitTouchbar" }, { "type": "exitTouchbar" },
{ "type": "brightnessDown" }, { "type": "brightnessDown" },
{ "type": "brightnessUp" }, { "type": "brightnessUp" },
{ "type": "volumeDown" }, { "type": "volumeDown", "width": 44 },
{ "type": "volumeUp" }, { "type": "volumeUp", "width": 44 },
{ "type": "previous" }, { "type": "previous" },
{ "type": "play" }, { "type": "play" },
{ "type": "next" }, { "type": "next" },