pages: the margin handle stories fit the page to the stage

The story showed a Letter page at 100 % centred on the stage, so on a stage narrower than the
page the left and top margins sat outside the canvas and the drag pressed nothing. It fits the
page with the canvas's Fit on Screen, and the drive fails loudly when the Left Margin handle is
missing.
This commit is contained in:
aiongg
2026-11-17 22:18:47 +00:00
parent c016bbb209
commit baceabad8e
2 changed files with 17 additions and 7 deletions
+13
View File
@@ -1,6 +1,7 @@
//! Pages for stories and tests: a document of US Letter pages with generated pictures.
use super::view::{PageSource, PlacedPage};
use craft_behavior::CanvasView;
use craft_ui::primitives::textures::{FakeThumbSource, Revision, ThumbRender, ThumbRequest, ThumbSource};
use craft_ui::style::DataColor;
use egui::{Color32, Pos2, Rect, Vec2, pos2};
@@ -13,6 +14,18 @@ const LETTER_HEIGHT: f64 = 792.0;
/// The gap between pages in [`demo_placed`], screen points.
const GAP: f32 = 12.0;
/// The room around the page [`fitted_page`] leaves, screen points: the margin handles sit just outside the page.
const FIT_MARGIN: f64 = 24.0;
/// Fits `view` to one US Letter page and returns that page as the view places it, so every edge of the page is on
/// screen whatever the size of `view.rect`.
pub fn fitted_page(view: &mut CanvasView, pixels_per_point: f32) -> PlacedPage {
let size = Size::new(LETTER_WIDTH, LETTER_HEIGHT);
craft_behavior::canvas::zoom::fit(view, size, pixels_per_point, FIT_MARGIN);
let rect = Rect::from_min_max(view.to_screen(kurbo::Point::ZERO), view.to_screen(kurbo::Point::new(LETTER_WIDTH, LETTER_HEIGHT)));
PlacedPage::new(0, rect, view.zoom, (0.0, rect.height()))
}
/// The paper of the demo document.
pub const PAPER: DataColor = DataColor::new(Color32::from_rgb(0xfa, 0xf8, 0xf2));
/// The ink of the demo document's text.
+4 -7
View File
@@ -2,7 +2,7 @@
use super::{MarginHandles, PageEdges};
use crate::setup::Margins;
use crate::ui::fixtures::demo_placed;
use crate::ui::fixtures::fitted_page;
use craft_behavior::CanvasView;
use craft_behavior::kurbo::Point;
use craft_ui::canvas::Canvas;
@@ -11,14 +11,11 @@ use craft_ui::runtime::env::{Message, UnitKind};
use craft_ui::testing::{Drive, Fixture, StoryData, StoryOutput, story};
use egui::{Ui, Vec2};
/// The document point the view is centred on.
const CENTRE: Point = Point::new(300.0, 400.0);
/// How far the drag moves the left edge, in drag thresholds.
const DRAG_THRESHOLDS: f32 = 12.0;
fn drag_left_edge(fixture: &mut Fixture) {
let Some(target) = fixture.by_label("Left Margin") else { return };
let target = fixture.by_label("Left Margin").expect("the handles name their left edge Left Margin");
let travel = DRAG_THRESHOLDS * craft_ui::testing::input::slop(fixture.ctx());
fixture.drag(&target, Vec2::new(travel, 0.0));
}
@@ -26,12 +23,12 @@ fn drag_left_edge(fixture: &mut Fixture) {
/// The handles over `edges` on a page canvas.
fn handles(ui: &mut Ui, edges: Bind<'_, PageEdges>) -> StoryOutput {
let full = craft_ui::testing::stage(ui);
let view = CanvasView::new(full, CENTRE, 1.0);
let mut view = CanvasView::new(full, Point::ZERO, 1.0);
let page = fitted_page(&mut view, ui.ctx().pixels_per_point());
let mut edges = edges;
Canvas::new("Pages")
.view(view)
.show_at(ui, full, |ui, body| {
let page = demo_placed(1, CanvasView::new(body.rect, CENTRE, 1.0).to_screen(Point::ZERO)).remove(0);
Some(MarginHandles::new("Margins", &page, edges.reborrowed()).header_footer().unit(UnitKind::Inches).show_at(ui, body.rect))
})
.inner