Skip to main content

brep_app/
select_mods.rs

1//! Selection modifiers, one meaning on every platform (multi-select rework,
2//! owner 2026-11-26).
3//!
4//! * The PRIMARY modifier is Cmd on macOS and Ctrl on Windows/Linux. egui's
5//!   `Modifiers::command` is not enough on its own: the native (egui-winit)
6//!   runner sets it to Cmd on macOS, but eframe's WEB runner sets it to
7//!   `ctrl || meta` on every OS, so a Mac browser's Ctrl+click would read as a
8//!   primary click. On macOS the primary modifier is therefore `mac_cmd`.
9//! * On macOS a Ctrl+click is the secondary (right) click, and a Ctrl+drag is
10//!   a right drag (pan). `MacSecondary::rewrite` turns those button events
11//!   into secondary-button events in the raw input, once, before any widget
12//!   sees them — so every context menu, `secondary_clicked()` and
13//!   `dragged_by(Secondary)` path behaves as it does for a real right button,
14//!   natively and in the browser build, and Ctrl never adds to a selection.
15//! * Shift means "more": in the 3D view Shift+click adds/toggles like the
16//!   primary modifier; in ordered lists (feature tree, assembly tree, BOM)
17//!   Shift+click selects the RANGE from the anchor row (`range`).
18
19/// Whether the PRIMARY modifier (Cmd on macOS, Ctrl elsewhere) is held.
20pub fn primary(os: egui::os::OperatingSystem, mods: egui::Modifiers) -> bool {
21    if os == egui::os::OperatingSystem::Mac {
22        mods.mac_cmd
23    } else {
24        mods.ctrl || mods.command
25    }
26}
27
28/// [`primary`] for the context's OS.
29pub fn primary_in(ctx: &egui::Context) -> bool {
30    let os = ctx.os();
31    ctx.input(|i| primary(os, i.modifiers))
32}
33
34/// Whether a click adds to (toggles in) the 3D selection: the primary
35/// modifier or Shift.
36pub fn additive(os: egui::os::OperatingSystem, mods: egui::Modifiers) -> bool {
37    primary(os, mods) || mods.shift
38}
39
40/// How an ordered list row click applies (feature tree, assembly tree, BOM).
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum RowClick {
43    /// Plain click: this row alone.
44    Replace,
45    /// Primary+click: toggle this row in the selection.
46    Toggle,
47    /// Shift+click: the range from the anchor to this row, alone.
48    Range,
49    /// Primary+Shift+click: add the range from the anchor to this row.
50    AddRange,
51}
52
53impl RowClick {
54    pub fn of(os: egui::os::OperatingSystem, mods: egui::Modifiers) -> Self {
55        match (primary(os, mods), mods.shift) {
56            (false, false) => Self::Replace,
57            (true, false) => Self::Toggle,
58            (false, true) => Self::Range,
59            (true, true) => Self::AddRange,
60        }
61    }
62
63    pub fn in_ctx(ctx: &egui::Context) -> Self {
64        let os = ctx.os();
65        ctx.input(|i| Self::of(os, i.modifiers))
66    }
67
68    pub fn is_range(self) -> bool {
69        matches!(self, Self::Range | Self::AddRange)
70    }
71
72    /// Whether this click sets the anchor (plain and primary clicks do; a
73    /// range click keeps the anchor it ran from).
74    pub fn sets_anchor(self) -> bool {
75        !self.is_range()
76    }
77}
78
79/// The rows from `anchor` to `clicked` inclusive, in display order, or `None`
80/// when either is not in `rows` (the anchor row is gone: the caller selects
81/// the clicked row alone).
82pub fn range<'a, T: PartialEq>(rows: &'a [T], anchor: &T, clicked: &T) -> Option<&'a [T]> {
83    let a = rows.iter().position(|r| r == anchor)?;
84    let b = rows.iter().position(|r| r == clicked)?;
85    Some(&rows[a.min(b)..=a.max(b)])
86}
87
88/// The macOS Ctrl+click → secondary click rewrite (see the module docs). One
89/// per app: it remembers which presses it turned so their releases match even
90/// when Ctrl is let go first.
91#[derive(Debug, Default)]
92pub struct MacSecondary {
93    turned: bool,
94}
95
96impl MacSecondary {
97    /// Rewrite this frame's `raw` input in place. A no-op off macOS.
98    pub fn rewrite(&mut self, os: egui::os::OperatingSystem, raw: &mut egui::RawInput) {
99        if os != egui::os::OperatingSystem::Mac {
100            self.turned = false;
101            return;
102        }
103        for event in raw.events.iter_mut() {
104            if let egui::Event::PointerButton { button, pressed, modifiers, .. } = event {
105                if *button != egui::PointerButton::Primary {
106                    continue;
107                }
108                if *pressed {
109                    if modifiers.ctrl && !modifiers.mac_cmd {
110                        *button = egui::PointerButton::Secondary;
111                        self.turned = true;
112                    }
113                } else if std::mem::take(&mut self.turned) {
114                    *button = egui::PointerButton::Secondary;
115                }
116            }
117        }
118    }
119}
120
121/// Apply [`MacSecondary::rewrite`] to a frame's raw input, keeping its state
122/// in the context (one per window). Every host calls it after its own input
123/// is in `raw`: the app's `raw_input_hook` and the headless MCP host.
124pub fn rewrite_raw(ctx: &egui::Context, raw: &mut egui::RawInput) {
125    let id = egui::Id::new("brep-mac-secondary-turned");
126    let os = ctx.os();
127    let turned = ctx.data(|d| d.get_temp::<bool>(id)).unwrap_or(false);
128    let mut rewrite = MacSecondary { turned };
129    rewrite.rewrite(os, raw);
130    if rewrite.turned != turned {
131        ctx.data_mut(|d| d.insert_temp(id, rewrite.turned));
132    }
133}
134