mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Toolbar: Alt/Option-click cycles the flyout tool slot (#10301)
* Toolbar: Alt/Option-click cycles the flyout tool slot. Matches Photoshop: plain click keeps the shown tool; Alt-click advances through the slot without opening the menu (including tools Shift+letter skips). * Toolbar: don't fit-on-screen on rapid Alt-clicks through Hand. Alt-click only cycles the flyout slot; exclude Alt from the Hand/Zoom double-click handlers and cover inactive-slot cycling. * Toolbar: ignore Hand/Zoom double-click after an Alt-click. egui pairs a plain click with a prior Alt-click as a double-click; remember the previous click's Alt so fit-on-screen and actual-pixels only run for two plain clicks. * Toolbar: read click modifiers from the slot's pointer release. Match DesignCraft/EffectCraft: bind release mods to the tool rect, treat any modifier as blocking Hand/Zoom double-click, and harden the Alt-cycle test by clearing i.modifiers before release. * Toolbar: cover 1/2/3-tool Alt-click cycles and hidden-tool slots. Addresses ReviewerA on #10301: next_slot_tool wrap cases for short flyouts, and visible_sections so Edit › Toolbar hidden tools shrink the Alt-click cycle. * style(ui): rustfmt after rebase onto main * Toolbar: next_slot_tool never panics on an empty slot Index with checked arithmetic and get() (house rule: no panics on input-derived indices); an empty slot returns the current tool. Test added. --------- Co-authored-by: FredApps <[email protected]>
This commit is contained in:
1 parent
37a01ca874
commit
4d0ecd746f
4 files changed
+230
-7
No files matched your search
@@ -110,7 +110,9 @@ pub use status_bar::{status_bar, status_bar_heights};
|
||||
use title_bar::title_x;
|
||||
pub use title_bar::{sync_window_title, system_title_bar, title_bar, window_title};
|
||||
#[cfg(test)]
|
||||
use toolbar::{STUDIO_CHIP, TOOL_SECTIONS, chips_height, color_chips, tool_flyout_drag_open, tool_flyout_release, toolbar_icon_size, visible_sections};
|
||||
use toolbar::{
|
||||
STUDIO_CHIP, TOOL_SECTIONS, chips_height, color_chips, next_slot_tool, tool_flyout_drag_open, tool_flyout_release, toolbar_icon_size, visible_sections,
|
||||
};
|
||||
pub use toolbar::{paint_toolbar_flyout_indicator, toolbar};
|
||||
|
||||
fn c32(c: [f32; 4]) -> Color32 {
|
||||
|
||||
@@ -44,6 +44,13 @@ fn slot_tool(ui: &egui::Ui, current: Tool, slot: &[Tool], key: egui::Id) -> Tool
|
||||
ui.data(|d| d.get_temp::<Tool>(key)).filter(|t| slot.contains(t)).unwrap_or(slot[0])
|
||||
}
|
||||
|
||||
/// Next tool in a toolbar flyout slot after `from`, wrapping (Photoshop Alt/Option-click). An
|
||||
/// empty slot returns `from` unchanged rather than dividing by zero.
|
||||
pub(super) fn next_slot_tool(slot: &[Tool], from: Tool) -> Tool {
|
||||
let i = slot.iter().position(|&t| t == from).unwrap_or(0);
|
||||
i.checked_add(1).and_then(|n| n.checked_rem(slot.len())).and_then(|n| slot.get(n)).copied().unwrap_or(from)
|
||||
}
|
||||
|
||||
/// [`TOOL_SECTIONS`] without the tools hidden in Edit › Toolbar (`hidden` holds tool names: the
|
||||
/// dialog writes `Tool` debug names, `edit.toolbar` takes any name `Tool::from_name` reads). A
|
||||
/// slot keeps only its visible tools, so its flyout lists just those; a slot with none left is
|
||||
@@ -167,19 +174,59 @@ pub fn toolbar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
if slot.len() > 1 {
|
||||
paint_toolbar_flyout_indicator(ui, &t, &resp);
|
||||
}
|
||||
if resp.clicked() && ui.data(|d| d.get_temp::<egui::Id>(held_id)) != Some(key) {
|
||||
crate::tool_memory::select(app, tool);
|
||||
// Plain click selects the shown tool; Alt/Option-click cycles the
|
||||
// flyout slot to the next tool, as Shift+letter does for the key group.
|
||||
// Prefer the primary-release event's modifiers (Alt may already be
|
||||
// up), and only a release on this slot — the frame's event list is
|
||||
// shared with every other widget.
|
||||
let click_mods =
|
||||
ui.input(|i| {
|
||||
i.events
|
||||
.iter()
|
||||
.rev()
|
||||
.find_map(|e| match e {
|
||||
egui::Event::PointerButton {
|
||||
pos, button: egui::PointerButton::Primary, pressed: false, modifiers, ..
|
||||
} if resp.rect.contains(*pos) => Some(*modifiers),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or(i.modifiers)
|
||||
});
|
||||
let alt = click_mods.alt;
|
||||
let modified = click_mods.alt || click_mods.shift || click_mods.ctrl || click_mods.command;
|
||||
// egui's double-click ignores modifiers: a plain click after a
|
||||
// modified click within max_double_click_delay still reports
|
||||
// double_clicked(). Remember the previous click so Hand fit /
|
||||
// Zoom 100% only run for two plain clicks.
|
||||
let prev_mod_id = key.with("click-mod");
|
||||
let prev_modified = ui.data(|d| d.get_temp::<bool>(prev_mod_id)).unwrap_or(false);
|
||||
let slot_click = resp.clicked() && ui.data(|d| d.get_temp::<egui::Id>(held_id)) != Some(key);
|
||||
if slot_click {
|
||||
let pick = if slot.len() > 1 && alt {
|
||||
let from = if slot.contains(&app.ui.tool) { app.ui.tool } else { tool };
|
||||
next_slot_tool(slot, from)
|
||||
} else {
|
||||
tool
|
||||
};
|
||||
crate::tool_memory::select(app, pick);
|
||||
}
|
||||
// Double-clicking the Hand tool fits the image on screen (Photoshop).
|
||||
if resp.double_clicked() && tool == Tool::Hand {
|
||||
// Modified clicks only cycle / select: a second rapid Alt-click, or a
|
||||
// plain click that egui pairs with a prior modified click, must not
|
||||
// count as Hand/Zoom double-click after cycling onto those tools.
|
||||
let plain_double = resp.double_clicked() && !modified && !prev_modified;
|
||||
if plain_double && tool == Tool::Hand {
|
||||
crate::tool_memory::select(app, tool);
|
||||
// With no document open there is nothing to fit.
|
||||
let _ = app.run("view.fitOnScreen", json!({}));
|
||||
}
|
||||
if resp.double_clicked() && tool == Tool::Zoom {
|
||||
if plain_double && tool == Tool::Zoom {
|
||||
crate::tool_memory::select(app, tool);
|
||||
let _ = app.run("view.actualPixels", json!({}));
|
||||
}
|
||||
if slot_click {
|
||||
ui.data_mut(|d| d.insert_temp(prev_mod_id, modified));
|
||||
}
|
||||
// Right-click, long-press, or press-drag away opens the flyout (Photoshop).
|
||||
let held_for =
|
||||
resp.is_pointer_button_down_on().then(|| ui.input(|i| i.pointer.press_start_time().map(|t0| i.time - t0))).flatten();
|
||||
|
||||
@@ -426,3 +426,176 @@ fn long_press_y_button_selects_art_history() {
|
||||
frame(&mut app, &ctx, 2.05, vec![pointer(row, false)]);
|
||||
assert_eq!(app.ui.tool, Tool::ArtHistoryBrush);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn next_slot_tool_wraps_around_the_flyout() {
|
||||
// One-tool slot: Alt-click is a no-op (still “cycles” to itself).
|
||||
assert_eq!(next_slot_tool(&[Tool::Frame], Tool::Frame), Tool::Frame);
|
||||
// Two-tool slot (Hand ↔ RotateView).
|
||||
let two = [Tool::Hand, Tool::RotateView];
|
||||
assert_eq!(next_slot_tool(&two, Tool::Hand), Tool::RotateView);
|
||||
assert_eq!(next_slot_tool(&two, Tool::RotateView), Tool::Hand, "wrap");
|
||||
// Three-tool slot.
|
||||
let three = [Tool::Lasso, Tool::PolygonLasso, Tool::MagneticLasso];
|
||||
assert_eq!(next_slot_tool(&three, Tool::Lasso), Tool::PolygonLasso);
|
||||
assert_eq!(next_slot_tool(&three, Tool::PolygonLasso), Tool::MagneticLasso);
|
||||
assert_eq!(next_slot_tool(&three, Tool::MagneticLasso), Tool::Lasso, "wrap");
|
||||
// Four-tool marquee flyout: starts from the current tool, not always the first.
|
||||
let slot = [Tool::RectMarquee, Tool::EllipseMarquee, Tool::SingleRowMarquee, Tool::SingleColumnMarquee];
|
||||
assert_eq!(next_slot_tool(&slot, Tool::RectMarquee), Tool::EllipseMarquee);
|
||||
assert_eq!(next_slot_tool(&slot, Tool::EllipseMarquee), Tool::SingleRowMarquee);
|
||||
assert_eq!(next_slot_tool(&slot, Tool::SingleColumnMarquee), Tool::RectMarquee);
|
||||
assert_eq!(next_slot_tool(&slot, Tool::Brush), Tool::EllipseMarquee, "unknown from → after the first");
|
||||
// Empty slot: no panic, the current tool stays.
|
||||
assert_eq!(next_slot_tool(&[], Tool::Brush), Tool::Brush);
|
||||
}
|
||||
|
||||
/// Edit › Toolbar hidden tools shrink the Alt-click cycle to the remaining flyout members.
|
||||
#[test]
|
||||
fn visible_sections_drop_hidden_tools_from_alt_click_slots() {
|
||||
let hidden = vec!["EllipseMarquee".into(), "SingleRowMarquee".into()];
|
||||
let sections = visible_sections(&hidden);
|
||||
let marquee = sections
|
||||
.iter()
|
||||
.flat_map(|s| s.iter())
|
||||
.find(|(_, tools)| tools.contains(&Tool::RectMarquee))
|
||||
.map(|(_, tools)| tools.as_slice())
|
||||
.expect("marquee slot remains");
|
||||
assert_eq!(marquee, &[Tool::RectMarquee, Tool::SingleColumnMarquee]);
|
||||
assert_eq!(next_slot_tool(marquee, Tool::RectMarquee), Tool::SingleColumnMarquee);
|
||||
assert_eq!(next_slot_tool(marquee, Tool::SingleColumnMarquee), Tool::RectMarquee, "wrap over the visible pair");
|
||||
}
|
||||
|
||||
/// Photoshop: Alt/Option-click a toolbar button cycles its flyout tools without opening the menu.
|
||||
#[test]
|
||||
fn alt_click_cycles_the_tool_flyout_slot() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
let ctx = egui::Context::default();
|
||||
PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
frame(&mut app, &ctx, 0.0, vec![]);
|
||||
frame(&mut app, &ctx, 0.1, vec![]);
|
||||
let index = TOOL_SECTIONS.iter().flat_map(|section| section.iter()).position(|slot| slot.contains(&Tool::RectMarquee)).unwrap();
|
||||
let slot = TOOL_SECTIONS.iter().flat_map(|section| section.iter()).nth(index).copied().unwrap();
|
||||
assert_eq!(slot, &[Tool::RectMarquee, Tool::EllipseMarquee, Tool::SingleRowMarquee, Tool::SingleColumnMarquee]);
|
||||
let bx = if Tokens::get(&ctx).pro { 30.0 } else { 36.0 };
|
||||
let mut buttons: Vec<Rect> = ctx.viewport(|v| {
|
||||
v.prev_pass
|
||||
.widgets
|
||||
.layers()
|
||||
.flat_map(|(_, w)| w.iter())
|
||||
.filter(|w| w.rect.size() == egui::Vec2::splat(bx) && w.sense.senses_click())
|
||||
.map(|w| w.rect)
|
||||
.collect()
|
||||
});
|
||||
buttons.sort_by(|a, b| a.top().total_cmp(&b.top()));
|
||||
let at = buttons[index].center();
|
||||
let alt = egui::Modifiers { alt: true, ..Default::default() };
|
||||
let pointer = |pos, pressed, modifiers| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers };
|
||||
let flyout = |ctx: &egui::Context| ctx.data(|d| d.get_temp::<(egui::Id, Rect)>(egui::Id::unique("tool-flyout"))).is_some();
|
||||
app.ui.tool = Tool::RectMarquee;
|
||||
// Plain click keeps the shown tool.
|
||||
frame(&mut app, &ctx, 1.0, vec![egui::Event::PointerMoved(at), pointer(at, true, Default::default())]);
|
||||
frame(&mut app, &ctx, 1.05, vec![pointer(at, false, Default::default())]);
|
||||
frame(&mut app, &ctx, 1.1, vec![]);
|
||||
assert_eq!(app.ui.tool, Tool::RectMarquee);
|
||||
assert!(!flyout(&ctx));
|
||||
// Alt-click advances through every flyout entry, including ones Shift+M skips.
|
||||
// Clear i.modifiers before the release while keeping Alt on the release event, so a
|
||||
// regression that only reads i.modifiers (and ignores the pointer-release modifiers) fails.
|
||||
for (t, want) in [(1.5, Tool::EllipseMarquee), (2.0, Tool::SingleRowMarquee), (2.5, Tool::SingleColumnMarquee), (3.0, Tool::RectMarquee)] {
|
||||
frame(&mut app, &ctx, t, vec![egui::Event::ModifiersChanged(alt), egui::Event::PointerMoved(at), pointer(at, true, alt)]);
|
||||
frame(&mut app, &ctx, t + 0.05, vec![egui::Event::ModifiersChanged(Default::default()), pointer(at, false, alt)]);
|
||||
frame(&mut app, &ctx, t + 0.1, vec![]);
|
||||
assert_eq!(app.ui.tool, want, "alt-click at {t}");
|
||||
assert!(!flyout(&ctx), "alt-click must not open the flyout");
|
||||
}
|
||||
// From another tool, Alt-click advances from the slot's remembered (shown) tool: Rect after the wrap.
|
||||
app.ui.tool = Tool::Brush;
|
||||
frame(&mut app, &ctx, 4.0, vec![egui::Event::ModifiersChanged(alt), egui::Event::PointerMoved(at), pointer(at, true, alt)]);
|
||||
frame(&mut app, &ctx, 4.05, vec![egui::Event::ModifiersChanged(Default::default()), pointer(at, false, alt)]);
|
||||
frame(&mut app, &ctx, 4.1, vec![]);
|
||||
assert_eq!(app.ui.tool, Tool::EllipseMarquee, "inactive slot cycles from its shown tool");
|
||||
}
|
||||
|
||||
/// #10301 CodeRabbit: two rapid Alt-clicks on Hand ↔ RotateView must not fit-on-screen.
|
||||
#[test]
|
||||
fn rapid_alt_clicks_on_hand_slot_do_not_fit_on_screen() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
frame(&mut app, &ctx, 0.0, vec![]);
|
||||
frame(&mut app, &ctx, 0.1, vec![]);
|
||||
let index = TOOL_SECTIONS.iter().flat_map(|section| section.iter()).position(|slot| slot.contains(&Tool::Hand)).unwrap();
|
||||
let bx = if Tokens::get(&ctx).pro { 30.0 } else { 36.0 };
|
||||
let mut buttons: Vec<Rect> = ctx.viewport(|v| {
|
||||
v.prev_pass
|
||||
.widgets
|
||||
.layers()
|
||||
.flat_map(|(_, w)| w.iter())
|
||||
.filter(|w| w.rect.size() == egui::Vec2::splat(bx) && w.sense.senses_click())
|
||||
.map(|w| w.rect)
|
||||
.collect()
|
||||
});
|
||||
buttons.sort_by(|a, b| a.top().total_cmp(&b.top()));
|
||||
let at = buttons[index].center();
|
||||
let alt = egui::Modifiers { alt: true, ..Default::default() };
|
||||
let pointer = |pos, pressed, modifiers| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers };
|
||||
app.ui.tool = Tool::RotateView;
|
||||
let zoom_before = app.ui.views.first().map(|v| v.zoom).unwrap_or(1.0);
|
||||
// Two Alt-clicks within egui's double-click window: cycle Hand → RotateView, never fit-on-screen.
|
||||
frame(&mut app, &ctx, 1.0, vec![egui::Event::ModifiersChanged(alt), egui::Event::PointerMoved(at), pointer(at, true, alt)]);
|
||||
frame(&mut app, &ctx, 1.02, vec![egui::Event::ModifiersChanged(alt), pointer(at, false, alt)]);
|
||||
frame(&mut app, &ctx, 1.04, vec![]);
|
||||
assert_eq!(app.ui.tool, Tool::Hand);
|
||||
frame(&mut app, &ctx, 1.08, vec![egui::Event::ModifiersChanged(alt), egui::Event::PointerMoved(at), pointer(at, true, alt)]);
|
||||
frame(&mut app, &ctx, 1.10, vec![egui::Event::ModifiersChanged(alt), pointer(at, false, alt)]);
|
||||
frame(&mut app, &ctx, 1.12, vec![]);
|
||||
assert_eq!(app.ui.tool, Tool::RotateView, "second Alt-click cycles back; must not stick on Hand from double-click");
|
||||
let zoom_after = app.ui.views.first().map(|v| v.zoom).unwrap_or(1.0);
|
||||
assert_eq!(zoom_after, zoom_before, "Alt double-click must not run view.fitOnScreen");
|
||||
}
|
||||
|
||||
/// #10301 CodeRabbit: plain click after Alt-click within the double-click window must not fit.
|
||||
#[test]
|
||||
fn plain_click_after_alt_on_hand_slot_does_not_fit_on_screen() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
// Far from fit-on-screen so a real Hand double-click is observable.
|
||||
if let Some(v) = app.ui.views.first_mut() {
|
||||
v.zoom = 4.0;
|
||||
}
|
||||
let ctx = egui::Context::default();
|
||||
PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
frame(&mut app, &ctx, 0.0, vec![]);
|
||||
frame(&mut app, &ctx, 0.1, vec![]);
|
||||
let index = TOOL_SECTIONS.iter().flat_map(|section| section.iter()).position(|slot| slot.contains(&Tool::Hand)).unwrap();
|
||||
let bx = if Tokens::get(&ctx).pro { 30.0 } else { 36.0 };
|
||||
let mut buttons: Vec<Rect> = ctx.viewport(|v| {
|
||||
v.prev_pass
|
||||
.widgets
|
||||
.layers()
|
||||
.flat_map(|(_, w)| w.iter())
|
||||
.filter(|w| w.rect.size() == egui::Vec2::splat(bx) && w.sense.senses_click())
|
||||
.map(|w| w.rect)
|
||||
.collect()
|
||||
});
|
||||
buttons.sort_by(|a, b| a.top().total_cmp(&b.top()));
|
||||
let at = buttons[index].center();
|
||||
let alt = egui::Modifiers { alt: true, ..Default::default() };
|
||||
let none = egui::Modifiers::default();
|
||||
let pointer = |pos, pressed, modifiers| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers };
|
||||
app.ui.tool = Tool::RotateView;
|
||||
let zoom_of = |app: &PhotocraftApp| app.ui.views.first().map(|v| v.zoom).unwrap_or(1.0);
|
||||
assert_eq!(zoom_of(&app), 4.0);
|
||||
// Alt-click cycles to Hand; a plain click soon after is egui's "second" click of a double.
|
||||
frame(&mut app, &ctx, 1.0, vec![egui::Event::ModifiersChanged(alt), egui::Event::PointerMoved(at), pointer(at, true, alt)]);
|
||||
frame(&mut app, &ctx, 1.02, vec![egui::Event::ModifiersChanged(alt), pointer(at, false, alt)]);
|
||||
frame(&mut app, &ctx, 1.04, vec![]);
|
||||
assert_eq!(app.ui.tool, Tool::Hand);
|
||||
frame(&mut app, &ctx, 1.08, vec![egui::Event::ModifiersChanged(none), egui::Event::PointerMoved(at), pointer(at, true, none)]);
|
||||
frame(&mut app, &ctx, 1.10, vec![egui::Event::ModifiersChanged(none), pointer(at, false, none)]);
|
||||
frame(&mut app, &ctx, 1.12, vec![]);
|
||||
assert_eq!(app.ui.tool, Tool::Hand, "plain click keeps Hand selected");
|
||||
assert_eq!(zoom_of(&app), 4.0, "plain click paired with prior Alt-click must not fitOnScreen");
|
||||
}
|
||||
+3
-2
@@ -49,9 +49,10 @@ Triangle and Curvature Pen (minus legacy Rounded Rectangle and Targeted Adjustme
|
||||
|
||||
| Toolbar behaviour | Status | Evidence |
|
||||
|---|---|---|
|
||||
| Letter shortcuts, ⇧+letter cycles the group, "Use Shift Key for Tool Switch" (on by default, as in Photoshop) | done | `shortcuts.rs:413` |
|
||||
| Letter shortcuts, ⇧+letter cycles the group, "Use Shift Key for Tool Switch" (on by default, as in Photoshop) | done | `shortcuts.rs` `select_tool_by_key` |
|
||||
| Alt/Option-click a toolbar button cycles its flyout slot (including tools with no letter key) | done | `toolbar.rs` `next_slot_tool`; `type_flyout_tests.rs` `alt_click_cycles_the_tool_flyout_slot` |
|
||||
| Group remembers the last tool used | partial | L always resets to Lasso (#2608) |
|
||||
| Press-and-hold or click-drag opens the flyout | missing | right-click only (#2301) |
|
||||
| Press-and-hold or click-drag opens the flyout | done | `toolbar.rs`; right-click, long-press, press-drag |
|
||||
| Edit Toolbar customization | done | `edit.toolbar` |
|
||||
| Last selected tool restored after quitting and relaunching; temporary tool switches excluded | done | `tool_memory.rs`; `prefs_ui::tests::selected_tool_survives_a_restart_without_option_changes`, `selected_tool_excludes_temporary_switches` |
|
||||
|
||||
|
||||
Reference in new issue
Block a user