Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 80 additions & 24 deletions crates/ui-egui/src/mapview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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 {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
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 =
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
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));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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<Deletion> {
let names: Vec<String> = 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<Deletion> = 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") };
Expand Down Expand Up @@ -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<String> = 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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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`]).
Expand Down
44 changes: 32 additions & 12 deletions crates/ui-egui/src/ribbon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K: Clone + Default + PartialEq + Send + Sync + 'static>(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.
Expand Down Expand Up @@ -204,12 +221,22 @@ fn scroll_buttons<R>(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<R>(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<R>(
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::<f32>(key));
if let Some(x) = pending {
area = area.horizontal_scroll_offset(x);
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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);
}
}
});
Expand Down
Loading
Loading