mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Document dialogs show a small page picture with its pager and notes beside it (L-304)
This commit is contained in:
@@ -771,6 +771,12 @@ impl<'a> ParamDialog<'a> {
|
||||
});
|
||||
});
|
||||
}
|
||||
(Some(state), ParamLayout::Document) => {
|
||||
Stack::new().show(ui, |ui, _| {
|
||||
show_rows(ui, values);
|
||||
shown_preview = show_thumbnail_preview(ui, &preview_label, state, &mut extras, click_centres, &host_notes);
|
||||
});
|
||||
}
|
||||
(Some(state), layout) => {
|
||||
let zoom = match layout {
|
||||
ParamLayout::Document => ZoomRow::Hidden,
|
||||
@@ -796,8 +802,11 @@ impl<'a> ParamDialog<'a> {
|
||||
if let Some(BelowRows(draw)) = &below {
|
||||
draw(ui, values);
|
||||
}
|
||||
for note in &host_notes {
|
||||
Text::new(note.clone()).show(ui);
|
||||
// A document dialog's notes stand beside its small picture.
|
||||
if !(preview.is_some() && layout == ParamLayout::Document) {
|
||||
for note in &host_notes {
|
||||
Text::new(note.clone()).show(ui);
|
||||
}
|
||||
}
|
||||
*cx.memory::<Live>() = live.clone();
|
||||
latest = Some(values.clone());
|
||||
@@ -1079,6 +1088,43 @@ enum ZoomRow {
|
||||
Hidden,
|
||||
}
|
||||
|
||||
/// A document dialog's preview: a small picture of the page at the start, and beside it the pager ("Preview: page 1 of
|
||||
/// 3") with the host's `notes` under it. There is no well across the dialog and no zoom row.
|
||||
fn show_thumbnail_preview(
|
||||
ui: &mut Ui,
|
||||
label: &Label,
|
||||
state: &PreviewState,
|
||||
extras: &mut PaneExtras<'_>,
|
||||
click_centres: bool,
|
||||
notes: &[Label],
|
||||
) -> ShownPreview {
|
||||
let picture = match state {
|
||||
PreviewState::Ready(texture) => Some(texture.size),
|
||||
PreviewState::Pending(_) | PreviewState::Failed(_) => None,
|
||||
};
|
||||
let mut shown = ShownPreview::default();
|
||||
Row::new().show(ui, |ui, _| {
|
||||
let mut pane = PreviewPane::state(label.clone(), state.clone(), &PreviewView::fixed()).click_centres(click_centres).thumbnail(true);
|
||||
if let Some(tint) = extras.tint {
|
||||
pane = pane.tint(tint);
|
||||
}
|
||||
// `ParamDialog::show` leaves no overlay in a measuring run, which paints nothing.
|
||||
if let Some(overlay) = extras.overlay.take() {
|
||||
pane = pane.overlay(overlay.into_paint());
|
||||
}
|
||||
let pane = pane.show(ui);
|
||||
let mut step = None;
|
||||
Stack::new().show(ui, |ui, _| {
|
||||
step = extras.pager.as_ref().and_then(|pager| pager.clone().show_in(ui, true));
|
||||
for note in notes {
|
||||
Text::new(note.clone()).show(ui);
|
||||
}
|
||||
});
|
||||
shown = ShownPreview { clicked: pane.clicked, centre: picture.map(|size| pane.shown.centre(size)), step };
|
||||
});
|
||||
shown
|
||||
}
|
||||
|
||||
/// The pane, the zoom row where `zoom_row` puts it and the pager centred under them. Only a `Ready` picture has a zoom
|
||||
/// row above the pane, and a centre: a pending or failed preview has nothing to zoom (the row under it shows always).
|
||||
fn show_preview(
|
||||
|
||||
@@ -45,6 +45,9 @@ const ZOOM_EPSILON: f32 = 1e-4;
|
||||
/// that touches both edges can round to a hair over.
|
||||
const PAN_TOLERANCE: f32 = 0.01;
|
||||
|
||||
/// The height of a thumbnail pane in its widths: a page's proportion, near enough.
|
||||
const THUMBNAIL_HEIGHT_IN_WIDTHS: f32 = 4.0 / 3.0;
|
||||
|
||||
/// The share of the well an arrow key pans the view by.
|
||||
const PAN_SHARE: f32 = 0.1;
|
||||
|
||||
@@ -57,6 +60,9 @@ craft_ui::words! {
|
||||
rendering: "preview.rendering" = "Rendering preview";
|
||||
page_of: template "preview.pageOf" = "Page {p} of {n}";
|
||||
sheet_of: template "preview.sheetOf" = "Sheet {p} of {n}";
|
||||
/// The caption of the pager beside a document dialog's small preview picture.
|
||||
preview_page_of: template "preview.previewPageOf" = "Preview: page {p} of {n}";
|
||||
preview_sheet_of: template "preview.previewSheetOf" = "Preview: sheet {p} of {n}";
|
||||
no_pages: "preview.noPages" = "No pages";
|
||||
no_sheets: "preview.noSheets" = "No sheets";
|
||||
previous: "preview.previous" = "Previous";
|
||||
@@ -275,6 +281,10 @@ pub struct PreviewPane<'a> {
|
||||
/// one its container gives, at least the plot height.
|
||||
#[prop(quantity = "aspect ratio")]
|
||||
aspect: Option<f32>,
|
||||
/// A small picture of the first page beside its caption, as a document dialog shows it, not a well across the
|
||||
/// dialog: the pane is a fixed sample-sized box and the view is fitted.
|
||||
#[prop]
|
||||
thumbnail: bool,
|
||||
/// The view after this pass, when the user changed it.
|
||||
moved: Option<PreviewView>,
|
||||
/// The well took the second press of a double click this pass.
|
||||
@@ -312,6 +322,7 @@ impl PreviewPane<'_> {
|
||||
view: view.clone(),
|
||||
click_centres: true,
|
||||
aspect: None,
|
||||
thumbnail: false,
|
||||
moved: None,
|
||||
double_clicked: false,
|
||||
clicked: None,
|
||||
@@ -452,6 +463,10 @@ impl Component for PreviewPane<'_> {
|
||||
}
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
if self.thumbnail {
|
||||
let width = cx.tokens.size.thumb.sample.lg;
|
||||
return Vec2::new(width, width * THUMBNAIL_HEIGHT_IN_WIDTHS);
|
||||
}
|
||||
let width = cx.picture_width(cx.tokens.size.plot.width, f32::INFINITY);
|
||||
Vec2::new(width, self.aspect.filter(|aspect| *aspect > 0.0).map_or(0.0, |aspect| width / aspect))
|
||||
}
|
||||
@@ -739,6 +754,11 @@ pub struct PanePager {
|
||||
impl PanePager {
|
||||
/// The caption: "Page {p} of {n}" or "Sheet {p} of {n}", or "No pages" or "No sheets" when `count` is 0.
|
||||
pub fn caption(&self, env: &Env) -> Label {
|
||||
self.caption_in(env, false)
|
||||
}
|
||||
|
||||
/// [`caption`](PanePager::caption), as "Preview: page {p} of {n}" when `previewing`.
|
||||
pub fn caption_in(&self, env: &Env, previewing: bool) -> Label {
|
||||
let words = env.words::<PreviewWords>();
|
||||
if self.count == 0 {
|
||||
return match self.unit {
|
||||
@@ -751,9 +771,11 @@ impl PanePager {
|
||||
None => env.format().integer(self.shown as u64),
|
||||
};
|
||||
let count = env.format().integer(self.count as u64);
|
||||
let template = match self.unit {
|
||||
PagerUnit::Page => &words.page_of,
|
||||
PagerUnit::Sheet => &words.sheet_of,
|
||||
let template = match (self.unit, previewing) {
|
||||
(PagerUnit::Page, false) => &words.page_of,
|
||||
(PagerUnit::Sheet, false) => &words.sheet_of,
|
||||
(PagerUnit::Page, true) => &words.preview_page_of,
|
||||
(PagerUnit::Sheet, true) => &words.preview_sheet_of,
|
||||
};
|
||||
Label::from(template.fill(&[("p", &shown), ("n", &count)]))
|
||||
}
|
||||
@@ -761,9 +783,14 @@ impl PanePager {
|
||||
/// Shows the row; the step a press made in this pass. A second pass of a frame finds no press, so a step is
|
||||
/// reported once.
|
||||
pub fn show(self, ui: &mut Ui) -> Option<PagerStep> {
|
||||
self.show_in(ui, false)
|
||||
}
|
||||
|
||||
/// [`show`](PanePager::show), with the caption of [`caption_in`](PanePager::caption_in).
|
||||
pub fn show_in(self, ui: &mut Ui, previewing: bool) -> Option<PagerStep> {
|
||||
let env = Env::of(ui);
|
||||
let words = env.words::<PreviewWords>();
|
||||
let caption = self.caption(&env);
|
||||
let caption = self.caption_in(&env, previewing);
|
||||
let mut step = None;
|
||||
Row::new().show(ui, |ui, _| {
|
||||
let back = IconButton::new(IconId::new("chevron_left")).label(words.previous.clone()).enabled(self.back).show(ui);
|
||||
|
||||
@@ -110,3 +110,31 @@ fn an_unchanged_dialog_shows_close_until_a_field_changes() {
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("OK").is_some() && fixture.by_label("Cancel").is_some(), "after an edit: OK and Cancel");
|
||||
}
|
||||
|
||||
/// A document dialog shows its preview as production does: a small picture of the page at the start, and beside it the
|
||||
/// pager captioned "Preview: page 2 of 3", not a well across the dialog with the pager centred under it.
|
||||
#[trial]
|
||||
fn a_document_dialog_shows_a_small_picture_with_its_pager_beside_it() {
|
||||
use craft_ui::patterns::form_dialogs::{PanePager, PreviewState};
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let mut fields = DialogFields::new();
|
||||
let rows = vec![Param::number("size", "Size", 1.0, 1.0, 0)];
|
||||
let texture = egui::load::SizedTexture::new(egui::TextureId::default(), vec2(210.0, 297.0));
|
||||
let pager = PanePager { shown: 2, count: 3, back: true, next: true, ..PanePager::default() };
|
||||
|
||||
ParamDialog::new("thumb", "Add watermark", rows)
|
||||
.layout(ParamLayout::Document)
|
||||
.previews(true)
|
||||
.preview(PreviewState::Ready(texture))
|
||||
.preview_pager(pager)
|
||||
|
||||
.values(Bind::bound(&mut fields))
|
||||
.show(ui);
|
||||
});
|
||||
fixture.room(vec2(900.0, 700.0));
|
||||
fixture.settle();
|
||||
let pane = fixture.by_label("Preview").expect("the pane is shown").rect;
|
||||
assert!(pane.width() < 150.0, "the picture is small: {pane:?}");
|
||||
let caption = caption(&fixture, "Preview: page 2 of 3");
|
||||
assert!(caption.min.x >= pane.max.x, "the pager stands beside the picture: {caption:?} {pane:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user