mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
canvas-w4: net change of 1 commits
- canvas-w4: written by an agent
This commit is contained in:
@@ -309,7 +309,6 @@ fn artboard_tool(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
kept.note = Some(note);
|
||||
}
|
||||
}
|
||||
// P3(canvas-w4): cv.F24
|
||||
if let Some((index, _)) = named {
|
||||
boards.selected = vec![index];
|
||||
}
|
||||
|
||||
@@ -7,15 +7,20 @@
|
||||
mod stories;
|
||||
|
||||
use super::space::canvas_space;
|
||||
use crate::contract::Rename;
|
||||
use crate::contract::{Access, Common, Component, Cx, MeasureCx, Name, NoValue, Outcome, PaintUi, PartsCx, Props as _, Role};
|
||||
use crate::contract::overlay::{Dismiss, Outside};
|
||||
use crate::contract::{
|
||||
Access, Bind, Common, Component, Cx, MeasureCx, Name, NoValue, Outcome, Overlay, OverlayUi, Overlays as _, PaintUi, PartsCx, Pointer as _,
|
||||
Props as _, Rename, Role,
|
||||
};
|
||||
use crate::primitives::{Checker, CheckerArea, PaintShape, PaintSlot, Rule, StrokeWidth, TextRun, paint_surround};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::runtime::focus::{FocusPolicy, Initial};
|
||||
use crate::runtime::memory::Remember as _;
|
||||
use crate::style::{
|
||||
ComponentStyle, DataColor, RuleLook, RuleWidth, StyleColor, StyleCx, SurfaceEdge, SurfaceKind, SurfaceStyle, SurroundLook, TypeRole,
|
||||
};
|
||||
use craft_behavior::kurbo::{Point, Rect as DocRect};
|
||||
use craft_behavior::{CanvasView, EditEvent, HitLevel, HitPriority, HitShape, Metrics, PartKey, Sense};
|
||||
use craft_behavior::{CancelReason, CanvasView, EditEvent, GestureId, HitLevel, HitPriority, HitShape, Metrics, Mixed, PartKey, Sense};
|
||||
use craft_keymap::Mods;
|
||||
use craft_theme::Rgba;
|
||||
use egui::{Color32, CornerRadius, Pos2, Rect, Response, Vec2};
|
||||
@@ -197,7 +202,9 @@ impl Outcome for PasteboardOutput {
|
||||
/// overlays. The pages' shadows and fills paint in the canvas's picture slot, ahead of the pictures shown after it, so
|
||||
/// a picture under the controls ([`RasterDisplay::picture`](super::RasterDisplay::picture)) lies over them. The
|
||||
/// surround of plain pages paints there too; the surround between artboards (pages marked with [`PageFrame::active()`]) paints over the
|
||||
/// pictures, hiding the document outside the boards except where [`uncovered`](Self::uncovered) leaves it open. The
|
||||
/// pictures, hiding the document outside the boards except where [`uncovered`](Self::uncovered) leaves it open, unless
|
||||
/// [`shows_outside`](Self::shows_outside) puts it under them. A double press on the name of a frame that
|
||||
/// [`renames`](PageFrame::renames) opens a rename field over it, reported in [`PasteboardOutput::rename`]. The
|
||||
/// page edges and names paint in place, over the pictures. The canvas must be given a view; without one nothing is
|
||||
/// shown.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
@@ -214,10 +221,12 @@ pub struct Pasteboard<'a> {
|
||||
uncovered: &'a [DocRect],
|
||||
/// The surround paints under the pictures while the frames keep `active` edges and names.
|
||||
#[prop(flag)]
|
||||
#[allow(dead_code)] // P3(canvas-w4): cv.F24
|
||||
shows_outside: bool,
|
||||
/// The size of each page's name, measured in `parts`; zero for a page without one.
|
||||
name_sizes: Vec<Vec2>,
|
||||
/// The size of each page's name, measured in `parts`; `None` for a page without one and for one whose page is
|
||||
/// narrower than the name's readable least.
|
||||
name_sizes: Vec<Option<Vec2>>,
|
||||
/// The page whose name is being renamed, read from the rename's memory in `update`.
|
||||
renaming_at: Option<usize>,
|
||||
name_clicked: Option<(usize, Mods)>,
|
||||
rename: Option<EditEvent<Rename<usize>>>,
|
||||
context_at: Option<Point>,
|
||||
@@ -233,6 +242,7 @@ impl<'a> Pasteboard<'a> {
|
||||
uncovered: &[],
|
||||
shows_outside: false,
|
||||
name_sizes: Vec::new(),
|
||||
renaming_at: None,
|
||||
name_clicked: None,
|
||||
rename: None,
|
||||
context_at: None,
|
||||
@@ -300,15 +310,52 @@ fn name_color(style: &PasteboardStyle, active: Option<bool>) -> StyleColor {
|
||||
if active == Some(true) { style.name_active } else { style.name }
|
||||
}
|
||||
|
||||
/// The page's top-left corner on screen: the origin of its checker and the anchor of its name, whatever the view's
|
||||
/// turn or flip.
|
||||
fn page_corner(view: &CanvasView, page: &PageFrame) -> Pos2 {
|
||||
view.to_screen(Point::new(page.rect.x0.min(page.rect.x1), page.rect.y0.min(page.rect.y1)))
|
||||
}
|
||||
|
||||
/// The rect of a page's name: above its top-left corner on screen, `size` big.
|
||||
fn name_rect(view: &CanvasView, page: &PageFrame, gap: f32, size: Vec2) -> Rect {
|
||||
let corner = view.to_screen(Point::new(page.rect.x0.min(page.rect.x1), page.rect.y0.min(page.rect.y1)));
|
||||
let corner = page_corner(view, page);
|
||||
Rect::from_min_size(Pos2::new(corner.x, corner.y - gap - size.y), size)
|
||||
}
|
||||
|
||||
/// The rename of a page's name: the page, its gesture, its name when it began and the text being typed. Kept in the
|
||||
/// session so the draft lasts while the field shows.
|
||||
#[derive(Clone, Default)]
|
||||
struct Renaming {
|
||||
open: Option<RenameOpen>,
|
||||
draft: String,
|
||||
}
|
||||
|
||||
/// The fixed part of an open rename.
|
||||
#[derive(Clone)]
|
||||
struct RenameOpen {
|
||||
at: usize,
|
||||
gesture: GestureId,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl RenameOpen {
|
||||
fn before(&self) -> Mixed<Rename<usize>> {
|
||||
Mixed::Same(Rename::new(self.at, self.name.clone()))
|
||||
}
|
||||
|
||||
/// The end of the rename by `typed`: a commit of a changed, non-empty name, else a cancel for `reason`.
|
||||
fn end(&self, typed: String, reason: CancelReason) -> EditEvent<Rename<usize>> {
|
||||
if typed != self.name && !typed.trim().is_empty() {
|
||||
EditEvent::commit(self.gesture, self.before(), Rename::new(self.at, typed))
|
||||
} else {
|
||||
EditEvent::cancel(self.gesture, reason, Some(self.before()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for Pasteboard<'_> {
|
||||
const ROLE: Role = Role::GROUP;
|
||||
const OVERLAYS: bool = false;
|
||||
const OVERLAYS: bool = true;
|
||||
type Style = PasteboardStyle;
|
||||
type Output = PasteboardOutput;
|
||||
|
||||
@@ -337,7 +384,8 @@ impl Component for Pasteboard<'_> {
|
||||
Vec2::ZERO
|
||||
}
|
||||
|
||||
/// The page names: a click away from the tool. They have no keyboard path of their own; the app's artboard list
|
||||
/// The page names, each cut to its page's on-screen width and left out when the page is narrower than the name's
|
||||
/// readable least: a click away from the tool. They have no keyboard path of their own; the app's artboard list
|
||||
/// selects the same page. Then one inert part per visible pasteboard piece, so a press on the pasteboard keeps a
|
||||
/// typed draft (a zoom being typed) unapplied, and a press on a page, which the pieces leave out, applies it. The
|
||||
/// press itself still reaches the canvas's background part.
|
||||
@@ -346,13 +394,15 @@ impl Component for Pasteboard<'_> {
|
||||
self.name_sizes = self
|
||||
.pages
|
||||
.iter()
|
||||
.map(|page| page.name.as_ref().map_or(Vec2::ZERO, |name| cx.layout(&name_run(name, name_color(cx.style, page.active))).size()))
|
||||
.map(|page| {
|
||||
let run = name_run(page.name.as_ref()?, name_color(cx.style, page.active)).elide();
|
||||
let width = bounds(&space.view.quad(page.rect)).width();
|
||||
(width >= cx.least_width(&run)).then(|| cx.layout_at(&run, width).size())
|
||||
})
|
||||
.collect();
|
||||
for (at, page) in self.pages.iter().enumerate() {
|
||||
if page.name.is_none() {
|
||||
continue;
|
||||
}
|
||||
cx.part(NAME.at(at as u64), name_rect(&space.view, page, cx.style.name_gap, self.name_sizes[at]))
|
||||
let Some(size) = self.name_sizes[at] else { continue };
|
||||
cx.part(NAME.at(at as u64), name_rect(&space.view, page, cx.style.name_gap, size))
|
||||
.sense(Sense::CLICK)
|
||||
.priority(HitPriority::new(HitLevel::Body, 0))
|
||||
.pointer_only("the application's artboard list selects the same page");
|
||||
@@ -368,14 +418,23 @@ impl Component for Pasteboard<'_> {
|
||||
|
||||
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
||||
let Some(space) = canvas_space(&cx.env) else { return };
|
||||
let popup = cx.popup("rename");
|
||||
for (at, page) in self.pages.iter().enumerate() {
|
||||
if page.name.is_none() {
|
||||
continue;
|
||||
let (Some(name), Some(_)) = (&page.name, self.name_sizes.get(at).copied().flatten()) else { continue };
|
||||
let part = NAME.at(at as u64);
|
||||
if cx.part_activated(part).is_some() {
|
||||
self.name_clicked = Some((at, crate::runtime::pointer::frame(&cx.core.ctx).pick_mods));
|
||||
}
|
||||
if cx.part_activated(NAME.at(at as u64)).is_some() {
|
||||
self.name_clicked = Some((at, Mods::NONE)); // P3(canvas-w4): cv.F24
|
||||
// Only the pass that receives the second press begins; a rerun pass finds the rename open.
|
||||
if page.renames && cx.keep::<Renaming>().open.is_none() && cx.part_if_declared(part).is_some_and(|result| result.press.double_pressed()) {
|
||||
let gesture = crate::runtime::edit::next_gesture_id(&cx.core.ctx);
|
||||
let open = RenameOpen { at, gesture, name: name.clone() };
|
||||
self.rename = Some(EditEvent::begin(gesture, open.before(), cx.core.label_arc(), None));
|
||||
*cx.keep::<Renaming>() = Renaming { draft: name.clone(), open: Some(open) };
|
||||
popup.open(cx);
|
||||
}
|
||||
}
|
||||
self.renaming_at = cx.keep::<Renaming>().open.as_ref().map(|open| open.at);
|
||||
let background = space.background();
|
||||
if let Some(pos) = background.context {
|
||||
let doc = space.view.to_doc(pos);
|
||||
@@ -399,7 +458,7 @@ impl Component for Pasteboard<'_> {
|
||||
// leaves it open. The page edges and names, which mark the pages, paint in place over the pictures. Outside a
|
||||
// canvas, or in a space with no slot, everything paints in place.
|
||||
let under = space.picture_slot().map(|_| PaintSlot::reserve(&edges.painter));
|
||||
let artboards = self.pages.iter().any(|page| page.active.is_some());
|
||||
let artboards = self.pages.iter().any(|page| page.active.is_some()) && !self.shows_outside;
|
||||
let polygon = |quad: &[Pos2; 4]| PaintShape::Polygon(quad.to_vec());
|
||||
// A colour the canvas sets is a plain data fill, with no pattern, as in production.
|
||||
let fill = space.pasteboard().fill();
|
||||
@@ -440,7 +499,10 @@ impl Component for Pasteboard<'_> {
|
||||
}
|
||||
match page.fill {
|
||||
Some(PageFill::Color(color)) => draw.data_fill(&polygon(&quad), color),
|
||||
Some(PageFill::Checker(_)) => todo!("cv.F24"),
|
||||
Some(PageFill::Checker(checker)) => {
|
||||
let grid = checker.shape(edges.painter.ctx(), &cx.env.tokens, CheckerArea::Quad(quad), page_corner(&view, page));
|
||||
edges.painter.add(grid);
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
@@ -460,11 +522,11 @@ impl Component for Pasteboard<'_> {
|
||||
if let Some(active) = page.active {
|
||||
edge(&quad, if active { style.active_edge } else { style.outline });
|
||||
}
|
||||
if let Some(name) = &page.name {
|
||||
let run = name_run(name, name_color(style, page.active));
|
||||
let rect = name_rect(&view, page, style.name_gap, self.name_sizes.get(at).copied().unwrap_or(Vec2::ZERO));
|
||||
if rect.intersects(view.rect) {
|
||||
draw.text(rect, &run);
|
||||
if let (Some(name), Some(size)) = (&page.name, self.name_sizes.get(at).copied().flatten()) {
|
||||
let rect = name_rect(&view, page, style.name_gap, size);
|
||||
// The field of an open rename stands in the name's place.
|
||||
if rect.intersects(view.rect) && self.renaming_at != Some(at) {
|
||||
draw.text(rect, &name_run(name, name_color(style, page.active)).elide());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -472,12 +534,67 @@ impl Component for Pasteboard<'_> {
|
||||
|
||||
fn access(&self, access: &mut Access, _: &Cx<'_, Self>) {
|
||||
for (at, page) in self.pages.iter().enumerate() {
|
||||
if let Some(name) = &page.name {
|
||||
if let (Some(name), Some(_)) = (&page.name, self.name_sizes.get(at).copied().flatten()) {
|
||||
access.node(NAME.at(at as u64), Role::BUTTON).name = Name::Text(name.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The rename field over a page's name while a rename is open. The field ends the rename: Enter or a blur commits a
|
||||
/// changed, non-empty name, Escape or an unchanged name cancels. A page that went away or whose name is no longer
|
||||
/// shown cancels it.
|
||||
fn overlay(&mut self, ui: &mut OverlayUi<'_>, cx: &mut Cx<'_, Self>) {
|
||||
let popup = cx.popup("rename");
|
||||
let state = cx.keep::<Renaming>().clone();
|
||||
let Some(space) = canvas_space(&cx.env) else { return };
|
||||
let field = state.open.as_ref().and_then(|open| {
|
||||
let page = self.pages.get(open.at)?;
|
||||
let size = self.name_sizes.get(open.at).copied().flatten()?;
|
||||
let rect = name_rect(&space.view, page, cx.style.name_gap, size);
|
||||
// Typing needs room beyond a name cut to its page: the field is as wide as the page.
|
||||
Some(Rect::from_min_size(rect.min, Vec2::new(rect.width().max(bounds(&space.view.quad(page.rect)).width()), rect.height())))
|
||||
});
|
||||
let layer = Overlay::popover()
|
||||
.placed(field.unwrap_or(Rect::NOTHING))
|
||||
.dismiss(Dismiss::new().outside(Outside::Keep))
|
||||
.focus(FocusPolicy::POPUP.initial(Initial::First));
|
||||
let mut draft = state.draft;
|
||||
let editor = ui.show(popup, layer, |ui, _| {
|
||||
let (open, field) = (state.open.as_ref()?, field?);
|
||||
let size = field.size();
|
||||
let at = Rect::from_min_size(ui.max_rect().min, size);
|
||||
Some(crate::text::rename_field(ui, Bind::bound(&mut draft), at, &open.name))
|
||||
});
|
||||
let Some(open) = state.open else { return };
|
||||
// `None`: the popup is not shown yet (it opens at the end of the pass that began the rename).
|
||||
let Some(editor) = editor else { return };
|
||||
let cancel = |reason| EditEvent::cancel(open.gesture, reason, Some(open.before()));
|
||||
let end = match editor {
|
||||
Some(editor) => editor
|
||||
.events
|
||||
.iter()
|
||||
.rev()
|
||||
.find_map(|event| match event {
|
||||
EditEvent::Commit { after, .. } => Some(open.end(after.clone(), CancelReason::Unchanged)),
|
||||
EditEvent::Cancel { reason, .. } => Some(cancel(*reason)),
|
||||
_ => None,
|
||||
})
|
||||
.or_else(|| editor.escaped.then(|| cancel(CancelReason::Escape)))
|
||||
.or_else(|| editor.submitted.then(|| open.end(draft.clone(), CancelReason::Unchanged)))
|
||||
.or_else(|| ui.closing(popup).map(|_| cancel(CancelReason::Dismissed))),
|
||||
// The page or its name is gone: nothing to edit.
|
||||
None => Some(cancel(CancelReason::Removed)),
|
||||
};
|
||||
match end {
|
||||
Some(end) => {
|
||||
self.rename = Some(end);
|
||||
*cx.keep::<Renaming>() = Renaming::default();
|
||||
popup.close(cx);
|
||||
}
|
||||
None => cx.keep::<Renaming>().draft = draft,
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> PasteboardOutput {
|
||||
PasteboardOutput { response: cx.response.clone(), name_clicked: self.name_clicked, rename: self.rename, context_at: self.context_at }
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ use super::{NAME, PageBorder, PageFrame, Pasteboard, PasteboardColor};
|
||||
use crate::canvas::Canvas;
|
||||
use crate::canvas::testing::canvas_story::{StoryDocument, on_canvas_view, on_canvas_with, story_view};
|
||||
use crate::contract::Show as _;
|
||||
use crate::primitives::Checker;
|
||||
use crate::style::DataColor;
|
||||
use crate::testing::{Drive, Fixture, StoryData, StoryOutput, stage, story};
|
||||
use craft_behavior::CanvasView;
|
||||
@@ -81,3 +82,60 @@ fn turned_view(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let pages: &[PageFrame] = data.value("pages", boards());
|
||||
on_canvas_view(ui, view, Some(&StoryDocument::STANDARD), |ui, at, _| Pasteboard::new("Pasteboard", pages).border(border).show_at(ui, at))
|
||||
}
|
||||
|
||||
/// Double presses the name of the first page.
|
||||
fn rename_first_name(fixture: &mut Fixture) {
|
||||
click_first_name(fixture);
|
||||
click_first_name(fixture);
|
||||
}
|
||||
|
||||
/// A page with a transparency grid beside one with a plain fill.
|
||||
#[story(Pasteboard<'static>, covers = [Default], drive = Drive::Never)]
|
||||
fn page_checker(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let view = view_values(data, 0.0);
|
||||
let border = border_of(data, PageBorder::DropShadow);
|
||||
let pages: &[PageFrame] = data.value(
|
||||
"pages",
|
||||
vec![
|
||||
PageFrame::new(DocRect::new(40.0, 40.0, 240.0, 200.0)).name("Transparent").active(true).checker(Checker::default()),
|
||||
PageFrame::new(DocRect::new(280.0, 40.0, 440.0, 160.0)).name("Filled").active(false).fill(DataColor::new(Color32::from_gray(u8::MAX))),
|
||||
],
|
||||
);
|
||||
on_canvas_view(ui, view, Some(&StoryDocument::STANDARD), |ui, at, _| Pasteboard::new("Pasteboard", pages).border(border).show_at(ui, at))
|
||||
}
|
||||
|
||||
/// Artboards whose surround lies under the pictures, so the document shows between and around them.
|
||||
#[story(Pasteboard<'static>, covers = [Default], drive = Drive::Never)]
|
||||
fn artboards_show_outside(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let view = view_values(data, 0.0);
|
||||
let border = border_of(data, PageBorder::DropShadow);
|
||||
let pages: &[PageFrame] = data.value("pages", boards());
|
||||
on_canvas_view(ui, view, Some(&StoryDocument::STANDARD), |ui, at, _| {
|
||||
Pasteboard::new("Pasteboard", pages).shows_outside(true).border(border).show_at(ui, at)
|
||||
})
|
||||
}
|
||||
|
||||
/// A narrow page and a wide one at one zoom: the narrow page's name is cut to its width and the narrowest shows none.
|
||||
#[story(Pasteboard<'static>, covers = [Default], drive = Drive::Never)]
|
||||
fn long_page_names(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let view = view_values(data, 0.0);
|
||||
let border = border_of(data, PageBorder::DropShadow);
|
||||
let pages: &[PageFrame] = data.value(
|
||||
"pages",
|
||||
vec![
|
||||
PageFrame::new(DocRect::new(40.0, 40.0, 100.0, 200.0)).name("A very long artboard name").active(true),
|
||||
PageFrame::new(DocRect::new(120.0, 40.0, 130.0, 200.0)).name("Too narrow for any name").active(false),
|
||||
PageFrame::new(DocRect::new(150.0, 40.0, 440.0, 200.0)).name("A very long artboard name").active(false),
|
||||
],
|
||||
);
|
||||
on_canvas_view(ui, view, Some(&StoryDocument::STANDARD), |ui, at, _| Pasteboard::new("Pasteboard", pages).border(border).show_at(ui, at))
|
||||
}
|
||||
|
||||
/// A double press on a page's name opens its rename field.
|
||||
#[story(Pasteboard<'static>, covers = [Default], drive = Drive::Custom(rename_first_name))]
|
||||
fn renaming_a_page_name(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let view = view_values(data, 0.0);
|
||||
let border = border_of(data, PageBorder::DropShadow);
|
||||
let pages: &[PageFrame] = data.value("pages", boards().into_iter().map(|page| page.renames(true)).collect::<Vec<_>>());
|
||||
on_canvas_view(ui, view, Some(&StoryDocument::STANDARD), |ui, at, _| Pasteboard::new("Pasteboard", pages).border(border).show_at(ui, at))
|
||||
}
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
//! a secondary click on the pasteboard does where it lands; the pasteboard is the canvas minus the pages and the
|
||||
//! uncovered rects in any view; and a press on it keeps a typed zoom unapplied, as a press on the image does not.
|
||||
|
||||
use craft_behavior::CanvasView;
|
||||
use craft_behavior::kurbo::{Point, Rect, Size};
|
||||
use craft_behavior::{CanvasView, EditEvent, PixelSize};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::canvas::{Canvas, NavigatorZoomRow, PageFrame, Pasteboard};
|
||||
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
|
||||
use craft_ui::canvas::{AppPaint, Canvas, NavigatorZoomRow, PageFrame, Pasteboard, PasteboardColor, RasterDisplay};
|
||||
use craft_ui::contract::{Bind, Rename, Show as _, ShowContainer as _};
|
||||
use craft_ui::primitives::Checker;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Key, Modifiers, Pos2, pos2, vec2};
|
||||
use std::cell::Cell;
|
||||
use egui::{Color32, Key, Mesh, Modifiers, Pos2, Shape, TextureId, pos2, vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
#[trial]
|
||||
@@ -144,3 +145,174 @@ fn a_press_on_the_pasteboard_keeps_a_typed_zoom_and_one_on_the_image_applies_it(
|
||||
assert!(press_after_typing(0.0, PressOn::Image).0, "a press on the image applies the typed zoom");
|
||||
assert!(press_after_typing(0.0, PressOn::Button).0, "a press on a button elsewhere applies the typed zoom");
|
||||
}
|
||||
|
||||
const LONG_NAME: &str = "A fairly long artboard name";
|
||||
|
||||
/// The painted name `LONG_NAME` of a page 400 document units wide at `zoom`, with the page's on-screen width.
|
||||
fn painted_name_at(zoom: f64) -> (f32, Option<egui::Rect>, bool) {
|
||||
let pages = [PageFrame::new(Rect::new(0.0, 0.0, 400.0, 100.0)).name(LONG_NAME)];
|
||||
let width = Rc::new(Cell::new(0.0_f32));
|
||||
let seen = Rc::clone(&width);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let view = CanvasView::new(ui.available_rect_before_wrap(), Point::new(200.0, 50.0), zoom);
|
||||
seen.set(view.to_screen(Point::new(400.0, 0.0)).x - view.to_screen(Point::ZERO).x);
|
||||
let _ = Canvas::new("Document").view(view).show(ui, |ui, body| Pasteboard::new("Pasteboard", &pages).show_at(ui, body.rect));
|
||||
});
|
||||
fixture.room(vec2(900.0, 300.0)).settle();
|
||||
let name = fixture.texts().iter().find(|text| text.text == LONG_NAME);
|
||||
(width.get(), name.map(|text| text.rect), name.is_some_and(|text| text.elided))
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_long_page_name_is_cut_inside_its_pages_width_and_is_gone_below_the_shortest_cut() {
|
||||
let (wide, name, whole_elided) = painted_name_at(1.0);
|
||||
let name = name.expect("a wide page shows its name");
|
||||
assert!(!whole_elided, "a wide page shows its name whole");
|
||||
let (narrow, cut, cut_elided) = painted_name_at(0.25);
|
||||
let cut = cut.expect("a narrower page still shows a cut name");
|
||||
assert!(narrow < wide && cut_elided, "narrowing the page cuts the name");
|
||||
assert!(cut.width() <= narrow, "the cut name stays inside its page's width: {} in {narrow}", cut.width());
|
||||
assert!(cut.width() < name.width(), "the cut name is shorter than the whole one");
|
||||
let (_, gone, _) = painted_name_at(0.03);
|
||||
assert!(gone.is_none(), "below the shortest readable cut the name is left out");
|
||||
}
|
||||
|
||||
/// The pages of the rename trial: the first renames, the second does not.
|
||||
fn rename_pages() -> [PageFrame; 2] {
|
||||
[
|
||||
PageFrame::new(Rect::new(100.0, 80.0, 300.0, 220.0)).name("Artboard 1").renames(true),
|
||||
PageFrame::new(Rect::new(340.0, 80.0, 500.0, 220.0)).name("Artboard 2"),
|
||||
]
|
||||
}
|
||||
|
||||
/// Double presses the name `label`, then runs `then`; returns every rename event the pasteboard reported, in order, and
|
||||
/// how many painted texts read `label` once the rename is open.
|
||||
fn rename_events(label: &str, then: impl FnOnce(&mut Fixture)) -> (Vec<EditEvent<Rename<usize>>>, usize) {
|
||||
let pages = rename_pages();
|
||||
let log: Rc<RefCell<Vec<EditEvent<Rename<usize>>>>> = Rc::default();
|
||||
let seen = Rc::clone(&log);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let view = CanvasView::new(ui.available_rect_before_wrap(), Point::new(300.0, 150.0), 1.0);
|
||||
let shown = Canvas::new("Document").view(view).show(ui, |ui, body| Pasteboard::new("Pasteboard", &pages).show_at(ui, body.rect));
|
||||
if let Some(event) = shown.inner.and_then(|out| out.rename) {
|
||||
seen.borrow_mut().push(event);
|
||||
}
|
||||
});
|
||||
fixture.room(vec2(700.0, 400.0)).settle();
|
||||
let name = fixture.by_label(label).expect("the page's name");
|
||||
fixture.click(&name).click(&name).settle();
|
||||
let painted = fixture.texts().iter().filter(|text| text.text == label).count();
|
||||
then(&mut fixture);
|
||||
fixture.settle();
|
||||
let events = log.borrow().clone();
|
||||
(events, painted)
|
||||
}
|
||||
|
||||
/// The rename a `Commit` carries.
|
||||
fn commit_of(event: &EditEvent<Rename<usize>>) -> Option<&Rename<usize>> {
|
||||
match event {
|
||||
EditEvent::Commit { after, .. } => Some(after),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Types `text` over the open rename field.
|
||||
fn type_over(fixture: &mut Fixture, text: &str) {
|
||||
fixture.key(Key::A, Modifiers::COMMAND).type_text(text);
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_double_press_on_a_renaming_name_begins_a_rename_that_commits_once_or_cancels_and_a_locked_frame_does_not_begin() {
|
||||
let begins = |events: &[EditEvent<Rename<usize>>]| events.iter().filter(|event| matches!(event, EditEvent::Begin { .. })).count();
|
||||
let (committed, painted) = rename_events("Artboard 1", |fixture| {
|
||||
type_over(fixture, "Renamed");
|
||||
fixture.key(Key::Enter, Modifiers::NONE);
|
||||
});
|
||||
assert_eq!(begins(&committed), 1, "a double press begins one rename: {committed:?}");
|
||||
assert!(painted <= 1, "the name is not painted beside the open field: {painted} runs read it");
|
||||
let commits: Vec<_> = committed.iter().filter_map(commit_of).collect();
|
||||
assert_eq!(commits.len(), 1, "Enter commits once: {committed:?}");
|
||||
assert_eq!((commits[0].key, commits[0].name.as_str()), (0, "Renamed"), "the commit carries the page and the typed name");
|
||||
|
||||
let (escaped, _) = rename_events("Artboard 1", |fixture| {
|
||||
type_over(fixture, "Renamed");
|
||||
fixture.key(Key::Escape, Modifiers::NONE);
|
||||
});
|
||||
assert!(escaped.iter().any(|event| matches!(event, EditEvent::Cancel { .. })), "Escape cancels: {escaped:?}");
|
||||
assert!(escaped.iter().all(|event| commit_of(event).is_none()), "Escape commits nothing: {escaped:?}");
|
||||
|
||||
let (unchanged, _) = rename_events("Artboard 1", |fixture| {
|
||||
fixture.key(Key::Enter, Modifiers::NONE);
|
||||
});
|
||||
assert!(unchanged.iter().any(|event| matches!(event, EditEvent::Cancel { .. })), "an unchanged name cancels: {unchanged:?}");
|
||||
assert!(unchanged.iter().all(|event| commit_of(event).is_none()), "an unchanged name commits nothing: {unchanged:?}");
|
||||
|
||||
let (locked, _) = rename_events("Artboard 2", |_| {});
|
||||
assert!(locked.is_empty(), "a frame that does not rename begins nothing: {locked:?}");
|
||||
}
|
||||
|
||||
/// The colour the pasteboard surround takes in the order trial.
|
||||
const SURROUND: Color32 = Color32::from_gray(40);
|
||||
/// The picture's own colour in the order trial.
|
||||
const PICTURE: Color32 = Color32::from_rgb(204, 128, 77);
|
||||
|
||||
/// The plain fill of a shape, if it has one.
|
||||
fn fill_of(shape: &Shape) -> Option<Color32> {
|
||||
match shape {
|
||||
Shape::Rect(rect) => Some(rect.fill),
|
||||
Shape::Path(path) if path.closed => Some(path.fill),
|
||||
Shape::Mesh(mesh) => mesh.vertices.first().map(|vertex| vertex.color),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The view and the shapes of a frame of a canvas on a dark pasteboard that shows `pages` and then a picture over the
|
||||
/// whole canvas.
|
||||
fn pasteboard_shapes(pages: Vec<PageFrame>, shows_outside: bool) -> (CanvasView, Vec<Shape>) {
|
||||
let room = vec2(400.0, 300.0);
|
||||
let view = CanvasView::new(egui::Rect::from_min_size(Pos2::ZERO, room), Point::new(200.0, 150.0), 1.0);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
Canvas::new("Document").view(view).pasteboard(PasteboardColor::DarkGray).show(ui, |ui, body| {
|
||||
let _ = Pasteboard::new("Pasteboard", &pages).shows_outside(shows_outside).show_at(ui, body.rect);
|
||||
let paint = AppPaint::new(|painter, view: CanvasView| {
|
||||
painter.painter.add(Shape::rect_filled(view.rect, 0.0, PICTURE));
|
||||
});
|
||||
RasterDisplay::painted("Picture", PixelSize::new(400, 300), paint).picture().show_at(ui, body.rect);
|
||||
});
|
||||
});
|
||||
fixture.room(room).settle();
|
||||
(view, fixture.frame_shapes())
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn with_shows_outside_a_picture_shows_between_artboards_and_a_checker_page_paints_its_grid_only_on_the_page() {
|
||||
let boards = || {
|
||||
vec![
|
||||
PageFrame::new(Rect::new(20.0, 20.0, 180.0, 280.0)).name("One").active(true),
|
||||
PageFrame::new(Rect::new(220.0, 20.0, 380.0, 280.0)).name("Two").active(false),
|
||||
]
|
||||
};
|
||||
for shows_outside in [false, true] {
|
||||
let (_, shapes) = pasteboard_shapes(boards(), shows_outside);
|
||||
let picture = shapes.iter().position(|shape| fill_of(shape) == Some(PICTURE)).expect("the picture paints");
|
||||
let surround: Vec<usize> = shapes.iter().enumerate().filter(|(_, shape)| fill_of(shape) == Some(SURROUND)).map(|(at, _)| at).collect();
|
||||
assert!(!surround.is_empty(), "shows_outside {shows_outside}: the surround paints");
|
||||
assert_eq!(
|
||||
surround.iter().all(|&at| at < picture),
|
||||
shows_outside,
|
||||
"shows_outside {shows_outside}: the surround lies under the picture only when the document shows outside the boards"
|
||||
);
|
||||
}
|
||||
let page = Rect::new(20.0, 20.0, 180.0, 280.0);
|
||||
let (view, shapes) = pasteboard_shapes(vec![PageFrame::new(page).checker(Checker::default()), PageFrame::new(Rect::new(220.0, 20.0, 380.0, 280.0))], false);
|
||||
let quad = egui::Rect::from_points(&view.quad(page)).expand(0.5);
|
||||
let grids: Vec<&Mesh> = shapes
|
||||
.iter()
|
||||
.filter_map(|shape| match shape {
|
||||
Shape::Mesh(mesh) if mesh.texture_id != TextureId::default() => Some(&**mesh),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert!(!grids.is_empty(), "the checker page paints a grid");
|
||||
assert!(grids.iter().all(|mesh| mesh.vertices.iter().all(|vertex| quad.contains(vertex.pos))), "the grid lies inside the page's quad only");
|
||||
}
|
||||
|
||||
@@ -15,6 +15,14 @@ pub struct Rename<K> {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl<K> Rename<K> {
|
||||
// PLAN: `Rename` is `#[non_exhaustive]` and had no constructor, so a control outside this crate (the pasteboard's page-name rename) could not build one.
|
||||
/// The rename of `key` to `name`.
|
||||
pub fn new(key: K, name: impl Into<String>) -> Self {
|
||||
Rename { key, name: name.into() }
|
||||
}
|
||||
}
|
||||
|
||||
/// A reorder: the items `keys` move together, and the first of them lands at position `to`.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct ReorderMove<K> {
|
||||
|
||||
@@ -94,8 +94,8 @@ fn text_part<V>() -> TextKey<V> {
|
||||
TextKey::new(TEXT)
|
||||
}
|
||||
|
||||
/// The text field every collection's `rename_editor` shows over an item's label: it selects the name on focus, so
|
||||
/// typing replaces it.
|
||||
/// The text field every collection's `rename_editor` and the canvas `Pasteboard`'s page-name rename show over a label:
|
||||
/// it selects the name on focus, so typing replaces it.
|
||||
pub fn rename_field(ui: &mut egui::Ui, draft: Bind<'_, String>, label: egui::Rect, name: &str) -> EditResponse<String> {
|
||||
use crate::contract::Show as _;
|
||||
TextField::new(name.to_owned(), draft).select_on_focus().show_at(ui, label)
|
||||
|
||||
Reference in New Issue
Block a user