diff --git a/crates/ui-egui/src/dialogs/color_picker.rs b/crates/ui-egui/src/dialogs/color_picker.rs index f86ee17f51..c16d8d9e6e 100644 --- a/crates/ui-egui/src/dialogs/color_picker.rs +++ b/crates/ui-egui/src/dialogs/color_picker.rs @@ -663,8 +663,9 @@ fn swatch_list(app: &VectorcraftApp, ui: &mut Ui, d: &mut Dialog, current: &Colo ui.add_space(4.0); widgets::list_box(ui, |ui| { let max_h = (side - 28.0).max(1.0); - egui::ScrollArea::vertical().id_salt("cp-swatches").max_height(max_h).show(ui, |ui| { - ui.set_width(list_w - 8.0); + // The rows take the list's width less its scroll bar's column. + egui::ScrollArea::vertical().id_salt("cp-swatches").max_width(list_w - 8.0).max_height(max_h).auto_shrink([false, true]).show(ui, |ui| { + ui.set_width(ui.available_width()); let cur = d.str("library"); if cur.is_empty() || cur == swatchlib::DOCUMENT_SWATCHES { let Some(st) = app.session.active() else { diff --git a/crates/ui-egui/src/dialogs/color_picker/tests.rs b/crates/ui-egui/src/dialogs/color_picker/tests.rs index 0786df7ca9..79864f8d13 100644 --- a/crates/ui-egui/src/dialogs/color_picker/tests.rs +++ b/crates/ui-egui/src/dialogs/color_picker/tests.rs @@ -775,6 +775,53 @@ fn compact_picker_shrinks_then_scrolls_without_enlarging_the_window() { } } +/// #5941: in a window short enough to scroll the picker, at any width, its scroll bar shows +/// inside the window and the content fits beside it: no label is cut off at the side. +#[test] +fn a_scrolling_picker_shows_its_bar_and_fits_its_content_beside_it() { + fn sideways_clipped(shape: &egui::Shape, clip: Rect, out: &mut Vec) { + match shape { + egui::Shape::Text(t) => { + let r = t.visual_bounding_rect(); + let rows_shown = clip.top() <= r.top() && r.bottom() <= clip.bottom(); + if rows_shown && !t.galley.text().trim().is_empty() && (r.left() < clip.left() - 1.0 || r.right() > clip.right() + 1.0) { + out.push(format!("{:?} {r:?} in {clip:?}", t.galley.text())); + } + } + egui::Shape::Vec(v) => v.iter().for_each(|s| sideways_clipped(s, clip, out)), + _ => {} + } + } + for width in (400..=800).step_by(8) { + let size = vec2(width as f32, 380.0); + let (ctx, mut app) = (context(), app()); + app.run("ui.colorPicker", json!({"color": "#0000ff"})).unwrap(); + settled(&ctx, &mut app, size); + let input = egui::RawInput { screen_rect: Some(Rect::from_min_size(egui::Pos2::ZERO, size)), ..Default::default() }; + let mut out = ctx.run_ui(input, |ui| super::super::show(&mut app, ui.ctx())); + out.textures_delta.clear(); + let mut clipped = vec![]; + for s in &out.shapes { + sideways_clipped(&s.shape, s.clip_rect, &mut clipped); + } + assert!(clipped.is_empty(), "{size:?}: {clipped:?}"); + let outer = ctx.memory(|m| m.area_rect(egui::Id::unique(("dialog", KIND)))).unwrap(); + assert!(outer.left() >= -0.5 && outer.right() <= size.x + 0.5, "{size:?}: the window {outer:?} stays on screen"); + // The content's scroll bar: a narrow filled rectangle taller than a row, inside the window. + let mut bars = vec![]; + let mut walk = vec![]; + out.shapes.iter().for_each(|c| walk.push(c.shape.clone())); + while let Some(s) = walk.pop() { + match s { + egui::Shape::Vec(v) => walk.extend(v), + egui::Shape::Rect(r) if r.fill.a() > 0 && r.rect.width() <= 8.0 && r.rect.height() > 40.0 => bars.push(r.rect), + _ => {} + } + } + assert!(bars.iter().any(|b| outer.contains_rect(*b)), "{size:?}: the content scrolls and its bar shows in {outer:?}: {bars:?}"); + } +} + /// Type all three Lab components through the numeric fields, then apply the picked color. #[test] fn native_lab_entry_in_color_picker() { diff --git a/crates/ui-egui/src/panels/glyphs.rs b/crates/ui-egui/src/panels/glyphs.rs index 355b75f01b..888fca34fb 100644 --- a/crates/ui-egui/src/panels/glyphs.rs +++ b/crates/ui-egui/src/panels/glyphs.rs @@ -61,6 +61,18 @@ fn nudge_cell(index: usize, delta: i32) -> usize { (index as i32 + delta).clamp(0, last) as usize } +/// The cells in a row of a grid `w` wide that holds `count` cells of side `cell` in a list at most +/// `list_h` tall. When the rows run past the list, its scroll bar takes a column of its own and the +/// cells go beside it; when they fit, the cells take the whole width. +fn grid_cols(ui: &Ui, w: f32, cell: f32, count: usize, list_h: f32) -> usize { + let fit = |room: f32| ((room / cell).floor() as usize).max(1); + let gap = ui.spacing().item_spacing.y; + let cols = fit(w); + // The height `ScrollArea::show_rows` gives the rows: the gaps between them, none after the last. + let content_h = count.div_ceil(cols) as f32 * (cell + gap) - gap; + if list_h.min(ui.available_height()).ceil() < content_h { fit(w - ui.spacing().scroll.allocated_width()) } else { cols } +} + /// Character subsets offered by the Show menu. const SUBSETS: [(&str, u32, u32); 7] = [ ("Entire Font", 0, 0x10FFFF), @@ -372,10 +384,10 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let (chars, unencoded) = grid_glyphs(&face, category.and_then(|i| face.glyph_categories().get(i)), subset, (lo, hi)); let total = chars.len() + unencoded.len(); let cell = CELL_STEPS[cell_index(app)]; - let cols = ((w / cell).floor() as usize).max(1); - let rows = total.div_ceil(cols); let px = (cell * ui.ctx().pixels_per_point()).round().clamp(8.0, 256.0) as u32; let list_h = widgets::panel_list_height(ui, (ui.available_height() - 64.0).max(cell * 3.0), 33.0); + let cols = grid_cols(ui, w, cell, total, list_h); + let rows = total.div_ceil(cols); let hover_id = egui::Id::unique("gl-hover"); let selected = selected_char(app); // What a double-click on an unencoded glyph can do to the selected character. @@ -603,10 +615,10 @@ fn alternates(app: &mut VectorcraftApp, ui: &mut Ui, w: f32) { default: false, })); let cell = CELL_STEPS[cell_index(app)]; - let cols = ((w / cell).floor() as usize).max(1); - let rows = list.len().div_ceil(cols); let px = (cell * ui.ctx().pixels_per_point()).round().clamp(8.0, 256.0) as u32; let list_h = widgets::panel_list_height(ui, (ui.available_height() - 64.0).max(cell * 3.0), 33.0); + let cols = grid_cols(ui, w, cell, list.len(), list_h); + let rows = list.len().div_ceil(cols); let mut pick = None; // A scroll position of its own for each character, so the grid starts at the top for another. let area = egui::ScrollArea::vertical() @@ -2476,6 +2488,117 @@ mod tests { assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1, "one undo step"); } + /// The panel drawn `width` by `height` after a few frames (the scroll bar fades in): the left + /// edge of its scroll bar, when one is drawn, the grid's cells and the rectangle they are + /// clipped to. + fn grid_with_bar(ctx: &egui::Context, app: &mut VectorcraftApp, width: f32, height: f32, time: &mut f64) -> (Option, Vec, Rect) { + fn flat(s: Shape, out: &mut Vec) { + match s { + Shape::Vec(v) => v.into_iter().for_each(|s| flat(s, out)), + s => out.push(s), + } + } + let mut shapes = vec![]; + let mut clip = Rect::NOTHING; + for _ in 0..4 { + *time += 0.5; + let raw = + egui::RawInput { time: Some(*time), screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(width, height))), ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| show(app, ui)); + out.textures_delta.clear(); + shapes.clear(); + for c in out.shapes { + if !squares(std::slice::from_ref(&c.shape)).is_empty() { + clip = c.clip_rect; + } + flat(c.shape, &mut shapes); + } + } + // The bar's track and handle: narrow filled rectangles taller than a cell. + let bar = shapes + .iter() + .filter_map(|s| match s { + Shape::Rect(r) if r.fill != Color32::TRANSPARENT && r.rect.width() <= 8.0 && r.rect.height() > 40.0 => Some(r.rect.left()), + _ => None, + }) + .min_by(f32::total_cmp); + (bar, squares(&shapes), clip) + } + + /// #5941: the grid's cells go beside its scroll bar. At every panel width the bar shows and the + /// last cell of a row ends before it. + #[test] + fn the_grids_cells_end_before_its_scroll_bar() { + use vectorcraft_text::test_fonts::{JOINING_ALTERNATES_FAMILY, joining_alternates_font}; + FontDb::global().add_font(joining_alternates_font().expect("the test font builds")); + let (mut app, id) = editing("\u{628}"); + app.run("text.setRangeStyle", json!({"id": id, "font": JOINING_ALTERNATES_FAMILY})).unwrap(); + let ctx = egui::Context::default(); + crate::theme::apply(&ctx, Default::default()); + let mut time = 0.0; + for width in 240..=300 { + let (bar, cells, _) = grid_with_bar(&ctx, &mut app, width as f32, 420.0, &mut time); + // Cells wider than the room beside the bar push the bar past the panel's edge. + let Some(bar) = bar else { panic!("width {width}: the grid scrolls and shows its bar") }; + assert!(!cells.is_empty(), "width {width}: the grid is drawn"); + let last = cells.iter().map(|r| r.right()).fold(f32::MIN, f32::max); + assert!(last <= bar - 1.0, "width {width}: the last cell ends at {last}, the bar starts at {bar}"); + } + } + + /// #5941: Alternates for Current Selection lays out its cells the same way. While its rows run + /// past the list, the cells end before the scroll bar; while they fit, no bar shows and the + /// cells fill the whole width. + #[test] + fn the_alternates_grid_leaves_room_for_its_scroll_bar_only_while_it_scrolls() { + use vectorcraft_text::test_fonts::{alternates_family, alternates_font}; + FontDb::global().add_font(alternates_font(true).expect("the test font builds")); + let (mut app, id) = editing("xay"); + app.run("text.setRangeStyle", json!({"id": id, "font": alternates_family(true)})).unwrap(); + select(&mut app, 2, 2); + let ctx = egui::Context::default(); + crate::theme::apply(&ctx, Default::default()); + set_pstate(&ctx, "gl-subset", SUBSETS.len()); + let mut time = 0.0; + let (mut scrolled, mut fitted) = (0, 0); + // The largest cells, so seven glyphs fill two to four rows. In a panel 360 pt tall, three + // rows of them run past the list; in one 640 pt tall, they fit. The sweep covers panel + // widths at three heights, then the heights around the one where three rows just fit. + let cell = CELL_STEPS[CELL_STEPS.len() - 1]; + set_cell_index(&mut app, CELL_STEPS.len() - 1); + let sizes = [360, 420, 640].into_iter().flat_map(|h| (240..=320).map(move |w| (w, h))).chain((360..=420).map(|h| (290, h))); + for (width, height) in sizes { + let (bar, cells, clip) = grid_with_bar(&ctx, &mut app, width as f32, height as f32, &mut time); + assert!(!cells.is_empty(), "{width}×{height}: the grid is drawn"); + let last = cells.iter().map(|r| r.right()).fold(f32::MIN, f32::max); + let left = cells.iter().map(|r| r.left()).fold(f32::MAX, f32::min); + let row = cells.iter().filter(|r| (r.top() - cells[0].top()).abs() < 1.0).count(); + // The cells a row holds across the whole width. + let room = (((width as f32 - 2.0 * left) / cell).floor() as usize).clamp(1, 7); + if let Some(bar) = bar { + scrolled += 1; + assert!(last <= bar - 1.0, "{width}×{height}: the last cell ends at {last}, the bar starts at {bar}"); + // Rows narrowed for the bar only when full-width rows would not fit either. + if row < room { + let step = cells.iter().map(|r| r.top() - cells[0].top()).filter(|d| *d > 1.0).fold(f32::MAX, f32::min); + let full = 7usize.div_ceil(room) as f32 * step - (step - cell); + assert!(full > clip.height() + 0.5, "{width}×{height}: {room} to a row would fit in {full} pt, the list is {}", clip.height()); + } + } else { + fitted += 1; + // Without a bar every cell shows whole; a grid that runs past its list would need one. + assert_eq!(cells.len(), 7, "{width}×{height}: a and its six alternates all show"); + let bottom = cells.iter().map(|r| r.bottom()).fold(f32::MIN, f32::max); + assert!( + bottom <= clip.bottom() + 0.5 && last <= clip.right() + 0.5, + "{width}×{height}: the cells end at ({last}, {bottom}), inside the list {clip:?}" + ); + assert_eq!(row, room, "{width}×{height}: without a bar a row takes the whole width (cells from {left})"); + } + } + assert!(scrolled > 0 && fitted > 0, "both layouts are drawn: {scrolled} scrolled, {fitted} fitted"); + } + /// Show › Entire Font also lists the glyphs no character maps. #[test] fn entire_font_lists_the_unencoded_glyphs() { diff --git a/crates/ui-egui/src/prefs_dialog/tests.rs b/crates/ui-egui/src/prefs_dialog/tests.rs index f7c676d441..9b3792ea6b 100644 --- a/crates/ui-egui/src/prefs_dialog/tests.rs +++ b/crates/ui-egui/src/prefs_dialog/tests.rs @@ -163,6 +163,53 @@ fn app() -> VectorcraftApp { VectorcraftApp::new(Session::new(), Services::default()) } +/// #5941: a page whose settings run past the bottom of the list showed no scroll bar, so a setting +/// lower on the page was easy to miss. Every page that scrolls shows its bar inside the window, and +/// no label is cut off at the side. +#[test] +fn a_page_that_scrolls_shows_its_bar() { + fn walk(shape: &egui::Shape, clip: egui::Rect, texts: &mut Vec<(String, egui::Rect, egui::Rect)>, bars: &mut Vec) { + match shape { + egui::Shape::Text(t) if !t.galley.text().trim().is_empty() => texts.push((t.galley.text().into(), t.visual_bounding_rect(), clip)), + egui::Shape::Rect(r) if r.fill.a() > 0 && r.rect.width() <= 8.0 && r.rect.height() > 40.0 => bars.push(r.rect), + egui::Shape::Vec(v) => v.iter().for_each(|s| walk(s, clip, texts, bars)), + _ => {} + } + } + let mut scrolled = 0; + for cat in PREF_CATEGORIES { + let mut a = app(); + open(&mut a, Some(cat)); + let ctx = egui::Context::default(); + crate::theme::install_fonts(&ctx); + crate::theme::apply(&ctx, Default::default()); + let (mut texts, mut bars) = (vec![], vec![]); + for time in [0.0, 0.25, 0.5, 0.75] { + let screen_rect = Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1200.0, 900.0))); + let raw = egui::RawInput { screen_rect, time: Some(time), ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| show(&mut a, ui.ctx())); + out.textures_delta.clear(); + (texts, bars) = (vec![], vec![]); + out.shapes.iter().for_each(|c| walk(&c.shape, c.clip_rect, &mut texts, &mut bars)); + } + let window = ctx.memory(|m| m.area_rect(egui::Id::unique("dialog-preferences"))).expect("the dialog window is shown"); + // A label below its clip: the page runs past the bottom of the list. + let scrolls = texts.iter().any(|(_, r, clip)| window.contains_rect(*clip) && r.top() >= clip.bottom()); + if scrolls { + scrolled += 1; + assert!(bars.iter().any(|b| window.contains_rect(*b)), "{cat}: the page scrolls and its bar shows in {window:?}: {bars:?}"); + } + let cut: Vec<_> = texts + .iter() + .filter(|(_, r, clip)| clip.y_range().contains(r.center().y) && (r.left() < clip.left() - 1.0 || r.right() > clip.right() + 1.0)) + .filter(|(_, r, _)| window.intersects(*r)) + .map(|(s, r, clip)| format!("{s:?} {r:?} in {clip:?}")) + .collect(); + assert!(cut.is_empty(), "{cat}: labels cut off at the side: {cut:?}"); + } + assert!(scrolled > 0, "at least one page runs past the bottom of its list"); +} + #[test] fn dialog_ok_applies_prefs_and_ui_fields() { let mut a = app(); diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index eeb00ceb62..2314e00e78 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -645,6 +645,9 @@ pub fn apply_with(ctx: &egui::Context, b: Brightness, accent: Accent, scheme: Sc s.spacing.interact_size = egui::vec2(24.0, 24.0); s.spacing.menu_margin = egui::Margin::same(6); s.spacing.slider_width = 120.0; + // Every scroll area shows its bar while it has more to show, in a column of its own, + // not only while the pointer is over it (#5941). + s.spacing.scroll = crate::widgets::list_scroll_style(); s.text_styles = [ (TextStyle::Small, FontId::proportional(11.0)), (TextStyle::Body, FontId::proportional(13.0)), diff --git a/crates/ui-egui/src/widgets/dropdowns.rs b/crates/ui-egui/src/widgets/dropdowns.rs index 7f088f25ef..431bc4e8f9 100644 --- a/crates/ui-egui/src/widgets/dropdowns.rs +++ b/crates/ui-egui/src/widgets/dropdowns.rs @@ -369,6 +369,8 @@ pub(crate) fn menu_room_either(below: f32, above: f32, tallest: f32) -> f32 { /// The scroll bar of a menu's list: slim, in a column of its own beside the items, so it never /// covers what sits at their right end (shortcuts, submenu arrows, a font's favourite star). +/// `theme::apply` makes it the style of every scroll area that does not set its own, so the bar +/// shows whenever there is more to scroll to (#5941). pub fn list_scroll_style() -> egui::style::ScrollStyle { egui::style::ScrollStyle { bar_width: 6.0, bar_inner_margin: 2.0, ..egui::style::ScrollStyle::solid() } } @@ -561,18 +563,19 @@ fn show_shortened_rows(ui: &Ui, painted: usize) { } /// A column of tool or panel buttons (the toolbar, the dock's icon column) that scrolls when the -/// window is too short for it: with the mouse wheel, or the scroll bar shown while the pointer is -/// over it. +/// window is too short for it: with the mouse wheel, or with its scroll bar. /// Its scroll bar stays a slim line at the edge, even while scrolled or dragged, so it never covers -/// the buttons (#808). +/// the buttons (#808). The line shows while the pointer is elsewhere too (#5941). pub fn strip_scroll(ui: &mut Ui, id: &str, add_contents: impl FnOnce(&mut Ui) -> R) -> R { ui.scope(|ui| { + let floating = egui::style::ScrollStyle::floating(); ui.spacing_mut().scroll = egui::style::ScrollStyle { bar_width: 4.0, floating_width: 2.0, bar_inner_margin: 0.0, bar_outer_margin: 1.0, - ..egui::style::ScrollStyle::floating() + dormant_handle_opacity: floating.active_handle_opacity, + ..floating }; egui::ScrollArea::vertical().id_salt(id).auto_shrink([false, true]).show(ui, add_contents).inner }) diff --git a/crates/ui-egui/src/widgets/look_tests.rs b/crates/ui-egui/src/widgets/look_tests.rs index 274861386b..df12c8ed54 100644 --- a/crates/ui-egui/src/widgets/look_tests.rs +++ b/crates/ui-egui/src/widgets/look_tests.rs @@ -67,6 +67,87 @@ fn the_strip_scroll_bar_stays_a_slim_line_while_the_pointer_is_on_it() { assert!(widest > 0.0, "the scroll bar is drawn"); assert!(widest <= 4.5, "the scroll bar is {widest} points wide"); } + +/// The filled rectangles of a scroll bar drawn at the right of `content` in `shapes`: narrow +/// rectangles that start at or past the content's right edge. +fn bar_rects(shapes: &[egui::Shape], content: Rect, widest: f32) -> Vec<(Rect, Color32)> { + shapes + .iter() + .filter_map(|s| match s { + egui::Shape::Rect(r) + if r.fill != Color32::TRANSPARENT && r.rect.width() <= widest && r.rect.height() > 4.0 && r.rect.min.x >= content.right() - 0.5 => + { + Some((r.rect, r.fill)) + } + _ => None, + }) + .collect() +} + +// Regression test for #5941: a scrolling list in a panel or a dialog showed no scroll bar until the +// pointer came over it, so nothing told the user that the list had more rows. In every brightness +// the bar shows while the pointer is elsewhere, in a column of its own beside the rows, and its +// handle stands out from its track. +#[test] +fn a_scroll_bar_shows_while_the_pointer_is_elsewhere() { + for brightness in theme::Brightness::ALL { + let ctx = themed(brightness); + let rows = std::cell::Cell::new(Rect::NOTHING); + let mut add = |ui: &mut Ui| { + ui.allocate_ui(vec2(160.0, 200.0), |ui| { + egui::ScrollArea::vertical().id_salt("list").auto_shrink([false, false]).show(ui, |ui| { + for i in 0..40 { + ui.label(format!("Row {i}")); + } + rows.set(ui.max_rect()); + }); + }); + }; + frame(&ctx, 0.0, vec![egui::Event::PointerMoved(pos2(500.0, 350.0))], &mut add); + let shapes = frame(&ctx, 1.0, vec![], &mut add); + let rows = rows.get(); + assert!(rows.width() < 160.0, "{brightness:?}: the rows leave a column for the bar: {rows:?}"); + let bar = bar_rects(&shapes, rows, 10.0); + let (Some(track), Some(handle)) = + (bar.iter().max_by(|a, b| a.0.height().total_cmp(&b.0.height())), bar.iter().min_by(|a, b| a.0.height().total_cmp(&b.0.height()))) + else { + panic!("{brightness:?}: the scroll bar is drawn at rest: {bar:?}"); + }; + assert!(handle.0.height() < track.0.height(), "{brightness:?}: a handle on a track: {bar:?}"); + assert_eq!(handle.1.a(), 255, "{brightness:?}: the handle is opaque: {:?}", handle.1); + let gap = (i32::from(handle.1.r()) - i32::from(track.1.r())).abs(); + assert!(gap >= 24, "{brightness:?}: handle {:?} on track {:?}", handle.1, track.1); + } +} + +// Regression test for #5941: the toolbar's and the dock icon column's scroll bar showed nothing +// until the pointer came over the column. Its slim line shows at rest, still at most 4 points +// wide (#808). +#[test] +fn the_strip_scroll_bar_shows_while_the_pointer_is_elsewhere() { + let ctx = themed(Default::default()); + let column = std::cell::Cell::new(Rect::NOTHING); + let mut add = |ui: &mut Ui| { + column.set( + ui.allocate_ui(vec2(80.0, 200.0), |ui| { + strip_scroll(ui, "strip", |ui| { + for i in 0..40 { + ui.label(format!("Tool {i}")); + } + }); + }) + .response + .rect, + ); + }; + frame(&ctx, 0.0, vec![egui::Event::PointerMoved(pos2(500.0, 350.0))], &mut add); + let shapes = frame(&ctx, 1.0, vec![], &mut add); + let column = column.get(); + // The bar lies along the column's right edge, inside it. + let buttons = Rect::from_min_max(column.min, pos2(column.right() - 6.0, column.bottom())); + let bar = bar_rects(&shapes, buttons, 4.5); + assert!(!bar.is_empty(), "the slim scroll bar is drawn at rest"); +} // Regression test for #900: a number field with a spinner and a preset list drew as three // boxes side by side: its text box was a point taller than the 26-point row, and each joint had // two borders.