diff --git a/src/app.rs b/src/app.rs index 7a1d5212..cfd8016e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -454,6 +454,9 @@ pub struct App { /// The Properties panel's Scale lock (#304; view state, not undoable): while on, editing /// one Scale axis adjusts the other so the selection keeps its width:height proportion. pub scale_lock: bool, + /// The zoom edit-bar field's draft while it has the keyboard (view state, not undoable): + /// `None` shows the current zoom, `Some` is what is being typed. + pub zoom_text: Option, /// The bone picked on the stage (ADR 0043; view state). pub bone_sel: Option, /// The Bind tool's options: Paint Weights, the brush, the focused point (ADR 0090). @@ -737,6 +740,7 @@ impl App { width_sel: None, open_fill_swatch: false, scale_lock: false, + zoom_text: None, bone_sel: None, bind: crate::bind_tool::BindOptions::default(), warp_pin: None, diff --git a/src/main.rs b/src/main.rs index be870dd8..348a03ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -433,3 +433,5 @@ mod rulers_tests; mod retime_ui_tests; #[cfg(test)] mod tilt_ui_tests; +#[cfg(test)] +mod zoom_tests; diff --git a/src/panels.rs b/src/panels.rs index 7a5b22d9..faeef7ab 100644 --- a/src/panels.rs +++ b/src/panels.rs @@ -388,18 +388,48 @@ pub fn edit_bar(app: &mut App, ui: &mut egui::Ui) { ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { let z = app.view.zoom; let canvas = app.stage_cache.canvas; - egui::ComboBox::from_id_salt("zoom").width(84.0).selected_text(text(Type::Data, format!("{:>4.0}%", z * 100.0))).show_ui(ui, |ui| { + // The presets arrow alone opens the menu (Flash parity): clicking the text never does. + let arrow = ui.menu_button("▾", |ui| { if ui.selectable_label(false, tr!("Fit in window")).clicked() { + ui.close(); app.view.fit = true; } for pct in [25.0f32, 50.0, 100.0, 200.0, 400.0, 800.0] { if ui.selectable_label((z * 100.0 - pct).abs() < 0.5, text(Type::Data, format!("{pct:>4.0}%"))).clicked() { + ui.close(); app.zoom_about(canvas, canvas.center(), pct / 100.0); } } - }) - .response - .set_name(tr!("Zoom")); + }); + arrow.response.set_name(tr!("Zoom presets")); + ui.ctx().data_mut(|d| d.insert_temp(egui::Id::new("zoom_arrow"), arrow.response.rect)); + // The percentage itself is typed here: focus to type a value, Enter or focus loss applies it. + let was_editing = app.zoom_text.is_some(); + let mut value = app.zoom_text.take().unwrap_or_else(|| format!("{:>4.0}%", z * 100.0)); + let field = with_type(ui, Type::Data, |ui| ui.add(egui::TextEdit::singleline(&mut value).desired_width(56.0).char_limit(7)).named(tr!("Zoom"))); + ui.ctx().data_mut(|d| d.insert_temp(egui::Id::new("zoom_field"), field.rect)); + if !was_editing && field.gained_focus() { + // Type digits over digits, not over the percent sign. + value = format!("{:.0}", z * 100.0); + } + let enter = ui.input(|i| i.key_pressed(egui::Key::Enter)); + if field.lost_focus() || (enter && field.has_focus()) { + let typed = value.trim(); + let typed = typed.strip_suffix('%').unwrap_or(typed).trim(); + if let Ok(pct) = typed.parse::() + && pct.is_finite() + { + app.zoom_about(canvas, canvas.center(), pct.clamp(5.0, 6400.0) / 100.0); + } + app.zoom_text = None; + if enter { + ui.memory_mut(|m| m.surrender_focus(field.id)); + } + } else if field.has_focus() { + app.zoom_text = Some(value); + } else { + app.zoom_text = None; + } // Beside the zoom, as in Animate (#251): a toggle, on = the pasteboard is hidden. let clip = app.view.clip_to_stage; if crate::timeline::icon_button(ui, tr!("Clip content outside the stage"), Some(clip), Icon::ClipStage).clicked() { diff --git a/src/zoom_tests.rs b/src/zoom_tests.rs new file mode 100644 index 00000000..2ad72bab --- /dev/null +++ b/src/zoom_tests.rs @@ -0,0 +1,101 @@ +//! The zoom edit-bar field (#43): an editable percentage with a separate presets arrow, +//! driven frame by frame like the retime tests. + +use crate::app::App; +use egui::{Event, Modifiers, PointerButton, Pos2, Rect, vec2}; + +fn frame(ctx: &egui::Context, app: &mut App, events: Vec, modifiers: Modifiers) { + let mut all = vec![Event::ModifiersChanged(modifiers)]; + all.extend(events); + let input = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 940.0))), events: all, ..Default::default() }; + ctx.run_ui(input, |ui| app.frame(ui)).textures_delta.clear(); +} + +fn settle(ctx: &egui::Context, app: &mut App) { + for _ in 0..3 { + frame(ctx, app, Vec::new(), Modifiers::NONE); + } +} + +fn button(pos: Pos2, pressed: bool, modifiers: Modifiers) -> Event { + Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers } +} + +fn click(ctx: &egui::Context, app: &mut App, at: Pos2, modifiers: Modifiers) { + frame(ctx, app, vec![Event::PointerMoved(at)], modifiers); + frame(ctx, app, vec![button(at, true, modifiers)], modifiers); + frame(ctx, app, vec![button(at, false, modifiers)], modifiers); + settle(ctx, app); +} + +fn key(ctx: &egui::Context, app: &mut App, key: egui::Key, modifiers: Modifiers) { + frame(ctx, app, vec![Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }], modifiers); + frame(ctx, app, vec![Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers }], modifiers); + settle(ctx, app); +} + +fn type_text(ctx: &egui::Context, app: &mut App, s: &str) { + frame(ctx, app, vec![Event::Text(s.to_owned())], Modifiers::NONE); + settle(ctx, app); +} + +fn select_all(ctx: &egui::Context, app: &mut App) { + frame(ctx, app, vec![Event::Key { key: egui::Key::A, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::CTRL }], Modifiers::CTRL); + frame(ctx, app, vec![Event::Key { key: egui::Key::A, physical_key: None, pressed: false, repeat: false, modifiers: Modifiers::CTRL }], Modifiers::CTRL); + settle(ctx, app); +} + +fn clear_field(ctx: &egui::Context, app: &mut App) { + select_all(ctx, app); + // Selection or not, this leaves the field empty (the seed is 3-4 digits). + for _ in 0..8 { + frame(ctx, app, vec![Event::Key { key: egui::Key::Backspace, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }], Modifiers::NONE); + frame(ctx, app, vec![Event::Key { key: egui::Key::Backspace, physical_key: None, pressed: false, repeat: false, modifiers: Modifiers::NONE }], Modifiers::NONE); + } + settle(ctx, app); +} + +fn zoom_rects(ctx: &egui::Context) -> (Rect, Rect) { + let field = ctx.data(|d| d.get_temp::(egui::Id::new("zoom_field"))).expect("the zoom field drew"); + let arrow = ctx.data(|d| d.get_temp::(egui::Id::new("zoom_arrow"))).expect("the zoom arrow drew"); + (field, arrow) +} + +#[test] +fn zoom_field_types_a_value_and_rejects_garbage() { + let ctx = egui::Context::default(); + let session = anicraft_engine::Session::new(anicraft_engine::doc::sample::demo()); + let mut app = App::with_context(&ctx, session, Some(std::path::PathBuf::from("unused.png"))); + app.view.fit = false; + app.view.zoom = 1.0; + settle(&ctx, &mut app); + app.view.fit = false; + app.view.zoom = 1.0; + settle(&ctx, &mut app); + + // Clicking the text focuses it without changing the zoom (no presets menu from the text). + let (field, arrow) = zoom_rects(&ctx); + let before = app.view.zoom; + click(&ctx, &mut app, field.center(), Modifiers::NONE); + assert!((app.view.zoom - before).abs() < 1e-6, "focusing the field leaves zoom alone"); + // Clicking the arrow alone does not change the zoom either (it only opens the presets). + click(&ctx, &mut app, arrow.center(), Modifiers::NONE); + assert!((app.view.zoom - before).abs() < 1e-6, "the presets arrow alone zooms nothing"); + key(&ctx, &mut app, egui::Key::Escape, Modifiers::NONE); + + // Type a custom value and press Enter: the stage zooms there. + let (field, _) = zoom_rects(&ctx); + click(&ctx, &mut app, field.center(), Modifiers::NONE); + clear_field(&ctx, &mut app); + type_text(&ctx, &mut app, "150"); + key(&ctx, &mut app, egui::Key::Enter, Modifiers::NONE); + assert!((app.view.zoom - 1.5).abs() < 0.01, "typed 150% zooms to 1.5, got {}", app.view.zoom); + + // Garbage reverts to the current zoom without changing anything. + let (field, _) = zoom_rects(&ctx); + click(&ctx, &mut app, field.center(), Modifiers::NONE); + clear_field(&ctx, &mut app); + type_text(&ctx, &mut app, "abc"); + key(&ctx, &mut app, egui::Key::Enter, Modifiers::NONE); + assert!((app.view.zoom - 1.5).abs() < 0.01, "invalid text keeps zoom at 1.5, got {}", app.view.zoom); +}