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

App id matching (#432)

* Add app id matching for buttons

* Hide MT button from  control strip when touch bar is empty

Co-authored-by: Wiktor Latanowicz <wiktor@latanowicz.com>
Co-authored-by: Anton Palgunov <Toxblh@users.noreply.github.com>
This commit is contained in:
Wiktor Latanowicz 2022-02-16 20:01:09 +01:00 committed by GitHub
parent ac0e44db4d
commit 36bf749a46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 13 deletions

View File

@ -658,6 +658,7 @@ enum GeneralParameter {
case bordered(_: Bool) case bordered(_: Bool)
case background(_: NSColor) case background(_: NSColor)
case title(_: String) case title(_: String)
case matchAppId(_: String)
} }
struct GeneralParameters: Decodable { struct GeneralParameters: Decodable {
@ -670,6 +671,7 @@ struct GeneralParameters: Decodable {
case bordered case bordered
case background case background
case title case title
case matchAppId
} }
init(from decoder: Decoder) throws { init(from decoder: Decoder) throws {
@ -699,6 +701,10 @@ struct GeneralParameters: Decodable {
result[.title] = .title(title) result[.title] = .title(title)
} }
if let matchAppId = try container.decodeIfPresent(String.self, forKey: .matchAppId) {
result[.matchAppId] = .matchAppId(matchAppId)
}
parameters = result parameters = result
} }
} }

View File

@ -67,4 +67,12 @@ class SwipeItem: NSCustomTouchBarItem {
} }
} }
} }
func isEqual(_ object: AnyObject?) -> Bool {
if let object = object as? SwipeItem {
return self.scriptApple?.source as String? == object.scriptApple?.source as String? && self.scriptBash == object.scriptBash && self.direction == object.direction && self.fingers == object.fingers && self.minOffset == object.minOffset
} else {
return false
}
}
} }

View File

@ -134,11 +134,48 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
touchBar = NSTouchBar() touchBar = NSTouchBar()
jsonItems = newJsonItems jsonItems = newJsonItems
itemDefinitions = [:] itemDefinitions = [:]
items = [:]
loadItemDefinitions(jsonItems: jsonItems) loadItemDefinitions(jsonItems: jsonItems)
updateActiveApp()
}
func didItemsChange(prevItems: [NSTouchBarItem.Identifier: NSTouchBarItem], prevSwipeItems: [SwipeItem]) -> Bool {
var changed = items.count != prevItems.count || swipeItems.count != prevSwipeItems.count
if !changed {
for (item, prevItem) in zip(items, prevItems) {
if item.key != prevItem.key {
changed = true
break
}
}
}
if !changed {
for (swipeItem, prevSwipeItem) in zip(swipeItems, prevSwipeItems) {
if !swipeItem.isEqual(prevSwipeItem) {
changed = true
break
}
}
}
return changed
}
func prepareTouchBar() {
let prevItems = items
let prevSwipeItems = swipeItems
createItems() createItems()
let changed = didItemsChange(prevItems: prevItems, prevSwipeItems: prevSwipeItems)
if !changed {
return
}
let centerItems = centerIdentifiers.compactMap({ (identifier) -> NSTouchBarItem? in let centerItems = centerIdentifiers.compactMap({ (identifier) -> NSTouchBarItem? in
items[identifier] items[identifier]
}) })
@ -146,6 +183,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
let centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString)) let centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString))
let scrollArea = ScrollViewItem(identifier: centerScrollArea, items: centerItems) let scrollArea = ScrollViewItem(identifier: centerScrollArea, items: centerItems)
basicViewIdentifier = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollView.".appending(UUID().uuidString))
touchBar.delegate = self touchBar.delegate = self
touchBar.defaultItemIdentifiers = [basicViewIdentifier] touchBar.defaultItemIdentifiers = [basicViewIdentifier]
@ -158,8 +197,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
basicView = BasicView(identifier: basicViewIdentifier, items:leftItems + [scrollArea] + rightItems, swipeItems: swipeItems) basicView = BasicView(identifier: basicViewIdentifier, items:leftItems + [scrollArea] + rightItems, swipeItems: swipeItems)
basicView?.legacyGesturesEnabled = AppSettings.multitouchGestures basicView?.legacyGesturesEnabled = AppSettings.multitouchGestures
updateActiveApp()
} }
@objc func activeApplicationChanged(_: Notification) { @objc func activeApplicationChanged(_: Notification) {
@ -170,10 +207,19 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
if frontmostApplicationIdentifier != nil && blacklistAppIdentifiers.firstIndex(of: frontmostApplicationIdentifier!) != nil { if frontmostApplicationIdentifier != nil && blacklistAppIdentifiers.firstIndex(of: frontmostApplicationIdentifier!) != nil {
dismissTouchBar() dismissTouchBar()
} else { } else {
presentTouchBar() prepareTouchBar()
if touchBarContainsAnyItems() {
presentTouchBar()
} else {
dismissTouchBar()
}
} }
} }
func touchBarContainsAnyItems() -> Bool {
return items.count != 0 || swipeItems.count != 0
}
func reloadStandardConfig() { func reloadStandardConfig() {
let presetPath = standardConfigPath let presetPath = standardConfigPath
if !FileManager.default.fileExists(atPath: presetPath), if !FileManager.default.fileExists(atPath: presetPath),
@ -212,12 +258,29 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
func createItems() { func createItems() {
items = [:]
swipeItems = []
for (identifier, definition) in itemDefinitions { for (identifier, definition) in itemDefinitions {
let item = createItem(forIdentifier: identifier, definition: definition) var show = true
if item is SwipeItem {
swipeItems.append(item as! SwipeItem) if let frontApp = frontmostApplicationIdentifier {
} else { if case let .matchAppId(regexString)? = definition.additionalParameters[.matchAppId] {
items[identifier] = item let regex = try! NSRegularExpression(pattern: regexString)
let range = NSRange(location: 0, length: frontApp.count)
if regex.firstMatch(in: frontApp, range: range) == nil {
show = false
}
}
}
if show {
let item = createItem(forIdentifier: identifier, definition: definition)
if item is SwipeItem {
swipeItems.append(item as! SwipeItem)
} else {
items[identifier] = item
}
} }
} }
} }
@ -231,26 +294,29 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
func updateControlStripPresence() { func updateControlStripPresence() {
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true) let showMtmrButtonOnControlStrip = touchBarContainsAnyItems()
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, showMtmrButtonOnControlStrip)
} }
@objc private func presentTouchBar() { @objc private func presentTouchBar() {
if AppSettings.showControlStripState { if AppSettings.showControlStripState {
updateControlStripPresence()
presentSystemModal(touchBar, systemTrayItemIdentifier: .controlStripItem) presentSystemModal(touchBar, systemTrayItemIdentifier: .controlStripItem)
} else { } else {
presentSystemModal(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem) presentSystemModal(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)
} }
updateControlStripPresence()
} }
@objc private func dismissTouchBar() { @objc private func dismissTouchBar() {
minimizeSystemModal(touchBar) if touchBarContainsAnyItems() {
minimizeSystemModal(touchBar)
}
updateControlStripPresence() updateControlStripPresence()
} }
@objc func resetControlStrip() { @objc func resetControlStrip() {
dismissTouchBar() dismissTouchBar()
presentTouchBar() updateActiveApp()
} }
func touchBar(_: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? { func touchBar(_: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {

View File

@ -488,6 +488,13 @@ by using background with color "#000000" and bordered == false you can create bu
} }
``` ```
- `matchAppId` displays the button only when active app's id matches given regexp
```json
"matchAppId": "Safari"
```
## Troubleshooting ## Troubleshooting
#### If you can't open preferences: #### If you can't open preferences: