mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:44:26 +00:00
AddBesideHandles stories: readout of the button pointed at and the last click
This commit is contained in:
@@ -1,14 +1,30 @@
|
||||
//! Stories of the "+" buttons: an artboard on a pasteboard with the buttons around it, one side kept clear by a
|
||||
//! neighbour's rect, and the connected-shape form.
|
||||
|
||||
use super::{AddBesideHandles, AddSide};
|
||||
use super::{AddBesideHandles, AddBesideWords, AddSide};
|
||||
use craft_behavior::CanvasView;
|
||||
use craft_behavior::kurbo::{Point, Rect as DocRect, Vec2};
|
||||
use craft_ui::canvas::{Canvas, PageFrame, Pasteboard};
|
||||
use craft_ui::contract::{Show as _, ShowContainer as _};
|
||||
use craft_ui::runtime::env::Env;
|
||||
use craft_ui::testing::{Drive, StoryData, StoryOutput, story};
|
||||
use egui::{Rect, Ui};
|
||||
|
||||
craft_ui::words! {
|
||||
/// The status line of the "+" button stories.
|
||||
#[non_exhaustive]
|
||||
pub(crate) struct AddBesideStoryWords {
|
||||
/// A button was clicked.
|
||||
clicked: template "addBesideStory.clicked" = "Clicked: {side}";
|
||||
/// A button was clicked with Alt: add with contents.
|
||||
copy: template "addBesideStory.copy" = "Clicked: {side}, with a copy";
|
||||
/// The button under the pointer or focused.
|
||||
hovered: template "addBesideStory.hovered" = "Pointing at: {side}";
|
||||
/// No button clicked so far.
|
||||
none: "addBesideStory.none" = "No button pressed";
|
||||
}
|
||||
}
|
||||
|
||||
/// The artboard the buttons surround, in document units: in the middle of the story document.
|
||||
fn artboard() -> DocRect {
|
||||
DocRect::new(220.0, 130.0, 420.0, 270.0)
|
||||
@@ -36,8 +52,21 @@ fn view(ui: &Ui, rect: Rect) -> CanvasView {
|
||||
craft_ui::testing::canvas_story::StoryDocument::STANDARD.view(rect, ui.pixels_per_point(), 100.0)
|
||||
}
|
||||
|
||||
/// Shows the artboard with `handles` over it on a canvas.
|
||||
fn on_canvas<'a>(ui: &mut Ui, rect: Rect, handles: impl Fn(Rect) -> AddBesideHandles<'a>) -> StoryOutput {
|
||||
/// The name of the button on `side`.
|
||||
fn side_name(words: &AddBesideWords, side: AddSide) -> &str {
|
||||
match side {
|
||||
AddSide::Top => words.above.as_str(),
|
||||
AddSide::Right => words.right.as_str(),
|
||||
AddSide::Bottom => words.below.as_str(),
|
||||
_ => words.left.as_str(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows the artboard with `handles` over it on a canvas. The readout names the button pointed at, else the last
|
||||
/// click.
|
||||
fn on_canvas<'a>(ui: &mut Ui, data: &mut StoryData, rect: Rect, handles: impl Fn(Rect) -> AddBesideHandles<'a>) -> StoryOutput {
|
||||
let env = Env::of(ui);
|
||||
let (words, sides) = (env.words::<AddBesideStoryWords>(), env.words::<AddBesideWords>());
|
||||
let board = [PageFrame::new(artboard()).name("Artboard 1").active(true)];
|
||||
let view = view(ui, rect);
|
||||
let shown = Canvas::new("Document").view(view).show_at(ui, rect, |ui, body| {
|
||||
@@ -45,16 +74,25 @@ fn on_canvas<'a>(ui: &mut Ui, rect: Rect, handles: impl Fn(Rect) -> AddBesideHan
|
||||
let name_clicked = Pasteboard::new("Pasteboard", &board).show_at(ui, body.rect).name_clicked.is_some();
|
||||
(handles(body.rect).show_at(ui, body.rect), name_clicked)
|
||||
});
|
||||
let last = data.note("clicked", None::<(AddSide, bool)>);
|
||||
let Some((output, name_clicked)) = shown.inner else { return StoryOutput::default() };
|
||||
if let Some(side) = output.added {
|
||||
*last = Some((side, output.copy));
|
||||
}
|
||||
let named = |template: &str, side: AddSide| craft_i18n::fmt(template, &[("side", side_name(&sides, side))]);
|
||||
let readout = match (output.hovered, *last) {
|
||||
(Some(side), _) => named(words.hovered.as_str(), side),
|
||||
(None, Some((side, copy))) => named(if copy { words.copy.as_str() } else { words.clicked.as_str() }, side),
|
||||
(None, None) => words.none.as_str().to_owned(),
|
||||
};
|
||||
// The artboard's name is a button too: its click is a click of the story.
|
||||
shown.inner.map_or_else(StoryOutput::default, |(output, name_clicked)| {
|
||||
let mut facts = StoryOutput::from(output);
|
||||
facts.clicked |= name_clicked;
|
||||
facts
|
||||
})
|
||||
let mut facts = StoryOutput::from(output);
|
||||
facts.clicked |= name_clicked;
|
||||
facts.readout(readout)
|
||||
}
|
||||
|
||||
#[story(AddBesideHandles<'static>, covers = [Default], drive = Drive::Never)]
|
||||
fn add_beside(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
fn add_beside(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let rect = craft_ui::testing::stage(ui);
|
||||
let view = view(ui, rect);
|
||||
let neighbour = Rect::from_two_pos(
|
||||
@@ -62,12 +100,12 @@ fn add_beside(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
view.to_screen(Point::new(artboard().x1 + 80.0, artboard().y1)),
|
||||
);
|
||||
let (clear, ghosts) = ([neighbour], ghosts());
|
||||
on_canvas(ui, rect, |_| AddBesideHandles::new("Add beside", artboard()).clear_of(&clear).ghosts(&ghosts))
|
||||
on_canvas(ui, data, rect, |_| AddBesideHandles::new("Add beside", artboard()).clear_of(&clear).ghosts(&ghosts))
|
||||
}
|
||||
|
||||
#[story(AddBesideHandles<'static>, covers = [Default], drive = Drive::Never)]
|
||||
fn add_beside_connecting(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
fn add_beside_connecting(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let rect = craft_ui::testing::stage(ui);
|
||||
let ghosts = ghosts();
|
||||
on_canvas(ui, rect, |_| AddBesideHandles::new("Add beside", artboard()).connecting().ghosts(&ghosts))
|
||||
on_canvas(ui, data, rect, |_| AddBesideHandles::new("Add beside", artboard()).connecting().ghosts(&ghosts))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user