Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Keep the blend-mode preview between list rows (#2553) (#2612)

Fabio Lussicf54d5e8a329
diff --git a/crates/ui-egui/src/blend_preview/tests.rs b/crates/ui-egui/src/blend_preview/tests.rs
index 0ea840d72..4eb407aad 100644
--- a/crates/ui-egui/src/blend_preview/tests.rs
+++ b/crates/ui-egui/src/blend_preview/tests.rs
@@ -104,3 +104,23 @@ fn hovering_the_layers_panel_blend_list_previews_and_a_click_commits() {
assert_eq!(steps(h.state()), undo + 1, "choosing records one step");
assert_eq!(previewed(&mut h), None);
}
+
+#[test]
+fn moving_between_modes_keeps_a_preview_across_the_gaps_between_them() {
+ // #2553: the pointer between two rows used to hover neither, flashing the document back.
+ let (s, _) = session();
+ let mut h = harness(s);
+ let dropdown = h.query_all_by_role(Role::ComboBox).map(|n| n.rect()).max_by(|a, b| a.top().total_cmp(&b.top())).unwrap();
+ click(&mut h, dropdown.center());
+ let (dissolve, darken) = (h.get_by_label("Dissolve").rect(), h.get_by_label("Darken").rect());
+ assert!(darken.top() > dissolve.bottom(), "the rows have a gap between them");
+ let multiply = h.get_by_label("Multiply").rect().center();
+ let mut y = dissolve.center().y;
+ while y <= multiply.y {
+ h.hover_at(Pos2::new(multiply.x, y));
+ h.run_steps(1);
+ assert!(previewed(&mut h).is_some(), "a mode previews at y = {y}");
+ y += 0.5;
+ }
+ assert_eq!(previewed(&mut h), Some(BlendMode::Multiply));
+}
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index 177f7f2c3..65dbbfa28 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -939,6 +939,9 @@ fn dropdown_with<T: PartialEq + Clone>(ui: &mut Ui, id: &str, current: &mut T, o
reveal |= wheeled;
chosen.extend(stepped);
}
+ // The row nearest the pointer, by its distance to each row.
+ let pointer = ui.ctx().pointer_hover_pos().filter(|_| ui.rect_contains_pointer(ui.clip_rect()));
+ let mut nearest: Option<(f32, &T)> = None;
for (v, l) in options {
let item = ui.selectable_label(v == current, tl!(l));
if reveal && v == current {
@@ -947,11 +950,22 @@ fn dropdown_with<T: PartialEq + Clone>(ui: &mut Ui, id: &str, current: &mut T, o
if item.hovered() {
hovered = Some(v.clone());
}
+ if let Some(p) = pointer {
+ let d = (item.rect.top() - p.y).max(p.y - item.rect.bottom()).max(0.0);
+ if nearest.is_none_or(|(n, _)| d < n) {
+ nearest = Some((d, v));
+ }
+ }
if item.clicked() {
*current = v.clone();
chosen.push(v.clone());
}
}
+ // Between two rows (item spacing) the pointer hovers neither; it is still over the list,
+ // so keep previewing the nearest row instead of flashing the current value back (#2553).
+ if hovered.is_none() {
+ hovered = nearest.map(|(_, v)| v.clone());
+ }
});
if combo_box_arrow_keys(ui, &response.response, current, options) {
chosen.push(current.clone());