Menu scroll arrows: import what dropdown.rs uses; arrows as wide as the rows (#2091)

The upstream split of widgets.rs moved menu_scroll into widgets/dropdown.rs,
whose imports lacked Sense, Stroke, Tokens, pos2 and vec2 that the arrows use,
so photocraft-ui-egui no longer compiled. The arrows were also allocated at the
popup's whole available width, which widened an overflowing menu on later
opens; they now take the rows' width from last frame (the scroll area's
content width; its inner_rect is the width asked for, before auto-shrink).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Higgs32584andClaude Opus 5.5 committed 2026-11-05 15:29:13 -05:00
1 parent a8f7c1fe2d
commit caf7b95dc2
2 files changed
+53 -16

No files matched your search

+13 -6
View File
@@ -1,7 +1,7 @@
use super::panel::chevron_icon;
use super::scrub::take_label_scrub_name;
use super::slider::wheel_notches_of;
use super::{Pos2, Rect, Response, Ui, Vec2, type_ahead};
use super::{Pos2, Rect, Response, Sense, Stroke, Tokens, Ui, Vec2, pos2, type_ahead, vec2};
/// A compact labelled dropdown in the studio style.
pub fn dropdown<T: PartialEq + Clone>(ui: &mut Ui, id: &str, name: &str, current: &mut T, options: &[(T, &str)], width: f32) -> bool {
@@ -228,6 +228,10 @@ pub fn menu_scroll<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> R
let id = ui.scope_id().with("menu_scroll");
let overflow = ui.data(|d| d.get_temp::<bool>(id)).unwrap_or(false);
let offset = ui.data(|d| d.get_temp::<f32>(id.with("offset"))).unwrap_or(0.0);
// The arrows are as wide as the rows were last frame, not the popup's whole available width:
// that widened an overflowing menu on every later open (a layer menu went from ~230 to ~600
// pt). The output's `inner_rect` is the width asked for, before auto-shrink fits the rows.
let width = ui.data(|d| d.get_temp::<f32>(id.with("width"))).unwrap_or(0.0);
let arrows = if overflow { 2.0 * MENU_ARROW_HEIGHT } else { 0.0 };
let room = (room - arrows).max(MENU_MIN_HEIGHT);
// The arrows sit flush against the rows, so no item spacing is added around them that `room`
@@ -236,7 +240,7 @@ pub fn menu_scroll<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> R
ui.spacing_mut().item_spacing.y = 0.0;
let mut step = 0.0;
if overflow {
step += menu_scroll_arrow(ui, true, offset > 0.5);
step += menu_scroll_arrow(ui, true, offset > 0.5, width);
}
// A popup's Ui is only as tall as the popup was last frame (400 pt on the first), so ask for
// the whole room: the area still shrinks to its rows when they need less.
@@ -255,12 +259,13 @@ pub fn menu_scroll<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> R
let max = (out.content_size.y - out.inner_rect.height()).max(0.0);
let offset = out.state.clamped_offset().y.clamp(0.0, max);
if overflow {
step += menu_scroll_arrow(ui, false, offset < max - 0.5);
step += menu_scroll_arrow(ui, false, offset < max - 0.5, width);
}
ui.spacing_mut().item_spacing.y = spacing;
ui.data_mut(|d| {
d.insert_temp(id, max > 0.5);
d.insert_temp(id.with("offset"), offset);
d.insert_temp(id.with("width"), out.content_size.x);
d.insert_temp(id.with("step"), (step, now));
});
out.inner
@@ -268,9 +273,11 @@ pub fn menu_scroll<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> R
/// One scroll arrow of an overflowing menu: hovering scrolls that way, as in Photoshop's and the
/// system menus. It doesn't page on a click: egui closes a context menu on any click inside it,
/// so the page would never be seen. Returns the scroll step for the next frame.
fn menu_scroll_arrow(ui: &mut Ui, up: bool, enabled: bool) -> f32 {
let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width().max(MENU_ARROW_HEIGHT), MENU_ARROW_HEIGHT), Sense::hover());
/// so the page would never be seen. `width` is the rows' width. Returns the scroll step for the
/// next frame.
fn menu_scroll_arrow(ui: &mut Ui, up: bool, enabled: bool, width: f32) -> f32 {
let width = if width.is_finite() { width.max(MENU_ARROW_HEIGHT) } else { MENU_ARROW_HEIGHT };
let (rect, resp) = ui.allocate_exact_size(vec2(width, MENU_ARROW_HEIGHT), Sense::hover());
let t = Tokens::get(ui.ctx());
let color = if enabled { t.text } else { t.text_dim.gamma_multiply(0.5) };
let (c, h) = (rect.center(), 3.5);
+40 -10
View File
@@ -1443,12 +1443,18 @@ fn the_wheel_leaves_a_plain_dropdown_alone() {
assert_eq!(*h.state(), 0);
}
/// The middle of an overflowing menu's lower scroll arrow, from the menu's laid-out rect.
fn lower_menu_arrow(menu: egui::Rect) -> egui::Pos2 {
let half = super::dropdown::MENU_ARROW_HEIGHT / 2.0;
egui::pos2(menu.left() + half, menu.bottom() - half)
}
/// #2091: a menu taller than the window shows scroll arrows; hovering the lower one scrolls
/// it, and a short menu gets none.
#[test]
fn overflowing_menus_get_scroll_arrows_that_scroll_on_hover() {
// (rows, overflowing, scroll offset, bottom of the menu)
type S = (usize, bool, f32, f32);
// (rows, overflowing, scroll offset, the menu's rect)
type S = (usize, bool, f32, egui::Rect);
let run = |rows: usize| {
let mut h = Harness::builder().with_size(egui::vec2(220.0, 300.0)).build_ui_state(
|ui, s: &mut S| {
@@ -1460,9 +1466,9 @@ fn overflowing_menus_get_scroll_arrows_that_scroll_on_hover() {
});
s.1 = ui.data(|d| d.get_temp::<bool>(id)).unwrap_or(false);
s.2 = ui.data(|d| d.get_temp::<f32>(id.with("offset"))).unwrap_or(0.0);
s.3 = ui.min_rect().bottom();
s.3 = ui.min_rect();
},
(rows, false, 0.0, 0.0),
(rows, false, 0.0, egui::Rect::NOTHING),
);
h.run_steps(3);
h
@@ -1472,7 +1478,7 @@ fn overflowing_menus_get_scroll_arrows_that_scroll_on_hover() {
let mut long = run(80);
assert!(long.state().1, "a long menu overflows");
assert_eq!(long.state().2, 0.0);
let below = egui::pos2(100.0, long.state().3 - super::dropdown::MENU_ARROW_HEIGHT / 2.0);
let below = lower_menu_arrow(long.state().3);
long.event(egui::Event::PointerMoved(below));
long.run_steps(10);
assert!(long.state().2 > 0.0, "hovering the lower arrow scrolls down");
@@ -1482,8 +1488,8 @@ fn overflowing_menus_get_scroll_arrows_that_scroll_on_hover() {
/// while hovering it opens again.
#[test]
fn a_reopened_menu_takes_no_stale_scroll_step() {
// (shown, scroll offset, bottom of the menu)
type S = (bool, f32, f32);
// (shown, scroll offset, the menu's rect)
type S = (bool, f32, egui::Rect);
let mut h = Harness::builder().with_size(egui::vec2(220.0, 300.0)).build_ui_state(
|ui, s: &mut S| {
let id = ui.scope_id().with("menu_scroll");
@@ -1493,14 +1499,14 @@ fn a_reopened_menu_takes_no_stale_scroll_step() {
ui.label(format!("Item {i}"));
}
});
s.2 = ui.min_rect().bottom();
s.2 = ui.min_rect();
}
s.1 = ui.data(|d| d.get_temp::<f32>(id.with("offset"))).unwrap_or(0.0);
},
(true, 0.0, 0.0),
(true, 0.0, egui::Rect::NOTHING),
);
h.run_steps(3);
h.event(egui::Event::PointerMoved(egui::pos2(100.0, h.state().2 - super::dropdown::MENU_ARROW_HEIGHT / 2.0)));
h.event(egui::Event::PointerMoved(lower_menu_arrow(h.state().2)));
h.run_steps(3);
let offset = h.state().1;
assert!(offset > 0.0, "hovering the lower arrow scrolls down");
@@ -1511,3 +1517,27 @@ fn a_reopened_menu_takes_no_stale_scroll_step() {
h.run();
assert_eq!(h.state().1, offset, "the reopened menu stays where it was left");
}
/// #2091: an overflowing menu's scroll arrows are as wide as its rows, so they don't widen the
/// menu to the whole width it is offered.
#[test]
fn menu_scroll_arrows_do_not_widen_the_menu() {
// (overflowing, width of the menu)
type S = (bool, f32);
let mut h = Harness::builder().with_size(egui::vec2(600.0, 300.0)).build_ui_state(
|ui, s: &mut S| {
let id = ui.scope_id().with("menu_scroll");
super::menu_scroll(ui, |ui| {
for i in 0..80 {
ui.label(format!("Item {i}"));
}
});
s.0 = ui.data(|d| d.get_temp::<bool>(id)).unwrap_or(false);
s.1 = ui.min_rect().width();
},
(false, 0.0),
);
h.run_steps(4);
assert!(h.state().0, "the menu overflows");
assert!(h.state().1 < 200.0, "the arrows follow the rows' width, not the 600 pt window: {}", h.state().1);
}