From 73c84301771c3ee6ca63654a584ff347631304d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 23 Nov 2026 11:10:22 -0500 Subject: [PATCH 1/2] fix(ui): map and layout tabs that don't fit scroll with arrows, and the one in view stays in sight (#2785) The view tabs above the map were painted at fixed positions, so with many maps and layouts the last ones, and the New Map button, ran past the window with no way to reach them, and a map or layout opened from the Browser, a script or a command could be active with its tab out of sight. They now sit in the ribbon tab strip's arrow scroll area, the active one scrolls into view when it changes or the strip's room or tab widths do, and New Map stays outside the scrolled part. The ribbon's "keep the active tab in sight for a few frames" rule moves to one function both strips call. The arrow scroll area is no longer animated: an animated scroll-to request waits a pass, so the next frame's request added its distance again and ran the strip past the active tab. --- crates/ui-egui/src/mapview.rs | 104 ++++++++++++++++----- crates/ui-egui/src/ribbon.rs | 44 ++++++--- crates/ui-egui/tests/smoke.rs | 171 ++++++++++++++++++++++++++++++++++ 3 files changed, 283 insertions(+), 36 deletions(-) diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index 02e1648840..03556645ef 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -4675,23 +4675,89 @@ fn navigator(app: &mut RustGisApp, ui: &mut Ui, p: &egui::Painter, rect: Rect, t } } +/// Width of the New Map button after the view tabs. +const NEW_TAB_W: f32 = 22.0; + +/// ModelBuilder's view tab, while it is open: its model's name. +fn model_tab_name(app: &RustGisApp) -> Option { + app.mb.open.then(|| if app.mb.name.trim().is_empty() { tl!("Untitled model").to_string() } else { app.mb.name.trim().to_string() }) +} + +/// A view tab's width: its name, icon, padding and close button. +fn view_tab_w(ui: &Ui, name: &str) -> f32 { + crate::widgets::text_w(ui, name, regular(12.0)) + 46.0 + TAB_CLOSE_W +} + +/// The next view tab's place in the row, `w` wide, below the strip's top margin. +fn next_view_tab(ui: &mut Ui, w: f32) -> Rect { + let (r, _) = ui.allocate_exact_size(vec2(w, 30.0), Sense::hover()); + Rect::from_min_max(pos2(r.min.x, r.min.y + 4.0), r.max) +} + /// View tabs above the map, one per map in the project, like document tabs. +/// +/// The tabs scroll between arrow buttons when the window is too narrow for them, as the ribbon's +/// tab strip does ([`crate::ribbon::arrow_scroll_area`]), and the one in view scrolls into sight +/// when it changes, however it was opened (a click, the Browser, a script, a command), or when the +/// strip's room or the tabs' widths do. The New Map button stays out of the scrolled part, so it +/// is always in sight. fn view_tabs(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let (r, _) = ui.allocate_exact_size(vec2(ui.available_width(), 30.0), Sense::hover()); ui.painter().rect_filled(r, 0.0, t.tab_strip); ui.painter().line_segment([pos2(r.min.x, r.max.y - 0.5), pos2(r.max.x, r.max.y - 0.5)], Stroke::new(1.0, t.border)); - let mut x = r.min.x + 6.0; + // The tabs take the strip but the New Map button's room and the gaps around it. + let room = (r.width() - 6.0 - 4.0 - NEW_TAB_W - 4.0).max(60.0); + let p = &app.session.project; + let model = model_tab_name(app); + let widths: f32 = + p.maps.iter().map(|m| &m.name).chain(p.layouts.iter().map(|l| &l.name)).chain(&model).map(|n| view_tab_w(ui, n) + 2.0).sum(); + let in_view = if app.mb.shown() { (2, 0) } else { app.session.active_layout.map_or((0, p.active_map), |i| (1, i)) }; + let follow = + crate::ribbon::follow_active(ui, egui::Id::unique("view_tabs_active"), (in_view, room.round() as i32, widths.round() as i32)); + let strip = Rect::from_min_size(pos2(r.min.x + 6.0, r.min.y), vec2(room, r.height())); + let mut tabs_ui = ui.new_child(egui::UiBuilder::new().max_rect(strip).layout(egui::Layout::left_to_right(egui::Align::Min))); + let area = egui::ScrollArea::horizontal().id_salt("view_tabs").max_width(room); + let tips = [tl!("Scroll the map tabs left"), tl!("Scroll the map tabs right")]; + let mut close = None; + crate::ribbon::arrow_scroll_area(&mut tabs_ui, t, "view_tabs", area, tips, |ui| { + ui.horizontal(|ui| { + ui.spacing_mut().item_spacing = vec2(2.0, 0.0); + close = view_tab_row(app, ui, t, follow); + }); + }); + // Right after the tabs, or at the strip's right edge once they scroll. + let x = tabs_ui.min_rect().max.x + 2.0; + let pr = Rect::from_min_size(pos2(x + 2.0, r.min.y + 6.0), vec2(NEW_TAB_W, 20.0)); + let resp = ui.interact(pr, egui::Id::unique("maptab_new"), Sense::click()); + resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!("New Map"))); + if resp.hovered() { + ui.painter().rect_filled(pr, CornerRadius::same(4), t.hover); + } + ui.painter().text(pr.center(), Align2::CENTER_CENTER, "+", regular(15.0), t.text_dim); + if resp.on_hover_text(tl!("New Map")).clicked() { + let _ = app.run("map.new", Value::Null); + } + if close.is_some() { + app.map.deleting = close; + } +} + +/// The view tabs in a row: maps, layouts, then ModelBuilder. The one in view scrolls into sight +/// while `follow` says so. The map or layout whose close button was clicked, to confirm. +fn view_tab_row(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, follow: bool) -> Option { let names: Vec = app.session.project.maps.iter().map(|m| m.name.clone()).collect(); let active = if app.session.active_layout.is_some() || app.mb.shown() { usize::MAX } else { app.session.project.active_map }; // A tab's close button asks to delete its map or layout. let mut close: Option = None; for (i, n) in names.iter().enumerate() { - let w = crate::widgets::text_w(ui, n, regular(12.0)) + 46.0 + TAB_CLOSE_W; - let tr = Rect::from_min_size(pos2(x, r.min.y + 4.0), vec2(w, r.height() - 4.0)); + let tr = next_view_tab(ui, view_tab_w(ui, n)); let resp = ui.interact(tr, egui::Id::unique(("maptab", i)), Sense::click()); // Painted, so named for screen readers (and tests). resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, n)); let is = i == active; + if is && follow { + crate::ribbon::scroll_tab_into_view(ui, tr); + } // A project keeps at least one map (`map.remove` refuses the last). let last = names.len() < 2; let tip = if last { tl!("A project keeps at least one map") } else { tl!("Delete Map") }; @@ -4728,16 +4794,17 @@ fn view_tabs(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let _ = app.run("ui.dialog", json!({"name": "renameMap", "target": n})); } resp.context_menu(|ui| crate::panes::map_menu(app, ui, n)); - x += w + 2.0; } // Layouts, after the maps. let layouts: Vec = app.session.project.layouts.iter().map(|l| l.name.clone()).collect(); for (i, n) in layouts.iter().enumerate() { - let w = crate::widgets::text_w(ui, n, regular(12.0)) + 46.0 + TAB_CLOSE_W; - let tr = Rect::from_min_size(pos2(x, r.min.y + 4.0), vec2(w, r.height() - 4.0)); + let tr = next_view_tab(ui, view_tab_w(ui, n)); let resp = ui.interact(tr, egui::Id::unique(("layouttab", i)), Sense::click()); resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, n)); let is = app.session.active_layout == Some(i) && !app.mb.shown(); + if is && follow { + crate::ribbon::scroll_tab_into_view(ui, tr); + } if is { ui.painter().rect_filled(tr, CornerRadius { nw: 6, ne: 6, sw: 0, se: 0 }, t.ribbon); ui.painter().rect_filled( @@ -4775,15 +4842,16 @@ fn view_tabs(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let _ = app.run("ui.dialog", json!({"name": "renameLayout", "target": n})); } resp.context_menu(|ui| crate::panes::layout_menu(app, ui, n)); - x += w + 2.0; } // ModelBuilder, after the layouts. - if app.mb.open { - let n = if app.mb.name.trim().is_empty() { tl!("Untitled model").to_string() } else { app.mb.name.trim().to_string() }; - let w = crate::widgets::text_w(ui, &n, regular(12.0)) + 46.0 + TAB_CLOSE_W; - let tr = Rect::from_min_size(pos2(x, r.min.y + 4.0), vec2(w, r.height() - 4.0)); + if let Some(n) = model_tab_name(app) { + let tr = next_view_tab(ui, view_tab_w(ui, &n)); let resp = ui.interact(tr, egui::Id::unique("modeltab"), Sense::click()); + resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &n)); let is = app.mb.shown(); + if is && follow { + crate::ribbon::scroll_tab_into_view(ui, tr); + } if is { ui.painter().rect_filled(tr, CornerRadius { nw: 6, ne: 6, sw: 0, se: 0 }, t.ribbon); ui.painter().rect_filled( @@ -4814,20 +4882,8 @@ fn view_tabs(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { if resp.clicked() && !is { let _ = app.run("ui.modelBuilder", Value::Null); } - x += w + 2.0; - } - let pr = Rect::from_min_size(pos2(x + 2.0, r.min.y + 6.0), vec2(22.0, 20.0)); - let resp = ui.interact(pr, egui::Id::unique("maptab_new"), Sense::click()); - if resp.hovered() { - ui.painter().rect_filled(pr, CornerRadius::same(4), t.hover); - } - ui.painter().text(pr.center(), Align2::CENTER_CENTER, "+", regular(15.0), t.text_dim); - if resp.on_hover_text(tl!("New Map")).clicked() { - let _ = app.run("map.new", Value::Null); - } - if close.is_some() { - app.map.deleting = close; } + close } /// A map or a layout, by name, to delete once the user confirms ([`confirm_delete`]). diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 8183048b63..52d3eeea35 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -166,6 +166,23 @@ pub fn show(app: &mut RustGisApp, ui: &mut Ui) { const SCROLL_BUTTON_W: f32 = 18.0; +/// Whether a tab strip's active tab scrolls into view on this frame: for a few frames (a pass that +/// egui discards drops its scroll request) after `key` changes, the key being the active tab, the +/// strip's room and its tabs' widths. Then the wheel and the arrows move the strip freely. +pub(crate) fn follow_active(ui: &Ui, id: egui::Id, key: K) -> bool { + let (shown, scrolls): (K, u8) = ui.data(|d| d.get_temp(id)).unwrap_or_default(); + let scrolls = if shown == key { scrolls } else { 0 }; + if scrolls < 3 { + ui.data_mut(|d| d.insert_temp(id, (key, scrolls + 1))); + } + scrolls < 3 +} + +/// Scrolls the active tab at `r` into view, clear of the arrow buttons. +pub(crate) fn scroll_tab_into_view(ui: &Ui, r: Rect) { + ui.scroll_to_rect_animation(r.expand2(vec2(SCROLL_BUTTON_W, 0.0)), None, egui::style::ScrollAnimation::none()); +} + /// Arrow buttons at the edges of a horizontal scroll area (the ribbon's groups, the tab strip) /// whose content is wider than the window: the scroll bar is hidden (it would cover the group /// labels and tabs) and the wheel alone is undiscoverable. @@ -204,12 +221,22 @@ fn scroll_buttons(ui: &mut Ui, t: &Tokens, scroll: &egui::scroll_area::Scroll /// A horizontal scroll area (`area`, named `name`) without a scroll bar, with [`scroll_buttons`] /// at its edges. -fn arrow_scroll_area(ui: &mut Ui, t: &Tokens, name: &str, area: egui::ScrollArea, tips: [&str; 2], add: impl FnOnce(&mut Ui) -> R) { +pub(crate) fn arrow_scroll_area( + ui: &mut Ui, + t: &Tokens, + name: &str, + area: egui::ScrollArea, + tips: [&str; 2], + add: impl FnOnce(&mut Ui) -> R, +) { // egui sets a scroll area's offset only as it is built, so a click moves it on the next frame. // It is asked for once, on a frame egui keeps: a discarded one doesn't move the area, and asking // on every frame would pin the area there against the wheel. let key = ui.make_persistent_id(("arrow_scroll_offset", name)); - let mut area = area.scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysHidden); + // Not animated: an animated scroll-to request (even with no animation) waits as a target until + // the next pass, so the next request, computed from the tab where it was still drawn, adds its + // distance again and the strip runs past the active tab ([`follow_active`] asks on a few frames). + let mut area = area.scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysHidden).animated(false); let pending = ui.data(|d| d.get_temp::(key)); if let Some(x) = pending { area = area.horizontal_scroll_offset(x); @@ -263,15 +290,8 @@ fn tab_strip(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { // Tighter tabs when the window is too narrow for the usual padding. let text: f32 = tabs.iter().map(|(_, c)| text_w(ui, c, semibold(12.5))).sum(); let pad = ((room - text) / tabs.len().max(1) as f32).clamp(8.0, 22.0); - // The active tab scrolls into view for a few frames (a pass that egui discards drops its - // scroll request) after it changes, or the strip's room or tab widths do. - let active_id = egui::Id::unique("tab_strip_active"); let key = (app.ui.tab.clone(), room.round() as i32, text.round() as i32); - let (shown, scrolls): ((String, i32, i32), u8) = ui.data(|d| d.get_temp(active_id)).unwrap_or_default(); - let scrolls = if shown == key { scrolls } else { 0 }; - if scrolls < 3 { - ui.data_mut(|d| d.insert_temp(active_id, (key, scrolls + 1))); - } + let follow = follow_active(ui, egui::Id::unique("tab_strip_active"), key); let area = egui::ScrollArea::horizontal().id_salt("tab_strip").max_width(room); let tips = [tl!("Scroll the tabs left"), tl!("Scroll the tabs right")]; arrow_scroll_area(ui, t, "tab_strip", area, tips, |ui| { @@ -320,8 +340,8 @@ fn tab_strip(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } // A tab that became active (clicked, or opened by a command) scrolls // into view, clear of the arrow buttons. - if active && scrolls < 3 { - ui.scroll_to_rect_animation(r.expand2(vec2(SCROLL_BUTTON_W, 0.0)), None, egui::style::ScrollAnimation::none()); + if active && follow { + scroll_tab_into_view(ui, r); } } }); diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs index f5c598b3e5..10b044595d 100644 --- a/crates/ui-egui/tests/smoke.rs +++ b/crates/ui-egui/tests/smoke.rs @@ -1005,6 +1005,177 @@ fn a_tab_strip_wider_than_the_window_scrolls_with_arrow_buttons() { assert!(h.query_by_label("Scroll the tabs left").is_none()); } +fn settle(h: &mut egui_kittest::Harness<'_, RustGisApp>) { + for _ in 0..4 { + h.step(); + } +} + +/// A 900-point window with the sample's map, nine more maps and three layouts: more view tabs +/// than fit above the map. The last one created (a layout) is active. +fn crowded_view_tabs() -> egui_kittest::Harness<'static, RustGisApp> { + let mut h = harness(); + h.set_size(egui::vec2(900.0, 600.0)); + for i in 1..=9 { + h.state_mut().run("map.new", json!({"name": format!("Tab Map {i}")})).unwrap(); + } + for i in 1..=3 { + h.state_mut().run("layout.new", json!({"name": format!("Tab Layout {i}")})).unwrap(); + } + settle(&mut h); + h +} + +/// Back to the first map, as a click on its tab does. +fn activate_world(h: &mut egui_kittest::Harness<'_, RustGisApp>) { + h.state_mut().run("layout.close", json!(null)).unwrap(); + h.state_mut().run("map.activate", json!({"name": "World"})).unwrap(); + settle(h); +} + +/// The view tab named `name` (the Browser and other panes can show the same name elsewhere): +/// the node on the strip's row, which the map tabs' scroll arrows span. +fn view_tab(h: &egui_kittest::Harness<'_, RustGisApp>, name: &str) -> egui::Rect { + let arrow = h.query_by_label("Scroll the map tabs right").or_else(|| h.query_by_label("Scroll the map tabs left")); + let row = arrow.expect("the strip overflows, so it has an arrow").rect().center().y; + h.query_all_by_label(name).map(|n| n.rect()).find(|r| (r.center().y - row).abs() < 8.0).unwrap_or_else(|| panic!("no view tab {name}")) +} + +/// Whether the view tab `name` is wholly in sight: inside the window and clear of the arrows. +fn view_tab_in_sight(h: &egui_kittest::Harness<'_, RustGisApp>, name: &str) -> bool { + let tab = view_tab(h, name); + let left = h.query_by_label("Scroll the map tabs left").map_or(0.0, |n| n.rect().right()); + let right = h.query_by_label("Scroll the map tabs right").map_or(h.ctx.content_rect().right(), |n| n.rect().left()); + tab.left() >= left - 0.5 && tab.right() <= right + 0.5 +} + +/// The name of the map or layout in view. +fn active_view(h: &egui_kittest::Harness<'_, RustGisApp>) -> String { + let s = &h.state().session; + s.active_layout.map_or_else(|| s.map().name.clone(), |i| s.project.layouts[i].name.clone()) +} + +#[test] +fn map_and_layout_tabs_wider_than_the_window_scroll_with_arrow_buttons() { + let mut h = harness(); + h.set_size(egui::vec2(900.0, 600.0)); + settle(&mut h); + // The sample's one map fits: no arrows. + assert!(h.query_by_label("Scroll the map tabs right").is_none()); + assert!(h.query_by_label("Scroll the map tabs left").is_none()); + let mut h = crowded_view_tabs(); + activate_world(&mut h); + // The first tab active: only the right arrow. + assert!(h.query_by_label("Scroll the map tabs left").is_none()); + assert!(view_tab_in_sight(&h, "World")); + assert!(!view_tab_in_sight(&h, "Tab Layout 3")); + h.get_by_label("Scroll the map tabs right").click(); + settle(&mut h); + let _ = h.get_by_label("Scroll the map tabs left"); + // A click on an arrow scrolls; it doesn't open the tab under it. + assert_eq!(active_view(&h), "World"); + for _ in 0..20 { + let Some(right) = h.query_by_label("Scroll the map tabs right") else { break }; + right.click(); + settle(&mut h); + } + assert!(h.query_by_label("Scroll the map tabs right").is_none()); + assert!(view_tab_in_sight(&h, "Tab Layout 3")); + assert_eq!(active_view(&h), "World"); + // The New Map button stays in the window, past the last tab. + let new_map = view_tab(&h, "New Map"); + assert!(new_map.right() <= 900.0, "New Map at {new_map:?}"); + assert!(new_map.left() >= view_tab(&h, "Tab Layout 3").right(), "New Map at {new_map:?}"); +} + +#[test] +fn the_active_view_tab_scrolls_into_view_however_it_is_activated() { + let mut h = crowded_view_tabs(); + // A new layout (the last tab) is active on its creation. + assert_eq!(active_view(&h), "Tab Layout 3"); + assert!(view_tab_in_sight(&h, "Tab Layout 3")); + // Activated by a command, as the Browser and scripts do. + activate_world(&mut h); + assert!(view_tab_in_sight(&h, "World")); + h.state_mut().run("map.activate", json!({"name": "Tab Map 9"})).unwrap(); + settle(&mut h); + assert!(view_tab_in_sight(&h, "Tab Map 9")); + activate_world(&mut h); + h.state_mut().run("layout.activate", json!({"name": "Tab Layout 2"})).unwrap(); + settle(&mut h); + assert!(view_tab_in_sight(&h, "Tab Layout 2")); + // ModelBuilder's tab, after the layouts. + activate_world(&mut h); + h.state_mut().run("ui.modelBuilder", json!(null)).unwrap(); + settle(&mut h); + let model = h.state().mb.name.trim().to_string(); + assert!(!model.is_empty() && view_tab_in_sight(&h, &model), "ModelBuilder's tab {model:?}"); + h.state_mut().run("ui.modelBuilder", json!({"close": true})).unwrap(); + // A new map, and a copy of one, come into view. + activate_world(&mut h); + h.state_mut().run("map.new", json!({"name": "Tab Map 10"})).unwrap(); + settle(&mut h); + assert!(view_tab_in_sight(&h, "Tab Map 10")); + activate_world(&mut h); + h.state_mut().run("map.duplicate", json!({"map": "Tab Map 9"})).unwrap(); + settle(&mut h); + let copy = active_view(&h); + assert_ne!(copy, "World"); + assert!(view_tab_in_sight(&h, ©)); + // A narrower window keeps the active tab in sight. + h.state_mut().run("map.activate", json!({"name": "Tab Map 10"})).unwrap(); + settle(&mut h); + h.set_size(egui::vec2(700.0, 600.0)); + settle(&mut h); + assert!(view_tab_in_sight(&h, "Tab Map 10")); + h.set_size(egui::vec2(900.0, 600.0)); + settle(&mut h); + // Renamed to a longer name, the active tab grows and stays in sight. + let long = "Tab Map 10 with a much longer name than before"; + h.state_mut().run("map.rename", json!({"name": long})).unwrap(); + settle(&mut h); + assert!(view_tab_in_sight(&h, long)); +} + +#[test] +fn view_tabs_lose_their_arrows_when_they_fit_and_the_wheel_moves_them_freely() { + let mut h = crowded_view_tabs(); + activate_world(&mut h); + // The wheel scrolls the strip away from the active tab, which doesn't pull it back. + let row = view_tab(&h, "World").center().y; + for _ in 0..6 { + h.input_mut().events.push(egui::Event::PointerMoved(egui::pos2(450.0, row))); + h.input_mut().events.push(egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + delta: egui::vec2(-200.0, 0.0), + phase: egui::TouchPhase::Move, + modifiers: Default::default(), + source: egui::MouseWheelSource::Wheel, + }); + h.step(); + } + settle(&mut h); + let _ = h.get_by_label("Scroll the map tabs left"); + assert!(!view_tab_in_sight(&h, "World")); + // Deleted until they fit: no arrows. + for i in 1..=3 { + h.state_mut().run("layout.remove", json!({"name": format!("Tab Layout {i}")})).unwrap(); + } + for i in 1..=9 { + h.state_mut().run("map.remove", json!({"name": format!("Tab Map {i}")})).unwrap(); + } + settle(&mut h); + assert!(h.query_by_label("Scroll the map tabs right").is_none()); + assert!(h.query_by_label("Scroll the map tabs left").is_none()); + // Undone, they overflow again, with the tab in view in sight. + for _ in 0..12 { + h.state_mut().run("edit.undo", json!(null)).unwrap(); + } + settle(&mut h); + assert_eq!(h.state().session.project.maps.len(), 10); + assert!(view_tab_in_sight(&h, &active_view(&h))); +} + #[test] fn plugins_install_from_a_file_after_a_prompt() { let _turn = PLUGINS.lock().unwrap_or_else(|e| e.into_inner()); From 545900290164702fe55bca7bd1192a2098c3e1d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 23 Nov 2026 11:38:20 -0500 Subject: [PATCH 2/2] fix(ui): follow the view tab in view by its place in the row, not the row's total width (#2785) Two names swapped across the active tab in one go (a script) move it without changing the row's total width, so the key the tab is followed by stayed the same and the tab could end up out of sight. The key now holds where the tab in view starts and how wide it is. --- crates/ui-egui/src/mapview.rs | 18 ++++++++++++++---- crates/ui-egui/src/ribbon.rs | 5 +++-- crates/ui-egui/tests/smoke.rs | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index 03556645ef..8d38d6f411 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -4709,11 +4709,21 @@ fn view_tabs(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let room = (r.width() - 6.0 - 4.0 - NEW_TAB_W - 4.0).max(60.0); let p = &app.session.project; let model = model_tab_name(app); - let widths: f32 = - p.maps.iter().map(|m| &m.name).chain(p.layouts.iter().map(|l| &l.name)).chain(&model).map(|n| view_tab_w(ui, n) + 2.0).sum(); let in_view = if app.mb.shown() { (2, 0) } else { app.session.active_layout.map_or((0, p.active_map), |i| (1, i)) }; - let follow = - crate::ribbon::follow_active(ui, egui::Id::unique("view_tabs_active"), (in_view, room.round() as i32, widths.round() as i32)); + // Where the tab in view starts in the row, and how wide it is: it moves when a tab before it + // changes width, even when the row's total width doesn't (two names swapped across it). + let tabs = p.maps.iter().enumerate().map(|(i, m)| ((0, i), &m.name)); + let tabs = tabs.chain(p.layouts.iter().enumerate().map(|(i, l)| ((1, i), &l.name))).chain(model.iter().map(|n| ((2, 0), n))); + let (mut start, mut span) = (0.0, (0.0, 0.0)); + for (tab, n) in tabs { + let w = view_tab_w(ui, n) + 2.0; + if tab == in_view { + span = (start, w); + } + start += w; + } + let key = (in_view, room.round() as i32, span.0.round() as i32, span.1.round() as i32); + let follow = crate::ribbon::follow_active(ui, egui::Id::unique("view_tabs_active"), key); let strip = Rect::from_min_size(pos2(r.min.x + 6.0, r.min.y), vec2(room, r.height())); let mut tabs_ui = ui.new_child(egui::UiBuilder::new().max_rect(strip).layout(egui::Layout::left_to_right(egui::Align::Min))); let area = egui::ScrollArea::horizontal().id_salt("view_tabs").max_width(room); diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 52d3eeea35..142a5126db 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -167,8 +167,9 @@ pub fn show(app: &mut RustGisApp, ui: &mut Ui) { const SCROLL_BUTTON_W: f32 = 18.0; /// Whether a tab strip's active tab scrolls into view on this frame: for a few frames (a pass that -/// egui discards drops its scroll request) after `key` changes, the key being the active tab, the -/// strip's room and its tabs' widths. Then the wheel and the arrows move the strip freely. +/// egui discards drops its scroll request) after `key` changes, the key being what puts the active +/// tab where it is: which tab it is, the strip's room, and the widths that place it. Then the wheel +/// and the arrows move the strip freely. pub(crate) fn follow_active(ui: &Ui, id: egui::Id, key: K) -> bool { let (shown, scrolls): (K, u8) = ui.data(|d| d.get_temp(id)).unwrap_or_default(); let scrolls = if shown == key { scrolls } else { 0 }; diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs index 10b044595d..01b8c6a2b0 100644 --- a/crates/ui-egui/tests/smoke.rs +++ b/crates/ui-egui/tests/smoke.rs @@ -1137,6 +1137,25 @@ fn the_active_view_tab_scrolls_into_view_however_it_is_activated() { assert!(view_tab_in_sight(&h, long)); } +#[test] +fn the_active_view_tab_stays_in_sight_when_tabs_before_it_change_width_but_not_the_total() { + let mut h = crowded_view_tabs(); + let long = "Tab Map 9, with a much longer name than the others"; + h.state_mut().run("map.rename", json!({"map": "Tab Map 9", "name": long})).unwrap(); + activate_world(&mut h); + h.state_mut().run("map.activate", json!({"name": "Tab Map 5"})).unwrap(); + settle(&mut h); + assert!(view_tab_in_sight(&h, "Tab Map 5")); + // A script swaps the names of a map before the active one and a map after it, in one go: the + // tabs' total width is the same, but the active tab moves right by the difference. + for (map, name) in [("Tab Map 1", "Swap"), (long, "Tab Map 1"), ("Swap", long)] { + h.state_mut().run("map.rename", json!({"map": map, "name": name})).unwrap(); + } + assert_eq!(h.state().session.map().name, "Tab Map 5"); + settle(&mut h); + assert!(view_tab_in_sight(&h, "Tab Map 5")); +} + #[test] fn view_tabs_lose_their_arrows_when_they_fit_and_the_wheel_moves_them_freely() { let mut h = crowded_view_tabs();