mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #11850 from DuBearte/fix/11816-brush-picker-escape
fix(ui): Escape in the Brush Preset picker reverts what was typed (#11816)
This commit is contained in:
4 files changed
+190
-3
No files matched your search
@@ -0,0 +1,126 @@
|
||||
//! #11816: Escape in the Brush Preset picker closes it and reverts a size or hardness typed in
|
||||
//! it; Enter keeps them, a preset picked before Escape stays picked, a change made outside the
|
||||
//! picker stays, and each opening reverts to its own start.
|
||||
|
||||
use egui::vec2;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::Tool;
|
||||
|
||||
fn harness() -> Harness<'static, PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
app.run("tools.setBrush", json!({"brush": {"size": 20, "hardness": 0.0}})).unwrap();
|
||||
app.ui.tool = Tool::Brush;
|
||||
app.sync_views();
|
||||
let mut h = Harness::builder().with_size(vec2(1200.0, 800.0)).with_step_dt(1.0 / 60.0).build_ui_state(
|
||||
|ui, app: &mut PhotocraftApp| {
|
||||
let ctx = ui.ctx().clone();
|
||||
if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
|
||||
return;
|
||||
}
|
||||
egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));
|
||||
},
|
||||
app,
|
||||
);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
fn open(h: &mut Harness<'static, PhotocraftApp>) {
|
||||
let c = h.state().last_canvas_rect.center();
|
||||
h.state_mut().ui.brush_picker = Some([c.x, c.y]);
|
||||
h.run_steps(2);
|
||||
}
|
||||
|
||||
/// Type `text` into the picker's `n`th spin field (0 Size, 1 Hardness), then Tab.
|
||||
fn type_into(h: &mut Harness<'static, PhotocraftApp>, n: usize, text: &str) {
|
||||
h.get_all_by_role(egui::accesskit::Role::SpinButton).nth(n).expect("the field").click();
|
||||
h.run_steps(1);
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
|
||||
h.event(egui::Event::Text(text.into()));
|
||||
h.run_steps(1);
|
||||
h.key_press(egui::Key::Tab);
|
||||
h.run_steps(2);
|
||||
}
|
||||
|
||||
fn brush(h: &Harness<'static, PhotocraftApp>) -> (f32, f32) {
|
||||
let b = &h.state().session.tools.brush;
|
||||
(b.size, b.hardness)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_reverts_typed_size_and_hardness() {
|
||||
let mut h = harness();
|
||||
let steps = h.state().session.active().unwrap().history.past_len();
|
||||
open(&mut h);
|
||||
type_into(&mut h, 0, "60");
|
||||
type_into(&mut h, 1, "100");
|
||||
assert!(h.state().ui.brush_picker.is_some(), "Tab keeps the picker open");
|
||||
h.key_press(egui::Key::Escape);
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().ui.brush_picker, None);
|
||||
assert_eq!(brush(&h), (20.0, 0.0), "back to the brush the picker opened with");
|
||||
assert_eq!(h.state().session.active().unwrap().history.past_len(), steps, "no history step");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enter_keeps_the_typed_values() {
|
||||
let mut h = harness();
|
||||
open(&mut h);
|
||||
type_into(&mut h, 0, "60");
|
||||
h.key_press(egui::Key::Enter);
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().ui.brush_picker, None);
|
||||
assert_eq!(brush(&h).0, 60.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_preset_picked_before_escape_stays_picked() {
|
||||
let mut h = harness();
|
||||
open(&mut h);
|
||||
let name = h.state().session.tools.presets.get(1).map(|p| p.name.clone()).expect("a preset");
|
||||
h.get_by_label(&name).click();
|
||||
h.run_steps(3);
|
||||
let picked = brush(&h);
|
||||
assert_ne!(picked, (20.0, 0.0), "the preset changed the brush");
|
||||
h.key_press(egui::Key::Escape);
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().ui.brush_picker, None);
|
||||
assert_eq!(brush(&h), picked);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_keeps_a_size_changed_outside_the_picker() {
|
||||
let mut h = harness();
|
||||
open(&mut h);
|
||||
type_into(&mut h, 1, "100");
|
||||
// As `]` or the options bar would, while the picker stays open.
|
||||
h.state_mut().run("tools.setBrush", json!({"brush": {"size": 45}})).unwrap();
|
||||
h.run_steps(2);
|
||||
h.key_press(egui::Key::Escape);
|
||||
h.run_steps(2);
|
||||
assert_eq!(brush(&h), (45.0, 0.0), "the typed hardness reverts, the size from elsewhere stays");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_reopened_picker_reverts_to_its_own_opening() {
|
||||
let mut h = harness();
|
||||
open(&mut h);
|
||||
type_into(&mut h, 0, "60");
|
||||
// Closed, changed and opened again (a control request) before the next frame.
|
||||
let ctx = h.ctx.clone();
|
||||
let app = h.state_mut();
|
||||
crate::brush_picker::close(&mut app.ui);
|
||||
app.run("tools.setBrush", json!({"brush": {"size": 33}})).unwrap();
|
||||
let at = app.last_canvas_rect.center();
|
||||
crate::brush_picker::open(&mut app.ui, &ctx, [at.x, at.y]);
|
||||
h.run_steps(2);
|
||||
h.key_press(egui::Key::Escape);
|
||||
h.run_steps(2);
|
||||
assert_eq!(brush(&h).0, 33.0, "the size this opening started with");
|
||||
}
|
||||
@@ -437,14 +437,22 @@ pub fn pointer_secondary(app: &mut PhotocraftApp, ctx: &egui::Context, down: boo
|
||||
/// edits the session brush like the Brushes panel; a press outside, Escape, Enter or a
|
||||
/// double-click on a preset closes it.
|
||||
pub fn show_picker(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
let Some([x, y]) = app.ui.brush_picker else { return };
|
||||
let opened_id = crate::brush_picker::opened_id();
|
||||
let Some([x, y]) = app.ui.brush_picker else {
|
||||
ctx.data_mut(|d| d.remove_temp::<crate::brush_picker::Opened>(opened_id));
|
||||
return;
|
||||
};
|
||||
if !has_brush_picker(app.ui.tool) {
|
||||
app.ui.brush_picker = None;
|
||||
ctx.data_mut(|d| d.remove_temp::<crate::brush_picker::Opened>(opened_id));
|
||||
return;
|
||||
}
|
||||
let mut opened = ctx.data_mut(|d| d.get_temp_mut_or_insert_with(opened_id, || crate::brush_picker::Opened::new(&app.session.tools.brush)).clone());
|
||||
// Escape and Enter close it once this frame's edits are in (a typed size applies), unless they
|
||||
// end a rename in its rename bar.
|
||||
let key_close = app.ui.brush_picker_list.renaming.is_none() && ctx.input(|i| i.key_pressed(egui::Key::Escape) || i.key_pressed(egui::Key::Enter));
|
||||
// end a rename in its rename bar. Escape then reverts what was typed (#11816).
|
||||
let typing_done = app.ui.brush_picker_list.renaming.is_none();
|
||||
let escape = typing_done && ctx.input(|i| i.key_pressed(egui::Key::Escape));
|
||||
let key_close = escape || (typing_done && ctx.input(|i| i.key_pressed(egui::Key::Enter)));
|
||||
let screen = ctx.content_rect();
|
||||
// The picker's content size: the one its grip was dragged to, or the one it last closed with
|
||||
// (kept in egui's own memory, which outlives the app's), or the default. The frame's chrome
|
||||
@@ -472,10 +480,25 @@ pub fn show_picker(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
app.ui.brush_picker_size = Some([new.x, new.y]);
|
||||
}
|
||||
crate::brush_panel::commit_gesture(app, ui.ctx(), &before, &b);
|
||||
if b != before {
|
||||
opened.typed(&b);
|
||||
}
|
||||
let closes = picks.iter().any(crate::brush_picker::Pick::closes);
|
||||
let picked =
|
||||
picks.iter().any(|p| matches!(p, crate::brush_picker::Pick::List(crate::brushes_tab::Action::Select(_) | crate::brushes_tab::Action::Choose(_))));
|
||||
crate::brush_picker::apply(app, ui.ctx(), picks);
|
||||
// A preset picked stays picked, Escape or not.
|
||||
if picked {
|
||||
opened = crate::brush_picker::Opened::new(&app.session.tools.brush);
|
||||
}
|
||||
closes
|
||||
});
|
||||
ctx.data_mut(|d| d.insert_temp(opened_id, opened.clone()));
|
||||
if escape {
|
||||
let now = app.session.tools.brush.clone();
|
||||
let back = opened.revert(&now);
|
||||
crate::brush_panel::commit_gesture(app, ctx, &now, &back);
|
||||
}
|
||||
// Not a press on a menu the picker opened (the gear, a preset's context menu), nor on the
|
||||
// options-bar chip, whose click toggles the picker. A menu floats over the picker in the
|
||||
// foreground order, so the press is told apart by the layer it lands on, not by any popup
|
||||
|
||||
@@ -770,6 +770,8 @@ impl SyntheticTarget {
|
||||
#[cfg(test)]
|
||||
mod artboard_add_mode_tests;
|
||||
#[cfg(test)]
|
||||
mod brush_picker_escape_tests;
|
||||
#[cfg(test)]
|
||||
mod shape_bounds_box_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -82,9 +82,45 @@ pub(crate) fn picker_id() -> egui::Id {
|
||||
/// All opening paths protect the same frame, including the control channel and options bar.
|
||||
pub(crate) fn open(ui: &mut crate::state::UiState, ctx: &egui::Context, at: [f32; 2]) {
|
||||
ui.brush_picker = Some(at);
|
||||
// A new opening takes the brush as it is then, not an earlier opening's.
|
||||
ctx.data_mut(|d| d.remove_temp::<Opened>(opened_id()));
|
||||
crate::canvas_popup::mark_open(ctx, picker_id());
|
||||
}
|
||||
|
||||
pub(crate) fn opened_id() -> egui::Id {
|
||||
picker_id().with("opened-with")
|
||||
}
|
||||
|
||||
/// What Escape in the picker goes back to (#11816): the Size and Hardness it opened with (or a
|
||||
/// picked preset brought), and the values its own fields last typed. Escape reverts a value only
|
||||
/// while it is still the typed one, so a change from elsewhere (`]`, the options bar) stays.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub(crate) struct Opened {
|
||||
opened: [f32; 2],
|
||||
typed: [f32; 2],
|
||||
}
|
||||
|
||||
impl Opened {
|
||||
pub(crate) fn new(b: &photocraft_engine::BrushSettings) -> Self {
|
||||
Self { opened: [b.size, b.hardness], typed: [b.size, b.hardness] }
|
||||
}
|
||||
|
||||
pub(crate) fn typed(&mut self, b: &photocraft_engine::BrushSettings) {
|
||||
self.typed = [b.size, b.hardness];
|
||||
}
|
||||
|
||||
pub(crate) fn revert(&self, now: &photocraft_engine::BrushSettings) -> photocraft_engine::BrushSettings {
|
||||
let mut b = now.clone();
|
||||
if b.size == self.typed[0] {
|
||||
b.size = self.opened[0];
|
||||
}
|
||||
if b.hardness == self.typed[1] {
|
||||
b.hardness = self.opened[1];
|
||||
}
|
||||
b
|
||||
}
|
||||
}
|
||||
|
||||
/// The options-bar brush chip that shows and hides the picker. A press on it isn't an outside
|
||||
/// press that closes the picker: the chip's click toggles it.
|
||||
pub fn chip_id() -> egui::Id {
|
||||
|
||||
Reference in new issue
Block a user