Skip to main content

truce_gui_types/
interaction.rs

1//! Mouse interaction for GUI widgets.
2//!
3//! Tracks widget hit regions and maps mouse drags to parameter value changes.
4
5use truce_core::Float;
6use truce_core::cast::{discrete_index, discrete_norm};
7
8use crate::layout::{
9    GRID_GAP, GRID_PADDING, GridLayout, Layout, PluginLayout, ROWS_COLUMN_GAP, ROWS_LAYOUT_TOP,
10    ROWS_ROW_GAP, ROWS_SECTION_LABEL_HEIGHT, WidgetKind, compute_section_offsets,
11};
12use crate::snapshot::ParamSnapshot;
13use crate::widgets::WidgetType;
14
15/// Lower an explicit `WidgetKind` from a layout helper into the
16/// runtime `WidgetType` the interaction code dispatches on. `None`
17/// (meaning "infer from param range") stays as Knob - callers that
18/// need inference overwrite `widget_type` after calling
19/// `build_regions_*`.
20//
21// `Some(Knob) => Knob` and `None => Knob` share a value but mean
22// different things - explicit user-specified Knob vs. an
23// inference-pending placeholder. Keep the arms separate so the
24// distinction is greppable.
25#[allow(clippy::match_same_arms)]
26fn widget_kind_to_type(kind: Option<WidgetKind>) -> WidgetType {
27    match kind {
28        Some(WidgetKind::Knob) => WidgetType::Knob,
29        Some(WidgetKind::Slider) => WidgetType::Slider,
30        Some(WidgetKind::Toggle) => WidgetType::Toggle,
31        Some(WidgetKind::Selector) => WidgetType::Selector,
32        Some(WidgetKind::Dropdown) => WidgetType::Dropdown,
33        Some(WidgetKind::Meter) => WidgetType::Meter,
34        Some(WidgetKind::XYPad) => WidgetType::XYPad,
35        None => WidgetType::Knob,
36    }
37}
38
39// ---------------------------------------------------------------------------
40// Platform-agnostic input events + edit outputs
41// ---------------------------------------------------------------------------
42
43/// Which mouse button triggered an event.
44#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45pub enum MouseButton {
46    Left,
47    Right,
48    Middle,
49}
50
51/// Keyboard modifier state at event time.
52// Standard four modifier flags - bitflags would just add ceremony.
53#[allow(clippy::struct_excessive_bools)]
54#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
55pub struct Modifiers {
56    pub shift: bool,
57    pub ctrl: bool,
58    pub alt: bool,
59    pub meta: bool,
60}
61
62/// Platform-agnostic input event consumed by `dispatch`.
63///
64/// Cursor coordinates are in logical pixels, matching what widgets draw at.
65///
66/// `pointer_id` distinguishes simultaneous pointers (multi-touch).
67/// Mouse-driven flows always pass [`SINGLE_POINTER`] (= 0); iOS touch
68/// dispatch uses the `UITouch*` cast to `u64` so each finger gets a
69/// stable identifier across `Down → Move → Up`.
70#[derive(Clone, Copy, Debug)]
71pub enum InputEvent {
72    MouseMove {
73        pointer_id: u64,
74        x: f32,
75        y: f32,
76    },
77    MouseDown {
78        pointer_id: u64,
79        x: f32,
80        y: f32,
81        button: MouseButton,
82    },
83    MouseUp {
84        pointer_id: u64,
85        x: f32,
86        y: f32,
87        button: MouseButton,
88    },
89    /// Synthesized when the host detects a second click within the
90    /// platform-specific threshold. `dispatch` uses this to reset params
91    /// to their defaults.
92    MouseDoubleClick {
93        x: f32,
94        y: f32,
95    },
96    /// Vertical wheel scroll. `dy > 0` = scroll up (away from user),
97    /// `dy < 0` = scroll down. Magnitude is in pixels.
98    Scroll {
99        x: f32,
100        y: f32,
101        dy: f32,
102    },
103    /// The cursor left the editor surface. Dispatch clears hover state.
104    MouseLeave,
105}
106
107/// Single-pointer sentinel for mouse-driven flows. iOS touch
108/// dispatch substitutes the `UITouch*` cast to `u64` so multiple
109/// fingers can drag independently.
110pub const SINGLE_POINTER: u64 = 0;
111
112/// Pixels of vertical drag (or wheel travel) that map to a full
113/// 0.0 → 1.0 normalized parameter range. Shared between knob drag
114/// and the scroll-wheel knob adjustment so the two feel uniform.
115const KNOB_PIXELS_PER_UNIT: f32 = 200.0;
116
117// The `BaseviewTranslator` lives in `truce-gui` (heavy crate) because
118// it depends on `baseview` for windowing-platform event translation.
119// Light backends (truce-egui, truce-iced, truce-slint) don't use it
120// - they translate their own framework's events into `InputEvent`s
121// and call `dispatch` directly.
122
123/// A requested edit to a host parameter, emitted by `dispatch`.
124///
125/// Callers replay these against their host interface:
126/// `Begin → Set* → End` matches the VST3 / CLAP / AU automation protocol.
127#[derive(Clone, Copy, Debug)]
128pub enum ParamEdit {
129    /// Parameter is about to be edited (begin gesture).
130    Begin { id: u32 },
131    /// Set normalized value.
132    Set { id: u32, normalized: f32 },
133    /// Edit gesture finished.
134    End { id: u32 },
135}
136
137/// A widget's hit region on screen.
138#[derive(Clone, Debug)]
139pub struct WidgetRegion {
140    pub param_id: u32,
141    pub widget_type: WidgetType,
142    pub x: f32,
143    pub y: f32,
144    pub w: f32,
145    pub h: f32,
146    /// Center x/y and radius for knob (circular hit test).
147    pub cx: f32,
148    pub cy: f32,
149    pub radius: f32,
150    pub normalized_value: f32,
151    /// Bottom Y of the dropdown button box, set at draw time.
152    /// Used to position the popup directly below the visual button.
153    pub dropdown_anchor_y: f32,
154}
155
156/// State for an open dropdown popup.
157pub struct DropdownState {
158    /// Region index of the dropdown widget that is open.
159    pub region_idx: usize,
160    /// Parameter ID of the open dropdown.
161    pub param_id: u32,
162    /// Popup bounding rect: (x, y, w, h).
163    pub popup_rect: (f32, f32, f32, f32),
164    /// Option labels.
165    pub options: Vec<String>,
166    /// Currently selected index.
167    pub selected: usize,
168    /// Index under the cursor within the popup.
169    pub hover_option: Option<usize>,
170    /// First visible option index (for scrollable popups).
171    pub scroll_offset: usize,
172    /// Number of visible options (may be less than `options.len()` if clamped).
173    pub visible_count: usize,
174}
175
176/// Tracks the current mouse / touch interaction state.
177#[derive(Default)]
178pub struct InteractionState {
179    pub knob_regions: Vec<WidgetRegion>,
180    /// One entry per active pointer (mouse: at most 1; touch: up
181    /// to one per finger). Keyed by `DragState::pointer_id`. Linear
182    /// scan - N is bounded by the device's reported max touches
183    /// (≤10 in practice).
184    pub drags: Vec<DragState>,
185    /// Region index under the cursor (for hover highlight).
186    pub hover_idx: Option<usize>,
187    /// Currently open dropdown popup (at most one at a time).
188    pub dropdown: Option<DropdownState>,
189    /// Active touch-drag on the open dropdown popup - set on
190    /// `MouseDown` inside the popup, updated on `MouseMove`
191    /// (mapping vertical motion to `scroll_offset` change),
192    /// cleared on `MouseUp`. iOS pattern: tap to select, swipe to
193    /// scroll. Desktop scroll-wheel handling stays through the
194    /// `Scroll` event.
195    pub popup_drag: Option<PopupDrag>,
196    /// Set by event handlers whose visible side effect isn't otherwise
197    /// observable to `dispatch_events` (e.g. `MouseLeave` clearing
198    /// hover state). The editor reads this via `take_repaint_request`
199    /// to avoid relying on diff-checks of every individual visible
200    /// field.
201    needs_repaint: bool,
202}
203
204/// Active touch-drag on the open dropdown popup.
205pub struct PopupDrag {
206    pub pointer_id: u64,
207    pub start_y: f32,
208    pub start_scroll_offset: usize,
209    /// True once the user has moved more than `ITEM_H / 2` from
210    /// `start_y`. Distinguishes a tap (select on release) from a
211    /// scroll-drag (keep popup open on release).
212    pub scrolled: bool,
213}
214
215pub struct DragState {
216    /// Identifier of the pointer (mouse or touch) driving this drag.
217    /// See [`SINGLE_POINTER`].
218    pub pointer_id: u64,
219    pub region_idx: usize,
220    pub param_id: u32,
221    pub start_value: f64,
222    pub start_y: f32,
223    pub widget_type: WidgetType,
224    pub region_x: f32,
225    pub region_y: f32,
226    pub region_w: f32,
227    pub region_h: f32,
228}
229
230impl InteractionState {
231    /// Read and clear the explicit repaint flag set by event handlers.
232    pub fn take_repaint_request(&mut self) -> bool {
233        std::mem::replace(&mut self.needs_repaint, false)
234    }
235
236    /// Rebuild hit regions from the layout. Call after render.
237    // Layout col counts widen `u32 as f32`; column counts are
238    // bounded by the editor's row width.
239    #[allow(clippy::cast_precision_loss)]
240    pub fn build_regions(&mut self, layout: &PluginLayout) {
241        // `dropdown_anchor_y` is filled in by the draw pass, not here.
242        // `update_interaction` rebuilds regions every frame, but the
243        // render that repopulates the anchor can be skipped (the macOS
244        // CPU path gates `render` behind a repaint check). Carry prior
245        // anchors over by index so an idle, non-rendering frame doesn't
246        // reset them to 0 and strand the next dropdown popup at the top
247        // of the window.
248        let prior_anchors: Vec<f32> = self
249            .knob_regions
250            .iter()
251            .map(|r| r.dropdown_anchor_y)
252            .collect();
253        self.knob_regions.clear();
254
255        let knob_size = layout.knob_size;
256        let pitch = knob_size + ROWS_COLUMN_GAP;
257        let mut y = ROWS_LAYOUT_TOP;
258
259        for row in &layout.rows {
260            if row.label.is_some() {
261                y += ROWS_SECTION_LABEL_HEIGHT;
262            }
263
264            let total_cols: u32 = row.knobs.iter().map(|k| k.span.max(1)).sum();
265            let total_w = total_cols as f32 * pitch - ROWS_COLUMN_GAP;
266            let start_x = (layout.width as f32 - total_w) / 2.0;
267
268            let mut col = 0u32;
269            for knob_def in &row.knobs {
270                let span = knob_def.span.max(1);
271                let x = start_x + col as f32 * pitch;
272                let widget_w = span as f32 * pitch - ROWS_COLUMN_GAP;
273                let cx = x + widget_w / 2.0;
274                let cy = y + knob_size / 2.0 - 5.0;
275                let radius = knob_size / 2.0 - 4.0;
276
277                let idx = self.knob_regions.len();
278                self.knob_regions.push(WidgetRegion {
279                    param_id: knob_def.param_id,
280                    widget_type: widget_kind_to_type(knob_def.widget),
281                    x,
282                    y,
283                    w: widget_w,
284                    h: knob_size,
285                    cx,
286                    cy,
287                    radius,
288                    normalized_value: 0.0,
289                    dropdown_anchor_y: prior_anchors.get(idx).copied().unwrap_or(0.0),
290                });
291                col += span;
292            }
293
294            y += knob_size + ROWS_ROW_GAP;
295        }
296    }
297
298    /// Check if a mouse position hits a widget. Returns the region index if so.
299    #[must_use]
300    pub fn hit_test(&self, mx: f32, my: f32) -> Option<usize> {
301        for (idx, region) in self.knob_regions.iter().enumerate() {
302            match region.widget_type {
303                WidgetType::Knob => {
304                    let dx = mx - region.cx;
305                    let dy = my - region.cy;
306                    if dx * dx + dy * dy <= region.radius * region.radius {
307                        return Some(idx);
308                    }
309                }
310                WidgetType::Meter => {}
311                WidgetType::Slider
312                | WidgetType::Toggle
313                | WidgetType::Selector
314                | WidgetType::Dropdown
315                | WidgetType::XYPad => {
316                    if mx >= region.x
317                        && mx <= region.x + region.w
318                        && my >= region.y
319                        && my <= region.y + region.h
320                    {
321                        return Some(idx);
322                    }
323                }
324            }
325        }
326        None
327    }
328
329    /// Get the widget type by region index.
330    #[must_use]
331    pub fn widget_type_at(&self, idx: usize) -> Option<WidgetType> {
332        self.knob_regions.get(idx).map(|r| r.widget_type)
333    }
334
335    /// Get the region by index.
336    #[must_use]
337    pub fn region_at(&self, idx: usize) -> Option<&WidgetRegion> {
338        self.knob_regions.get(idx)
339    }
340
341    /// Begin a drag on a widget by region index. Returns any prior
342    /// drag for the same `pointer_id` so the caller can emit a
343    /// matching `ParamEdit::End` for it - without this, hosts that
344    /// model gestures as a Begin/End stack (VST3, CLAP, AU on iOS)
345    /// see a stranded Begin and report the param as permanently
346    /// "being touched". iOS reliably triggers this when a system
347    /// gesture recognizer (Control Center swipe, multitasking
348    /// gesture) steals a touch without firing `touchesCancelled:`;
349    /// the next `touchesBegan:` may reuse the same `UITouch*`
350    /// pointer for a different finger.
351    #[must_use]
352    pub fn begin_drag(
353        &mut self,
354        pointer_id: u64,
355        idx: usize,
356        current_normalized: f64,
357        mouse_y: f32,
358    ) -> Option<DragState> {
359        let region = self.knob_regions.get(idx)?;
360        let param_id = region.param_id;
361        let wtype = region.widget_type;
362        let stranded = self
363            .drags
364            .iter()
365            .position(|d| d.pointer_id == pointer_id)
366            .map(|i| self.drags.swap_remove(i));
367        self.drags.push(DragState {
368            pointer_id,
369            region_idx: idx,
370            param_id,
371            start_value: current_normalized,
372            start_y: mouse_y,
373            widget_type: wtype,
374            region_x: region.x,
375            region_y: region.y,
376            region_w: region.w,
377            region_h: region.h,
378        });
379        stranded
380    }
381
382    /// Find the drag for a pointer (read-only).
383    #[must_use]
384    pub fn drag_for(&self, pointer_id: u64) -> Option<&DragState> {
385        self.drags.iter().find(|d| d.pointer_id == pointer_id)
386    }
387
388    /// Update a single drag's knob value (vertical-drag widgets).
389    /// Returns the new (`param_id`, normalized value) for the drag
390    /// matching `pointer_id`, or `None` if no such drag is active.
391    #[must_use]
392    pub fn update_drag(&self, pointer_id: u64, mouse_y: f32) -> Option<(u32, f64)> {
393        let drag = self.drag_for(pointer_id)?;
394        let dy = drag.start_y - mouse_y;
395        let delta = f64::from(dy) / f64::from(KNOB_PIXELS_PER_UNIT);
396        let new_value = (drag.start_value + delta).clamp(0.0, 1.0);
397        Some((drag.param_id, new_value))
398    }
399
400    /// Update a single horizontal-slider drag. Same shape as
401    /// [`InteractionState::update_drag`] but maps `x` rather than `y`.
402    #[must_use]
403    pub fn update_slider_drag(&self, pointer_id: u64, mouse_x: f32) -> Option<(u32, f64)> {
404        let drag = self.drag_for(pointer_id)?;
405        let margin = 4.0;
406        let rel = (mouse_x - drag.region_x - margin) / (drag.region_w - margin * 2.0);
407        let new_value = f64::from(rel).clamp(0.0, 1.0);
408        Some((drag.param_id, new_value))
409    }
410
411    /// End the drag for `pointer_id`. Returns the popped state so
412    /// callers can emit the `ParamEdit::End` (and the y-axis `End`
413    /// on XY pads) without re-searching the vec.
414    pub fn end_drag(&mut self, pointer_id: u64) -> Option<DragState> {
415        let idx = self.drags.iter().position(|d| d.pointer_id == pointer_id)?;
416        Some(self.drags.swap_remove(idx))
417    }
418
419    /// Test if a point is inside the open dropdown popup.
420    /// Returns the absolute option index (accounting for scroll) if hit, or None.
421    #[must_use]
422    // Hit-test math operates on f32 logical pixels bounded by the
423    // window size; `(my - py - padding) / item_h` lands in
424    // `[0, visible_count]`.
425    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
426    pub fn dropdown_popup_hit(&self, mx: f32, my: f32) -> Option<usize> {
427        let dd = self.dropdown.as_ref()?;
428        let (px, py, pw, ph) = dd.popup_rect;
429        if mx < px || mx > px + pw || my < py || my > py + ph {
430            return None;
431        }
432        let item_h = 18.0f32;
433        let padding = 4.0f32;
434        let local_idx = ((my - py - padding) / item_h) as usize;
435        let abs_idx = dd.scroll_offset + local_idx;
436        if abs_idx < dd.options.len() && local_idx < dd.visible_count {
437            Some(abs_idx)
438        } else {
439            None
440        }
441    }
442
443    /// Update the hovered option in the open dropdown popup.
444    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
445    pub fn dropdown_update_hover(&mut self, mx: f32, my: f32) {
446        if let Some(ref mut dd) = self.dropdown {
447            let (px, py, pw, ph) = dd.popup_rect;
448            if mx >= px && mx <= px + pw && my >= py && my <= py + ph {
449                let item_h = 18.0f32;
450                let padding = 4.0f32;
451                let local_idx = ((my - py - padding) / item_h) as usize;
452                let abs_idx = dd.scroll_offset + local_idx;
453                dd.hover_option = if abs_idx < dd.options.len() && local_idx < dd.visible_count {
454                    Some(abs_idx)
455                } else {
456                    None
457                };
458            } else {
459                dd.hover_option = None;
460            }
461        }
462    }
463
464    /// Whether a dropdown popup is currently open.
465    #[must_use]
466    pub fn dropdown_is_open(&self) -> bool {
467        self.dropdown.is_some()
468    }
469
470    /// Close the dropdown popup. Returns the region index of the
471    /// dropdown that was open, so the caller can suppress an
472    /// immediate-reopen click landing on the same button without
473    /// having to read `self.dropdown` *before* closing.
474    pub fn dropdown_close(&mut self) -> Option<usize> {
475        self.dropdown.take().map(|dd| dd.region_idx)
476    }
477
478    /// Scroll the dropdown popup by `delta` items (positive = down, negative = up).
479    // Dropdown option counts stay below i32::MAX in practice (UI lists
480    // never reach 2 billion).
481    #[allow(
482        clippy::cast_possible_truncation,
483        clippy::cast_possible_wrap,
484        clippy::cast_sign_loss
485    )]
486    pub fn dropdown_scroll(&mut self, delta: i32) {
487        if let Some(ref mut dd) = self.dropdown {
488            let max_offset = dd.options.len().saturating_sub(dd.visible_count);
489            let new_offset = (dd.scroll_offset as i32 + delta).clamp(0, max_offset as i32) as usize;
490            dd.scroll_offset = new_offset;
491        }
492    }
493
494    /// Rebuild hit regions from either layout variant.
495    pub fn build_regions_any(&mut self, layout: &Layout) {
496        match layout {
497            Layout::Rows(pl) => self.build_regions(pl),
498            Layout::Grid(gl) => self.build_regions_grid(gl),
499        }
500    }
501
502    /// Rebuild hit regions from a grid layout.
503    //
504    // Grid cell coordinates widen `u32 as f32`; cells indices fit in
505    // an editor's logical pixel range.
506    #[allow(clippy::cast_precision_loss)]
507    pub fn build_regions_grid(&mut self, layout: &GridLayout) {
508        // See `build_regions`: preserve `dropdown_anchor_y` across the
509        // per-frame rebuild so an idle frame that skips render doesn't
510        // strand the next dropdown popup at y = 0.
511        let prior_anchors: Vec<f32> = self
512            .knob_regions
513            .iter()
514            .map(|r| r.dropdown_anchor_y)
515            .collect();
516        self.knob_regions.clear();
517
518        let header_h = layout.header_height();
519        let section_offsets = compute_section_offsets(layout);
520
521        for gw in &layout.widgets {
522            let x = GRID_PADDING + gw.col as f32 * (layout.cell_size + GRID_GAP);
523            let y = header_h
524                + GRID_PADDING
525                + gw.row as f32 * (layout.cell_size + GRID_GAP)
526                + section_offsets[gw.row as usize];
527            let w = gw.col_span as f32 * (layout.cell_size + GRID_GAP) - GRID_GAP;
528            let h = gw.row_span as f32 * (layout.cell_size + GRID_GAP) - GRID_GAP;
529            let cx = x + w / 2.0;
530            let cy = y + h / 2.0 - 5.0;
531            let radius = w.min(h) / 2.0 - 4.0;
532
533            // Pre-populate widget_type from the explicit `widget` kind
534            // when the layout declares one. Callers that need
535            // range-based inference for `None` (BuiltinEditor) still
536            // overwrite this field after the call; for custom editors
537            // that always set `widget` via the `layout::dropdown` /
538            // `layout::knob` / … helpers, this means dispatch routes
539            // correctly out of the box.
540            let widget_type = widget_kind_to_type(gw.widget);
541
542            let idx = self.knob_regions.len();
543            self.knob_regions.push(WidgetRegion {
544                param_id: gw.param_id,
545                widget_type,
546                x,
547                y,
548                w,
549                h,
550                cx,
551                cy,
552                radius,
553                normalized_value: 0.0,
554                dropdown_anchor_y: prior_anchors.get(idx).copied().unwrap_or(0.0),
555            });
556        }
557    }
558}
559
560// ---------------------------------------------------------------------------
561// Public `dispatch` - drive widget interactions from input events.
562// ---------------------------------------------------------------------------
563
564/// Route a batch of input events through the widget tree, updating
565/// `state` in place (hover, drag origins, dropdown open/closed, …) and
566/// returning the sequence of parameter edits they imply.
567///
568/// `state.knob_regions` must be up to date for the current layout; callers
569/// typically call `state.build_regions_any(layout)` once after a layout
570/// change. `snapshot` provides read access to live parameter values.
571///
572/// This does NOT mutate any parameter store. Callers replay the returned
573/// `ParamEdit`s against their host interface.
574pub fn dispatch(
575    events: &[InputEvent],
576    layout: &Layout,
577    snapshot: &ParamSnapshot<'_>,
578    state: &mut InteractionState,
579) -> Vec<ParamEdit> {
580    let (w, h) = (layout.width(), layout.height());
581    dispatch_in(events, layout, (w, h), snapshot, state)
582}
583
584/// Like [`dispatch`] but takes explicit `window_size` in the same
585/// coordinate space as the layout - i.e. the size of the surface the
586/// layout is being composited onto.
587///
588/// Use this when the layout is a chrome panel overlaid on a larger
589/// custom-rendered surface (visualizers, graphs, canvases). It lets
590/// dropdown popups and other bounds-aware overlays use the full
591/// window rather than being clipped to the layout's bounding box -
592/// otherwise a popup that wouldn't fit below the button flips above
593/// it even when there's room below in the outer window.
594// Window dimensions widen `u32 as f32`; window sizes are bounded by
595// display dimensions, well below 2^23.
596#[allow(clippy::cast_precision_loss)]
597pub fn dispatch_in(
598    events: &[InputEvent],
599    layout: &Layout,
600    window_size: (u32, u32),
601    snapshot: &ParamSnapshot<'_>,
602    state: &mut InteractionState,
603) -> Vec<ParamEdit> {
604    let mut edits = Vec::new();
605    let window_w = window_size.0 as f32;
606    let window_h = window_size.1 as f32;
607
608    for ev in events {
609        match *ev {
610            InputEvent::MouseMove { pointer_id, x, y } => {
611                // Popup-drag wins over knob-drag - a finger that
612                // landed inside the open popup scrolls the list,
613                // not any widget under it.
614                if let Some(drag) = state.popup_drag.as_ref()
615                    && drag.pointer_id == pointer_id
616                {
617                    apply_popup_scroll_drag(y, state);
618                    continue;
619                }
620                let drag_info = state
621                    .drag_for(pointer_id)
622                    .map(|d| (d.widget_type, d.region_idx));
623                if let Some((wtype, region_idx)) = drag_info {
624                    let y_id = if wtype == WidgetType::XYPad {
625                        layout_param_id_y(layout, region_idx)
626                    } else {
627                        None
628                    };
629                    apply_drag(pointer_id, x, y, y_id, state, &mut edits);
630                } else {
631                    // Hover / dropdown-hover are single-cursor concepts;
632                    // skip for genuine multi-touch pointers so a second
633                    // finger landing doesn't yank hover state away from
634                    // the cursor's last position on a hybrid Mac.
635                    if pointer_id == SINGLE_POINTER {
636                        if state.dropdown_is_open() {
637                            state.dropdown_update_hover(x, y);
638                        }
639                        state.hover_idx = state.hit_test(x, y);
640                    }
641                }
642            }
643            InputEvent::MouseDown {
644                pointer_id,
645                x,
646                y,
647                button: MouseButton::Left,
648            } => {
649                handle_mouse_down(
650                    pointer_id, x, y, layout, snapshot, state, window_w, window_h, &mut edits,
651                );
652            }
653            InputEvent::MouseUp {
654                pointer_id,
655                x,
656                y,
657                button: MouseButton::Left,
658            } => {
659                // Popup-drag end: if the user didn't scroll
660                // appreciably (stayed within `ITEM_H / 2` of the
661                // start), treat the touch as a tap and commit the
662                // option under the release point. If they did
663                // scroll, just keep the popup open.
664                if let Some(drag) = state.popup_drag.take()
665                    && drag.pointer_id == pointer_id
666                {
667                    if !drag.scrolled
668                        && let Some(option_idx) = state.dropdown_popup_hit(x, y)
669                        && let Some(dd) = state.dropdown.as_ref()
670                    {
671                        let param_id = dd.param_id;
672                        let count = dd.options.len();
673                        let new_norm = f32::from_f64(discrete_norm(option_idx, count));
674                        edits.push(ParamEdit::Begin { id: param_id });
675                        edits.push(ParamEdit::Set {
676                            id: param_id,
677                            normalized: new_norm,
678                        });
679                        edits.push(ParamEdit::End { id: param_id });
680                        state.dropdown_close();
681                    }
682                    continue;
683                }
684                if let Some(drag) = state.end_drag(pointer_id) {
685                    edits.push(ParamEdit::End { id: drag.param_id });
686                    if drag.widget_type == WidgetType::XYPad
687                        && let Some(y_id) = layout_param_id_y(layout, drag.region_idx)
688                    {
689                        edits.push(ParamEdit::End { id: y_id });
690                    }
691                }
692            }
693            InputEvent::MouseDoubleClick { x, y } => {
694                if let Some(idx) = state.hit_test(x, y) {
695                    let param_id = state.knob_regions[idx].param_id;
696                    let default_norm = (snapshot.default_normalized)(param_id);
697                    edits.push(ParamEdit::Begin { id: param_id });
698                    edits.push(ParamEdit::Set {
699                        id: param_id,
700                        normalized: default_norm,
701                    });
702                    edits.push(ParamEdit::End { id: param_id });
703                }
704            }
705            InputEvent::Scroll { x, y, dy } => {
706                if state.dropdown_is_open() {
707                    // An open dropdown captures ALL scroll input: wheel
708                    // inside the popup scrolls the list, wheel outside
709                    // is absorbed (no-op) so it can't fall through to
710                    // the generic knob-scroll path below and silently
711                    // advance the param driving this very dropdown.
712                    let inside_popup = state.dropdown_popup_hit(x, y).is_some()
713                        || state.dropdown.as_ref().is_some_and(|dd| {
714                            let (px, py, pw, ph) = dd.popup_rect;
715                            x >= px && x <= px + pw && y >= py && y <= py + ph
716                        });
717                    if inside_popup {
718                        // dy == 0 should be a no-op - falling through to
719                        // the else branch would silently scroll +1 each
720                        // time a host emits a zero-magnitude wheel event.
721                        let delta = match dy.partial_cmp(&0.0) {
722                            Some(std::cmp::Ordering::Greater) => -1,
723                            Some(std::cmp::Ordering::Less) => 1,
724                            _ => 0,
725                        };
726                        if delta != 0 {
727                            state.dropdown_scroll(delta);
728                        }
729                    }
730                    continue;
731                }
732                if let Some(idx) = state.hit_test(x, y) {
733                    // Only scroll-adjust continuous-value widgets.
734                    // Dropdowns / Selectors / Toggles are discrete UI
735                    // affordances - the user expects click to cycle,
736                    // not wheel to drag them across their whole range.
737                    let wtype = state.knob_regions[idx].widget_type;
738                    if matches!(
739                        wtype,
740                        WidgetType::Knob | WidgetType::Slider | WidgetType::XYPad,
741                    ) {
742                        let param_id = state.knob_regions[idx].param_id;
743                        let norm = (snapshot.get_param)(param_id);
744                        let step = dy / KNOB_PIXELS_PER_UNIT;
745                        let new_norm = (norm + step).clamp(0.0, 1.0);
746                        edits.push(ParamEdit::Begin { id: param_id });
747                        edits.push(ParamEdit::Set {
748                            id: param_id,
749                            normalized: new_norm,
750                        });
751                        edits.push(ParamEdit::End { id: param_id });
752                    }
753                }
754            }
755            InputEvent::MouseLeave => {
756                if state.hover_idx.is_some() {
757                    state.hover_idx = None;
758                    state.needs_repaint = true;
759                }
760            }
761            // Right- and middle-click are intentionally ignored. The
762            // built-in editor doesn't have a context menu of its own,
763            // and most plugin hosts (VST3, AU, AAX) treat right-click
764            // inside the editor surface as their hook for the host's
765            // own automation / parameter-link menu - swallowing the
766            // event here would suppress that.
767            InputEvent::MouseDown { .. } | InputEvent::MouseUp { .. } => {}
768        }
769    }
770
771    edits
772}
773
774/// Mouse-down handling factored out of the big match so it's readable.
775fn handle_mouse_down(
776    pointer_id: u64,
777    x: f32,
778    y: f32,
779    layout: &Layout,
780    snapshot: &ParamSnapshot<'_>,
781    state: &mut InteractionState,
782    window_w: f32,
783    window_h: f32,
784    edits: &mut Vec<ParamEdit>,
785) {
786    // If a dropdown popup is open, handle it first.
787    if let Some(dd) = state.dropdown.as_ref() {
788        // MouseDown inside the popup starts a touch-drag - the
789        // commit-or-scroll decision is deferred to MouseUp based
790        // on whether the user moved or stayed still. Without
791        // this, every tap on the popup commits immediately and
792        // there's no way for touch users to scroll a list longer
793        // than the visible area.
794        let (px, py, pw, ph) = dd.popup_rect;
795        if x >= px && x <= px + pw && y >= py && y <= py + ph {
796            state.popup_drag = Some(PopupDrag {
797                pointer_id,
798                start_y: y,
799                start_scroll_offset: dd.scroll_offset,
800                scrolled: false,
801            });
802            return;
803        }
804        // Click outside popup: close. If it landed on the same dropdown
805        // button, swallow the click (don't reopen).
806        if let Some(open_region) = state.dropdown_close()
807            && let Some(idx) = state.hit_test(x, y)
808            && idx == open_region
809            && state.widget_type_at(idx) == Some(WidgetType::Dropdown)
810        {
811            return;
812        }
813        // Fall through to normal widget hit-test.
814    }
815
816    let Some(idx) = state.hit_test(x, y) else {
817        return;
818    };
819    let param_id = state.knob_regions[idx].param_id;
820    let wtype = state.widget_type_at(idx);
821
822    match wtype {
823        Some(WidgetType::Toggle) => {
824            let norm = (snapshot.get_param)(param_id);
825            let new_norm = if norm > 0.5 { 0.0 } else { 1.0 };
826            edits.push(ParamEdit::Begin { id: param_id });
827            edits.push(ParamEdit::Set {
828                id: param_id,
829                normalized: new_norm,
830            });
831            edits.push(ParamEdit::End { id: param_id });
832        }
833        Some(WidgetType::Selector) => {
834            let new_norm = (snapshot.next_discrete_normalized)(param_id);
835            edits.push(ParamEdit::Begin { id: param_id });
836            edits.push(ParamEdit::Set {
837                id: param_id,
838                normalized: new_norm,
839            });
840            edits.push(ParamEdit::End { id: param_id });
841        }
842        Some(WidgetType::Dropdown) => {
843            open_dropdown(idx, param_id, snapshot, state, window_w, window_h);
844        }
845        _ => {
846            // Knob / Slider / XYPad / Meter: begin a drag.
847            let norm = f64::from((snapshot.get_param)(param_id));
848            // If a system gesture stole the previous touch for this
849            // pointer_id without firing `touchesCancelled:`, the
850            // displaced drag's `Begin` is still on the host's
851            // gesture stack - flush an `End` for it (XY pads need
852            // both axes) before opening the new gesture.
853            if let Some(stranded) = state.begin_drag(pointer_id, idx, norm, y) {
854                edits.push(ParamEdit::End {
855                    id: stranded.param_id,
856                });
857                if stranded.widget_type == WidgetType::XYPad
858                    && let Some(y_id) = layout_param_id_y(layout, stranded.region_idx)
859                {
860                    edits.push(ParamEdit::End { id: y_id });
861                }
862            }
863            edits.push(ParamEdit::Begin { id: param_id });
864            if wtype == Some(WidgetType::XYPad)
865                && let Some(y_id) = layout_param_id_y(layout, idx)
866            {
867                edits.push(ParamEdit::Begin { id: y_id });
868            }
869        }
870    }
871}
872
873// Layout / hit-test math is f32 logical pixels bounded by window size;
874// `((avail_h - padding * 2.0) / item_h)` lands in `[0, options.len()]`.
875#[allow(
876    clippy::cast_possible_truncation,
877    clippy::cast_sign_loss,
878    clippy::cast_precision_loss
879)]
880fn open_dropdown(
881    region_idx: usize,
882    param_id: u32,
883    snapshot: &ParamSnapshot<'_>,
884    state: &mut InteractionState,
885    window_w: f32,
886    window_h: f32,
887) {
888    let options = (snapshot.get_options)(param_id);
889    if options.is_empty() {
890        return;
891    }
892    let count = options.len();
893    let current_norm = (snapshot.get_param)(param_id);
894    let selected = discrete_index(f64::from(current_norm), count);
895    let region = &state.knob_regions[region_idx];
896
897    let item_h = 18.0f32;
898    let padding = 4.0f32;
899
900    let anchor_below = region.dropdown_anchor_y; // bottom of button box
901    let popup_w = region.w.max(80.0);
902    let full_popup_h = options.len() as f32 * item_h + padding * 2.0;
903
904    // Always anchor the popup directly under the dropdown button.
905    // If the full list doesn't fit between `anchor_below` and the
906    // window's bottom, cap `visible_count` and scroll - DON'T
907    // shift the popup upward to make more items fit. Shifting up
908    // landed the popup near `y = 0` (literally the top of the
909    // editor) for any dropdown whose full option list was taller
910    // than the editor, far from the button the user just tapped.
911    // Scrolling is the lesser annoyance.
912    let popup_y = anchor_below.max(0.0);
913    let space_below = (window_h - popup_y).max(item_h + padding * 2.0);
914    let avail_h = full_popup_h.min(space_below);
915
916    let visible_count = ((avail_h - padding * 2.0) / item_h).floor().max(1.0) as usize;
917    let visible_count = visible_count.min(options.len());
918    let popup_h = visible_count as f32 * item_h + padding * 2.0;
919
920    let popup_x = region.x.clamp(0.0, (window_w - popup_w).max(0.0));
921    let scroll_offset = if selected >= visible_count {
922        selected - visible_count + 1
923    } else {
924        0
925    };
926
927    state.dropdown = Some(DropdownState {
928        region_idx,
929        param_id,
930        popup_rect: (popup_x, popup_y, popup_w, popup_h),
931        options,
932        selected,
933        hover_option: None,
934        scroll_offset,
935        visible_count,
936    });
937}
938
939/// Touch scroll-drag on the open dropdown popup. Maps vertical
940/// motion since the drag started into `scroll_offset` changes
941/// (one item per `item_h` of drag). If the user has moved more
942/// than half an item from the start, flips `scrolled = true` so
943/// the `MouseUp` handler treats the touch as a scroll instead of
944/// a commit-on-tap.
945//
946// Cast contract: `start_scroll_offset` is bounded by
947// `dd.options.len()` which (per the dropdown widget shape) caps
948// at a few hundred - well below `i32::MAX`. `items_scrolled` is
949// `(dy / item_h)` where `dy` is a finite single-frame motion;
950// the product never approaches i32 limits.
951#[allow(
952    clippy::cast_possible_truncation,
953    clippy::cast_sign_loss,
954    clippy::cast_possible_wrap
955)]
956fn apply_popup_scroll_drag(y: f32, state: &mut InteractionState) {
957    let item_h = 18.0f32;
958    let (start_y, start_scroll_offset) = match state.popup_drag.as_ref() {
959        Some(d) => (d.start_y, d.start_scroll_offset),
960        None => return,
961    };
962    let dy = start_y - y;
963    if dy.abs() > item_h / 2.0
964        && let Some(d) = state.popup_drag.as_mut()
965    {
966        d.scrolled = true;
967    }
968    let items_scrolled = (dy / item_h).round() as i32;
969    let new_offset = start_scroll_offset as i32 + items_scrolled;
970    if let Some(dd) = state.dropdown.as_mut() {
971        let max_offset = (dd.options.len() as i32 - dd.visible_count as i32).max(0);
972        dd.scroll_offset = new_offset.clamp(0, max_offset) as usize;
973    }
974}
975
976fn apply_drag(
977    pointer_id: u64,
978    x: f32,
979    y: f32,
980    y_id_for_xy: Option<u32>,
981    state: &InteractionState,
982    edits: &mut Vec<ParamEdit>,
983) {
984    let Some(drag) = state.drag_for(pointer_id) else {
985        return;
986    };
987    match drag.widget_type {
988        WidgetType::XYPad => {
989            let pad_margin = 4.0;
990            let label_h = 18.0;
991            let pad_x = drag.region_x + pad_margin;
992            let pad_w = drag.region_w - pad_margin * 2.0;
993            let pad_y_start = drag.region_y + pad_margin;
994            let pad_h = drag.region_h - pad_margin * 2.0 - label_h;
995
996            let norm_x = ((x - pad_x) / pad_w).clamp(0.0, 1.0);
997            let norm_y = (1.0 - (y - pad_y_start) / pad_h).clamp(0.0, 1.0);
998
999            edits.push(ParamEdit::Set {
1000                id: drag.param_id,
1001                normalized: norm_x,
1002            });
1003            if let Some(y_id) = y_id_for_xy {
1004                edits.push(ParamEdit::Set {
1005                    id: y_id,
1006                    normalized: norm_y,
1007                });
1008            }
1009        }
1010        WidgetType::Slider => {
1011            if let Some((pid, new_norm)) = state.update_slider_drag(pointer_id, x) {
1012                edits.push(ParamEdit::Set {
1013                    id: pid,
1014                    normalized: f32::from_f64(new_norm),
1015                });
1016            }
1017        }
1018        _ => {
1019            if let Some((pid, new_norm)) = state.update_drag(pointer_id, y) {
1020                edits.push(ParamEdit::Set {
1021                    id: pid,
1022                    normalized: f32::from_f64(new_norm),
1023                });
1024            }
1025        }
1026    }
1027}
1028
1029/// Look up the Y-axis parameter ID for a widget at `region_idx` in the layout.
1030/// Returns `None` if the widget is not an XY pad (or the index is invalid).
1031pub(crate) fn layout_param_id_y(layout: &Layout, region_idx: usize) -> Option<u32> {
1032    match layout {
1033        Layout::Rows(pl) => {
1034            let mut i = 0;
1035            for row in &pl.rows {
1036                for kd in &row.knobs {
1037                    if i == region_idx {
1038                        return kd.param_id_y;
1039                    }
1040                    i += 1;
1041                }
1042            }
1043            None
1044        }
1045        Layout::Grid(g) => g.widgets.get(region_idx).and_then(|w| w.param_id_y),
1046    }
1047}