mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
## Problem Two related gaps in global back/forward navigation. Fixes #3775. 1. The keyboard shortcuts almost never fire in real use — users fall back to clicking the toolbar chevrons and assume the shortcuts don't exist. 2. On macOS, mouse back/forward buttons (X1/X2) and horizontal swipe gestures do nothing, although they navigate in every browser and in Slack. **Duplicate check:** searched open PRs and issues — none found beyond #3775 (filed alongside this fix). #3078 / #3377 are next/previous-*channel* navigation, a different feature. ## Root causes **Keyboard:** `useBackForwardControls`'s keydown handler bailed whenever the event target was editable — but `useComposerAutofocus` deliberately focuses the message composer (a ProseMirror contenteditable) on mount and on every channel switch. In steady state focus almost always lives in the composer, so the chords were silently swallowed. Invisible to CI because `navigation.spec.ts` only ever clicked the `global-back` / `global-forward` buttons, never pressed the keys. **Mouse/swipe:** on macOS, WKWebView never delivers X1/X2 button events or swipe gestures to the page (Safari handles them natively in the app layer, not in page JS), and Buzz had no native handler. ## Fix ### Keyboard chords (web layer) Match the existing platform chord regardless of the event target and drop the editable-target guard: - `⌘[` / `⌘]` have no text-editing semantics in macOS text fields, and the TipTap/StarterKit editor config binds no `Mod-[` / `Mod-]` shortcuts (checked `useRichTextEditor.ts` — list indentation is Tab/Shift-Tab). - `preventDefault()` keeps the chord out of the editor — asserted in the e2e test. This matches browsers and Slack, where back/forward chords work while a text field is focused. Chord matching is extracted into a pure helper, `app/navigation/backForwardChords.ts`, so it can be unit tested; behavior (bindings, modifier exclusivity, `code`-based matching for non-US layouts) is unchanged. ### macOS mouse buttons and swipe gestures (native layer) An NSEvent local monitor in `mouse_nav.rs` catches what the webview can't see and emits a `mouse-nav` Tauri event to the main window (`emit_to`, so navigation stays scoped if multi-window ever lands) that the frontend acts on. Two AppKit event shapes map to navigation: - `otherMouseUp` with button 3/4 — mice whose X1/X2 buttons arrive as plain button events. These are swallowed after emitting so nothing downstream double-handles them. - `swipe` with a horizontal delta — AppKit's page-swipe gesture (`swipeWithEvent:`): `deltaX > 0` back, `deltaX < 0` forward. Sent by mouse drivers that synthesize a page-swipe gesture for the back/forward buttons instead of button-3/4 events (the hardware this was verified on). Stock Apple trackpad and Magic Mouse swipes arrive as phased scroll-wheel events instead, which this PR does not handle — that path (`ScrollWheel` + `trackSwipeEventWithOptions:`, which also needs scroll-edge detection) is deferred to a follow-up. Swipes are passed through (swallowing mid-gesture events could confuse AppKit gesture tracking). The swipe path was verified end to end on hardware whose back/forward buttons emit only swipe gestures, never button-3/4 events — an instrumented event monitor confirmed the events arrive as `NSEventType::Swipe` with `deltaX ±1`, and navigation worked after mapping them. ## Tests - **13 unit tests** for the web-side chord matcher (`backForwardChords.test.mjs`): supported chords, modifier exclusivity, `code` fallback, and preservation of line-editing shortcuts. - **6 Rust unit tests** for the native mapping helpers (`mouse_nav.rs`): button 3/4 directions, other buttons ignored, swipe delta sign → direction, zero-delta (gesture-begin) ignored. - **e2e regression case** in `navigation.spec.ts`: presses the platform chord *while the composer is focused* — the missing coverage. Verified it fails against the pre-fix implementation and passes with the fix. - Full desktop unit suite: 3832/3832 pass. Full Rust suite (`cargo test`, buzz-desktop): 1888 passed / 0 failed. `pnpm typecheck`, `biome check`, `pnpm check`, `cargo fmt --check`, `cargo clippy`: clean (no new warnings). - Full Playwright e2e: 958 passed; 6 failures are relay-infrastructure tests (live relay seeding / relay state seam) that fail identically without this change — `navigation.spec.ts` is fully green. ## Manual test 1. Open a channel, then another (composer autofocuses on each switch). 2. `⌘[` — returns to the previous channel; `⌘]` — forward again. Typing `[` / `]` in the composer inserts normally. 3. Mouse back/forward buttons navigate the same way, from anywhere in the window (verified on macOS on hardware using both event shapes). ## Update — 2026-07-31 Removed the redundant DOM mouse-button handler after verifying it was unnecessary. The native macOS path remains unchanged and was revalidated manually. --------- Signed-off-by: npub1yvnq5equak5errqpku8stskushny9wsvt0fc2ywcpwt79yslwaqswe7tse <23260a641ceda9918c01b70f05c2dc85e642ba0c5bd38511d80b97e2921f7741@buzz.block.builderlab.xyz> Signed-off-by: Matheus Iser <matheusiser@squareup.com> Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Co-authored-by: npub1yvnq5equak5errqpku8stskushny9wsvt0fc2ywcpwt79yslwaqswe7tse <23260a641ceda9918c01b70f05c2dc85e642ba0c5bd38511d80b97e2921f7741@buzz.block.builderlab.xyz> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz> Co-authored-by: Will Pfleger <pfleger.will@gmail.com>
143 lines
5.3 KiB
Rust
143 lines
5.3 KiB
Rust
//! Native macOS handler for back/forward navigation inputs (mouse X1/X2
|
|
//! buttons and horizontal swipe gestures).
|
|
//!
|
|
//! WKWebView never delivers these inputs to the web content layer, so a DOM
|
|
//! listener can't see them (Safari itself handles them natively in the app
|
|
//! layer, not in the page). This module installs an NSEvent local monitor
|
|
//! and emits a `mouse-nav` Tauri event that `useBackForwardControls` acts on
|
|
//! in the frontend. Two event shapes map to navigation:
|
|
//!
|
|
//! - `otherMouseUp` with button 3/4 — mice whose X1/X2 buttons reach the app
|
|
//! as plain mouse buttons.
|
|
//! - `swipe` with a horizontal delta — AppKit's page-swipe gesture
|
|
//! (`swipeWithEvent:`): `deltaX > 0` is back, `deltaX < 0` is forward.
|
|
//! Sent by mouse drivers that synthesize a page-swipe gesture for the
|
|
//! back/forward buttons instead of button-3/4 events (the hardware this
|
|
//! was verified on). Stock Apple trackpad and Magic Mouse swipes arrive
|
|
//! as phased scroll-wheel events instead, which this module does not
|
|
//! handle — that path (`ScrollWheel` + `trackSwipeEventWithOptions:`,
|
|
//! which also needs scroll-edge detection) is a follow-up.
|
|
//!
|
|
//! Compiled macOS-only (via `tray_menu`). Non-macOS X1/X2 behavior is left
|
|
//! to the underlying webview.
|
|
|
|
/// Maps an `otherMouseUp` button number to a navigation direction.
|
|
/// Buttons 3 and 4 are X1 (back) and X2 (forward).
|
|
fn direction_for_button(button: isize) -> Option<&'static str> {
|
|
match button {
|
|
3 => Some("back"),
|
|
4 => Some("forward"),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// Maps a swipe gesture's horizontal delta to a navigation direction,
|
|
/// following the AppKit `swipeWithEvent:` convention: positive is back,
|
|
/// negative is forward. A swipe arrives as a begin/end pair and only the
|
|
/// end event carries the direction, so `deltaX == 0` maps to `None`.
|
|
fn direction_for_swipe(delta_x: f64) -> Option<&'static str> {
|
|
if delta_x > 0.0 {
|
|
Some("back")
|
|
} else if delta_x < 0.0 {
|
|
Some("forward")
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
pub fn init<R: tauri::Runtime>(app_handle: &tauri::AppHandle<R>) {
|
|
use block2::RcBlock;
|
|
use objc2_app_kit::{NSEvent, NSEventMask, NSEventType};
|
|
use tauri::Emitter;
|
|
|
|
let app = app_handle.clone();
|
|
let block = RcBlock::new(move |event: std::ptr::NonNull<NSEvent>| -> *mut NSEvent {
|
|
// SAFETY: the monitor hands us a valid NSEvent for the matched mask.
|
|
let ev = unsafe { event.as_ref() };
|
|
|
|
match ev.r#type() {
|
|
NSEventType::OtherMouseUp => {
|
|
if let Some(direction) = direction_for_button(ev.buttonNumber()) {
|
|
// Emit to the main window explicitly instead of
|
|
// broadcasting (`emit`) so navigation stays scoped if
|
|
// multi-window ever lands. "main" is the default label
|
|
// for the single configured window (see deep_link.rs).
|
|
let _ = app.emit_to("main", "mouse-nav", direction);
|
|
// Swallow the release: nothing downstream should also act
|
|
// on it. The matching press deliberately passes through:
|
|
// WKWebView never delivers X1/X2 to the page, so the
|
|
// unmatched down is inert, and swallowing presses risks
|
|
// interfering with AppKit behaviors keyed off mouse-down.
|
|
return std::ptr::null_mut();
|
|
}
|
|
}
|
|
NSEventType::Swipe => {
|
|
if let Some(direction) = direction_for_swipe(ev.deltaX()) {
|
|
let _ = app.emit_to("main", "mouse-nav", direction);
|
|
}
|
|
// Pass swipes through: nothing else navigates on them, and
|
|
// swallowing mid-gesture events could confuse AppKit's
|
|
// gesture tracking.
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
event.as_ptr()
|
|
});
|
|
|
|
// SAFETY: the block returns either null or the pointer it was given, both
|
|
// valid per the monitor contract. The returned monitor token is
|
|
// deliberately leaked: the monitor must live for the whole app lifetime.
|
|
let monitor = unsafe {
|
|
NSEvent::addLocalMonitorForEventsMatchingMask_handler(
|
|
NSEventMask::OtherMouseUp | NSEventMask::Swipe,
|
|
&block,
|
|
)
|
|
};
|
|
|
|
if let Some(monitor) = monitor {
|
|
std::mem::forget(monitor);
|
|
} else {
|
|
eprintln!("buzz-desktop: mouse-nav: failed to install NSEvent monitor");
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn button_3_is_back() {
|
|
assert_eq!(direction_for_button(3), Some("back"));
|
|
}
|
|
|
|
#[test]
|
|
fn button_4_is_forward() {
|
|
assert_eq!(direction_for_button(4), Some("forward"));
|
|
}
|
|
|
|
#[test]
|
|
fn other_buttons_do_not_navigate() {
|
|
for button in [0, 1, 2, 5, -1] {
|
|
assert_eq!(direction_for_button(button), None);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn positive_swipe_delta_is_back() {
|
|
assert_eq!(direction_for_swipe(1.0), Some("back"));
|
|
assert_eq!(direction_for_swipe(0.5), Some("back"));
|
|
}
|
|
|
|
#[test]
|
|
fn negative_swipe_delta_is_forward() {
|
|
assert_eq!(direction_for_swipe(-1.0), Some("forward"));
|
|
assert_eq!(direction_for_swipe(-0.5), Some("forward"));
|
|
}
|
|
|
|
#[test]
|
|
fn zero_delta_swipe_begin_event_is_ignored() {
|
|
assert_eq!(direction_for_swipe(0.0), None);
|
|
}
|
|
}
|