diff options
| author | Tommaso Negri <me@tommasongr.com> | 2026-08-09 15:33:51 +0200 |
|---|---|---|
| committer | Tommaso Negri <me@tommasongr.com> | 2026-08-09 15:33:51 +0200 |
| commit | 0cdfc18daebd33d3ce0654257ccda1786eadeffd (patch) | |
| tree | e7b723d373ab0c77e7a4fe6e2cfd8ae4ffcaf70f /Sources/phbar/Views/BlockView.swift | |
| parent | d7bc6c009bb728aff82032162095228ce12e2495 (diff) | |
| download | bar-0cdfc18daebd33d3ce0654257ccda1786eadeffd.tar.gz bar-0cdfc18daebd33d3ce0654257ccda1786eadeffd.zip | |
allow blocks to react to clicks and scrolls
Diffstat (limited to 'Sources/phbar/Views/BlockView.swift')
| -rw-r--r-- | Sources/phbar/Views/BlockView.swift | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Sources/phbar/Views/BlockView.swift b/Sources/phbar/Views/BlockView.swift index 4bbea7a..85d6e6e 100644 --- a/Sources/phbar/Views/BlockView.swift +++ b/Sources/phbar/Views/BlockView.swift @@ -3,11 +3,58 @@ import SwiftUI struct BlockView: View { @ObservedObject var block: Block + @State private var hovering = false + var body: some View { HStack(spacing: 0) { ForEach(block.cells) { cell in CellView(cell: cell) } } + .onHover { hovering = $0 } + .onAppear { + NSEvent.addLocalMonitorForEvents(matching: [ + .leftMouseDown, + .rightMouseDown, + .otherMouseDown, + .scrollWheel, + ]) { event in + handle(event: event) + } + } + } + + private func handle(event: NSEvent) -> NSEvent { + guard hovering else { return event } + + switch event.type { + case .leftMouseDown, .rightMouseDown, .otherMouseDown: + block.runScript(env: ["BUTTON": String(event.buttonNumber)]) + break + case .scrollWheel: + if event.scrollingDeltaY > 0 { + block.runScript(env: ["SCROLL": "UP"]) + break + } + + if event.scrollingDeltaY < 0 { + block.runScript(env: ["SCROLL": "DOWN"]) + break + } + + if event.scrollingDeltaX > 0 { + block.runScript(env: ["SCROLL": "RIGHT"]) + break + } + + if event.scrollingDeltaX < 0 { + block.runScript(env: ["SCROLL": "LEFT"]) + break + } + default: + break + } + + return event } } |
