F-16 fix round 1: one canvas zoom field with the percent unit, NavigateBar::zoom over the canvas's ViewTarget, Fit Artboard margins 30 by 40

This commit is contained in:
aiongg
2026-11-13 13:17:30 +00:00
parent b809628b02
commit 3314ffeb28
10 changed files with 82 additions and 65 deletions
+7 -6
View File
@@ -11,7 +11,7 @@
use super::view::CanvasView;
use emath::Pos2;
use kurbo::{Point, Rect, Size};
use kurbo::{Point, Rect, Size, Vec2};
use std::ops::RangeInclusive;
/// A zoom within 0.1 % of a rung counts as on it.
@@ -139,21 +139,22 @@ pub fn fit(view: &mut CanvasView, size: Size, pixels_per_point: f32, margin: f64
/// [`fit`] with the device zoom allowed up to `max_device` (1 = 100 %) instead of 100 %: a small document is
/// magnified to fill the canvas, as Photoshop's Fit on Screen does.
pub fn fit_up_to(view: &mut CanvasView, size: Size, pixels_per_point: f32, max_device: f64, margin: f64) {
fit_rect_up_to(view, Rect::from_origin_size(Point::ORIGIN, size), size, pixels_per_point, max_device, margin);
fit_rect_up_to(view, Rect::from_origin_size(Point::ORIGIN, size), size, pixels_per_point, max_device, Vec2::new(margin, margin));
}
/// Fit a part of the document on screen (an artboard, the selected layers' bounds): centre `rect`, in document
/// units, and zoom so all of it shows inside `view.rect` less `margin` points on each side, up to the zoom ceiling.
/// units, and zoom so all of it shows inside `view.rect` less `margin.x` points at the left and right and `margin.y`
/// at the top and bottom, up to the zoom ceiling.
/// The zoom stays inside the range of a document of `size`; an empty `rect` zooms to that ceiling about its centre.
pub fn fit_rect(view: &mut CanvasView, rect: Rect, size: Size, pixels_per_point: f32, margin: f64) {
pub fn fit_rect(view: &mut CanvasView, rect: Rect, size: Size, pixels_per_point: f32, margin: Vec2) {
fit_rect_up_to(view, rect, size, pixels_per_point, ZoomLadder::MAX, margin);
}
fn fit_rect_up_to(view: &mut CanvasView, rect: Rect, size: Size, pixels_per_point: f32, max_device: f64, margin: f64) {
fn fit_rect_up_to(view: &mut CanvasView, rect: Rect, size: Size, pixels_per_point: f32, max_device: f64, margin: Vec2) {
let rect = rect.abs();
let (shown_w, shown_h, sin, cos) = shown_extent(view, rect.size());
let (bw, bh) = (shown_w * cos + shown_h * sin, shown_w * sin + shown_h * cos);
let scale = ((f64::from(view.rect.width()) - 2.0 * margin) / bw).min((f64::from(view.rect.height()) - 2.0 * margin) / bh);
let scale = ((f64::from(view.rect.width()) - 2.0 * margin.x) / bw).min((f64::from(view.rect.height()) - 2.0 * margin.y) / bh);
let device = ZoomLadder::clamp((scale * f64::from(pixels_per_point)).min(max_device), size);
view.zoom = CanvasView::points_per_unit(device, pixels_per_point);
view.center = rect.center();
+1
View File
@@ -69,6 +69,7 @@ mod momentary;
mod navigate;
mod rotate;
mod tools;
mod zoom_field;
mod zoom_motion;
pub use momentary::{Momentary, MomentaryCommands, MomentaryTool};
+12 -25
View File
@@ -2,7 +2,8 @@
use super::NavigationOptions;
use crate::buttons::Button;
use crate::canvas::{CanvasWords, ViewCommands};
use crate::canvas::zoom_field::show_zoom_field;
use crate::canvas::{CanvasWords, ViewCommands, ViewTarget};
use crate::choice::{Checkbox, Segmented};
use crate::contract::{Bind, BodyCx, Entry, Gap, NoValue, Outcome, Props as _, Show as _, ShowContainer as _};
use crate::display::Divider;
@@ -10,10 +11,8 @@ use crate::form::Field;
use crate::layout_components::Row;
use crate::runtime::env::{Env, UnitKind};
use crate::text::NumberField;
use craft_behavior::canvas::zoom;
use craft_behavior::geometry::crop::normalized_angle;
use craft_behavior::kurbo::Size;
use craft_behavior::{CanvasView, EditEvents, IconId, Mixed, ZoomLadder};
use craft_behavior::{CanvasView, EditEvents, IconId, Mixed};
use egui::{Response, Ui};
/// What the navigate bar reports.
@@ -36,7 +35,7 @@ impl Outcome for NavigateBarOutput {
/// The members of one tool's bar.
enum Members<'a> {
Zoom { view: Bind<'a, CanvasView>, document: Size, options: &'a mut NavigationOptions, out: Bind<'a, bool> },
Zoom { view: Bind<'a, CanvasView>, target: ViewTarget, options: &'a mut NavigationOptions, out: Bind<'a, bool> },
RotateView { view: Bind<'a, CanvasView> },
Hand { options: &'a mut NavigationOptions },
}
@@ -51,8 +50,8 @@ enum Members<'a> {
/// - **Rotate View:** the Angle field, then Reset View.
/// - **Hand:** Scroll All Windows, then 100%, Fit Screen and Fill Screen.
///
/// The fields edit the bound view: Zoom in percent, its range the document's zoom range widened to include the current
/// zoom, its arrows stepping the zoom ladder, the centre kept; Angle in degrees from -180 to 180. The buttons are the
/// The fields edit the bound view: Zoom is the canvas view's zoom field (percent, the document's range widened to the
/// current zoom, ladder steps, the centre kept); Angle is in degrees from -180 to 180. The buttons are the
/// [`ViewCommands`]: they act on the canvas that holds the key focus (else the one that held it last), which applies
/// them with its document and eases them under Animated Zoom; their enabled state comes from that canvas. The check
/// boxes write into the [`NavigationOptions`].
@@ -63,10 +62,10 @@ pub struct NavigateBar<'a> {
}
impl<'a> NavigateBar<'a> {
/// The Zoom tool's options over `view`, a document of `document` units, the user's `options` and the tool's
/// direction `out` (Zoom Out picked).
pub fn zoom(view: Bind<'a, CanvasView>, document: Size, options: &'a mut NavigationOptions, out: Bind<'a, bool>) -> Self {
NavigateBar { members: Members::Zoom { view, document, options, out } }
/// The Zoom tool's options over `view`, the user's `options` and the tool's direction `out` (Zoom Out picked).
/// `target` is the document the view shows, the one the canvas is given ([`Canvas::document`](crate::canvas::Canvas::document)).
pub fn zoom(view: Bind<'a, CanvasView>, target: impl Into<ViewTarget>, options: &'a mut NavigationOptions, out: Bind<'a, bool>) -> Self {
NavigateBar { members: Members::Zoom { view, target: target.into(), options, out } }
}
/// The Rotate View tool's options over `view`.
@@ -84,29 +83,17 @@ impl<'a> NavigateBar<'a> {
let NavigateBar { members } = self;
let env = Env::of(ui);
let words = env.words::<CanvasWords>();
let units = env.units();
let pixels_per_point = env.scale.0;
let mut events = EditEvents::new();
let row = Row::new().gap(Gap::Item);
let shown = match members {
Members::Zoom { mut view, document, options, mut out } => row.show(ui, |ui, cx| {
Members::Zoom { mut view, target, options, mut out } => row.show(ui, |ui, cx| {
let directions = [
Entry::new(false, words.navigate_zoom_in.clone()).icon(IconId::new("zoom_in")),
Entry::new(true, words.navigate_zoom_out.clone()).icon(IconId::new("zoom_out")),
];
Segmented::new(words.navigate_direction.clone(), out.reborrowed(), &directions).show(ui);
if let Some(Mixed::Same(current)) = view.reborrow().read(cx) {
let device = zoom::device(&current, pixels_per_point);
let ladder = |percent: f64, dir: i32| ZoomLadder::step(percent / 100.0, dir, document) * 100.0;
let field = NumberField::new(words.navigate_zoom.clone(), Bind::given(Some(device * 100.0)))
.codec(units.zoom_codec())
.range(ZoomLadder::percent_range(device, document))
.stepper(ladder)
.show(ui);
let at = |percent: Option<f64>| {
percent.map_or(current, |percent| zoom::with_device(&current, percent / 100.0, document, pixels_per_point))
};
events = write(&mut view, cx, field.edit.events.clone().map(at));
events = write(&mut view, cx, show_zoom_field(ui, words.navigate_zoom.clone(), current, target.document));
}
Divider::vertical().show(ui);
Checkbox::new(Bind::bound(&mut options.zoom_resizes_windows)).label(words.navigate_resize_windows.clone()).show(ui);
+12 -13
View File
@@ -3,11 +3,11 @@
use crate::bars::{Bar, BarKind};
use crate::canvas::rotate::RIM;
use crate::canvas::testing::canvas_story::{StoryDocument, on_canvas_with};
use crate::canvas::{Canvas, NavigateBar, NavigationOptions, RotateView, RotateViewOutput};
use crate::canvas::{Canvas, NavigateBar, NavigationOptions, RotateView, RotateViewOutput, ViewTarget};
use crate::contract::{Bind, Show as _, ShowContainer as _, Validity};
use crate::runtime::env::Message;
use crate::testing::{Drive, Fixture, StoryData, StoryOutput, story};
use craft_behavior::kurbo::{Point, Size};
use craft_behavior::kurbo::Point;
use craft_behavior::{CanvasView, Mixed};
use egui::{Rect, Ui, Vec2};
@@ -59,14 +59,14 @@ fn too_far(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
})
}
/// Shows a navigation tool's options bar from `bar` over the view `view` above a canvas on the story document that
/// shows the same view, and returns the bar's output. The canvas fits the view on its first pass and is the instance
/// the bar's view commands reach.
fn bar_over_canvas(ui: &mut Ui, view: &mut CanvasView, mut bar: impl FnMut(&mut Ui, Bind<'_, CanvasView>) -> StoryOutput) -> StoryOutput {
let shown = Bar::new("Options", BarKind::Options).show(ui, |ui, _| bar(ui, Bind::bound(&mut *view)));
/// Shows a navigation tool's options bar from `bar` over the view `view` and the story document's target above a canvas
/// that shows the same view of the same target, and returns the bar's output. The canvas fits the view on its first
/// pass and is the instance the bar's view commands reach.
fn bar_over_canvas(ui: &mut Ui, view: &mut CanvasView, mut bar: impl FnMut(&mut Ui, Bind<'_, CanvasView>, ViewTarget) -> StoryOutput) -> StoryOutput {
let target = ViewTarget::new(StoryDocument::STANDARD.rect().size());
let shown = Bar::new("Options", BarKind::Options).show(ui, |ui, _| bar(ui, Bind::bound(&mut *view), target));
let room = crate::testing::stage(ui);
let document = StoryDocument::STANDARD.rect().size();
on_canvas_with(ui, room, Canvas::new("Document").view(Bind::bound(view)).document(document), Some(&StoryDocument::STANDARD), |_, _| {
on_canvas_with(ui, room, Canvas::new("Document").view(Bind::bound(view)).document(target), Some(&StoryDocument::STANDARD), |_, _| {
StoryOutput::default()
});
shown.inner.unwrap_or_default()
@@ -90,18 +90,17 @@ impl Default for Navigation {
#[story(NavigateBar<'static>, covers = [], drive = Drive::Never)]
fn zoom_bar(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let Navigation { view, options, out } = data.value("navigation", Navigation::default());
let document: Size = StoryDocument::STANDARD.rect().size();
bar_over_canvas(ui, view, |ui, bound| NavigateBar::zoom(bound, document, &mut *options, Bind::bound(&mut *out)).show(ui).into())
bar_over_canvas(ui, view, |ui, bound, target| NavigateBar::zoom(bound, target, &mut *options, Bind::bound(&mut *out)).show(ui).into())
}
#[story(NavigateBar<'static>, covers = [], drive = Drive::Never)]
fn rotate_view_bar(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let Navigation { view, .. } = data.value("navigation", Navigation::default());
bar_over_canvas(ui, view, |ui, bound| NavigateBar::rotate_view(bound).show(ui).into())
bar_over_canvas(ui, view, |ui, bound, _| NavigateBar::rotate_view(bound).show(ui).into())
}
#[story(NavigateBar<'static>, covers = [], drive = Drive::Never)]
fn hand_bar(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let Navigation { view, options, .. } = data.value("navigation", Navigation::default());
bar_over_canvas(ui, view, |ui, _| NavigateBar::hand(&mut *options).show(ui).into())
bar_over_canvas(ui, view, |ui, _, _| NavigateBar::hand(&mut *options).show(ui).into())
}
@@ -3,7 +3,7 @@
//! artboards-only preview).
use craft_behavior::canvas::zoom;
use craft_behavior::kurbo::{Point, Rect as DocRect, Size};
use craft_behavior::kurbo::{Point, Rect as DocRect, Size, Vec2 as DocVec};
use craft_behavior::{CanvasView, ZoomLadder};
use egui::{Pos2, Rect, pos2, vec2};
@@ -76,7 +76,7 @@ pub fn zoom_to_document_rect(view: &mut CanvasView, doc_rect: DocRect, size: Siz
view.zoom = CanvasView::points_per_unit(device, ppp);
view.center = Point::new(x0 + width / 2.0, y0 + height / 2.0);
} else {
zoom::fit_rect(view, DocRect::new(x0, y0, x1, y1), size, ppp, 0.0);
zoom::fit_rect(view, DocRect::new(x0, y0, x1, y1), size, ppp, DocVec::ZERO);
}
*view != before
}
@@ -1,11 +1,11 @@
//! `NavigatorZoomRow`: the Navigator's percent field, Zoom Out, slider and Zoom In.
use crate::buttons::IconButton;
use crate::canvas::zoom_field::show_zoom_field;
use crate::canvas::{CanvasWords, ViewCommands};
use crate::contract::{Bind, NoValue, Outcome, Props as _, Show as _, ShowContainer as _};
use crate::layout_components::Row;
use crate::runtime::env::Env;
use crate::text::NumberField;
use crate::value_drag::{Slider, SliderScale};
use craft_behavior::canvas::zoom;
use craft_behavior::kurbo::Size;
@@ -34,10 +34,8 @@ impl Outcome for NavigatorZoomRowOutput {
/// The Navigator's zoom row: a percent field, Zoom Out, a slider over the whole zoom range and Zoom In, in reading
/// order.
///
/// - The field shows the zoom in percent, its range the document's zoom range widened to include the current zoom, so
/// the field never pulls the zoom back. A typed value applies without animation; Escape in the field restores the
/// zoom from before the edit.
/// - The field shows up to two decimals, and its arrows and wheel step the zoom ladder.
/// - The field is the canvas view's zoom field: percent, the document's range widened to the current zoom, ladder
/// steps. A typed value applies without animation; Escape in the field restores the zoom from before the edit.
/// - Zoom Out and Zoom In are the [`ViewCommands::ZoomOut`] and [`ViewCommands::ZoomIn`] commands: label, chord, enabled
/// state and action come from the active canvas, which steps its view and reports the ease under Animated Zoom in
/// [`CanvasOutput`](crate::canvas::CanvasOutput).
@@ -72,17 +70,10 @@ impl<'a> NavigatorZoomRow<'a> {
let shown = Row::new().show(ui, |ui, cx| {
let Some(Mixed::Same(current)) = view.reborrow().read(cx) else { return };
let device = zoom::device(&current, pixels_per_point);
let range = ZoomLadder::percent_range(device, document);
let at = |device: f64| zoom::with_device(&current, device, document, pixels_per_point);
let mut edits: Vec<EditEvent<CanvasView>> = Vec::new();
let ladder = |percent: f64, dir: i32| ZoomLadder::step(percent / 100.0, dir, document) * 100.0;
let field = NumberField::new(zoom_label.clone(), Bind::given(Some(device * 100.0)))
.codec(units.zoom_codec())
.range(range.clone())
.stepper(ladder)
.show(ui);
edits.extend(field.edit.events.clone().map(|percent| percent.map_or(current, |percent| at(percent / 100.0))));
edits.extend(show_zoom_field(ui, zoom_label.clone(), current, document));
IconButton::command(IconId::new("minus"), ViewCommands::ZoomOut).show(ui);
+5 -4
View File
@@ -13,7 +13,7 @@ use super::{CanvasTool, ToolAction, ToolContext, ToolKey, ToolPath, ToolPointer}
use crate::primitives::Draw;
use craft_behavior::canvas::zoom;
use craft_behavior::geometry::crop::normalized_angle;
use craft_behavior::kurbo::{Rect, Size};
use craft_behavior::kurbo::{Rect, Size, Vec2};
use craft_behavior::{CancelReason, CanvasView, Cursor, PaintedCursor, ZoomLadder};
use craft_keymap::Mods;
use egui::Pos2;
@@ -21,11 +21,12 @@ use egui::Pos2;
/// The rotation step of the Rotate View commands, in degrees.
const ROTATE_STEP_DEG: f64 = 15.0;
/// The room Fit Artboard on Screen leaves around the board, in points: the board's name sits above it.
const ARTBOARD_MARGIN: f64 = 40.0;
/// The room Fit Artboard on Screen leaves around the board, in points: at the sides, and above and below (the board's
/// name sits above it).
const ARTBOARD_MARGIN: Vec2 = Vec2::new(30.0, 40.0);
/// The room Fit Layers on Screen leaves around the layers' bounds, in points.
const LAYERS_MARGIN: f64 = 20.0;
const LAYERS_MARGIN: Vec2 = Vec2::new(20.0, 20.0);
/// Photoshop's default Screen Resolution (Preferences, Units & Rulers), in pixels per inch.
const SCREEN_PPI: f64 = 72.0;
+35
View File
@@ -0,0 +1,35 @@
//! The zoom field of a canvas view: the Navigator's and the Zoom tool bar's percent field.
use crate::contract::{Bind, Props as _, Show as _};
use crate::runtime::env::{Env, Label, UnitKind};
use crate::text::NumberField;
use craft_behavior::canvas::zoom;
use craft_behavior::kurbo::Size;
use craft_behavior::{CanvasView, EditEvents, ZoomLadder};
use egui::Ui;
/// Shows the zoom field named `label` over `current`, a view of a document of `document` units, and returns the views
/// it asks for this pass.
///
/// - The field shows the device zoom in percent, with the percent sign and up to two decimals.
/// - Its range is the document's zoom range widened to include the current zoom, so the field never pulls the zoom
/// back.
/// - Its arrows, Page keys and wheel step the zoom ladder.
/// - A typed zoom goes through [`ZoomLadder::clamp`] and keeps the view's centre; clearing the field keeps the view.
pub(crate) fn show_zoom_field(ui: &mut Ui, label: Label, current: CanvasView, document: Size) -> EditEvents<CanvasView> {
let env = Env::of(ui);
let pixels_per_point = env.scale.0;
let device = zoom::device(&current, pixels_per_point);
let ladder = |percent: f64, dir: i32| ZoomLadder::step(percent / 100.0, dir, document) * 100.0;
let field = NumberField::new(label, Bind::given(Some(device * 100.0)))
.codec(env.units().zoom_codec())
.unit_kind(UnitKind::Percent)
.range(ZoomLadder::percent_range(device, document))
.stepper(ladder)
.show(ui);
field
.edit
.events
.clone()
.map(|percent| percent.map_or(current, |percent| zoom::with_device(&current, percent / 100.0, document, pixels_per_point)))
}
@@ -10,7 +10,7 @@ use crate::display::{Progress, Text};
use crate::layout_components::Row;
use crate::primitives::ContentPainterCx;
use crate::runtime::access::Access;
use crate::runtime::env::{Env, Label};
use crate::runtime::env::{Env, Label, UnitKind};
use crate::runtime::focus::{ActivateKeys, KeepKeys, KeyUse};
use crate::style::{ComponentStyle, FocusLook, StyleCx};
use crate::text::NumberField;
@@ -596,6 +596,7 @@ impl PreviewZoom {
let out = IconButton::new(IconId::new("minus")).label(words.zoom_out.clone()).enabled(self.view.can_zoom_out()).show(ui);
let field = NumberField::new(words.zoom.clone(), Bind::given(Some(percent(self.view.transform.scale))))
.codec(env.units().zoom_codec())
.unit_kind(UnitKind::Percent)
.range(range.clone())
.enabled(!self.view.fixed)
.show(ui);
+2 -1
View File
@@ -8,7 +8,7 @@ use crate::display::{Elide, Text};
use crate::form::Field;
use crate::layout_components::Row;
use crate::runtime::commands::{self, CommandRef};
use crate::runtime::env::{Env, Label, Units};
use crate::runtime::env::{Env, Label, UnitKind, Units};
use craft_behavior::canvas::zoom;
use craft_behavior::kurbo::Size;
use craft_behavior::{CanvasView, EditEvents, IconId, Mixed, PixelSize, ZoomLadder};
@@ -212,6 +212,7 @@ impl<'a> StatusReadout<'a> {
let field = Field::new(words.readout_zoom.clone()).side(glyph).sizing(Sizing::Content).show(ui, |ui, _| {
let mut number = crate::text::NumberField::new(words.readout_zoom.clone(), Bind::given(device.map(|device| device * 100.0)))
.codec(env.units().zoom_codec())
.unit_kind(UnitKind::Percent)
.scrub_by_label()
.enabled(current.is_some());
if let Some(device) = device {