mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
custom width
This commit is contained in:
parent
0ba5984217
commit
11c8d167c0
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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" },
|
||||
|
||||
Loading…
Reference in New Issue
Block a user