diff options
| author | Tommaso Negri <me@tommasongr.com> | 2026-08-28 11:53:38 +0200 |
|---|---|---|
| committer | Tommaso Negri <me@tommasongr.com> | 2026-08-28 11:53:38 +0200 |
| commit | cdabaa50128c633f2097338a04aeb05c8bc408e4 (patch) | |
| tree | d3afff8fce28dbbb6d5a4d2d886ae7445e2b7df4 | |
| parent | a984283a806a7c85aebc605943ea5e0db941f5e3 (diff) | |
| download | bar-cdabaa50128c633f2097338a04aeb05c8bc408e4.tar.gz bar-cdabaa50128c633f2097338a04aeb05c8bc408e4.zip | |
move gestures to the cell level
| -rw-r--r-- | Sources/bar/Views/BlockView.swift | 29 | ||||
| -rw-r--r-- | Sources/bar/Views/CellView.swift | 2 | ||||
| -rw-r--r-- | Sources/bar/bar.docc/gestures.md | 7 |
3 files changed, 23 insertions, 15 deletions
diff --git a/Sources/bar/Views/BlockView.swift b/Sources/bar/Views/BlockView.swift index 4158f85..00d36aa 100644 --- a/Sources/bar/Views/BlockView.swift +++ b/Sources/bar/Views/BlockView.swift @@ -3,16 +3,22 @@ import SwiftUI struct BlockView: View { @ObservedObject var block: Block - @State private var hovering = false + @State private var hoveredCell: Cell? @State private var eventsMonitor: Any? var body: some View { HStack(spacing: 0) { ForEach(block.cells) { cell in CellView(cell: cell) + .onHover { hovering in + if hovering { + hoveredCell = cell + } else if hoveredCell?.id == cell.id { + hoveredCell = nil + } + } } } - .onHover { hovering = $0 } .onAppear { let monitor = NSEvent.addLocalMonitorForEvents(matching: [ .leftMouseDown, @@ -21,7 +27,7 @@ struct BlockView: View { .scrollWheel, ]) { [weak block] event in guard let block else { return event } - return handle(event: event, block: block) + return self.handle(event: event, block: block) } self.eventsMonitor = monitor as Any } @@ -33,25 +39,26 @@ struct BlockView: View { } } - /// Processes an event when the block is hovered, re-running the script - /// with the appropriate environment variable. + /// Processes an event when a cell is hovered, re-running the script + /// with the appropriate environment variable and the hovered cell's + /// content. /// /// - Returns: The original event so the system continues normal delivery. private func handle(event: NSEvent, block: Block) -> NSEvent { - guard hovering else { return event } + guard let cell = hoveredCell else { return event } switch event.type { case .leftMouseDown, .rightMouseDown, .otherMouseDown: - block.runScript(env: ["BUTTON": String(event.buttonNumber)]) + block.runScript(env: ["BUTTON": String(event.buttonNumber), "CELL": cell.rawValue]) case .scrollWheel: if event.scrollingDeltaY > 0 { - block.runScript(env: ["SCROLL": "UP"]) + block.runScript(env: ["SCROLL": "UP", "CELL": cell.rawValue]) } else if event.scrollingDeltaY < 0 { - block.runScript(env: ["SCROLL": "DOWN"]) + block.runScript(env: ["SCROLL": "DOWN", "CELL": cell.rawValue]) } else if event.scrollingDeltaX > 0 { - block.runScript(env: ["SCROLL": "RIGHT"]) + block.runScript(env: ["SCROLL": "RIGHT", "CELL": cell.rawValue]) } else if event.scrollingDeltaX < 0 { - block.runScript(env: ["SCROLL": "LEFT"]) + block.runScript(env: ["SCROLL": "LEFT", "CELL": cell.rawValue]) } default: break diff --git a/Sources/bar/Views/CellView.swift b/Sources/bar/Views/CellView.swift index 6b39eb1..e902b20 100644 --- a/Sources/bar/Views/CellView.swift +++ b/Sources/bar/Views/CellView.swift @@ -1,7 +1,7 @@ import SwiftUI struct CellView: View { - var cell: Cell + let cell: Cell var body: some View { Text(cell.rawValue) diff --git a/Sources/bar/bar.docc/gestures.md b/Sources/bar/bar.docc/gestures.md index e9cceed..ec1939c 100644 --- a/Sources/bar/bar.docc/gestures.md +++ b/Sources/bar/bar.docc/gestures.md @@ -1,15 +1,16 @@ # Gestures -React to click or scroll gestures on a block. +React to click or scroll gestures. -Each time you click or scroll over a ``Block``, the command is executed and you can optionally react to it. +Each time you click or scroll over a ``Cell``, the command is executed and you can optionally react to it. ## Environment variables -When a ``Block/command`` is executed by a gesture, a couple environment variables are set to let you know what just happened. +When a ``Block/command`` is executed by a gesture, a few environment variables are set to let you know what just happened. | Variable | What holds | When set | Example value | |---|---|---|---| +| `CELL` | The ``Cell/rawValue`` of the targeted ``Cell`` | Always | `" 1 "`, `" Fri 30, 12:30:00"`, `" 50% "`, etc | | `BUTTON` | The mouse button number | On click | `"0"` (left), `"1"` (right), `"2"` (middle), etc | | `SCROLL` | The scroll direction | On scroll | `"UP"`, `"DOWN"`, `"LEFT"`, `"RIGHT"` | |
