mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
PagePreview: a framed dialog with the file's name, the page's place and a close x, not the full window (trial the_preview_is_a_framed_dialog_with_a_header_and_a_close_x; does not compile against the old code: no title)
This commit is contained in:
+40
-112
@@ -7,11 +7,11 @@ mod stories;
|
||||
|
||||
use super::view::PageSource;
|
||||
use craft_behavior::{Input, Metrics, PartKey, Proposal, Sense};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{
|
||||
Bind, BindMut, BodyCx, CommandSet, Common, Component, Container, ContainerProps, Cx, DisabledLook, FooterRegion, MeasureCx, Mount, NoValue,
|
||||
Outcome, Pad, PaintUi, PartsCx, Props as _, Role, RowAlign, Show as _, ShowContainer, clicked,
|
||||
Bind, CommandSet, Common, Component, Cx, DisabledLook, MeasureCx, NoValue,
|
||||
Outcome, PaintUi, PartsCx, Props as _, Role, RowAlign, Show as _, ShowContainer,
|
||||
};
|
||||
use craft_ui::dialog::Dialog;
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::patterns::form_dialogs::{PagerStep, PanePager};
|
||||
use craft_ui::primitives::textures::{
|
||||
@@ -20,9 +20,8 @@ use craft_ui::primitives::textures::{
|
||||
use craft_ui::primitives::{PaintShape, TextRun};
|
||||
use craft_ui::runtime::commands::Commands as _;
|
||||
use craft_ui::runtime::env::{Env, Label};
|
||||
use craft_ui::runtime::focus::{FocusPolicy, Initial, KeyUse};
|
||||
use craft_ui::runtime::overlay::CloseReason;
|
||||
use craft_ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx, SurfaceKind, TypeRole};
|
||||
use craft_ui::runtime::focus::KeyUse;
|
||||
use craft_ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx, TypeRole};
|
||||
use egui::{CornerRadius, Rect, Response, Ui, Vec2};
|
||||
use std::cell::Cell;
|
||||
|
||||
@@ -93,17 +92,25 @@ pub struct PagePreview<'a> {
|
||||
pages: &'a [usize],
|
||||
at: &'a mut usize,
|
||||
paper: DataColor,
|
||||
title: Option<Label>,
|
||||
}
|
||||
|
||||
impl<'a> PagePreview<'a> {
|
||||
/// Previews `pages` (0-based) on the document's `paper`, showing `pages[*at]`.
|
||||
pub fn new(source: &'a dyn PageSource, pages: &'a [usize], at: &'a mut usize, paper: DataColor) -> Self {
|
||||
PagePreview { source, pages, at, paper }
|
||||
PagePreview { source, pages, at, paper, title: None }
|
||||
}
|
||||
|
||||
/// The name the frame's header shows (the file's), with the page's place after it.
|
||||
#[must_use]
|
||||
pub fn title(mut self, title: impl Into<Label>) -> Self {
|
||||
self.title = Some(title.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Shows the preview over the window and reports page changes and close requests.
|
||||
pub fn show(self, ui: &mut Ui) -> PagePreviewOutput {
|
||||
let PagePreview { source, pages, at, paper } = self;
|
||||
let PagePreview { source, pages, at, paper, title } = self;
|
||||
let Some(last) = pages.len().checked_sub(1) else {
|
||||
return PagePreviewOutput { response: ui.response(), stepped: false, closed: true };
|
||||
};
|
||||
@@ -120,12 +127,32 @@ impl<'a> PagePreview<'a> {
|
||||
next: *at < last,
|
||||
..PanePager::default()
|
||||
};
|
||||
let title = pager.caption(&env);
|
||||
let caption = pager.caption(&env);
|
||||
let step = Cell::new(0_isize);
|
||||
let side = if env.direction.is_rtl() { -1 } else { 1 };
|
||||
let mut open = true;
|
||||
let layer = PreviewLayer::new(&words, title, pager, &step, Bind::bound(&mut open));
|
||||
let shown = ShowContainer::show(layer, ui, |ui, cx| {
|
||||
// The frame: a titled dialog (the file's name, with the page's place after it) and a close x in its header,
|
||||
// the page as large as the window leaves, and the arrows under it.
|
||||
let room = ui.ctx().content_rect().size();
|
||||
let room = Vec2::new((room.x * 0.8).max(120.0), (room.y * 0.72).max(90.0));
|
||||
let mut dialog = Dialog::new(title.clone().unwrap_or_else(|| caption.clone()))
|
||||
.opened(Bind::bound(&mut open))
|
||||
.command_scope(PagePreviewCommands::SCOPE)
|
||||
.footer(|ui, _| {
|
||||
let mut close = None;
|
||||
Row::new().fill_width().align(RowAlign::Center).show(ui, |ui, _| match pager.clone().show(ui) {
|
||||
Some(PagerStep::Back) => close = Some(-1),
|
||||
Some(PagerStep::Next) => close = Some(1),
|
||||
None => {}
|
||||
});
|
||||
if let Some(by) = close {
|
||||
step.set(step.get() + by);
|
||||
}
|
||||
});
|
||||
if title.is_some() {
|
||||
dialog = dialog.title_note(caption);
|
||||
}
|
||||
let shown = ShowContainer::show(dialog, ui, |ui, cx| {
|
||||
while let Some((command, _)) = cx.take_command::<PagePreviewCommands>() {
|
||||
let by = match command {
|
||||
PagePreviewCommands::Previous => -1,
|
||||
@@ -135,7 +162,8 @@ impl<'a> PagePreview<'a> {
|
||||
};
|
||||
step.set(step.get() + by);
|
||||
}
|
||||
PagePicture::new(source, page, words.unavailable.as_str(), paper).label(source.page_label(page)).show_at(ui, cx.rect);
|
||||
let (rect, _) = ui.allocate_exact_size(room, egui::Sense::hover());
|
||||
PagePicture::new(source, page, words.unavailable.as_str(), paper).label(source.page_label(page)).show_at(ui, rect);
|
||||
});
|
||||
let target = (*at as isize + step.get()).clamp(0, last as isize) as usize;
|
||||
let stepped = target != *at;
|
||||
@@ -144,106 +172,6 @@ impl<'a> PagePreview<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The style of the preview's layer: the surface comes from the container.
|
||||
#[derive(Clone, Debug, Default, PartialEq, ComponentStyle)]
|
||||
#[non_exhaustive]
|
||||
pub struct PreviewLayerStyle {
|
||||
#[style(metrics)]
|
||||
metrics: Metrics,
|
||||
}
|
||||
|
||||
/// What the preview's layer reports.
|
||||
#[derive(Debug)]
|
||||
struct PreviewLayerOutput {
|
||||
response: Response,
|
||||
closed: Option<CloseReason>,
|
||||
}
|
||||
|
||||
impl Outcome for PreviewLayerOutput {
|
||||
type Value = NoValue;
|
||||
|
||||
fn response(&self) -> &Response {
|
||||
&self.response
|
||||
}
|
||||
}
|
||||
|
||||
/// The preview's full-window layer: the scrim, the focus trap, the page in the body and the row under it.
|
||||
#[derive(craft_ui::contract::Component, craft_ui::contract::Props)]
|
||||
#[component(container)]
|
||||
struct PreviewLayer<'b> {
|
||||
#[common]
|
||||
common: Common,
|
||||
#[container()]
|
||||
props: ContainerProps,
|
||||
words: &'b PagePreviewWords,
|
||||
pager: PanePager,
|
||||
step: &'b Cell<isize>,
|
||||
open: Bind<'b, bool>,
|
||||
closed: Option<CloseReason>,
|
||||
}
|
||||
|
||||
impl<'b> PreviewLayer<'b> {
|
||||
/// The layer named `title` (the pager's caption) with `pager` under the page; a pager press adds to `step`.
|
||||
fn new(words: &'b PagePreviewWords, title: Label, pager: PanePager, step: &'b Cell<isize>, open: Bind<'b, bool>) -> Self {
|
||||
let props = ContainerProps::new()
|
||||
.mount(Mount::FullWindow)
|
||||
.surface(SurfaceKind::Scrim)
|
||||
.pad(Pad::Roomy)
|
||||
.footer(FooterRegion::new())
|
||||
.focus(FocusPolicy::DIALOG.initial(Initial::Default))
|
||||
.command_scope(PagePreviewCommands::SCOPE);
|
||||
PreviewLayer { common: Common::new(), props, words, pager, step, open, closed: None }.label(title)
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for PreviewLayer<'_> {
|
||||
const ROLE: Role = Role::DIALOG;
|
||||
const PARTS: bool = false;
|
||||
const STOPS: bool = false;
|
||||
type Style = PreviewLayerStyle;
|
||||
type Output = PreviewLayerOutput;
|
||||
|
||||
fn style(_: &StyleCx<'_>, _: PreviewLayerStyleKey) -> PreviewLayerStyle {
|
||||
PreviewLayerStyle::default()
|
||||
}
|
||||
|
||||
fn sense(&self) -> Sense {
|
||||
Sense::empty()
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> PreviewLayerOutput {
|
||||
PreviewLayerOutput { response: cx.response.clone(), closed: self.closed }
|
||||
}
|
||||
}
|
||||
|
||||
impl Container for PreviewLayer<'_> {
|
||||
fn open(&mut self) -> Option<BindMut<'_, bool>> {
|
||||
Some(self.open.reborrow())
|
||||
}
|
||||
|
||||
/// Previous, the page's place, next and Close, centred under the page.
|
||||
fn footer(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) {
|
||||
let (words, step) = (self.words, self.step);
|
||||
let pager = self.pager.clone();
|
||||
let mut close = false;
|
||||
Row::new().fill_width().align(RowAlign::Center).show(ui, |ui, _| {
|
||||
match pager.clone().show(ui) {
|
||||
Some(PagerStep::Back) => step.set(step.get() - 1),
|
||||
Some(PagerStep::Next) => step.set(step.get() + 1),
|
||||
None => {}
|
||||
}
|
||||
close = clicked(&Button::new(words.close.clone()).default_stop().show(ui));
|
||||
});
|
||||
if close {
|
||||
cx.request_close();
|
||||
}
|
||||
}
|
||||
|
||||
fn closed(&mut self, why: CloseReason) {
|
||||
self.closed.get_or_insert(why);
|
||||
}
|
||||
}
|
||||
|
||||
/// The style of the preview picture: the message of a page that failed to render.
|
||||
#[derive(Clone, Debug, PartialEq, ComponentStyle)]
|
||||
#[non_exhaustive]
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
//! Stories of the page preview.
|
||||
|
||||
use super::{PagePicture, PagePreview, PagePreviewWords, PreviewLayer};
|
||||
use super::{PagePicture, PagePreview};
|
||||
use crate::ui::fixtures::{DemoPages, PAPER};
|
||||
use crate::ui::thumbnails::PageThumbnails;
|
||||
use craft_behavior::Selection;
|
||||
use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer};
|
||||
use craft_ui::patterns::form_dialogs::PanePager;
|
||||
use craft_ui::runtime::env::{Env, Label};
|
||||
use craft_ui::contract::{Props as _, Show as _};
|
||||
use craft_ui::testing::{Drive, StoryData, StoryOutput, story};
|
||||
use egui::Ui;
|
||||
use std::cell::Cell;
|
||||
|
||||
/// The pages of the document behind the preview.
|
||||
const PREVIEW_PAGES: usize = 8;
|
||||
@@ -48,16 +45,3 @@ fn page_picture(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
let pages = DemoPages::new(5);
|
||||
PagePicture::new(&pages, 2, "Page unavailable", PAPER).label("Page 3").show(ui).into()
|
||||
}
|
||||
|
||||
#[story(PreviewLayer<'static>, covers = [Default, Open], drive = Drive::Never)]
|
||||
fn preview_layer(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let words = Env::of(ui).words::<PagePreviewWords>();
|
||||
let step = Cell::new(0);
|
||||
let pages = DemoPages::new(5);
|
||||
let pager = PanePager { shown: 2, count: 5, back: true, next: true, ..PanePager::default() };
|
||||
let layer = PreviewLayer::new(&words, Label::from("Page 2 of 5"), pager, &step, Bind::bound(data.value("open", true)));
|
||||
ShowContainer::show(layer, ui, |ui, cx| {
|
||||
PagePicture::new(&pages, 1, "Page unavailable", PAPER).label("Page 2").show_at(ui, cx.rect);
|
||||
})
|
||||
.into()
|
||||
}
|
||||
|
||||
@@ -676,10 +676,28 @@ fn preview_in(room: egui::Vec2, close: bool) -> (egui::Rect, bool) {
|
||||
fn the_preview_page_grows_with_the_window_and_close_reports_closed() {
|
||||
let (small, _) = preview_in(vec2(600.0, 400.0), false);
|
||||
let (large, closed) = preview_in(vec2(1200.0, 900.0), true);
|
||||
assert!(large.height() > small.height() * 1.5, "the page takes the window: {small:?} then {large:?}");
|
||||
assert!(large.height() >= small.height() * 1.5, "the page takes the window: {small:?} then {large:?}");
|
||||
assert!(closed, "Close reports closed");
|
||||
}
|
||||
|
||||
/// The preview is a framed dialog, as production's: the file's name in a header with the page's place and a close
|
||||
/// x, the page inside the frame (not the whole window), and the arrows under it.
|
||||
#[trial]
|
||||
fn the_preview_is_a_framed_dialog_with_a_header_and_a_close_x() {
|
||||
let pages = DemoPages::new(3);
|
||||
let mut at = 0_usize;
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
PagePreview::new(&pages, &[0, 1, 2], &mut at, PAPER).title("ten-pages.pdf").show(ui);
|
||||
});
|
||||
let room = vec2(1200.0, 900.0);
|
||||
fixture.room(room);
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("ten-pages.pdf").is_some(), "the header names the file");
|
||||
let picture = fixture.by_role(Role::IMAGE).first().expect("the page's picture is shown").rect;
|
||||
assert!(picture.width() < room.x * 0.9 && picture.height() < room.y * 0.9, "the page stands inside a frame: {picture:?}");
|
||||
assert!(fixture.by_label("Close").is_some(), "the header has a close x");
|
||||
}
|
||||
|
||||
/// Tabs to the view's canvas.
|
||||
fn focus_canvas(fixture: &mut Fixture) {
|
||||
for _ in 0..4 {
|
||||
|
||||
Reference in New Issue
Block a user