From db97b3d10fa47611cf8b7cd05ecc324a8124692b Mon Sep 17 00:00:00 2001 From: ViktorBystrov72 <18964918+ViktorBystrov72@users.noreply.github.com> Date: Wed, 25 Nov 2026 12:52:51 +0500 Subject: [PATCH] fix(stage): preserve fitted zoom and stage coordinates --- src/app.rs | 12 ++++++++++-- src/panels.rs | 25 +++++++++++++------------ src/zoom_tests.rs | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/app.rs b/src/app.rs index fec96a4e..33bd4218 100644 --- a/src/app.rs +++ b/src/app.rs @@ -220,6 +220,12 @@ impl StageView { } } +/// The percentage being typed, with edits tracked across frames until it is submitted. +pub struct ZoomDraft { + pub text: String, + pub edited: bool, +} + /// In-progress pointer gesture on the stage. #[derive(Clone, Copy, Debug, PartialEq)] pub enum Drag { @@ -532,7 +538,7 @@ pub struct App { pub xf: crate::xform::State, /// 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, + 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). @@ -4135,7 +4141,9 @@ impl App { pub fn view_center_stage(&self) -> egui::Vec2 { let size = egui::vec2(self.s.doc.stage.width as f32, self.s.doc.stage.height as f32); - size * 0.5 - self.view.pan / self.view.zoom.max(1e-3) + let zoom = self.view.zoom; + let divisor = if zoom.is_finite() && zoom > 0.0 { zoom } else { 1e-3 }; + size * 0.5 - self.view.pan / divisor } pub fn zoom_by(&mut self, canvas: egui::Rect, screen: egui::Pos2, factor: f32, pixels_per_point: f32) { diff --git a/src/panels.rs b/src/panels.rs index 0dc1207a..831e1bb0 100644 --- a/src/panels.rs +++ b/src/panels.rs @@ -1,6 +1,6 @@ //! Tools bar, edit bar, status bar, Properties and Library panels. -use crate::app::{App, Tool}; +use crate::app::{App, Tool, ZoomDraft}; use crate::icons::{self, Icon}; use crate::theme::{Section, T, TARGET, TOOL_BUTTON, TOOL_ICON, Type, color, section, text, to_color32, with_type}; use anicraft_engine::doc::{ColorEffect, ColorStyle, ElementKind, LibraryItem, LineType, MAX_PATH_OFFSET, Paint, Rgba, SymbolKind, TextKind, evaluate_layer_elements}; @@ -426,32 +426,33 @@ pub fn edit_bar(app: &mut App, ui: &mut egui::Ui) { crate::icons::paint(ui.painter(), Rect::from_center_size(arrow.response.rect.center(), vec2(12.0, 12.0)), Icon::ChevronDown, if arrow.response.hovered() { T.icon_hot } else { T.icon }); arrow.response.set_name(tr!("Zoom presets")); ui.ctx().data_mut(|d| d.insert_temp(egui::Id::unique("zoom_arrow"), arrow.response.rect)); - // The percentage itself is typed here: focus to type a value, Enter or focus loss applies it. + // The percentage itself is typed here: Enter or focus loss applies edits. 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"))); + let mut draft = app.zoom_text.take().unwrap_or_else(|| ZoomDraft { text: format!("{:>4.0}%", z * 100.0), edited: false }); + let field = with_type(ui, Type::Data, |ui| { + ui.add(egui::TextEdit::singleline(&mut draft.text).desired_width(56.0).char_limit(7)).named(tr!("Zoom")) + }); ui.ctx().data_mut(|d| d.insert_temp(egui::Id::unique("zoom_field"), field.rect)); - if !was_editing && field.gained_focus() { + if !was_editing && field.gained_focus() && !field.changed() { // Type digits over digits, not over the percent sign. - value = format!("{:.0}", z * 100.0); + draft.text = format!("{:.0}", z * 100.0); } + draft.edited |= field.changed(); 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 = draft.text.trim(); let typed = typed.strip_suffix('%').unwrap_or(typed).trim(); - if let Ok(pct) = typed.parse::() + if draft.edited + && let Ok(pct) = typed.parse::() && pct.is_finite() { app.zoom_about(canvas, canvas.center(), pct / (100.0 * ppp), ppp); } - 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; + app.zoom_text = Some(draft); } // Beside the zoom, as in Animate (#251): a toggle, on = the pasteboard is hidden. let clip = app.view.clip_to_stage; diff --git a/src/zoom_tests.rs b/src/zoom_tests.rs index 6146dd86..ac2381f6 100644 --- a/src/zoom_tests.rs +++ b/src/zoom_tests.rs @@ -171,4 +171,26 @@ fn fit_in_window_shows_the_whole_large_stage_below_the_manual_zoom_limit() { sized_frame(&ctx, &mut app, size, vec![event], modifiers); } assert_eq!(app.view.zoom, fitted, "Zoom Out from Fit does not jump up to the manual minimum"); + + let (field, _) = zoom_rects(&ctx); + let stage_center = app.stage_cache.canvas.center(); + for (at, editing) in [(field.center(), true), (stage_center, false)] { + for event in [Event::PointerMoved(at), button(at, true, Modifiers::NONE), button(at, false, Modifiers::NONE)] { + sized_frame(&ctx, &mut app, size, vec![event], Modifiers::NONE); + } + sized_frame(&ctx, &mut app, size, Vec::new(), Modifiers::NONE); + assert_eq!(app.zoom_text.is_some(), editing, "the field receives and then loses focus"); + assert_eq!(app.view.zoom, fitted, "leaving the unedited field keeps the fitted zoom"); + } +} + +#[test] +fn view_centre_inverts_the_actual_zoom_below_a_thousandth() { + let ctx = egui::Context::default(); + let mut app = App::with_context(&ctx, anicraft_engine::Session::default(), Some(std::path::PathBuf::from("unused.png"))); + app.view.zoom = 0.000625; + app.view.pan = vec2(10.0, -8.0); + let stage_center = vec2(app.s.doc.stage.width as f32, app.s.doc.stage.height as f32) * 0.5; + let offset_points = (stage_center - app.view_center_stage()) * app.view.zoom; + assert!((offset_points - app.view.pan).length() < 0.01, "expected pan {:?}, got {offset_points:?}", app.view.pan); }