fw sync: L-324/L-314: PageInsets::Theme (the port's inset spacing, opt-in; the theme side-margin trial opts in), PageView::go_to_place(GoToPlace::GapAbove) for production's Go To, PageView::fill_screen for full screen edge to edge

This commit is contained in:
Dylan M. Taylor committed 2026-11-23 02:57:02 -05:00
1 parent 85659945c3
commit 08e5ee8ebf
4 files changed
+287 -11

No files matched your search

+1 -1
View File
@@ -63,5 +63,5 @@ pub use thumbnails::{
ThumbnailWords, moved_page,
};
pub use view::{
GoTo, PICTURE_REACH, PageBody, PageRegion, PageRevision, PageSnap, PageSource, PageView, PageViewOutput, PageViewState, PageViewWords, PlacedPage,
GoTo, GoToPlace, PICTURE_REACH, PageBody, PageInsets, PageRegion, PageRevision, PageSnap, PageSource, PageView, PageViewOutput, PageViewState, PageViewWords, PlacedPage,
};
+138 -6
View File
@@ -117,6 +117,34 @@ pub struct PageViewState {
pub turn: Option<bool>,
}
/// Where the creative set's page view ([`ComponentSet::Creative`](craft_theme::ComponentSet::Creative)) takes its
/// spacing from. An app sets it once at its root (`RootEnv::values`); the productivity set's spacing is its own either
/// way.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, craft_ui::runtime::env::EnvValue, craft_ui::contract::MemoProps)]
#[env(style)]
#[non_exhaustive]
pub enum PageInsets {
/// The section gap between pages, the roomy pad above and below, the rail's width at the sides.
#[default]
Standard,
/// The theme's page insets (`space.inset.page_gap`, `page_margin` and `page_side`): PdfCraft's page view.
Theme,
}
/// Where Go To ([`GoTo::Top`]) puts a page's top in the view.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GoToPlace {
/// The view's top where what shows of the row above ends (the document's start for the first row), so the space
/// the layout keeps above the page shows and nothing of the page before it; a fraction of the page goes to the
/// view's top.
#[default]
AfterRowAbove,
/// PdfCraft's, as production places it: the page gap above the page's top, or above the fraction of it, never
/// above the document's start.
GapAbove,
}
/// Where the next pass takes the view.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
@@ -448,6 +476,8 @@ pub struct PageView<'a> {
tool_regions: Vec<PageRegion>,
rulers: bool,
reach: f32,
go_to_place: GoToPlace,
fill_screen: bool,
}
/// How far from the view, in view heights above and below it (and one view width either side), the page view keeps a
@@ -476,9 +506,28 @@ impl<'a> PageView<'a> {
tool_regions: Vec::new(),
rulers: false,
reach: PICTURE_REACH,
go_to_place: GoToPlace::AfterRowAbove,
fill_screen: false,
}
}
/// Where Go To puts a page's top in the view: after the row above by default, the page gap above it with
/// [`GoToPlace::GapAbove`] (PdfCraft's).
#[must_use]
pub fn go_to_place(mut self, place: GoToPlace) -> Self {
self.go_to_place = place;
self
}
/// Full screen's edge-to-edge view: no margin above the first page or below the last, no side gutter, no page
/// border or shadow, and in Two-up that is not continuous the shown spread fitted by its own pages (two pages'
/// widths side by side, a page alone keeping a spread's room) and centred. Off by default.
#[must_use]
pub fn fill_screen(mut self, on: bool) -> Self {
self.fill_screen = on;
self
}
/// The colour round the pages: the theme's by default; production's full screen view is black.
#[must_use]
pub fn pasteboard(mut self, colour: PasteboardColor) -> Self {
@@ -645,8 +694,18 @@ impl<'a> PageView<'a> {
tool_regions,
rulers,
reach,
go_to_place,
fill_screen,
} = self;
let spacing = PageMetrics::new().show_at(ui, Rect::from_min_size(rect.min, Vec2::ZERO)).spacing;
let mut spacing = PageMetrics::new().show_at(ui, Rect::from_min_size(rect.min, Vec2::ZERO)).spacing;
// Filling the screen, the pages keep no margin above or below and no gutter beside them, and draw no border.
let border = if fill_screen {
spacing.margin = 0.0;
spacing.side = 0.0;
PageBorder::None
} else {
border
};
let count = source.page_count();
let sizes: Vec<Size> = (0..count).map(|page| source.page_size(page)).collect();
// With rulers the canvas shows inside the strips, so its view lies inside them. Its body is clipped to its view
@@ -666,7 +725,7 @@ impl<'a> PageView<'a> {
// The canvas is given the view of the state as the pass will read it.
let mut start = state.clone();
start.normalise();
let build = Geometry { sizes: &sizes, viewport, options, spacing, bodies, cache: RefCell::new(None) };
let build = Geometry { sizes: &sizes, viewport, options, spacing, bodies, go_to_place, fill_screen, cache: RefCell::new(None) };
let geo = build.of(&start);
let frame = Frame::of(&geo);
let given = frame.view(rect, start.offset, geo.scale / SCREEN_POINTS_PER_POINT);
@@ -1255,7 +1314,7 @@ impl Pending {
let went = go_to.is_some();
if let Some(go_to) = go_to {
geo = place_go_to(build, &mut state, go_to, (vw, vh));
want = go_to_offset(go_to, &geo, state.continuous, want, (vw, vh), &build.spacing);
want = go_to_offset(go_to, &geo, state.continuous, want, (vw, vh), &build.spacing, build.go_to_place);
}
// Turned by Left or Right at an edge, a row wider than the view starts at the edge the reader comes from, so
// the next key scrolls across it rather than turning again.
@@ -1339,7 +1398,15 @@ fn place_go_to(build: &Geometry, state: &mut PageViewState, go_to: GoTo, (vw, vh
/// gives as NaN. A page's own top puts the view's top where what shows of the row above ends (the document's start
/// for the first row), so the space the layout keeps above the page shows and nothing of the page before it; a point
/// inside the page goes to the view's top. Out of a continuous view, a page's own top is the top of its row.
fn go_to_offset(go_to: GoTo, geo: &Placed, continuous: bool, want: (f64, f64), (vw, vh): (f64, f64), spacing: &PageSpacing) -> (f64, f64) {
fn go_to_offset(
go_to: GoTo,
geo: &Placed,
continuous: bool,
want: (f64, f64),
(vw, vh): (f64, f64),
spacing: &PageSpacing,
place: GoToPlace,
) -> (f64, f64) {
let page = go_to.page().min(geo.layout.rects.len().saturating_sub(1));
let Some(&r) = geo.layout.rects.get(page) else { return (want.0, geo.doc.y0) };
let scale = geo.scale;
@@ -1349,7 +1416,16 @@ fn go_to_offset(go_to: GoTo, geo: &Placed, continuous: bool, want: (f64, f64), (
GoTo::Top { fraction, .. } => {
let (top, bottom) = geo.layout.bands[page];
let start = r.y0 + top;
let y = if fraction > 0.0 {
// Production's place: the page gap above the page's top, or above the fraction of it, never above the
// document's start; out of a continuous view a page's own top is the top of its row.
let gap_above = match place {
GoToPlace::GapAbove if continuous || fraction > 0.0 => Some((start + fraction * (bottom - top) - spacing.gap).max(geo.doc.y0)),
GoToPlace::GapAbove => Some(geo.doc.y0),
_ => None,
};
let y = if let Some(y) = gap_above {
y
} else if fraction > 0.0 {
start + fraction * (bottom - top)
} else if continuous {
geo.layout.rects.iter().zip(&geo.layout.bands).map(|(r, band)| r.y0 + band.1).filter(|&end| end <= start).fold(geo.doc.y0, f64::max)
@@ -1382,6 +1458,48 @@ fn go_to_offset(go_to: GoTo, geo: &Placed, continuous: bool, want: (f64, f64), (
}
}
/// The scale that fits the spread `spread` (its one or two pages' sizes) into `viewport` by `fit`, as full screen fits
/// the spread it shows: two pages side by side by their own widths, a page alone keeping a spread's room (a cover keeps
/// its blank slot); `None` for no fit or no pages.
fn spread_fit_scale(spread: &[Size], viewport: Size, fit: Fit, options: &LayoutOptions, spacing: &PageSpacing) -> Option<f64> {
let dims = |s: &Size| {
(if s.width.is_finite() && s.width > 0.0 { s.width } else { 1.0 }, if s.height.is_finite() && s.height > 0.0 { s.height } else { 1.0 })
};
let width = match spread {
[a, b] => dims(a).0 + dims(b).0,
_ => spread.iter().map(|s| dims(s).0).fold(0.0, f64::max) * 2.0,
};
let height = spread.iter().map(|s| dims(s).1).fold(0.0, f64::max);
if fit == Fit::None || spread.is_empty() || width <= 0.0 || height <= 0.0 {
return None;
}
let extent = |v: f64| if v.is_finite() { v.max(0.0) } else { 0.0 };
let gap = if options.page_gaps { spacing.gap } else { 0.0 };
let by_width = (extent(viewport.width) - 2.0 * spacing.side - gap).max(1.0) / (width + 2.0 * options.markup.max(0.0));
let by_height = (extent(viewport.height) - 2.0 * spacing.margin).max(1.0) / height;
let scale = match fit {
Fit::Width => by_width,
Fit::Height => by_height,
_ => by_width.min(by_height),
};
scale.is_finite().then(|| scale.clamp(ZOOM_RANGE.start() * SCREEN_POINTS_PER_POINT, ZOOM_RANGE.end() * SCREEN_POINTS_PER_POINT))
}
/// Centres the pages of `row` across a view `width` wide and makes the layout that wide when the row fits it: the
/// content is as wide as the spread full screen shows, not as the document's widest spread, which would centre a
/// narrower spread past the screen's edge.
fn centre_row(layout: &mut PagedLayout, row: Range<usize>, width: f64) {
let Some(shown) = layout.rects.get(row.clone()).and_then(|rects| rects.iter().copied().reduce(|a, b| a.union(b))) else { return };
if !width.is_finite() || shown.width() > width {
return;
}
let dx = (width - shown.width()) / 2.0 - shown.x0;
for rect in layout.rects.get_mut(row).into_iter().flatten() {
*rect = *rect + craft_behavior::kurbo::Vec2::new(dx, 0.0);
}
layout.size.width = width;
}
/// A scroll smaller than this many screen points is the arithmetic of the view's round trip, not a scroll.
const OFFSET_EPSILON: f64 = 1e-6;
@@ -1579,6 +1697,10 @@ struct Geometry<'a> {
options: LayoutOptions,
spacing: PageSpacing,
bodies: Option<&'a [(f64, f64)]>,
/// Where Go To puts a page's top (`PageView::go_to_place`).
go_to_place: GoToPlace,
/// `PageView::fill_screen`: a spread that is not continuous fits by its own pages and stands centred.
fill_screen: bool,
/// The last layout and its inputs.
cache: RefCell<Option<(LayoutKey, Arc<Placed>)>>,
}
@@ -1618,10 +1740,20 @@ impl Geometry<'_> {
fn build(&self, state: &PageViewState) -> Placed {
let options = self.layout_options(state);
let scale = fit_scale(self.sizes, self.viewport, state.fit, &options, &self.spacing, Some(state.current))
let spread = self.fill_screen && options.display == PageDisplay::TwoUp && !options.continuous;
let row = shown_row(self.sizes.len(), state.current, &options);
let fitted = if spread {
spread_fit_scale(self.sizes.get(row.clone()).unwrap_or(&[]), self.viewport, state.fit, &options, &self.spacing)
} else {
fit_scale(self.sizes, self.viewport, state.fit, &options, &self.spacing, Some(state.current))
};
let scale = fitted
.unwrap_or(state.zoom * SCREEN_POINTS_PER_POINT)
.clamp(ZOOM_RANGE.start() * SCREEN_POINTS_PER_POINT, ZOOM_RANGE.end() * SCREEN_POINTS_PER_POINT);
let mut layout = place_pages(self.sizes, scale, self.viewport, &options, &self.spacing);
if spread {
centre_row(&mut layout, row, self.viewport.width);
}
if let Some(bodies) = self.bodies.filter(|_| state.hide_white_space) {
layout.hide_white_space(bodies, scale, self.viewport.height, &self.spacing);
}
+12
View File
@@ -92,6 +92,18 @@ impl Component for PageMetrics {
fn style(cx: &StyleCx<'_>, _: PageMetricsStyleKey) -> PageMetricsStyle {
let tokens = cx.tokens;
match cx.set() {
// With `PageInsets::Theme` (PdfCraft's page view) the creative set's spacing is the theme's page insets.
ComponentSet::Creative if cx.env::<super::PageInsets>() == super::PageInsets::Theme => {
let inset = tokens.space.inset;
PageMetricsStyle {
metrics: Metrics::new(),
gap: inset.page_gap,
margin: inset.page_margin,
side: inset.page_side,
white_space_pad: tokens.space.gap.inline,
seam: tokens.stroke.thin,
}
}
ComponentSet::Creative => PageMetricsStyle {
metrics: Metrics::new(),
gap: tokens.space.gap.section,
+136 -4
View File
@@ -1880,15 +1880,17 @@ fn the_wheel_over_the_pages_scrolls_and_reports_it_while_a_field_beside_them_has
assert!(scrolled.get(), "the view reports the reader's scroll");
}
/// A page fitted to the width stands the theme's page side margin from each edge of the view, and the first page stands
/// the theme's page margin below its top: the spacing is the theme's (production's 70 and 28 points at the comfortable
/// density), not a size derived from the tool rail.
/// With `PageInsets::Theme` (PdfCraft's page view) a page fitted to the width stands the theme's page side margin from
/// each edge of the view, and the first page stands the theme's page margin below its top: the spacing is the theme's
/// (production's 70 and 28 points at the comfortable density), not a size derived from the tool rail.
#[trial]
fn a_page_fitted_to_the_width_keeps_the_theme_side_margin() {
use craft_theme::{Contrast, Mode};
let tokens = craft_ui::testing::style::tokens(Mode::Dark, Contrast::Standard);
let (side, margin) = (f64::from(tokens.space.inset.page_side), f64::from(tokens.space.inset.page_margin));
let view = rig(3, state_with(|s| s.fit = Fit::Width));
let mut view = rig_over(DemoPages::new(3), state_with(|s| s.fit = Fit::Width), NavigationOptions::default(), false);
view.fixture.root_value(craft_pages::ui::PageInsets::Theme);
view.fixture.settle();
let out = view.out.borrow();
let page = out.rects[0];
let left = page.x0 - out.offset.0;
@@ -2550,3 +2552,133 @@ fn page_view_forwards_inactive_edits_to_its_editor_canvas() {
fixture.settle();
assert!(resolve(fixture.ctx(), EditCommands::Cut).enabled, "active Cut resolves enabled on focused PageView");
}
/// The layout of three demo pages in a 900 × 600 room of the creative set, at 100 % in one column, with the app's
/// `PageInsets` when `insets` names one, and the tokens' roomy pad and theme page margin.
fn creative_layout(insets: Option<craft_pages::ui::PageInsets>) -> (PagedLayout, f64, f64) {
let seen: Rc<RefCell<(PagedLayout, f64, f64)>> = Rc::default();
let recorded = Rc::clone(&seen);
let pages = DemoPages::new(3);
let mut state = state_with(|s| {
s.fit = Fit::None;
s.zoom = 1.0;
});
let mut fixture = Fixture::new(move |ui| {
let tokens = Env::of(ui).tokens.clone();
let out = PageView::new("Pages", &pages, &mut state, PAPER).options(LayoutOptions::ACROBAT).show(ui, |_, _| ());
*recorded.borrow_mut() = (out.layout, f64::from(tokens.space.pad.roomy), f64::from(tokens.space.inset.page_margin));
});
fixture.set(ComponentSet::Creative);
if let Some(insets) = insets {
fixture.root_value(insets);
}
fixture.room(vec2(900.0, 600.0));
fixture.settle();
seen.borrow().clone()
}
/// Upstream's creative spacing by default: the first page sits the roomy pad down; with `PageInsets::Theme`
/// (PdfCraft's page view) it sits the theme's page margin down.
#[trial]
fn the_creative_page_spacing_is_upstreams_by_default_and_the_themes_insets_when_asked() {
let (standard, roomy, _) = creative_layout(None);
assert!((standard.rects[0].y0 - roomy).abs() < 0.5, "the first page sits the roomy pad ({roomy}) down: {}", standard.rects[0].y0);
let (themed, _, page_margin) = creative_layout(Some(craft_pages::ui::PageInsets::Theme));
assert!(
(themed.rects[0].y0 - page_margin).abs() < 0.5,
"the first page sits the theme's page margin ({page_margin}) down: {}",
themed.rects[0].y0
);
}
/// The view's offset after a Go To a quarter into page 2 of five, continuous at 100 %, with Go To placed as `place`
/// says; and the page's top and the layout's page gap.
fn go_to_quarter(place: craft_pages::ui::GoToPlace) -> (f64, f64, f64) {
let state = Rc::new(RefCell::new(state_with(|s| {
s.fit = Fit::None;
s.zoom = 1.0;
})));
let seen: Rc<RefCell<Option<PagedLayout>>> = Rc::default();
let (kept, recorded) = (Rc::clone(&state), Rc::clone(&seen));
let pages = DemoPages::new(5);
let mut fixture = Fixture::new(move |ui| {
let mut now = kept.borrow().clone();
let out = PageView::new("Pages", &pages, &mut now, PAPER).options(LayoutOptions::ACROBAT).go_to_place(place).show(ui, |_, _| ());
*recorded.borrow_mut() = Some(out.layout);
*kept.borrow_mut() = now;
});
fixture.room(vec2(900.0, 600.0));
fixture.settle();
state.borrow_mut().go_to_fraction(2, 0.25);
fixture.settle();
let layout = seen.borrow().clone().expect("a pass ran");
let top = layout.rects[2].y0 + 0.25 * layout.rects[2].height();
let gap = layout.rects[2].y0 - layout.rects[1].y1;
let offset = state.borrow().offset.1;
(offset, top, gap)
}
/// By default Go To puts the quarter of the page at the view's top; with `GoToPlace::GapAbove` (production's place)
/// the page gap stands above it.
#[trial]
fn go_to_puts_the_point_at_the_top_by_default_and_a_gap_above_it_when_asked() {
let (offset, top, _) = go_to_quarter(craft_pages::ui::GoToPlace::AfterRowAbove);
assert!((offset - top).abs() < 0.5, "the quarter is at the view's top: offset {offset}, point {top}");
let (offset, top, gap) = go_to_quarter(craft_pages::ui::GoToPlace::GapAbove);
assert!(gap > 0.0, "the layout keeps a page gap");
assert!((offset - (top - gap)).abs() < 0.5, "the page gap ({gap}) stands above the quarter: offset {offset}, point {top}");
}
/// The page rects of `count` demo pages shown one row at a time at Fit Page in `display`, with `fill_screen`, and the
/// view's rect.
fn filled(display: PageDisplay, count: usize, fill: bool) -> (Vec<kurbo::Rect>, (f64, f64), egui::Rect) {
let state = Rc::new(RefCell::new(state_with(|s| {
s.fit = Fit::Page;
s.continuous = false;
s.display = display;
})));
let seen = Rc::new(RefCell::new((Vec::<kurbo::Rect>::new(), egui::Rect::NOTHING)));
let (kept, recorded) = (Rc::clone(&state), Rc::clone(&seen));
let pages = DemoPages::new(count);
let mut fixture = Fixture::new(move |ui| {
let mut now = kept.borrow().clone();
let out = PageView::new("Pages", &pages, &mut now, PAPER).options(LayoutOptions::ACROBAT).fill_screen(fill).show(ui, |_, _| ());
*recorded.borrow_mut() = (out.layout.rects.clone(), out.canvas.response.rect);
*kept.borrow_mut() = now;
});
fixture.room(vec2(900.0, 600.0));
fixture.settle();
let (rects, view) = seen.borrow().clone();
let offset = state.borrow().offset;
(rects, offset, view)
}
/// By default a page shown alone at Fit Page keeps the view's margin above and below it; with `fill_screen` (full
/// screen's edge-to-edge view) it spans the view's height, its top at the view's top, centred across.
#[trial]
fn full_screen_fills_the_screen_edge_to_edge() {
let (rects, offset, view) = filled(PageDisplay::Single, 3, false);
let height = f64::from(view.height());
assert!(rects[0].height() < height - 1.0, "a margin stays above and below the page: {} in {height}", rects[0].height());
let (rects, offset_filled, view) = filled(PageDisplay::Single, 3, true);
assert!((rects[0].height() - height).abs() < 1.0, "the page spans the view's height: {} in {height}", rects[0].height());
assert!((rects[0].y0 - offset_filled.1).abs() < 1.0, "the page's top is the view's top: {} at offset {}", rects[0].y0, offset_filled.1);
let centre = offset_filled.0 + f64::from(view.width()) / 2.0;
assert!((rects[0].center().x - centre).abs() < 1.0, "the page is centred across: {} against {centre}", rects[0].center().x);
let _ = offset;
}
/// Filling the screen in Two-up one spread at a time, the spread fits by its two pages, side by side and centred.
#[trial]
fn full_screen_two_page_view_fits_the_spread_it_shows() {
let (rects, offset, view) = filled(PageDisplay::TwoUp, 4, true);
let spread = rects[0].union(rects[1]);
let (width, height) = (f64::from(view.width()), f64::from(view.height()));
assert!(spread.width() <= width + 1.0 && spread.height() <= height + 1.0, "the spread fits the view: {spread:?} in {width} × {height}");
assert!(
(spread.width() - width).abs() < 1.0 || (spread.height() - height).abs() < 1.0,
"the spread meets the view on one axis: {spread:?} in {width} × {height}"
);
let centre = offset.0 + width / 2.0;
assert!((spread.center().x - centre).abs() < 1.0, "the spread is centred across: {} against {centre}", spread.center().x);
}