mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 05:37:04 +00:00
pdf-workflows-w3: net change of 3 commits
- pdf-workflows-w3: written from its sheet - pdf-workflows-w3: repair 1 - pdf-workflows-w3: review fixes (review-1)
This commit is contained in:
@@ -4,11 +4,18 @@
|
||||
//! and scroll before it is drawn, in the same pass. The halves and the divider are craft-ui's [`SplitViews`] with
|
||||
//! [`SplitLayout::TwoUpVertical`]: pane 0 is the left half, pane 1 the right half and main view.
|
||||
|
||||
#[cfg(feature = "screen-testing")]
|
||||
mod stories;
|
||||
|
||||
use super::view::{PageViewOutput, PageViewState};
|
||||
use craft_behavior::Proposal;
|
||||
use craft_ui::canvas::{SplitAt, SplitLayout, SplitViews};
|
||||
use craft_ui::contract::{Bind, EditResponse, Outcome, ShowContainer as _};
|
||||
use craft_ui::contract::{Bind, EditResponse, Outcome, Pad, Props as _, Show as _, ShowContainer as _};
|
||||
use craft_ui::display::{Elide, Shade, Text};
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::runtime::env::Label;
|
||||
use egui::{Rect, Response, Ui};
|
||||
use craft_ui::style::{SurfaceKind, TypeRole};
|
||||
use egui::{Rect, Response, Ui, pos2};
|
||||
|
||||
/// Which half leads.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
@@ -96,6 +103,7 @@ impl<'a> SyncedViews<'a> {
|
||||
}
|
||||
|
||||
/// Splits the room in two halves (`SplitViews`, its divider and gap) and runs `half` for each, the leader first.
|
||||
/// With [`captions`](Self::captions) each half shows its caption band at its top and `half` gets the rect under it.
|
||||
// pdfcraft: crates/ui-egui/src/workflows/compare_ui.rs:344 `compare_area`.
|
||||
pub fn halves(
|
||||
self,
|
||||
@@ -104,7 +112,6 @@ impl<'a> SyncedViews<'a> {
|
||||
mut half: impl FnMut(&mut Ui, Rect, &mut PageViewState, Lead) -> PageViewOutput,
|
||||
) -> SyncedViewsOutput {
|
||||
let SyncedViews { label, state, captions } = self;
|
||||
let _ = captions; // P3(pdf-workflows-w3): WF-W15
|
||||
let (left, right) = states;
|
||||
let lead = state.lead;
|
||||
let SyncedViewsState { at, last_offset, last_current, .. } = state;
|
||||
@@ -124,9 +131,23 @@ impl<'a> SyncedViews<'a> {
|
||||
// The follower is a copy per run, so the body writes nothing kept; `settle` stores the last copy.
|
||||
let mut followed = None;
|
||||
let rects = divider.placed(layout, 2, body.rect, body.env.direction().is_rtl());
|
||||
// The band is the framework's `Stack` (panel surface, `Pad::Control`) around a caption `Text`, so its padding
|
||||
// and line height come from the components' own styles and the pattern reads no tokens.
|
||||
// Both captions share one type role, so one measurement gives both bands' height.
|
||||
let band = captions.as_ref().map(|(left, _)| caption_band().measure_in(ui, Proposal::Ideal, |ui, _| caption(left).show(ui)).y);
|
||||
for pane in [active, 1 - active] {
|
||||
let Some(&rect) = rects.get(pane) else { continue };
|
||||
let this = if pane == 0 { Lead::Left } else { Lead::Right };
|
||||
// The band is cut inside the pane, so the divider and the active edge keep the full height.
|
||||
let rect = match (&captions, band) {
|
||||
(Some((left, right)), Some(height)) => {
|
||||
let (band, view) = cut_band(rect, height);
|
||||
let text = if this == Lead::Left { left } else { right };
|
||||
caption_band().show_at(ui, band, |ui, _| caption(text).show(ui));
|
||||
view
|
||||
}
|
||||
_ => rect,
|
||||
};
|
||||
views[pane] = Some(if this == lead {
|
||||
half(ui, rect, leader, this)
|
||||
} else {
|
||||
@@ -155,6 +176,22 @@ impl<'a> SyncedViews<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The band a caption sits in: the panel surface with control padding, as wide as its half.
|
||||
fn caption_band() -> Stack<'static> {
|
||||
Stack::new().surface(SurfaceKind::Panel).pad(Pad::Control).fill_width()
|
||||
}
|
||||
|
||||
/// A half's caption: one muted caption line, cut at the end when the half is narrow.
|
||||
fn caption(text: &Label) -> Text {
|
||||
Text::new(text.clone()).role(TypeRole::Caption).shade(Shade::Muted).elide(Elide::End)
|
||||
}
|
||||
|
||||
/// Cuts `rect` into the band of `height` at its top and the rest under it; a pane shorter than the band is all band.
|
||||
fn cut_band(rect: Rect, height: f32) -> (Rect, Rect) {
|
||||
let edge = (rect.min.y + height).min(rect.max.y);
|
||||
(Rect::from_min_max(rect.min, pos2(rect.max.x, edge)), Rect::from_min_max(pos2(rect.min.x, edge), rect.max))
|
||||
}
|
||||
|
||||
/// The follower takes the leader's zoom, layout and rotation, its scroll offset once the leader has moved since the
|
||||
/// last copy, and, when one page shows at a time, the leader's current page once it has changed (a continuous view's
|
||||
/// current page follows from the offset). Returns whether anything changed.
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
//! Stories of the synced pair: the base look and the captioned variant.
|
||||
|
||||
use super::{Lead, SyncedViews, SyncedViewsState};
|
||||
use crate::ui::fixtures::{DemoPages, PAPER};
|
||||
use crate::ui::view::{PageView, PageViewState};
|
||||
use craft_ui::testing::{Drive, StoryData, StoryOutput, story};
|
||||
use egui::Ui;
|
||||
|
||||
/// Pages in each half's document: enough to scroll.
|
||||
const PAGES: usize = 4;
|
||||
|
||||
fn pair(ui: &mut Ui, data: &mut StoryData, captions: Option<(&str, &str)>) -> StoryOutput {
|
||||
let (state, left, right) = data.value("pair", (SyncedViewsState::default(), PageViewState::default(), PageViewState::default()));
|
||||
let pages = DemoPages::new(PAGES);
|
||||
let mut views = SyncedViews::new("Compare", state);
|
||||
if let Some((old, new)) = captions {
|
||||
views = views.captions(old, new);
|
||||
}
|
||||
let out = views.halves(ui, (left, right), |ui, rect, state, half| {
|
||||
let label = if half == Lead::Left { "Old pages" } else { "New pages" };
|
||||
PageView::new(label, &pages, state, PAPER).show_at(ui, rect, |_, _| ())
|
||||
});
|
||||
out.edit.response.into()
|
||||
}
|
||||
|
||||
#[story(SyncedViews<'static>, covers = [], drive = Drive::Never)]
|
||||
fn plain_pair(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
pair(ui, data, None)
|
||||
}
|
||||
|
||||
#[story(SyncedViews<'static>, covers = [], drive = Drive::Never)]
|
||||
fn captioned_pair(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
pair(ui, data, Some(("Old file: report.pdf", "New file: report.pdf")))
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
//! The synced pair: the caption band over each half and the room left to its view.
|
||||
|
||||
|
||||
use craft_i18n::Direction;
|
||||
use craft_pages::ui::fixtures::{DemoPages, PAPER};
|
||||
use craft_pages::ui::{Lead, PageView, PageViewState, SyncedViews, SyncedViewsState};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Rect, vec2};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// The view rects each half's closure got, left then right.
|
||||
type Views = Rc<RefCell<[Option<Rect>; 2]>>;
|
||||
|
||||
/// A captioned pair over two demo documents in `direction`, settled.
|
||||
fn captioned_pair(direction: Direction) -> (Fixture, Views) {
|
||||
let views: Views = Rc::default();
|
||||
let seen = Rc::clone(&views);
|
||||
let pages = DemoPages::new(3);
|
||||
let mut pair = SyncedViewsState::default();
|
||||
let (mut left, mut right) = (PageViewState::default(), PageViewState::default());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut rects = [None; 2];
|
||||
SyncedViews::new("Compare", &mut pair).captions("Old", "New").halves(ui, (&mut left, &mut right), |ui, rect, state, half| {
|
||||
rects[usize::from(half == Lead::Right)] = Some(rect);
|
||||
let label = if half == Lead::Left { "Old pages" } else { "New pages" };
|
||||
PageView::new(label, &pages, state, PAPER).show_at(ui, rect, |_, _| ())
|
||||
});
|
||||
*seen.borrow_mut() = rects;
|
||||
});
|
||||
fixture.room(vec2(900.0, 600.0)).direction(direction).settle();
|
||||
(fixture, views)
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn captions_sit_over_each_half_and_the_view_takes_the_rest() {
|
||||
for direction in [Direction::Ltr, Direction::Rtl] {
|
||||
let rtl = matches!(direction, Direction::Rtl);
|
||||
let (fixture, views) = captioned_pair(direction);
|
||||
let room = fixture.shown();
|
||||
let shown = *views.borrow();
|
||||
let (Some(old_view), Some(new_view)) = (shown[0], shown[1]) else { panic!("both halves drew ({direction:?})") };
|
||||
let old = fixture.by_label("Old").expect("the left half's caption").rect;
|
||||
let new = fixture.by_label("New").expect("the right half's caption").rect;
|
||||
|
||||
// The left half reads first: on the left, or on the right in a right-to-left interface.
|
||||
let ((first, first_view), (second, second_view)) = if rtl { ((new, new_view), (old, old_view)) } else { ((old, old_view), (new, new_view)) };
|
||||
assert!(first_view.max.x < second_view.min.x, "the divider lies between the views ({direction:?}): {first_view:?} {second_view:?}");
|
||||
assert!(first.max.x <= second_view.min.x && second.min.x >= first_view.max.x, "the captions lie either side of the divider ({direction:?})");
|
||||
|
||||
for (name, caption, view) in [("Old", old, old_view), ("New", new, new_view)] {
|
||||
assert!(caption.min.x >= view.min.x - 0.5 && caption.max.x <= view.max.x + 0.5, "{name}'s caption stays over its half ({direction:?})");
|
||||
assert!(caption.min.y >= room.min.y && caption.max.y <= view.min.y, "{name}'s caption sits above its view, clear of it ({direction:?})");
|
||||
let band = view.min.y - room.min.y;
|
||||
assert!(band > caption.height(), "{name}'s band pads its caption ({direction:?}): band {band}, caption {caption:?}");
|
||||
assert!((band + view.height() - room.height()).abs() < 0.5, "{name}'s band and view share the room ({direction:?}): {view:?} in {room:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -151,3 +151,24 @@ fn a_result_of_an_older_revision_is_discarded() {
|
||||
assert!(matches!(state.into_inner(), TextureState::Ready(texture) if texture.size == size), "the revision-2 result is stored");
|
||||
assert_eq!(runs.load(Ordering::Relaxed), 3);
|
||||
}
|
||||
|
||||
/// A job that finishes with no input after it still gets a pass, so its picture is drawn without waiting for the
|
||||
/// pointer to move.
|
||||
#[trial]
|
||||
fn a_finished_render_asks_for_a_repaint() {
|
||||
let mut frames = Frames::new();
|
||||
let ctx = frames.ctx.clone();
|
||||
let (loader, jobs) = loader_with(&ctx, 1, 16);
|
||||
let runs = Arc::new(AtomicUsize::new(0));
|
||||
frame(&mut frames, || {
|
||||
loader.request(&ctx, &1, Revision::new(1), Priority::VISIBLE, || counted(&runs, SMALL));
|
||||
});
|
||||
assert!(!frames.repaint_requested(), "a waiting job asks for no pass");
|
||||
jobs.run_all();
|
||||
assert_eq!(runs.load(Ordering::Relaxed), 1, "the job ran");
|
||||
assert!(frames.repaint_requested(), "the finished job asks for a pass");
|
||||
let state = Cell::new(TextureState::Missing);
|
||||
frame(&mut frames, || state.set(loader.ask(&ctx, &1, Revision::new(1), Priority::VISIBLE).expect("settled")));
|
||||
let state = state.into_inner();
|
||||
assert!(ready(&state), "the pass it asked for shows the picture: {state:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user