Skip to content

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

Type options: the wheel over the font menu steps through the families (#3350) (#3524)

Valentin Kleincca24dd0d8dd
diff --git a/crates/ui-egui/src/type_font_picker_tests.rs b/crates/ui-egui/src/type_font_picker_tests.rs
index 2fc600511..f17032523 100644
--- a/crates/ui-egui/src/type_font_picker_tests.rs
+++ b/crates/ui-egui/src/type_font_picker_tests.rs
@@ -463,3 +463,30 @@ fn font_menu_rows_search_both_names_and_sort_by_label() {
assert_eq!(rows(Some(&labels), "雅黑"), [("Microsoft YaHei".to_string(), "微软雅黑".to_string())]);
assert!(rows(Some(&labels), "nothing").is_empty());
}
+
+#[test]
+fn wheel_over_the_closed_font_button_steps_through_the_families() {
+ let mut h = harness();
+ let button = h.query_all_by_role(Role::ComboBox).next().unwrap().rect().center();
+ h.hover_at(button);
+ h.run();
+ let wheel = |h: &mut Harness<'static, State>, y: f32| {
+ h.event(egui::Event::MouseWheel { unit: egui::MouseWheelUnit::Line, delta: vec2(0.0, y), phase: egui::TouchPhase::Move, modifiers: Modifiers::NONE });
+ h.run();
+ };
+ wheel(&mut h, -2.0);
+ assert_eq!(h.state().current, "Family 02", "two notches down pick the second next family");
+ assert!(popup_rect(&h).is_none(), "the list stays closed");
+ wheel(&mut h, 1.0);
+ assert_eq!(h.state().current, "Family 01");
+ wheel(&mut h, 5.0);
+ assert_eq!(h.state().current, "Family 00", "stops at the first family");
+}
+
+#[test]
+fn wheel_family_index_clamps_to_the_ends() {
+ assert_eq!(super::wheel_family_index(0, 3, -5.0), Some(2));
+ assert_eq!(super::wheel_family_index(1, 3, 1.0), Some(0));
+ assert_eq!(super::wheel_family_index(1, 3, 0.0), None);
+ assert_eq!(super::wheel_family_index(0, 0, -1.0), None);
+}
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs
index 53a1e6c02..f00fb5715 100644
--- a/crates/ui-egui/src/type_tool.rs
+++ b/crates/ui-egui/src/type_tool.rs
@@ -1529,6 +1529,26 @@ pub(crate) fn family_picker_in(
press.after(&response.response);
let hovered_font = response.inner.unwrap_or_default();
+ // The wheel over the closed button steps through the families, a notch each, like Photoshop's
+ // options-bar font menu: no need to open the list (#3350).
+ if !egui::ComboBox::is_open(ui.ctx(), response.response.id) && ui.is_enabled() {
+ let hovered = response.response.hovered();
+ if hovered {
+ ui.input_mut(|i| i.smooth_scroll_delta = egui::Vec2::ZERO);
+ }
+ let notches = crate::widgets::wheel_notches_of(ui, response.response.id.with("family-wheel"), hovered, 1.0);
+ let rows = font_menu_rows(families, labels, "");
+ if let Some(i) = rows.iter().position(|(f, _)| *f == current)
+ && let Some(n) = wheel_family_index(i, rows.len(), notches)
+ && let Some((f, _)) = rows.get(n)
+ && **f != *current
+ {
+ current.clone_from(f);
+ photocraft_text::served::request(f);
+ changed = true;
+ }
+ }
+
if salt == "type-font" {
let hover_id = egui::Id::new("type-font-hover");
if let Some(f) = hovered_font {
@@ -1541,6 +1561,17 @@ pub(crate) fn family_picker_in(
changed
}
+/// The row `notches` wheel notches from row `index` of `len` (down, negative, is the next one),
+/// stopping at the ends; none when the wheel did not move.
+fn wheel_family_index(index: usize, len: usize, notches: f32) -> Option<usize> {
+ let steps = notches.abs().min(len as f32) as usize;
+ if steps == 0 || len == 0 {
+ return None;
+ }
+ let last = len - 1;
+ Some(if notches < 0.0 { index.saturating_add(steps).min(last) } else { index.saturating_sub(steps) })
+}
+
/// Representative for the displayed properties: the edit layer, else a selected type layer.
pub(crate) fn target(app: &PhotocraftApp) -> Option<(u64, Option<[usize; 2]>)> {
if let Some(ed) = &app.ui.text_edit {
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index 474bef66e..9e56022ce 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -856,7 +856,7 @@ fn wheel_notches(ui: &Ui, resp: &Response) -> f32 {
}
/// [`wheel_notches`] of widget `id` while `hovered`, a notch with Shift counting `shift_notches`.
-fn wheel_notches_of(ui: &Ui, id: egui::Id, hovered: bool, shift_notches: f32) -> f32 {
+pub(crate) fn wheel_notches_of(ui: &Ui, id: egui::Id, hovered: bool, shift_notches: f32) -> f32 {
if !hovered {
return 0.0;
}