mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 00:58:37 +00:00
Add support for multiple actions and same trigger
This commit is contained in:
parent
f25fe57c7c
commit
3f0d9da721
@ -8,13 +8,25 @@
|
||||
|
||||
import Cocoa
|
||||
|
||||
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
|
||||
struct ItemAction {
|
||||
typealias TriggerClosure = (() -> Void)?
|
||||
var actions: [Action.Trigger: TriggerClosure] = [:] {
|
||||
|
||||
let trigger: Action.Trigger
|
||||
let closure: TriggerClosure
|
||||
|
||||
init(trigger: Action.Trigger, _ closure: TriggerClosure) {
|
||||
self.trigger = trigger
|
||||
self.closure = closure
|
||||
}
|
||||
}
|
||||
|
||||
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
|
||||
|
||||
var actions: [ItemAction] = [] {
|
||||
didSet {
|
||||
multiClick.isDoubleClickEnabled = actions[.doubleTap] != nil
|
||||
multiClick.isTripleClickEnabled = actions[.tripleTap] != nil
|
||||
longClick.isEnabled = actions[.longTap] != nil
|
||||
multiClick.isDoubleClickEnabled = actions.filter({ $0.trigger == .doubleTap }).count > 0
|
||||
multiClick.isTripleClickEnabled = actions.filter({ $0.trigger == .tripleTap }).count > 0
|
||||
longClick.isEnabled = actions.filter({ $0.trigger == .longTap }).count > 0
|
||||
}
|
||||
}
|
||||
var finishViewConfiguration: ()->() = {}
|
||||
@ -123,26 +135,29 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
||||
return true
|
||||
}
|
||||
|
||||
func callActions(for trigger: Action.Trigger) {
|
||||
let itemActions = self.actions.filter { $0.trigger == trigger }
|
||||
for itemAction in itemActions {
|
||||
itemAction.closure?()
|
||||
}
|
||||
}
|
||||
|
||||
@objc func handleGestureSingleTap() {
|
||||
guard let singleTap = self.actions[.singleTap] else { return }
|
||||
singleTap?()
|
||||
callActions(for: .singleTap)
|
||||
}
|
||||
|
||||
@objc func handleGestureDoubleTap() {
|
||||
guard let doubleTap = self.actions[.doubleTap] else { return }
|
||||
doubleTap?()
|
||||
callActions(for: .doubleTap)
|
||||
}
|
||||
|
||||
@objc func handleGestureTripleTap() {
|
||||
guard let tripleTap = self.actions[.tripleTap] else { return }
|
||||
tripleTap?()
|
||||
callActions(for: .tripleTap)
|
||||
}
|
||||
|
||||
@objc func handleGestureLong(gr: NSPressGestureRecognizer) {
|
||||
switch gr.state {
|
||||
case .possible: // tiny hack because we're calling action manually
|
||||
guard let longTap = self.actions[.longTap] else { return }
|
||||
longTap?()
|
||||
callActions(for: .longTap)
|
||||
break
|
||||
default:
|
||||
break
|
||||
|
||||
@ -323,15 +323,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
}
|
||||
|
||||
if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem {
|
||||
item.actions[.singleTap] = action
|
||||
item.actions.append(ItemAction(trigger: .singleTap, action))
|
||||
}
|
||||
if let longAction = self.longAction(forItem: item), let item = barItem as? CustomButtonTouchBarItem {
|
||||
item.actions[.longTap] = longAction
|
||||
item.actions.append(ItemAction(trigger: .longTap, longAction))
|
||||
}
|
||||
|
||||
if let touchBarItem = barItem as? CustomButtonTouchBarItem {
|
||||
for action in item.actions {
|
||||
touchBarItem.actions[action.trigger] = self.closure(for: action)
|
||||
touchBarItem.actions.append(ItemAction(trigger: action.trigger, self.closure(for: action)))
|
||||
}
|
||||
}
|
||||
if case let .bordered(bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem {
|
||||
|
||||
@ -82,12 +82,14 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem {
|
||||
public func createAppButton(for app: DockItem) -> DockBarItem {
|
||||
let item = DockBarItem(app)
|
||||
item.isBordered = false
|
||||
item.actions[.singleTap] = { [weak self] in
|
||||
self?.switchToApp(app: app)
|
||||
}
|
||||
item.actions[.longTap] = { [weak self] in
|
||||
self?.handleHalfLongPress(item: app)
|
||||
}
|
||||
item.actions.append(contentsOf: [
|
||||
ItemAction(trigger: .singleTap) { [weak self] in
|
||||
self?.switchToApp(app: app)
|
||||
},
|
||||
ItemAction(trigger: .longTap) { [weak self] in
|
||||
self?.handleHalfLongPress(item: app)
|
||||
}
|
||||
])
|
||||
item.killAppClosure = {[weak self] in
|
||||
self?.handleLongPress(item: app)
|
||||
}
|
||||
|
||||
@ -11,7 +11,7 @@ class DarkModeBarItem: CustomButtonTouchBarItem, Widget {
|
||||
isBordered = false
|
||||
setWidth(value: 24)
|
||||
|
||||
actions[.singleTap] = { [weak self] in self?.DarkModeToggle() }
|
||||
actions.append(ItemAction(trigger: .singleTap) { [weak self] in self?.DarkModeToggle() })
|
||||
|
||||
timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(refresh), userInfo: nil, repeats: true)
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ class DnDBarItem: CustomButtonTouchBarItem {
|
||||
isBordered = false
|
||||
setWidth(value: 32)
|
||||
|
||||
actions[.singleTap] = { [weak self] in self?.DnDToggle() }
|
||||
actions.append(ItemAction(trigger: .singleTap) { [weak self] in self?.DnDToggle() })
|
||||
|
||||
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(refresh), userInfo: nil, repeats: true)
|
||||
|
||||
|
||||
@ -18,9 +18,10 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
|
||||
|
||||
observeIputSourceChangedNotification()
|
||||
textInputSourceDidChange()
|
||||
actions[.singleTap] = { [weak self] in
|
||||
|
||||
actions.append(ItemAction(trigger: .singleTap) { [weak self] in
|
||||
self?.switchInputSource()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
required init?(coder _: NSCoder) {
|
||||
|
||||
@ -43,9 +43,9 @@ class MusicBarItem: CustomButtonTouchBarItem {
|
||||
isBordered = false
|
||||
|
||||
actions = [
|
||||
.singleTap: { [weak self] in self?.playPause() },
|
||||
.doubleTap: { [weak self] in self?.previousTrack() },
|
||||
.longTap: { [weak self] in self?.nextTrack() }
|
||||
ItemAction(trigger: .singleTap) { [weak self] in self?.playPause() },
|
||||
ItemAction(trigger: .doubleTap) { [weak self] in self?.previousTrack() },
|
||||
ItemAction(trigger: .longTap) { [weak self] in self?.nextTrack() }
|
||||
]
|
||||
|
||||
refreshAndSchedule()
|
||||
|
||||
@ -30,8 +30,8 @@ class NightShiftBarItem: CustomButtonTouchBarItem {
|
||||
super.init(identifier: identifier, title: "")
|
||||
isBordered = false
|
||||
setWidth(value: 28)
|
||||
|
||||
actions[.singleTap] = { [weak self] in self?.nightShiftAction() }
|
||||
|
||||
actions.append(ItemAction(trigger: .singleTap) { [weak self] in self?.nightShiftAction() })
|
||||
|
||||
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(refresh), userInfo: nil, repeats: true)
|
||||
|
||||
|
||||
@ -51,10 +51,10 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
|
||||
self.workTime = workTime
|
||||
self.restTime = restTime
|
||||
super.init(identifier: identifier, title: defaultTitle)
|
||||
actions = [
|
||||
.singleTap: { [weak self] in self?.startStopWork() },
|
||||
.longTap: { [weak self] in self?.startStopRest() }
|
||||
]
|
||||
actions.append(contentsOf: [
|
||||
ItemAction(trigger: .singleTap) { [weak self] in self?.startStopWork() },
|
||||
ItemAction(trigger: .longTap) { [weak self] in self?.startStopRest() }
|
||||
])
|
||||
}
|
||||
|
||||
required init?(coder _: NSCoder) {
|
||||
|
||||
@ -75,9 +75,9 @@ class UpNextScrubberTouchBarItem: NSCustomTouchBarItem {
|
||||
let item = UpNextItem(event: event)
|
||||
item.backgroundColor = self.getBackgroundColor(startDate: event.startDate)
|
||||
// Bind tap event
|
||||
item.actions[.singleTap] = { [weak self] in
|
||||
item.actions.append(ItemAction(trigger: .singleTap) { [weak self] in
|
||||
self?.switchToApp(event: event)
|
||||
}
|
||||
})
|
||||
// Add to view
|
||||
self.items.append(item)
|
||||
// Check if should display any more
|
||||
|
||||
@ -50,8 +50,13 @@ class YandexWeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate
|
||||
manager.delegate = self
|
||||
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
|
||||
manager.startUpdatingLocation()
|
||||
|
||||
actions[.singleTap] = actions[.singleTap] ?? defaultTapAction
|
||||
|
||||
if actions.filter({ $0.trigger == .singleTap }).isEmpty {
|
||||
actions.append(ItemAction(
|
||||
trigger: .singleTap,
|
||||
defaultTapAction
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
required init?(coder _: NSCoder) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user