diff --git a/MTMR/Base.lproj/Main.storyboard b/MTMR/Base.lproj/Main.storyboard index 925123a..3f295b2 100644 --- a/MTMR/Base.lproj/Main.storyboard +++ b/MTMR/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + @@ -619,7 +619,7 @@ - + diff --git a/MTMR/Info.plist b/MTMR/Info.plist index 0ac05db..fea29f8 100644 --- a/MTMR/Info.plist +++ b/MTMR/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString 0.21 CFBundleVersion - 207 + 252 LSApplicationCategoryType public.app-category.utilities LSMinimumSystemVersion diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index fabc858..fb3fd1c 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -197,9 +197,12 @@ class SupportedTypesHolder { ) }, - "dock": { _ in - ( - item: .dock(), + "dock": { decoder in + enum CodingKeys: String, CodingKey { case autoResize } + let container = try decoder.container(keyedBy: CodingKeys.self) + let autoResize = try container.decodeIfPresent(Bool.self, forKey: .autoResize) ?? false + return ( + item: .dock(autoResize: autoResize), action: .none, longAction: .none, parameters: [:] @@ -327,7 +330,7 @@ enum ItemType: Decodable { case appleScriptTitledButton(source: SourceProtocol, refreshInterval: Double) case timeButton(formatTemplate: String, timeZone: String?) case battery() - case dock() + case dock(autoResize: Bool) case volume() case brightness(refreshInterval: Double) case weather(interval: Double, units: String, api_key: String, icon_type: String) @@ -361,6 +364,7 @@ enum ItemType: Decodable { case workTime case restTime case flip + case autoResize } enum ItemTypeRaw: String, Decodable { @@ -405,7 +409,8 @@ enum ItemType: Decodable { self = .battery() case .dock: - self = .dock() + let autoResize = try container.decodeIfPresent(Bool.self, forKey: .autoResize) ?? false + self = .dock(autoResize: autoResize) case .volume: self = .volume() diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index eab726f..1389460 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -27,7 +27,7 @@ extension ItemType { return "com.toxblh.mtmr.timeButton." case .battery(): return "com.toxblh.mtmr.battery." - case .dock(): + case .dock(autoResize: _): return "com.toxblh.mtmr.dock" case .volume(): return "com.toxblh.mtmr.volume" @@ -253,8 +253,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template, timeZone: timeZone) case .battery(): barItem = BatteryBarItem(identifier: identifier) - case .dock: - barItem = AppScrubberTouchBarItem(identifier: identifier) + case let .dock(autoResize: autoResize): + barItem = AppScrubberTouchBarItem(identifier: identifier, autoResize: autoResize) case .volume: if case let .image(source)? = item.additionalParameters[.image] { barItem = VolumeViewController(identifier: identifier, image: source.image) diff --git a/MTMR/Widgets/AppScrubberTouchBarItem.swift b/MTMR/Widgets/AppScrubberTouchBarItem.swift index 8d15966..7c5cd39 100644 --- a/MTMR/Widgets/AppScrubberTouchBarItem.swift +++ b/MTMR/Widgets/AppScrubberTouchBarItem.swift @@ -17,6 +17,8 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub private let minTicks: Int = 5 private let maxTicks: Int = 20 private var lastSelected: Int = 0 + private var autoResize: Bool = false + private var widthConstraint: NSLayoutConstraint? private var persistentAppIdentifiers: [String] = [] private var runningAppsIdentifiers: [String] = [] @@ -27,17 +29,25 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub } private var applications: [DockItem] = [] + + convenience override init(identifier: NSTouchBarItem.Identifier) { + self.init(identifier: identifier, autoResize: false) + } + + static var iconWidth = 36 + static var spacingWidth = 2 - override init(identifier: NSTouchBarItem.Identifier) { + init(identifier: NSTouchBarItem.Identifier, autoResize: Bool) { super.init(identifier: identifier) - + self.autoResize = autoResize + scrubber = NSScrubber() scrubber.delegate = self scrubber.dataSource = self scrubber.mode = .free // .fixed let layout = NSScrubberFlowLayout() - layout.itemSize = NSSize(width: 36, height: 32) - layout.itemSpacing = 2 + layout.itemSize = NSSize(width: AppScrubberTouchBarItem.iconWidth, height: 32) + layout.itemSpacing = CGFloat(AppScrubberTouchBarItem.spacingWidth) scrubber.scrubberLayout = layout scrubber.selectionBackgroundStyle = .roundedBackground scrubber.showsAdditionalContentIndicators = true @@ -79,9 +89,22 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub applications = newApplications applications += getDockPersistentAppsList() scrubber.reloadData() + updateSize() scrubber.selectedIndex = index ?? 0 } + + func updateSize() { + if self.autoResize { + if let constraint: NSLayoutConstraint = self.widthConstraint { + constraint.isActive = false + self.scrubber.removeConstraint(constraint) + } + let width = (AppScrubberTouchBarItem.iconWidth + AppScrubberTouchBarItem.spacingWidth) * self.applications.count - AppScrubberTouchBarItem.spacingWidth + self.widthConstraint = self.scrubber.widthAnchor.constraint(equalToConstant: CGFloat(width)) + self.widthConstraint!.isActive = true + } + } public func numberOfItems(for _: NSScrubber) -> Int { return applications.count diff --git a/README.md b/README.md index b0b10c2..8fc0f6c 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,15 @@ To close a group, use the button: }, ``` +#### `dock` +> Dock plugin +```js +{ + "type": "dock", + "autoResize": true +}, +``` + ## Actions: - `hidKey` > https://github.com/aosm/IOHIDFamily/blob/master/IOHIDSystem/IOKit/hidsystem/ev_keymap.h use only numbers