SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Status bar: switch the mouse mapping from a menu (#7023) (#8856)
phanoob4cd90e68069b
diff --git a/assets/locales/pending/status-bar-mouse-mapping.json b/assets/locales/pending/status-bar-mouse-mapping.jsonnew file mode 100644index 0000000000..d0d8cba7ba--- /dev/null+++ b/assets/locales/pending/status-bar-mouse-mapping.json@@ -0,0 +1,42 @@+{+ "en": {+ "status.navigation": "Mouse mapping: {mapping}",+ "status.navigation_tip": "How the mouse moves the view. Click to choose another mapping"+ },+ "ar": {+ "status.navigation": "تعيين أزرار الفأرة: {mapping}",+ "status.navigation_tip": "كيف تحرّك الفأرة العرض. انقر لاختيار تعيين آخر"+ },+ "de": {+ "status.navigation": "Mausbelegung: {mapping}",+ "status.navigation_tip": "Wie die Maus die Ansicht bewegt. Zum Wählen einer anderen Belegung klicken"+ },+ "es": {+ "status.navigation": "Asignación del ratón: {mapping}",+ "status.navigation_tip": "Cómo mueve el ratón la vista. Haz clic para elegir otra asignación"+ },+ "fr": {+ "status.navigation": "Affectation des boutons de la souris : {mapping}",+ "status.navigation_tip": "La façon dont la souris déplace la vue. Cliquez pour en choisir une autre"+ },+ "he": {+ "status.navigation": "מיפוי העכבר: {mapping}",+ "status.navigation_tip": "איך העכבר מזיז את התצוגה. לחצו כדי לבחור מיפוי אחר"+ },+ "it": {+ "status.navigation": "Assegnazione del mouse: {mapping}",+ "status.navigation_tip": "Come il mouse muove la vista. Fai clic per sceglierne un'altra"+ },+ "ja": {+ "status.navigation": "マウスの割り当て: {mapping}",+ "status.navigation_tip": "マウスでビューを動かす方法。クリックして別の割り当てを選択"+ },+ "vi": {+ "status.navigation": "Gán chức năng chuột: {mapping}",+ "status.navigation_tip": "Cách chuột di chuyển khung nhìn. Nhấp để chọn cách gán khác"+ },+ "zh": {+ "status.navigation": "鼠标映射:{mapping}",+ "status.navigation_tip": "鼠标移动视图的方式。单击可选择其他映射"+ }+}diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rsindex 687d82a4d6..98ced562a2 100644--- a/crates/ui-egui/src/context_menu.rs+++ b/crates/ui-egui/src/context_menu.rs@@ -63,6 +63,8 @@ pub enum Target {Origin,/// Document Settings › Units.Units,+ /// The mouse mappings (Preferences › Navigation), from the status bar (#7023).+ Navigation,/// An assembly joint.Joint {id: u64,@@ -418,6 +420,13 @@ pub fn items(app: &SolveApp, target: &Target) -> Vec<Item> {if app.session.doc.units == *u { item.key("current") } else { item }}).collect(),+ Target::Navigation => crate::prefs::NAV_PRESETS+ .iter()+ .map(|preset| {+ let item = act("ui.navPreset", preset.translated_label(), "").with(json!({ "preset": preset.id }));+ if app.preferences.nav == preset.id { item.key("current") } else { item }+ })+ .collect(),Target::NamedView { name } => crate::browser::view_items(app, name),};if !crate::render_workspace::active(app) {@@ -1518,6 +1527,12 @@ pub fn run_item(app: &mut SolveApp, item: &Item, at: Pos2) {"ui.toolbarPin" | "ui.toolbarRemove" | "ui.toolbarReset" | "ui.toolbarCustomize" | "ui.toolbarTab" | "ui.shortcutPin" => {crate::toolbar_custom::run(app, &item.id, &p)}+ "ui.navPreset" => {+ // Picked here, a preset sets what Preferences › Navigation sets for it.+ if let Some(preset) = crate::prefs::NAV_PRESETS.iter().find(|preset| p.get("preset").and_then(Value::as_str) == Some(preset.id)) {+ crate::prefs::choose_nav_preset(&mut app.preferences, preset);+ }+ }"ui.historyPlayButton" => {if let Err(e) = crate::prefs::set(app, &json!({ "history_play_button": p["show"] })) {app.set_status(e, true);diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rsindex fbbf540c52..1755da0fc1 100644--- a/crates/ui-egui/src/prefs.rs+++ b/crates/ui-egui/src/prefs.rs@@ -241,7 +241,7 @@ impl NavPreset {}}- fn translated_label(&self) -> &str {+ pub(crate) fn translated_label(&self) -> &str {if self.id == TOUCHPAD_NAV { crate::i18n::tr("nav.touchpad.label") } else { crate::i18n::tr_label(self.label) }}}diff --git a/crates/ui-egui/src/shell_tests.rs b/crates/ui-egui/src/shell_tests.rsindex 7537703e45..9315908e8a 100644--- a/crates/ui-egui/src/shell_tests.rs+++ b/crates/ui-egui/src/shell_tests.rs@@ -1099,6 +1099,61 @@ fn status_bar_units_open_the_units_menu_above_the_bar_and_change_units_in_one_unassert!(h.app.viewport.context_menu.is_none());}+#[test]+fn status_bar_mouse_mapping_opens_the_mappings_and_picks_one_as_the_preferences_page_does() {+ use crate::context_menu::Target;+ let mut h = Harness::new();+ h.call("ui.resize", json!({"width":1400,"height":900}));+ h.call("ui.prefs", json!({"status_bar": true}));+ h.app.run("solid.box", json!({"length":40,"width":30,"height":20})).unwrap();+ h.settle();+ let doc = h.app.session.doc.to_json();+ let undo = h.app.session.undo.len();+ let button = rect_of("status:navigation").unwrap();+ assert!(rect_of("status:bar").unwrap().contains_rect(button));+ assert!(button.right() < rect_of("status:units").unwrap().left(), "the mapping sits left of the units");+ assert_eq!(crate::status_bar::contents(&h.app).navigation, "Fusion");+ assert!(painted_texts(&h).iter().any(|t| t == "Fusion"));+ h.step(&json!({"click":{"handle":"status:navigation"}})).unwrap();+ h.settle();+ assert_eq!(h.app.menu.target, Target::Navigation);+ assert_eq!(h.app.viewport.context_menu, Some(button.right_top()));+ let menu = crate::context_menu::items(&h.app, &Target::Navigation);+ let labels: Vec<_> = menu.iter().map(|item| item.label.as_str()).collect();+ assert_eq!(labels, crate::prefs::NAV_PRESETS.iter().map(|preset| preset.translated_label()).collect::<Vec<_>>());+ assert_eq!(menu.iter().filter(|item| item.shortcut == "current").map(|item| item.label.as_str()).collect::<Vec<_>>(), ["Fusion"]);+ let list = h.ctx.memory(|m| m.area_rect(egui::Id::new("sc_context_menu"))).unwrap();+ assert!(list.bottom() <= button.top() + 0.5 && list.top() >= 0.0, "{list:?} {button:?}");+ // Each mapping, picked from the menu, sets what Preferences › Navigation sets for it.+ for preset in crate::prefs::NAV_PRESETS {+ let mut expected = h.app.preferences.clone();+ crate::prefs::choose_nav_preset(&mut expected, preset);+ if h.app.viewport.context_menu.is_none() {+ h.step(&json!({"click":{"handle":"status:navigation"}})).unwrap();+ h.settle();+ }+ h.call("ui.menuPick", json!({"item": preset.translated_label()}));+ h.settle();+ assert_eq!(serde_json::to_value(&h.app.preferences).unwrap(), serde_json::to_value(&expected).unwrap(), "{}", preset.id);+ assert!(h.app.viewport.context_menu.is_none());+ assert_eq!(crate::status_bar::contents(&h.app).navigation, preset.translated_label());+ }+ assert_eq!(h.app.session.doc.to_json(), doc, "a preference, not a design edit");+ assert_eq!(h.app.session.undo.len(), undo);+ // An unknown mapping changes nothing.+ let before = serde_json::to_value(&h.app.preferences).unwrap();+ let hostile = crate::context_menu::Item::action("ui.navPreset", "Nope", "").with(json!({"preset": "nope"}));+ crate::context_menu::run_item(&mut h.app, &hostile, egui::pos2(0.0, 0.0));+ assert_eq!(serde_json::to_value(&h.app.preferences).unwrap(), before);+ // A second click on the button closes the menu it opened.+ h.step(&json!({"click":{"handle":"status:navigation"}})).unwrap();+ h.settle();+ assert!(h.app.viewport.context_menu.is_some());+ h.step(&json!({"click":{"handle":"status:navigation"}})).unwrap();+ h.settle();+ assert!(h.app.viewport.context_menu.is_none());+}+#[test]fn status_bar_stays_out_of_the_embed() {let mut h = Harness::new();diff --git a/crates/ui-egui/src/status_bar.rs b/crates/ui-egui/src/status_bar.rsindex 8e4618493a..b44914a1c5 100644--- a/crates/ui-egui/src/status_bar.rs+++ b/crates/ui-egui/src/status_bar.rs@@ -1,8 +1,10 @@//! The status bar (Preferences › Interface › Status bar, #7243; off by default): a thin bar//! along the window's bottom edge, below the History strip, with what is being edited, the open//! sketch's state in that state's colour, and the design's units. A click on the units opens-//! the Units menu, whose rows run Change Design Units (`document.units`, one undo step). While-//! the bar is on, the sketch's state is not also painted in the view's top-left corner.+//! the Units menu, whose rows run Change Design Units (`document.units`, one undo step). Beside+//! it, the mouse mapping (Preferences › Navigation) opens a menu of the mappings, and picking one+//! sets what that page sets for it (#7023). While the bar is on, the sketch's state is not also+//! painted in the view's top-left corner.//!//! The bar is window chrome like the application bar: full width and flat in either workspace//! layout. It is allocated before the viewport, so the view and everything anchored to its@@ -12,6 +14,7 @@use egui::{Color32, FontId, Rect, Sense, pos2, vec2};use crate::SolveApp;+use crate::context_menu::Target;use crate::theme::Tokens;use crate::ui_kit::{Container, Edge, Surface};@@ -83,6 +86,8 @@ pub(crate) struct Contents {pub sketch: Option<SketchState>,/// The design's units (`mm`, `cm`, `m`, `in`, `ft`).pub units: String,+ /// The mouse mapping's name.+ pub navigation: String,}pub(crate) fn contents(app: &SolveApp) -> Contents {@@ -100,6 +105,7 @@ pub(crate) fn contents(app: &SolveApp) -> Contents {editing: edited.map(|name| crate::i18n::format("status.editing", &[("name", &crate::i18n::tr_name(&name))])),sketch: sketch.map(SketchState::of),units: app.session.doc.units.clone(),+ navigation: crate::prefs::nav_preset(&app.preferences.nav).translated_label().to_string(),}}@@ -124,9 +130,9 @@ pub(crate) fn show(app: &mut SolveApp, ui: &mut egui::Ui) {crate::scenario::publish_target("status:bar", response.rect);}-/// The items, right-aligned from the window's right edge: units, the sketch's state, what is-/// being edited. The left end stays free (for next-step hints, #2325). An item that would not-/// fit is left out, the leftmost first.+/// The items, right-aligned from the window's right edge: units, the mouse mapping, the sketch's+/// state, what is being edited. The left end stays free (for next-step hints, #2325). An item+/// that would not fit is left out, the leftmost first.fn paint(app: &mut SolveApp, ui: &mut egui::Ui, contents: &Contents) {let t = Tokens::get();let r = ui.max_rect();@@ -136,28 +142,12 @@ fn paint(app: &mut SolveApp, ui: &mut egui::Ui, contents: &Contents) {let left = r.left() + t.space_8;let mut right = r.right() - t.space_6;- // The units: a flat button with the kit's dropdown chevron.- let units = painter.layout_no_wrap(contents.units.clone(), font.clone(), t.text);- let chevron = t.space_10;- let width = t.space_6 + units.size().x + t.space_4 + chevron + t.space_6;- let button = Rect::from_min_max(pos2(right - width, r.top() + t.space_3), pos2(right, r.bottom() - t.space_3));- let response = ui.interact(button, egui::Id::new("sc_status_units"), Sense::click());- let open = app.viewport.context_menu.is_some() && app.menu.target == crate::context_menu::Target::Units;- if response.hovered() || open {- painter.rect_filled(button, t.control_radius, t.hover);- ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);- }- let units_at = pos2(button.left() + t.space_6, button.center().y - units.size().y / 2.0);- painter.galley(units_at, units, t.text);- let chevron_rect = Rect::from_center_size(pos2(button.right() - t.space_6 - chevron / 2.0, button.center().y), vec2(chevron, chevron));- crate::ui_kit::dropdown_indicator(&painter, chevron_rect, t.text_dim);- let label = crate::i18n::format("browser.units", &[("units", &contents.units)]);- response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, &label));- crate::scenario::publish_target("status:units", button);- if response.on_hover_text(crate::i18n::tr("status.units_tip")).clicked() {- // The menu opens above the bar, its right edge on the button's.- crate::context_menu::toggle_for(app, button, button.right_top(), crate::context_menu::Target::Units);- }+ // The units, then the mouse mapping: flat buttons that open their menus.+ let units = crate::i18n::format("browser.units", &[("units", &contents.units)]);+ let button = menu_button(app, ui, right, Target::Units, &contents.units, &units, crate::i18n::tr("status.units_tip"));+ let navigation = crate::i18n::format("status.navigation", &[("mapping", &contents.navigation)]);+ let tip = crate::i18n::tr("status.navigation_tip");+ let button = menu_button(app, ui, button.left() - t.space_4, Target::Navigation, &contents.navigation, &navigation, tip);right = button.left() - t.space_6;// The sketch's state, after a dot in its colour; then what is being edited.@@ -190,6 +180,35 @@ fn paint(app: &mut SolveApp, ui: &mut egui::Ui, contents: &Contents) {}}+/// A flat button with the kit's dropdown chevron, its right edge at `right`; a click opens (or+/// closes) the `target` menu above the bar, its right edge on the button's.+fn menu_button(app: &mut SolveApp, ui: &mut egui::Ui, right: f32, target: Target, text: &str, label: &str, tip: &str) -> Rect {+ let handle = if target == Target::Units { "status:units" } else { "status:navigation" };+ let t = Tokens::get();+ let r = ui.max_rect();+ let painter = ui.painter().clone();+ let galley = painter.layout_no_wrap(text.to_string(), FontId::proportional(t.font_body), t.text);+ let chevron = t.space_10;+ let width = t.space_6 + galley.size().x + t.space_4 + chevron + t.space_6;+ let button = Rect::from_min_max(pos2(right - width, r.top() + t.space_3), pos2(right, r.bottom() - t.space_3));+ let response = ui.interact(button, egui::Id::new(("sc_status_menu", handle)), Sense::click());+ let open = app.viewport.context_menu.is_some() && app.menu.target == target;+ if response.hovered() || open {+ painter.rect_filled(button, t.control_radius, t.hover);+ ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);+ }+ let text_at = pos2(button.left() + t.space_6, button.center().y - galley.size().y / 2.0);+ painter.galley(text_at, galley, t.text);+ let chevron_rect = Rect::from_center_size(pos2(button.right() - t.space_6 - chevron / 2.0, button.center().y), vec2(chevron, chevron));+ crate::ui_kit::dropdown_indicator(&painter, chevron_rect, t.text_dim);+ response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, label));+ crate::scenario::publish_target(handle, button);+ if response.on_hover_text(tip).clicked() {+ crate::context_menu::toggle_for(app, button, button.right_top(), target);+ }+ button+}+#[cfg(test)]mod tests {use super::*;