quadraui 0.6.0

Cross-platform Rust UI toolkit: write one AppLogic and run it unmodified on TUI, GTK4 (Linux), macOS, and Windows backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! `ContextMenuController` — the "one call" context-menu entry point,
//! composed on top of the existing [`ContextMenu`] primitive,
//! [`Backend::draw_context_menu`] rasteriser, [`Backend::show_context_menu`]
//! native path, and [`ModalStack`].
//!
//! An app that wants a right-click menu writes exactly one code path,
//! regardless of platform or [`MenuStyle`]:
//! ```ignore
//! // Setup: one field on the app.
//! struct MyApp {
//!     ctx_menu: ContextMenuController,
//!     // ...
//! }
//!
//! // In handle(), before other routing — mirrors `MenuSystem::handle`'s
//! // contract:
//! fn handle(&mut self, event: UiEvent, backend: &mut dyn Backend) -> Reaction {
//!     if let ContextMenuOutcome::Event(ev) = self.ctx_menu.handle(&event, backend) {
//!         return self.handle_menu_event(ev); // same UiEvent::ContextMenuItemActivated
//!                                             // / ContextMenuDismissed either path took
//!     }
//!     match event {
//!         UiEvent::MouseDown { button: MouseButton::Right, position, .. } => {
//!             let menu = self.build_context_menu();
//!             // The one call: native on macOS under `Auto`, painted
//!             // everywhere else — same call either way.
//!             self.ctx_menu.open(menu, position, backend);
//!             Reaction::Redraw
//!         }
//!         // A native-path activation/dismissal also arrives here as a
//!         // top-level event (queued by `Backend::show_context_menu`) —
//!         // route it through the same handler for one code path.
//!         UiEvent::ContextMenuItemActivated(id) => {
//!             self.handle_menu_event(UiEvent::ContextMenuItemActivated(id))
//!         }
//!         UiEvent::ContextMenuDismissed => {
//!             self.handle_menu_event(UiEvent::ContextMenuDismissed)
//!         }
//!         _ => Reaction::Continue,
//!     }
//! }
//!
//! // In render(), after base content:
//! fn render(&self, backend: &mut dyn Backend, area: ()) {
//!     // ... paint the app's own content first ...
//!     self.ctx_menu.render(backend);
//! }
//! ```
//!
//! # Why this exists instead of a `Backend` default method
//!
//! [`Backend::effective_menu_style`] resolves [`MenuStyle`] purely (no
//! state needed), but *acting* on a `Custom` resolution needs somewhere
//! to keep "a menu is open, at this layout, with this selection" between
//! the `MouseDown` that opened it and the click/keypress that closes it.
//! A bare `&mut dyn Backend` default method has no field to put that in
//! without every backend struct growing one. This controller is that
//! state holder — the same shape [`crate::compose::MenuSystem`] already
//! uses for menu-bar dropdowns, applied to a single anchored popup with
//! no bar.
//!
//! # Scope (v1)
//!
//! Single-level menus only — an item's `submenu` field is accepted by
//! [`crate::ContextMenu`] but this controller doesn't open nested
//! pull-right popups for it yet (unlike [`crate::compose::MenuSystem`],
//! which does, for menu-bar dropdowns). A submenu item still renders
//! (with its `▶` affordance where the rasteriser draws one) but clicking
//! it is inert — no [`ContextMenuOutcome::Event`], just `Consumed`. Real
//! nested-submenu support is a follow-up if a consumer's right-click
//! menu needs one; [`crate::compose::MenuSystem`]'s `submenu_path`/
//! `submenu_selected` machinery is the pattern to lift when it lands.
//!
//! # Call this from event-handling code only — never from `render`
//!
//! Same contract as [`Backend::show_context_menu`] (see its doc): call
//! [`ContextMenuController::open`] from `AppLogic::handle`, typically on
//! `MouseDown { button: Right, .. }`. Never from `render` — the `Native`
//! path blocks on AppKit's modal pop-up loop, and running that from
//! inside a paint closure re-enters painting while the paint closure
//! still holds the borrows it needs to finish its own frame
//! (`JDonaghy/vimcode#1580`).

use crate::backend::{Backend, ResolvedMenuStyle};
use crate::event::{Point, UiEvent};
use crate::primitives::context_menu::{ContextMenu, ContextMenuHit, ContextMenuLayout};
use crate::{Key, NamedKey};

/// What [`ContextMenuController::handle`] did with an event.
#[derive(Debug, Clone, PartialEq)]
pub enum ContextMenuOutcome {
    /// Terminal: an item was activated, or the menu was dismissed.
    /// Always [`UiEvent::ContextMenuItemActivated`] or
    /// [`UiEvent::ContextMenuDismissed`] — literally the same two events
    /// the `Native` path delivers via the normal event queue, so a
    /// caller can route both through one match arm.
    Event(UiEvent),
    /// The event was consumed by menu navigation/hit-testing (selection
    /// moved, a click landed on an inert region). The caller should
    /// redraw; there's nothing further to route.
    Consumed,
    /// No menu is open, or the event wasn't relevant to the open one.
    Ignored,
}

/// Owns zero-or-one open [`ContextMenu`] and routes it through either
/// the backend's native pop-up or an in-window painted popup, per
/// [`Backend::effective_menu_style`]. See the module doc for the full
/// recipe.
#[derive(Debug, Default)]
pub struct ContextMenuController {
    open: Option<OpenMenu>,
}

#[derive(Debug, Clone)]
struct OpenMenu {
    menu: ContextMenu,
    anchor: Point,
}

/// Item height / separator height / menu width in line-height units —
/// the same constants [`crate::compose::MenuSystem::dropdown_stack`]
/// uses, kept here rather than shared so this controller has no
/// dependency on `MenuSystem`'s bar-anchored internals.
fn item_measure(lh: f32) -> (f32, f32, f32) {
    let item_h = (lh * 1.4).round().max(lh);
    let sep_h = (lh * 0.5).round().max(1.0);
    let menu_width = 20.0 * lh;
    (item_h, sep_h, menu_width)
}

impl ContextMenuController {
    pub fn new() -> Self {
        Self { open: None }
    }

    /// Is a `Custom`-style menu currently open and painted? `false` for
    /// the `Native` path (that popup is AppKit's own modal state, not
    /// this controller's).
    pub fn is_open(&self) -> bool {
        self.open.is_some()
    }

    /// **The one call.** Open `menu` at `anchor` (view-local
    /// coordinates). Resolves [`Backend::effective_menu_style`] and
    /// routes to [`Backend::show_context_menu`] (`Native`) or in-window
    /// painted state (`Custom`) — the caller doesn't branch on either.
    ///
    /// See the module doc's "Call this from event-handling code only"
    /// section — this must be called from `AppLogic::handle`, never
    /// `render`.
    pub fn open(&mut self, menu: ContextMenu, anchor: Point, backend: &mut dyn Backend) {
        match backend.effective_menu_style() {
            ResolvedMenuStyle::Native => {
                self.close(backend);
                backend.show_context_menu(&menu, anchor);
            }
            ResolvedMenuStyle::Custom => {
                let id = menu.id;
                let layout = self.layout_for(&menu, anchor, backend);
                backend
                    .modal_stack_handle()
                    .borrow_mut()
                    .push(id, layout.bounds);
                self.open = Some(OpenMenu { menu, anchor });
            }
        }
    }

    /// Close the painted menu, if one is open. A no-op on the `Native`
    /// path (AppKit owns dismissal there) and if nothing is open.
    pub fn close(&mut self, backend: &mut dyn Backend) {
        if let Some(open) = self.open.take() {
            backend.modal_stack_handle().borrow_mut().pop(&open.menu.id);
        }
    }

    /// Process one event against the currently open painted menu.
    /// Returns [`ContextMenuOutcome::Ignored`] immediately if nothing is
    /// open — safe to call unconditionally before an app's own routing,
    /// same contract as [`crate::compose::MenuSystem::handle`].
    pub fn handle(&mut self, event: &UiEvent, backend: &mut dyn Backend) -> ContextMenuOutcome {
        let Some(open) = self.open.clone() else {
            return ContextMenuOutcome::Ignored;
        };

        match event {
            UiEvent::KeyPressed {
                key: Key::Named(NamedKey::Escape),
                ..
            } => {
                self.close(backend);
                ContextMenuOutcome::Event(UiEvent::ContextMenuDismissed)
            }
            UiEvent::KeyPressed {
                key: Key::Named(NamedKey::Down),
                ..
            } => {
                let next = open.menu.move_selection(open.menu.selected_idx, 1);
                self.set_selected(next);
                ContextMenuOutcome::Consumed
            }
            UiEvent::KeyPressed {
                key: Key::Named(NamedKey::Up),
                ..
            } => {
                let next = open.menu.move_selection(open.menu.selected_idx, -1);
                self.set_selected(next);
                ContextMenuOutcome::Consumed
            }
            UiEvent::KeyPressed {
                key: Key::Named(NamedKey::Enter),
                ..
            } => {
                let item = open.menu.items.get(open.menu.selected_idx);
                match item.and_then(|i| i.id) {
                    Some(id) => {
                        self.close(backend);
                        ContextMenuOutcome::Event(UiEvent::ContextMenuItemActivated(id))
                    }
                    None => ContextMenuOutcome::Consumed,
                }
            }
            UiEvent::MouseDown { position, .. } => {
                let layout = self.layout_for(&open.menu, open.anchor, backend);
                match layout.hit_test(position.x, position.y) {
                    ContextMenuHit::Item(id) => {
                        self.close(backend);
                        ContextMenuOutcome::Event(UiEvent::ContextMenuItemActivated(id))
                    }
                    ContextMenuHit::Inert => ContextMenuOutcome::Consumed,
                    ContextMenuHit::Empty => {
                        self.close(backend);
                        ContextMenuOutcome::Event(UiEvent::ContextMenuDismissed)
                    }
                }
            }
            _ => ContextMenuOutcome::Ignored,
        }
    }

    /// Paint the currently open painted menu, if any. A no-op on the
    /// `Native` path (nothing to paint — AppKit owns that surface) and
    /// if nothing is open. Call from `render`, after the app's own
    /// content so the menu layers on top.
    pub fn render(&self, backend: &mut dyn Backend) {
        let Some(open) = &self.open else {
            return;
        };
        let layout = self.layout_for(&open.menu, open.anchor, backend);
        let _ = backend.draw_context_menu(&open.menu, &layout);
    }

    fn set_selected(&mut self, idx: usize) {
        if let Some(open) = &mut self.open {
            open.menu.selected_idx = idx;
        }
    }

    fn layout_for(
        &self,
        menu: &ContextMenu,
        anchor: Point,
        backend: &dyn Backend,
    ) -> ContextMenuLayout {
        let lh = backend.chrome_line_height();
        let (item_h, sep_h, menu_width) = item_measure(lh);
        let vp = backend.context_menu_viewport();
        menu.layout(anchor.x, anchor.y, vp, menu_width, |i| {
            if menu.items[i].is_separator() {
                crate::primitives::context_menu::ContextMenuItemMeasure::new(sep_h)
            } else {
                crate::primitives::context_menu::ContextMenuItemMeasure::new(item_h)
            }
        })
    }
}

// Unit tests below drive a real `TuiBackend` (not a hand-rolled mock —
// `Backend` has dozens of required `draw_*`/`*_layout` methods with no
// default body, so a mock covering only what this controller calls
// doesn't satisfy the trait; see `crate::testing::RecordingBackend` for
// the same trade-off spelled out in its own doc). `TuiBackend::new()`
// is cheap and needs no real terminal for anything this controller
// touches (`modal_stack_handle`, `viewport`, `line_height`,
// `effective_menu_style`, `show_context_menu`) — the one thing it
// *can't* do headless is `draw_context_menu` (needs a live
// `ratatui::Frame` via `enter_frame_scope`), so `render()` itself is
// covered by the `TuiDriver`-based example-driver test instead
// (`tests/tui_example_driver.rs`), which does have one.
//
// `native_menu` is always `false` on `TuiBackend::backend_caps()`, so
// `MenuStyle::Auto`/`Native` both resolve `Custom` here — exactly the
// path these tests exercise. The `Native`-resolves-to-`Native` half of
// the matrix is covered by `MenuStyle::resolve`'s own unit tests in
// `backend.rs` (pure, no backend needed) and by the macOS-caps test in
// `src/macos/backend.rs`.
#[cfg(all(test, feature = "tui"))]
mod tests {
    use super::*;
    use crate::primitives::context_menu::ContextMenuItem;
    use crate::tui::TuiBackend;
    use crate::types::{Modifiers, StyledText, WidgetId};
    use crate::MouseButton;

    fn item(id: &str, label: &str) -> ContextMenuItem {
        ContextMenuItem {
            id: Some(WidgetId::new(id)),
            label: StyledText::plain(label),
            ..Default::default()
        }
    }

    fn menu() -> ContextMenu {
        ContextMenu {
            id: WidgetId::new("ctx"),
            items: vec![item("copy", "Copy"), item("paste", "Paste")],
            selected_idx: 0,
            bg: None,
            placement: crate::primitives::context_menu::ContextMenuPlacement::AnchorPoint,
        }
    }

    #[test]
    fn open_paints_when_resolved_custom() {
        let mut backend = TuiBackend::new();
        assert_eq!(backend.effective_menu_style(), ResolvedMenuStyle::Custom);
        let mut ctl = ContextMenuController::new();
        ctl.open(menu(), Point::new(5.0, 5.0), &mut backend);
        assert!(ctl.is_open());
        assert_eq!(
            backend
                .modal_stack_handle()
                .borrow()
                .top()
                .map(|e| e.id.clone()),
            Some(WidgetId::new("ctx"))
        );
    }

    #[test]
    fn click_on_item_activates_and_closes() {
        let mut backend = TuiBackend::new();
        let mut ctl = ContextMenuController::new();
        ctl.open(menu(), Point::new(0.0, 0.0), &mut backend);

        // Anchored flush at the viewport's top-left corner: `TuiBackend`'s
        // `context_menu_viewport` keeps row/column 0 for the box border,
        // so the first item ("copy") lands at (1.0, 1.0), not at the raw
        // anchor origin.
        let outcome = ctl.handle(
            &UiEvent::MouseDown {
                widget: None,
                button: MouseButton::Left,
                position: Point::new(1.0, 1.0),
                modifiers: Modifiers::default(),
            },
            &mut backend,
        );
        assert_eq!(
            outcome,
            ContextMenuOutcome::Event(UiEvent::ContextMenuItemActivated(WidgetId::new("copy")))
        );
        assert!(!ctl.is_open());
    }

    #[test]
    fn click_outside_dismisses() {
        let mut backend = TuiBackend::new();
        let mut ctl = ContextMenuController::new();
        ctl.open(menu(), Point::new(10.0, 10.0), &mut backend);

        let outcome = ctl.handle(
            &UiEvent::MouseDown {
                widget: None,
                button: MouseButton::Left,
                position: Point::new(0.0, 0.0),
                modifiers: Modifiers::default(),
            },
            &mut backend,
        );
        assert_eq!(
            outcome,
            ContextMenuOutcome::Event(UiEvent::ContextMenuDismissed)
        );
        assert!(!ctl.is_open());
    }

    #[test]
    fn escape_dismisses() {
        let mut backend = TuiBackend::new();
        let mut ctl = ContextMenuController::new();
        ctl.open(menu(), Point::new(0.0, 0.0), &mut backend);

        let outcome = ctl.handle(
            &UiEvent::KeyPressed {
                key: Key::Named(NamedKey::Escape),
                modifiers: Modifiers::default(),
                repeat: false,
            },
            &mut backend,
        );
        assert_eq!(
            outcome,
            ContextMenuOutcome::Event(UiEvent::ContextMenuDismissed)
        );
        assert!(!ctl.is_open());
    }

    #[test]
    fn handle_ignores_when_nothing_open() {
        let mut backend = TuiBackend::new();
        let mut ctl = ContextMenuController::new();
        let outcome = ctl.handle(
            &UiEvent::KeyPressed {
                key: Key::Named(NamedKey::Escape),
                modifiers: Modifiers::default(),
                repeat: false,
            },
            &mut backend,
        );
        assert_eq!(outcome, ContextMenuOutcome::Ignored);
    }

    #[test]
    fn down_then_enter_activates_second_item() {
        let mut backend = TuiBackend::new();
        let mut ctl = ContextMenuController::new();
        ctl.open(menu(), Point::new(0.0, 0.0), &mut backend);

        let down = ctl.handle(
            &UiEvent::KeyPressed {
                key: Key::Named(NamedKey::Down),
                modifiers: Modifiers::default(),
                repeat: false,
            },
            &mut backend,
        );
        assert_eq!(down, ContextMenuOutcome::Consumed);

        let outcome = ctl.handle(
            &UiEvent::KeyPressed {
                key: Key::Named(NamedKey::Enter),
                modifiers: Modifiers::default(),
                repeat: false,
            },
            &mut backend,
        );
        assert_eq!(
            outcome,
            ContextMenuOutcome::Event(UiEvent::ContextMenuItemActivated(WidgetId::new("paste")))
        );
    }

    #[test]
    fn set_menu_style_custom_stays_painted_even_with_native_caps() {
        // `TuiBackend` never declares `native_menu`, so this mainly
        // guards the `set_menu_style` plumbing (Custom always paints
        // in-window) rather than exercising a real native fallback —
        // that combination is covered on the macOS side.
        let mut backend = TuiBackend::new();
        backend.set_menu_style(crate::backend::MenuStyle::Custom);
        assert_eq!(backend.menu_style(), crate::backend::MenuStyle::Custom);
        assert_eq!(backend.effective_menu_style(), ResolvedMenuStyle::Custom);
    }
}