mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 01:44:25 +00:00
Merge branch 'red/lag' into red/t4d
This commit is contained in:
@@ -10,7 +10,11 @@
|
||||
//! }
|
||||
//! // In the app's canvas pass, while a dropper is armed:
|
||||
//! let mut tool = DropperTool::new(|doc| composite_color(doc));
|
||||
//! tool_host.run(ui, &canvas, &mut tool);
|
||||
//! Canvas::new("Document").show(ui, |ui, body| {
|
||||
//! if let Some(canvas) = tool_pass(&body.env) {
|
||||
//! tool_host.run(ui, &canvas, &mut tool);
|
||||
//! }
|
||||
//! });
|
||||
//! if let Some(color) = tool.take() {
|
||||
//! requests.answer_eyedropper(key, color);
|
||||
//! }
|
||||
|
||||
@@ -6,7 +6,11 @@
|
||||
//! EyedropperBar::new(&mut options).show(ui);
|
||||
//! });
|
||||
//! let mut tool = EyedropperTool::new(options, |doc, size, source| composite_color(doc, size, source)).foreground(foreground);
|
||||
//! tool_host.run(ui, &canvas, &mut tool);
|
||||
//! Canvas::new("Document").show(ui, |ui, body| {
|
||||
//! if let Some(canvas) = tool_pass(&body.env) {
|
||||
//! tool_host.run(ui, &canvas, &mut tool);
|
||||
//! }
|
||||
//! });
|
||||
//! if let Some(pick) = tool.take() {
|
||||
//! *(if pick.background { &mut background } else { &mut foreground }) = pick.color;
|
||||
//! }
|
||||
|
||||
@@ -11,7 +11,7 @@ use craft_color::ui::{
|
||||
};
|
||||
use craft_keymap::Mods;
|
||||
use craft_theme::{ColorModel, HsbModel, ModelValues, Srgba};
|
||||
use craft_ui::canvas::{Canvas, CanvasTool, ToolAction, ToolContext, ToolHost, ToolInput, ToolPath, ToolPointer};
|
||||
use craft_ui::canvas::{Canvas, CanvasTool, ToolAction, ToolContext, ToolHost, ToolInput, ToolPath, ToolPointer, tool_pass};
|
||||
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
|
||||
use craft_ui::primitives::Draw;
|
||||
use craft_ui::runtime::overlay::CloseReason;
|
||||
@@ -301,10 +301,13 @@ fn color_hud_picks_at_an_injected_move() {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let view = CanvasView::new(room, Point::new(300.0, 200.0), 1.0);
|
||||
let shown = Canvas::new("Document").document(Size::new(600.0, 400.0)).view(view).show(ui, |_, _| {});
|
||||
let shown = Canvas::new("Document").document(Size::new(600.0, 400.0)).view(view).show(ui, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let _ = ToolHost::new().run(ui, &pass, &mut tool);
|
||||
}
|
||||
});
|
||||
let id = shown.output.response.id;
|
||||
shown_canvas.set(id);
|
||||
let _ = ToolHost::new().run(ui, &shown.output, &mut tool);
|
||||
let Some(at) = tool.open else { return };
|
||||
let out = ColorHud::new("Color HUD", at, Bind::given(c.get())).start(start).held(ToolHost::in_gesture(ui.ctx(), id)).show(ui);
|
||||
for event in out.events {
|
||||
|
||||
@@ -139,10 +139,18 @@ impl<'a> CropInput<'a> {
|
||||
/// let document = CropDocument::new(CropDocKey::new(1), Size::new(1200.0, 800.0), 300.0);
|
||||
/// let input = CropInput::new(&ctx, &settings, &document);
|
||||
/// let mut frame = None;
|
||||
/// let canvas = Canvas::new("Document").show(ui, |ui, body| frame = Some(crop.show_frame(ui, body.rect, &input)));
|
||||
/// let mut pass = crop.pass(input, frame);
|
||||
/// let host = ToolHost::new().run(ui, &canvas.output, &mut pass);
|
||||
/// for event in pass.finish().events {
|
||||
/// let mut pass = None;
|
||||
/// let mut host = None;
|
||||
/// let _ = Canvas::new("Document").show(ui, |ui, body| {
|
||||
/// // The canvas controls first, then the host on the canvas's pass.
|
||||
/// frame = Some(crop.show_frame(ui, body.rect, &input));
|
||||
/// if let Some(canvas) = tool_pass(&body.env) {
|
||||
/// let mut tool = crop.pass(input, frame.take());
|
||||
/// host = Some(ToolHost::new().run(ui, &canvas, &mut tool));
|
||||
/// pass = Some(tool);
|
||||
/// }
|
||||
/// });
|
||||
/// for event in pass.map(|pass| pass.finish().events).unwrap_or_default() {
|
||||
/// match event {
|
||||
/// CropEvent::Apply(commit) => { /* run the crop command, then */ crop.applied(true); }
|
||||
/// CropEvent::Settings(next) => settings = next,
|
||||
|
||||
@@ -6,13 +6,17 @@
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use craft_draw::ui::slice::{SliceCommand, SliceEvent, SliceItem, SliceOverlay, SliceTool};
|
||||
//! use craft_ui::canvas::{Canvas, ToolHost, ToolPath};
|
||||
//! use craft_ui::canvas::{Canvas, ToolHost, ToolPath, tool_pass};
|
||||
//! use craft_ui::contract::{Show as _, ShowContainer as _};
|
||||
//! # fn demo(ui: &mut egui::Ui, tool: &mut SliceTool, slices: &[SliceItem], selected: usize, locked: bool) {
|
||||
//! let shown = Canvas::new("Document").show(ui, |ui, body| {
|
||||
//! SliceOverlay::new("Slices", slices).selected_slice(selected).pick(true).locked(locked).show_at(ui, body.rect)
|
||||
//! let output = SliceOverlay::new("Slices", slices).selected_slice(selected).pick(true).locked(locked).show_at(ui, body.rect);
|
||||
//! // The host runs after the canvas controls.
|
||||
//! if let Some(pass) = tool_pass(&body.env) {
|
||||
//! let _ = ToolHost::new().run(ui, &pass, &mut *tool);
|
||||
//! }
|
||||
//! output
|
||||
//! });
|
||||
//! let _ = ToolHost::new().run(ui, &shown.output, tool);
|
||||
//! let mut events = tool.take_events();
|
||||
//! events.extend(shown.inner.map(|output| output.events).unwrap_or_default());
|
||||
//! for event in events {
|
||||
|
||||
@@ -6,7 +6,7 @@ use craft_draw::ui::{
|
||||
AnchorDelete, AnchorTool, AnchorsShown, BezierHit, BezierOverlay, BezierTool, EditPath, EditSubpath, HandleSide, HandlesShown, Knot, KnotRef,
|
||||
PathEdit, PathSelection, PathTool, SegmentMark,
|
||||
};
|
||||
use craft_ui::canvas::{Canvas, Guide, ToolHost};
|
||||
use craft_ui::canvas::{Canvas, Guide, ToolHost, tool_pass};
|
||||
use craft_ui::canvas_controls::Guides;
|
||||
use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _};
|
||||
use craft_ui::testing::{Fixture, pointer, trial};
|
||||
@@ -260,23 +260,27 @@ fn harness_with(paths: Vec<EditPath>, pans: bool) -> Harness {
|
||||
let canvas_id = ui.scope_id().with("document canvas");
|
||||
let canvas = Canvas::new("Document").id(canvas_id).view(view).precise_cursors(st.precise);
|
||||
let canvas = if st.pans { canvas.document(Size::new(4000.0, 4000.0)) } else { canvas };
|
||||
let mut reply = None;
|
||||
let canvas = canvas.show(ui, |ui, body| {
|
||||
// The guides lie under the paths: a stack position hides the levels of the ones below it.
|
||||
if !st.guides.is_empty() {
|
||||
Guides::new("Guides", canvas_id, Bind::bound(&mut st.guides)).interactive(true).show_at(ui, body.rect);
|
||||
}
|
||||
BezierOverlay::new("Paths", Bind::bound(&mut st.paths), &selection)
|
||||
let output = BezierOverlay::new("Paths", Bind::bound(&mut st.paths), &selection)
|
||||
.tool(st.tool)
|
||||
.handles(st.handles)
|
||||
.anchors(st.anchors)
|
||||
.segment_mark(st.segment_mark)
|
||||
.show_at(ui, body.rect)
|
||||
.show_at(ui, body.rect);
|
||||
if let (Some(tool), Some(pass)) = (st.canvas_tool, tool_pass(&body.env)) {
|
||||
reply = Some(ToolHost::new().run(ui, &pass, &mut AnchorTool::new(tool)));
|
||||
}
|
||||
output
|
||||
});
|
||||
if canvas.output.click_at().is_some() {
|
||||
st.tool_clicks += 1;
|
||||
}
|
||||
if let Some(tool) = st.canvas_tool {
|
||||
let reply = ToolHost::new().run(ui, &canvas.output, &mut AnchorTool::new(tool));
|
||||
if let Some(reply) = reply {
|
||||
st.tool_gestures += usize::from(reply.gesture.is_some());
|
||||
}
|
||||
if st.pans {
|
||||
|
||||
@@ -5,7 +5,7 @@ use craft_behavior::CanvasView;
|
||||
use craft_behavior::geometry::CropBox;
|
||||
use craft_behavior::kurbo::{Point, Rect, Size};
|
||||
use craft_draw::ui::crop::{CropDocKey, CropDocument, CropEvent, CropInput, CropRatio, CropSession, CropSettings, CropTool};
|
||||
use craft_ui::canvas::{Canvas, NavigationOptions, Snap, ToolHost};
|
||||
use craft_ui::canvas::{Canvas, NavigationOptions, Snap, ToolHost, tool_pass};
|
||||
use craft_ui::contract::ShowContainer as _;
|
||||
use craft_ui::runtime::access::Role;
|
||||
use craft_ui::runtime::env::{Env, Units};
|
||||
@@ -133,7 +133,6 @@ impl Rig {
|
||||
let base = CanvasView::new(room, Point::new(200.0, 150.0), 1.0);
|
||||
let view = host.view.unwrap_or(if host.turned_start { base.rotation(30.0) } else { base });
|
||||
let showing = host.shown;
|
||||
let input = CropInput::new(&host.ctx, &host.settings, &host.document);
|
||||
// Overscroll on: the rig's canvas shows the whole document at fit, where Overscroll off holds the view, and
|
||||
// image mode moves and turns the view.
|
||||
let mut navigation = NavigationOptions::default();
|
||||
@@ -145,15 +144,18 @@ impl Rig {
|
||||
// The body and the host run as an app with a split body runs them: the body's report reaches the host's
|
||||
// pass of the same frame.
|
||||
let tool = &mut host.tool;
|
||||
let mut frame = None;
|
||||
let canvas = canvas.show(ui, |ui, body| {
|
||||
if showing {
|
||||
frame = Some(tool.show_frame(ui, body.rect, &input));
|
||||
let (ctx, settings, document) = (&host.ctx, &host.settings, &host.document);
|
||||
let mut hosted = None;
|
||||
let _ = canvas.show(ui, |ui, body| {
|
||||
let input = CropInput::new(ctx, settings, document);
|
||||
let frame = showing.then(|| tool.show_frame(ui, body.rect, &input));
|
||||
if let Some(canvas) = tool_pass(&body.env) {
|
||||
let mut pass = tool.pass(input, frame);
|
||||
let response = ToolHost::new().run(ui, &canvas, &mut pass);
|
||||
hosted = Some((response, pass.finish().events));
|
||||
}
|
||||
});
|
||||
let mut pass = host.tool.pass(input, frame);
|
||||
let response = ToolHost::new().run(ui, &canvas.output, &mut pass);
|
||||
let events = pass.finish().events;
|
||||
let (response, events) = hosted.unwrap_or_default();
|
||||
host.past_edge_passes += usize::from(response.past_edge.is_some());
|
||||
host.snap_passes += usize::from(!response.snapped.is_empty());
|
||||
if let Some(view) = response.view {
|
||||
|
||||
@@ -8,7 +8,7 @@ use craft_behavior::CanvasView;
|
||||
use craft_behavior::geometry::Constraint;
|
||||
use craft_behavior::kurbo::{Point, Size};
|
||||
use craft_draw::ui::crop::{CropDocKey, PerspectiveCropEvent, PerspectiveCropTool, PerspectiveInput, grid_lines, grid_steps, is_valid};
|
||||
use craft_ui::canvas::{Canvas, ToolHost};
|
||||
use craft_ui::canvas::{Canvas, ToolHost, tool_pass};
|
||||
use craft_ui::contract::ShowContainer as _;
|
||||
use craft_ui::runtime::access::Role;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
@@ -41,11 +41,13 @@ impl Rig {
|
||||
let view = CanvasView::new(room, Point::new(200.0, 150.0), 1.0);
|
||||
let input = PerspectiveInput { document: active.get(), ..Default::default() };
|
||||
let mut pass = tool.pass(input);
|
||||
let canvas = Canvas::new("Document").document(SIZE).view(view).show(ui, |ui, body| {
|
||||
let _ = Canvas::new("Document").document(SIZE).view(view).show(ui, |ui, body| {
|
||||
let output = pass.show_quad(ui, body.rect);
|
||||
applied.set(applied.get() || output.apply);
|
||||
if let Some(canvas) = tool_pass(&body.env) {
|
||||
let _ = ToolHost::new().run(ui, &canvas, &mut pass);
|
||||
}
|
||||
});
|
||||
let _ = ToolHost::new().run(ui, &canvas.output, &mut pass);
|
||||
});
|
||||
fixture.settle();
|
||||
Rig { fixture, tool, document, apply }
|
||||
|
||||
@@ -28,7 +28,7 @@ fn segments_clip_to_the_page_and_vanish_when_the_centre_line_misses_it() {
|
||||
#[trial]
|
||||
fn the_open_transform_symmetry_takes_escape_and_every_press_on_the_canvas() {
|
||||
use craft_draw::ui::symmetry::{SymmetryHandles, SymmetryTool, SymmetryTransform};
|
||||
use craft_ui::canvas::{Canvas, ToolHost};
|
||||
use craft_ui::canvas::{Canvas, ToolHost, tool_pass};
|
||||
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
|
||||
use craft_ui::testing::Fixture;
|
||||
use std::cell::RefCell;
|
||||
@@ -47,16 +47,20 @@ fn the_open_transform_symmetry_takes_escape_and_every_press_on_the_canvas() {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let view = craft_behavior::CanvasView::new(ui.available_rect_before_wrap(), Point::new(200.0, 150.0), 1.0);
|
||||
let mut preview = shown.borrow().preview();
|
||||
let canvas = Canvas::new("Document")
|
||||
.view(view)
|
||||
.show(ui, |ui, body| SymmetryHandles::new("Symmetry", Bind::bound(&mut preview), page).session().show_at(ui, body.rect));
|
||||
let mut reply = None;
|
||||
let canvas = Canvas::new("Document").view(view).show(ui, |ui, body| {
|
||||
let out = SymmetryHandles::new("Symmetry", Bind::bound(&mut preview), page).session().show_at(ui, body.rect);
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let held = shown.borrow();
|
||||
reply = Some(ToolHost::new().run(ui, &pass, &mut SymmetryTool::new(&held, true)));
|
||||
}
|
||||
out
|
||||
});
|
||||
if let Some(out) = canvas.inner {
|
||||
out.edit.events.iter().for_each(|event| shown.borrow_mut().apply(event));
|
||||
sn.borrow_mut().discarded |= out.discard;
|
||||
}
|
||||
let held = shown.borrow();
|
||||
let reply = ToolHost::new().run(ui, &canvas.output, &mut SymmetryTool::new(&held, true));
|
||||
sn.borrow_mut().ignored_presses += usize::from(reply.ignored_press.is_some());
|
||||
sn.borrow_mut().ignored_presses += usize::from(reply.is_some_and(|reply| reply.ignored_press.is_some()));
|
||||
});
|
||||
fixture.settle().key(egui::Key::Escape, egui::Modifiers::NONE).settle();
|
||||
assert!(seen.borrow().discarded, "Escape with nothing focused discards the session");
|
||||
|
||||
@@ -559,7 +559,7 @@ fn a_move_only_overlay_takes_a_press_only_in_its_reach_and_moves_the_box() {
|
||||
#[trial]
|
||||
fn the_session_boxs_snap_lines_reach_the_tool_host() {
|
||||
use craft_draw::ui::transform::TransformTool;
|
||||
use craft_ui::canvas::ToolHost;
|
||||
use craft_ui::canvas::{ToolHost, tool_pass};
|
||||
let session = Rc::new(RefCell::new(TransformSession::<()>::new(RECT)));
|
||||
let view = Rc::new(RefCell::new(CanvasView::new(egui::Rect::NOTHING, Point::ZERO, 1.0)));
|
||||
let lines = Rc::new(RefCell::new(Vec::new()));
|
||||
@@ -574,17 +574,18 @@ fn the_session_boxs_snap_lines_reach_the_tool_host() {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let at = CanvasView::new(room, Point::new(f64::from(room.width()) / 2.0, f64::from(room.height()) / 2.0), 1.0);
|
||||
*shown_view.borrow_mut() = at;
|
||||
let mut response = None;
|
||||
let canvas = Canvas::new("Document").view(at).snap(snap.clone()).show(ui, |ui, body| {
|
||||
let session = shown_session.borrow();
|
||||
TransformOverlay::new("Free Transform", &session, &settings).show_at(ui, body.rect)
|
||||
let shown = TransformOverlay::new("Free Transform", &session, &settings).show_at(ui, body.rect);
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let mut tool = TransformTool::new(&session, shown.snapped.clone());
|
||||
response = Some(ToolHost::new().run(ui, &pass, &mut tool));
|
||||
}
|
||||
shown
|
||||
});
|
||||
let Some(shown) = canvas.inner else { return };
|
||||
let response = {
|
||||
let session = shown_session.borrow();
|
||||
let mut tool = TransformTool::new(&session, shown.snapped);
|
||||
ToolHost::new().run(ui, &canvas.output, &mut tool)
|
||||
};
|
||||
shown_lines.borrow_mut().push(response.snapped.len());
|
||||
shown_lines.borrow_mut().push(response.unwrap_or_default().snapped.len());
|
||||
let mut session = shown_session.borrow_mut();
|
||||
for edit in shown.edits {
|
||||
session.apply(edit, 0.0);
|
||||
|
||||
@@ -16,7 +16,12 @@
|
||||
//! let probe = DocSelection { doc: &doc }; // the app's SelectionProbe
|
||||
//! self.lasso.operation(bar.operation); // the options bar's mode
|
||||
//! let mut pass = self.lasso.pass(&probe);
|
||||
//! let _ = ToolHost::new().run(ui, &canvas, &mut pass);
|
||||
//! let _ = Canvas::new("Document").show(ui, |ui, body| {
|
||||
//! // After the canvas controls, before the picture.
|
||||
//! if let Some(canvas) = tool_pass(&body.env) {
|
||||
//! let _ = ToolHost::new().run(ui, &canvas, &mut pass);
|
||||
//! }
|
||||
//! });
|
||||
//! for event in self.lasso.take_events() {
|
||||
//! match event {
|
||||
//! SelectEvent::Commit { shape, operation } => app.run_select(shape, operation), // one undo step
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use craft_behavior::CanvasView;
|
||||
use craft_ui::canvas::{Canvas, ToolHost};
|
||||
use craft_ui::canvas::{Canvas, ToolHost, tool_pass};
|
||||
use craft_ui::contract::{Show as _, ShowContainer as _};
|
||||
use craft_ui::testing::{Drive, Fixture, StoryData, StoryOutput, pointer, story};
|
||||
use egui::Ui;
|
||||
@@ -115,11 +115,11 @@ fn tool_drawing_a_rectangle(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let annotations = samples();
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let view = CanvasView::new(room, Point::new(f64::from(room.width()) / 2.0, f64::from(room.height()) / 2.0), 1.0);
|
||||
let canvas = Canvas::new("Page").view(view).show(ui, |ui, cx| {
|
||||
let _ = Canvas::new("Page").view(view).show(ui, |ui, cx| {
|
||||
AnnotationOverlay::new("Annotations", &annotations, &[]).show_at(ui, cx.rect);
|
||||
if let (Some(pass), Ok(mut tool)) = (tool_pass(&cx.env), shared.0.lock()) {
|
||||
let _ = ToolHost::new().run(ui, &pass, &mut *tool);
|
||||
}
|
||||
});
|
||||
if let Ok(mut tool) = shared.0.lock() {
|
||||
let _ = ToolHost::new().run(ui, &canvas.output, &mut *tool);
|
||||
}
|
||||
StoryOutput::default()
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use craft_review::collab::{PEOPLE_COLORS, ParticipantId};
|
||||
use craft_ui::canvas::{Canvas, ToolHost};
|
||||
use craft_ui::canvas::{Canvas, ToolHost, tool_pass};
|
||||
use craft_ui::contract::ShowContainer as _;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
|
||||
@@ -49,8 +49,11 @@ fn escape_cancels_a_pending_edit_before_it_stops_following() {
|
||||
}
|
||||
let stopped = craft_review::ui::FollowFrame::new("Following", PEOPLE_COLORS[1], "Following Bo Chen")
|
||||
.show(ui, |ui, _| {
|
||||
let shown = Canvas::new("Document").view(view).show(ui, |_, _| {});
|
||||
let _ = ToolHost::new().run(ui, &shown.output, &mut Editing(edit.clone()));
|
||||
let _ = Canvas::new("Document").view(view).show(ui, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let _ = ToolHost::new().run(ui, &pass, &mut Editing(edit.clone()));
|
||||
}
|
||||
});
|
||||
})
|
||||
.output
|
||||
.stopped;
|
||||
|
||||
@@ -12,7 +12,7 @@ use craft_behavior::kurbo::Point;
|
||||
use craft_behavior::{Axis, CanvasView, ColumnKey, ColumnWidth, DisplayOrder, ItemSource, PixelSize, RulerUnits, Selection};
|
||||
use craft_host::target::ShellInfo;
|
||||
use craft_ui::canvas::{
|
||||
Canvas, Guide, GuideKey, HandTool, NavigationOptions, PixelRect, RasterDisplay, RasterSource, RulerText, Rulers, ToolHost, pan,
|
||||
Canvas, Guide, GuideKey, HandTool, NavigationOptions, PixelRect, RasterDisplay, RasterSource, RulerText, Rulers, ToolHost, pan, tool_pass,
|
||||
};
|
||||
use craft_ui::canvas_controls::Guides;
|
||||
use craft_ui::contract::ColumnSpec;
|
||||
@@ -474,12 +474,16 @@ impl Scene for CanvasViewScene {
|
||||
pan(view, vec2(3.0, 2.0));
|
||||
let text = RulerText::new(RulerUnits::pixels(), UnitKind::Pixels);
|
||||
let canvas = ui.scope_id().with("canvas scene");
|
||||
let mut hosted = None;
|
||||
let shown = Canvas::new("Document").id(canvas).view(*view).rulers(text, text, *zero).show_rulers(true).show(ui, |ui, body| {
|
||||
let _ = RasterDisplay::new("Picture", &SOURCE).show_at(ui, body.rect);
|
||||
let _ = Guides::new("Guides", canvas, Bind::bound(&mut *guides)).show_at(ui, body.rect);
|
||||
let _ = Rulers::new("Rulers", Bind::bound(&mut *zero)).show_at(ui, body.rect);
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
hosted = Some(ToolHost::new().run(ui, &pass, &mut *hand));
|
||||
}
|
||||
let _ = RasterDisplay::new("Picture", &SOURCE).picture().show_at(ui, body.rect);
|
||||
});
|
||||
if let Some(next) = ToolHost::new().run(ui, &shown.output, hand).view {
|
||||
if let Some(next) = hosted.and_then(|hosted| hosted.view).or(shown.output.view) {
|
||||
*view = next;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
//! `FullWindowCanvas`: the canvas region of a full-window dialog, and `RasterOverlay`, a further image drawn over it.
|
||||
|
||||
use super::view::FullWindowView;
|
||||
use crate::canvas::{Canvas, CanvasTool, HandTool, Momentary, NavigationOptions, PixelRect, RasterDisplay, RasterSource, ToolHost, ToolHostResponse};
|
||||
use crate::canvas::{
|
||||
Canvas, CanvasTool, HandTool, Momentary, NavigationOptions, PixelRect, RasterDisplay, RasterSource, ToolHost, ToolHostResponse, tool_pass,
|
||||
};
|
||||
use crate::contract::{Bind, NoValue, Outcome, Props as _, Show as _, ShowContainer as _};
|
||||
use crate::dialog::DialogWords;
|
||||
use crate::layout_components::Stack;
|
||||
@@ -211,11 +213,36 @@ impl<'a> FullWindowCanvas<'a> {
|
||||
// side column) sizes from its other content instead of taking the whole room the canvas is offered.
|
||||
let rect = ui.available_rect_before_wrap();
|
||||
let shown = given.settled(rect, size, ppp);
|
||||
// What the tool host did; none in a measuring run, where it does not run.
|
||||
let mut hosted = None;
|
||||
let canvas = Canvas::new(label.clone()).view(shown.view()).document(shown.size()).navigation(navigation).outer_view_commands().show_at(
|
||||
ui,
|
||||
rect,
|
||||
|ui, body| {
|
||||
let mut image = RasterDisplay::new(label.clone(), source).outlined();
|
||||
if let Some(controls) = controls.as_mut() {
|
||||
controls(ui, body.rect);
|
||||
}
|
||||
// The tool runs after the controls, which take a press first, and before the image and overlays,
|
||||
// which show what it did with this pass's input. The body runs for the measuring pass and again
|
||||
// for the real one, so the tool is reborrowed on each run.
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let key = global("craft-ui full-window canvas tools", pass.id);
|
||||
hosted = Some(with_session(&ui.ctx().clone(), key, |slot: &mut KeptTools| {
|
||||
let momentary = slot.momentary.get_or_insert_with(|| Momentary::new(navigation));
|
||||
momentary.set_options(navigation);
|
||||
let host = ToolHost::new().momentary(momentary);
|
||||
match tool.as_deref_mut() {
|
||||
Some(tool) => host.run(ui, &pass, tool),
|
||||
None => {
|
||||
let hand = slot.hand.get_or_insert_with(|| HandTool::new(navigation));
|
||||
hand.options = navigation;
|
||||
host.run(ui, &pass, hand)
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
// The image and its overlays paint in the canvas's picture slot, under the controls and the tool.
|
||||
let mut image = RasterDisplay::new(label.clone(), source).outlined().picture();
|
||||
if let Some(checker) = checkerboard {
|
||||
image = image.checkerboard(checker);
|
||||
}
|
||||
@@ -226,7 +253,7 @@ impl<'a> FullWindowCanvas<'a> {
|
||||
Some(tinted) => tinted,
|
||||
None => overlay.source,
|
||||
};
|
||||
let mut display = RasterDisplay::new(Label::Empty, source).id_salt(("overlay", n));
|
||||
let mut display = RasterDisplay::new(Label::Empty, source).id_salt(("overlay", n)).picture();
|
||||
if let Some((frame, cells, placed)) = overlay.mapping {
|
||||
display = display.mapped(frame, cells);
|
||||
if let Some(placed) = placed {
|
||||
@@ -235,30 +262,12 @@ impl<'a> FullWindowCanvas<'a> {
|
||||
}
|
||||
display.show_at(ui, body.rect);
|
||||
}
|
||||
if let Some(controls) = controls.as_mut() {
|
||||
controls(ui, body.rect);
|
||||
}
|
||||
},
|
||||
);
|
||||
let output = canvas.output;
|
||||
// The body runs for the measuring pass and again for the real one. The tool is reborrowed on each run; the
|
||||
// host delivers no input in the measuring run.
|
||||
let key = global("craft-ui full-window canvas tools", output.response.id);
|
||||
let hosted = with_session(&ui.ctx().clone(), key, |slot: &mut KeptTools| {
|
||||
let momentary = slot.momentary.get_or_insert_with(|| Momentary::new(navigation));
|
||||
momentary.set_options(navigation);
|
||||
let host = ToolHost::new().momentary(momentary);
|
||||
match tool.as_deref_mut() {
|
||||
Some(tool) => host.run(ui, &output, tool),
|
||||
None => {
|
||||
let hand = slot.hand.get_or_insert_with(|| HandTool::new(navigation));
|
||||
hand.options = navigation;
|
||||
host.run(ui, &output, hand)
|
||||
}
|
||||
}
|
||||
});
|
||||
let hosted: ToolHostResponse = hosted.unwrap_or_default();
|
||||
// The host's view is the tool's, else the canvas's own (the wheel, a middle-button pan, the clamp).
|
||||
let next = shown.took(hosted.view.unwrap_or_else(|| shown.view()));
|
||||
let next = shown.took(hosted.view.or(output.view).unwrap_or_else(|| shown.view()));
|
||||
if next != given && measure::effect(&ui.ctx().clone(), "full-window canvas view write") {
|
||||
let event = EditEvent::set(Mixed::Same(given), next, name.clone(), None);
|
||||
let _ = bind.write(cx, &event);
|
||||
|
||||
@@ -37,7 +37,7 @@ pub use words::CanvasWords;
|
||||
pub use tool::tool_probe;
|
||||
pub use tool::{CanvasTool, NoTool, ToolAccess, ToolAction, ToolAnchor, ToolClaims, ToolContext, ToolKey, ToolPointer, ViewEase};
|
||||
pub use tool_groups::{PickStep, tool_pick};
|
||||
pub use tool_host::{ContextRequest, StandInKey, ToolHost, ToolHostResponse, ToolInput, ToolReadout, ViewPan};
|
||||
pub use tool_host::{ContextRequest, StandInKey, ToolHost, ToolHostResponse, ToolInput, ToolReadout, ViewPan, tool_pass};
|
||||
|
||||
mod raster_source;
|
||||
pub use raster_source::{PixelRect, RasterSource};
|
||||
@@ -106,6 +106,7 @@ use crate::runtime::commands::{CommandSet as _, CommandState, Commands as _};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::runtime::focus::KeyUse;
|
||||
use crate::runtime::lock_keys;
|
||||
use crate::runtime::measure;
|
||||
use crate::runtime::motion::{Animate as _, spec};
|
||||
use crate::style::{ComponentStyle, StyleCx, SurfaceKind};
|
||||
use craft_behavior::geometry::crop::normalized_angle;
|
||||
@@ -481,6 +482,23 @@ impl<'a> Canvas<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The tool host's input of this pass: settled in `update`, so the body and `finish` read the same.
|
||||
fn tool_pass(&self, cx: &Cx<'_, Self>) -> ToolPass {
|
||||
let press = Self::background_press(cx).0;
|
||||
let caps_lock = lock_keys::caps_lock(&cx.response.ctx);
|
||||
let space = self.space(cx.response.id, cx.env.scale().0, BackgroundPress::of(&press, self.context_key), caps_lock);
|
||||
ToolPass {
|
||||
id: cx.response.id,
|
||||
rect: cx.response.rect,
|
||||
press,
|
||||
space,
|
||||
view: self.settled.map(|settled| settled.target).filter(|target| Some(*target) != self.given),
|
||||
snap: self.snapped(cx.env.shell().os),
|
||||
document: self.document_size(),
|
||||
navigation: self.navigation,
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows the body zoomed and moved by `transform`, so controls and members keep canvas-space geometry.
|
||||
#[must_use]
|
||||
pub fn transform(mut self, transform: CanvasTransform) -> Self {
|
||||
@@ -489,7 +507,7 @@ impl<'a> Canvas<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// What a canvas reports: its response and the press the tool gets.
|
||||
/// What a canvas reports: its response and the press the tool got.
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
pub struct CanvasOutput {
|
||||
@@ -498,25 +516,18 @@ pub struct CanvasOutput {
|
||||
/// The press on the background part: a press no canvas control claimed. A middle-button press pans the view and
|
||||
/// is never a tool press, so this is idle while one runs.
|
||||
pub press: PressState,
|
||||
/// The view on screen and the interface scale, for a canvas given a view. [`ToolHost::run`] hands it to the tool.
|
||||
/// The view on screen and the interface scale, for a canvas given a view.
|
||||
pub space: Option<CanvasSpace>,
|
||||
/// The canvas's view of this pass, when it differs from the view the app gave: this pass's rect, the first fit,
|
||||
/// the wheel, a middle-button pan, the view commands and the Overscroll clamp. A zoom that eases reports its
|
||||
/// target; the canvas shows the way there itself. [`ToolHost::run`] carries it in [`ToolHostResponse::view`]
|
||||
/// when no tool set a view, so an app with a tool host copies that one view.
|
||||
pub view: Option<CanvasView>,
|
||||
pub(crate) snap: Option<Snap>,
|
||||
pub(crate) document: Option<Size>,
|
||||
pub(crate) navigation: NavigationOptions,
|
||||
/// The pass's tool input, for the callers that still run the host after the canvas (see [`ToolHost::run`]).
|
||||
pub(crate) pass: ToolPass,
|
||||
}
|
||||
|
||||
impl CanvasOutput {
|
||||
/// `view` as the canvas keeps it: inside the document's edges with Overscroll off. The tool host passes a tool's
|
||||
/// view through it.
|
||||
pub(crate) fn clamped(&self, view: CanvasView) -> CanvasView {
|
||||
clamped(view, self.document, &self.navigation)
|
||||
}
|
||||
|
||||
/// Where a primary press on the background was released without a drag. A tool that creates geometry opens its
|
||||
/// command form at this point.
|
||||
pub fn click_at(&self) -> Option<Pos2> {
|
||||
@@ -527,6 +538,50 @@ impl CanvasOutput {
|
||||
}
|
||||
}
|
||||
|
||||
/// Transitional: a host run after the canvas reads the output's copy of the pass. Removed with the last such caller.
|
||||
impl AsRef<ToolPass> for CanvasOutput {
|
||||
fn as_ref(&self) -> &ToolPass {
|
||||
&self.pass
|
||||
}
|
||||
}
|
||||
|
||||
/// What the tool host of a canvas runs on in one pass: the canvas's id and rect, the press its background part got,
|
||||
/// its space, view, snapping and navigation. The canvas settles these before its body runs and publishes them to the
|
||||
/// body ([`tool_pass`]), so the body runs the host after its canvas controls (whose parts take a press first) and
|
||||
/// before its picture, and the picture shows what this pass's press and moves did. A measuring run publishes none:
|
||||
/// the host never runs in one.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub struct ToolPass {
|
||||
/// The canvas's id, which keys the host's state.
|
||||
pub id: egui::Id,
|
||||
/// The canvas's rect.
|
||||
pub rect: Rect,
|
||||
/// The press on the background part, as [`CanvasOutput::press`].
|
||||
pub press: PressState,
|
||||
/// The view on screen and the interface scale, for a canvas given a view. The host hands it to the tool.
|
||||
pub space: Option<CanvasSpace>,
|
||||
/// The canvas's view of this pass, when it differs from the view the app gave, as [`CanvasOutput::view`].
|
||||
pub view: Option<CanvasView>,
|
||||
pub(crate) snap: Option<Snap>,
|
||||
pub(crate) document: Option<Size>,
|
||||
pub(crate) navigation: NavigationOptions,
|
||||
}
|
||||
|
||||
impl ToolPass {
|
||||
/// `view` as the canvas keeps it: inside the document's edges with Overscroll off. The tool host passes a tool's
|
||||
/// view through it.
|
||||
pub(crate) fn clamped(&self, view: CanvasView) -> CanvasView {
|
||||
clamped(view, self.document, &self.navigation)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<ToolPass> for ToolPass {
|
||||
fn as_ref(&self) -> &ToolPass {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Outcome for CanvasOutput {
|
||||
type Value = NoValue;
|
||||
|
||||
@@ -582,11 +637,13 @@ impl Component for Canvas<'_> {
|
||||
};
|
||||
}
|
||||
|
||||
/// The canvas's [`CanvasSpace`] for its body; a canvas without a view hides an outer canvas's.
|
||||
/// The canvas's [`CanvasSpace`] and [`ToolPass`] for its body; a canvas without a view hides an outer canvas's
|
||||
/// space.
|
||||
fn content_env(&self, cx: &Cx<'_, Self>, values: &mut crate::runtime::env::AppValues) {
|
||||
let background = BackgroundPress::of(&Self::background_press(cx).0, self.context_key);
|
||||
space::publish(values, self.space(cx.response.id, cx.env.scale().0, background, lock_keys::caps_lock(&cx.response.ctx)));
|
||||
snap::publish(values, self.snapped(cx.env.shell().os));
|
||||
let pass = self.tool_pass(cx);
|
||||
space::publish(values, pass.space);
|
||||
snap::publish(values, pass.snap.clone());
|
||||
tool_host::publish(values, (!measure::active(&cx.response.ctx)).then_some(pass));
|
||||
}
|
||||
|
||||
/// The background part the active tool gets. It sits in the canvas root's space, the parent layer's. A press on it
|
||||
@@ -600,13 +657,8 @@ impl Component for Canvas<'_> {
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> CanvasOutput {
|
||||
let press = Self::background_press(&cx).0;
|
||||
let caps_lock = lock_keys::caps_lock(&cx.response.ctx);
|
||||
let space = self.space(cx.response.id, cx.env.scale().0, BackgroundPress::of(&press, self.context_key), caps_lock);
|
||||
let view = self.settled.map(|settled| settled.target).filter(|target| Some(*target) != self.given);
|
||||
let document = self.document_size();
|
||||
let snap = self.snapped(cx.env.shell().os);
|
||||
CanvasOutput { response: cx.response.clone(), press, space, view, snap, document, navigation: self.navigation }
|
||||
let pass = self.tool_pass(&cx);
|
||||
CanvasOutput { response: cx.response.clone(), press: pass.press, space: pass.space, view: pass.view, pass }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! `ToolFixture` (22.4, the tool row): drives a [`CanvasTool`] on a [`Canvas`] through press, drag, release and
|
||||
//! Escape, and checks what every tool owes the canvas.
|
||||
|
||||
use crate::canvas::{Canvas, CanvasTool, Snap, ToolHost, ToolPath};
|
||||
use crate::canvas::{Canvas, CanvasTool, Snap, ToolHost, ToolPath, tool_pass};
|
||||
use crate::contract::{BodyCx, Props as _, ShowContainer as _};
|
||||
use crate::runtime::access::Role;
|
||||
use crate::testing::{Fixture, Target, pointer};
|
||||
@@ -91,12 +91,16 @@ impl<T: CanvasTool + 'static> ToolFixture<T> {
|
||||
if let Some(snap) = &snap {
|
||||
shown_canvas = shown_canvas.snap(snap.clone());
|
||||
}
|
||||
let canvas = shown_canvas.show(ui, &mut body);
|
||||
let canvas = shown_canvas.show(ui, |ui, body_cx| {
|
||||
body(ui, body_cx);
|
||||
if let Some(pass) = tool_pass(&body_cx.env) {
|
||||
let reply = ToolHost::new().enabled(on.get()).run(ui, &pass, &mut *shown.borrow_mut());
|
||||
if let Some(phase) = reply.gesture {
|
||||
seen.borrow_mut().push(phase);
|
||||
}
|
||||
}
|
||||
});
|
||||
id.set(canvas.output.response.id);
|
||||
let reply = ToolHost::new().enabled(on.get()).run(ui, &canvas.output, &mut *shown.borrow_mut());
|
||||
if let Some(phase) = reply.gesture {
|
||||
seen.borrow_mut().push(phase);
|
||||
}
|
||||
});
|
||||
fixture.settle();
|
||||
ToolFixture { fixture, tool, label, gestures, canvas, enabled }
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
//! `ToolHost` (8.3): maps a canvas's background press, hover and keys to the calls of a [`CanvasTool`].
|
||||
//!
|
||||
//! The host runs after `Canvas::show`, with the [`CanvasOutput`] the canvas returned:
|
||||
//! The host runs in the canvas's body, on the [`ToolPass`] the canvas published there ([`tool_pass`]). Within one pass
|
||||
//! the order is input, tool, picture: the canvas settles its view and press before its body runs, the body shows its
|
||||
//! canvas controls, then runs the host, then shows the picture, so the picture shows what this pass's press and moves
|
||||
//! did without a second pass.
|
||||
//!
|
||||
//! 1. `CanvasOutput::press` is the press the arena's winner left for the background: a press on a canvas control
|
||||
//! 1. `ToolPass::press` is the press the arena's winner left for the background: a press on a canvas control
|
||||
//! part never reaches the tool. One pass carries one phase; a click inside one pass reaches the tool as `press` then
|
||||
//! `release`, and a press that moved past slop before it came up as `press`, `drag` and `release`: the tool decides
|
||||
//! a drag from whether `drag` was called.
|
||||
@@ -24,8 +27,8 @@
|
||||
//! ([`CanvasTool::autoscrolls_on_hover`]), report [`ToolHostResponse::past_edge`] and the pass's
|
||||
//! [`ToolHostResponse::scroll`]; the app moves its view by it with [`Autoscroll::scroll`].
|
||||
//!
|
||||
//! The host keeps its state under the canvas's id (`CanvasOutput::response.id`), so two canvases never share a
|
||||
//! gesture, and hands the tool the canvas's view from the output ([`ToolContext::space`]). A tool changes the view
|
||||
//! The host keeps its state under the canvas's id ([`ToolPass::id`]), so two canvases never share a
|
||||
//! gesture, and hands the tool the canvas's view from the pass ([`ToolContext::space`]). A tool changes the view
|
||||
//! with [`ToolContext::set_view`]; the host keeps it inside the document as the canvas does (Overscroll off) and
|
||||
//! reports the last one in [`ToolHostResponse::view`]. A view set with [`ToolContext::ease_view`] eases in the
|
||||
//! canvas's one zoom ease, which the canvas shows from its next pass. `run` also paints the tool's readout;
|
||||
@@ -65,12 +68,17 @@
|
||||
//!
|
||||
//! ```no_run
|
||||
//! # extern crate craft_ui_canvas as craft_ui;
|
||||
//! use craft_ui::canvas::{Canvas, CanvasTool, Momentary, ToolHost};
|
||||
//! use craft_ui::canvas::{Canvas, CanvasTool, Momentary, ToolHost, tool_pass};
|
||||
//! use craft_ui::contract::ShowContainer as _;
|
||||
//! # fn demo(ui: &mut egui::Ui, canvas: Canvas, tool: &mut dyn CanvasTool, momentary: &mut Momentary, view: &mut craft_behavior::CanvasView) {
|
||||
//! let shown = canvas.show(ui, |_, _| {});
|
||||
//! let reply = ToolHost::new().momentary(momentary).run(ui, &shown.output, tool);
|
||||
//! if let Some(next) = reply.view { *view = next; }
|
||||
//! let mut reply = None;
|
||||
//! let _ = canvas.show(ui, |ui, body| {
|
||||
//! // The canvas controls first, then the host, then the picture.
|
||||
//! if let Some(pass) = tool_pass(&body.env) {
|
||||
//! reply = Some(ToolHost::new().momentary(&mut *momentary).run(ui, &pass, &mut *tool));
|
||||
//! }
|
||||
//! });
|
||||
//! if let Some(next) = reply.and_then(|reply| reply.view) { *view = next; }
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
@@ -80,7 +88,7 @@ use super::navigate::Autoscroll;
|
||||
use super::readout::{paint_readout, shown as readouts_shown};
|
||||
use super::tool::{CanvasTool, March, ToolAccess, ToolAction, ToolClaims, ToolContext, ToolKey, ToolPointer, ToolRequests};
|
||||
use super::zoom_motion::{self, Steer};
|
||||
use super::{CanvasOutput, CanvasSpace, Snap, context_click};
|
||||
use super::{CanvasSpace, Snap, ToolPass, context_click};
|
||||
use crate::canvas_controls::paint_lines;
|
||||
use crate::canvas_controls::{SmartGuides, SmartLine};
|
||||
use crate::contract::{Component, Meta as _};
|
||||
@@ -88,7 +96,7 @@ use crate::primitives::{ContentPainterCx, Draw, DrawEnv, PaintRecord};
|
||||
use crate::runtime::access::{self, Name, Role, SubDecl, SubNode};
|
||||
use crate::runtime::commands;
|
||||
use crate::runtime::edit::next_gesture_id;
|
||||
use crate::runtime::env::{Env, Label};
|
||||
use crate::runtime::env::{AppValues, Env, EnvValue, EnvView, Label};
|
||||
use crate::runtime::memory::{global, with_session};
|
||||
use crate::runtime::overlay::{self, OpenFilter};
|
||||
use crate::runtime::pointer::{self, PointerFrame};
|
||||
@@ -244,7 +252,7 @@ pub struct ToolHostResponse {
|
||||
/// automation.
|
||||
pub readout: Option<ToolReadout>,
|
||||
/// The view a tool set with [`ToolContext::set_view`] this pass (the last one, inside the document with Overscroll
|
||||
/// off), else the canvas's own ([`CanvasOutput::view`]): the one view the app copies into its own. A view that
|
||||
/// off), else the canvas's own ([`ToolPass::view`]): the one view the app copies into its own. A view that
|
||||
/// eases is its target; the canvas shows the way there.
|
||||
pub view: Option<CanvasView>,
|
||||
/// The snap lines of this pass from `snap_rect` and `add_snapped`.
|
||||
@@ -379,7 +387,28 @@ fn keys_down(ctx: &Context, holds: &[Key]) -> Vec<Key> {
|
||||
down
|
||||
}
|
||||
|
||||
/// The on-canvas tool host. Built each pass, shown once with the canvas's output and the tool.
|
||||
/// The tool input a canvas publishes to its body.
|
||||
#[derive(Clone, PartialEq)]
|
||||
struct InToolPass(Option<ToolPass>);
|
||||
|
||||
impl EnvValue for InToolPass {
|
||||
fn root() -> Self {
|
||||
InToolPass(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Publishes the canvas's tool input of this pass (none in a measuring run) to its body.
|
||||
pub(super) fn publish(values: &mut AppValues, pass: Option<ToolPass>) {
|
||||
values.set(InToolPass(pass));
|
||||
}
|
||||
|
||||
/// The tool input of the innermost `Canvas` around the body, for [`ToolHost::run`]; `None` outside a canvas and in a
|
||||
/// measuring run, where no host runs.
|
||||
pub fn tool_pass(env: &EnvView<'_>) -> Option<ToolPass> {
|
||||
env.get::<InToolPass>().0
|
||||
}
|
||||
|
||||
/// The on-canvas tool host. Built each pass, run once in the canvas's body with the canvas's pass and the tool.
|
||||
#[derive(Default)]
|
||||
pub struct ToolHost<'m> {
|
||||
disabled: bool,
|
||||
@@ -434,16 +463,26 @@ impl<'m> ToolHost<'m> {
|
||||
|
||||
/// Delivers the pass's pointer and key events to `tool`, paints it over the canvas, and paints the readout it
|
||||
/// asked for beside the pointer.
|
||||
pub fn run(self, ui: &Ui, canvas: &CanvasOutput, tool: &mut dyn CanvasTool) -> ToolHostResponse {
|
||||
let ToolHost { disabled, mut momentary, mut command_stand_in, mut alt_stand_in, smart_guide_color } = self;
|
||||
///
|
||||
/// The host runs in the canvas's body on the [`ToolPass`] the canvas published there ([`tool_pass`]): after the
|
||||
/// body's canvas controls, so their parts take a press first and their reports of this pass reach the tool, and
|
||||
/// before the body's picture, so the picture shows what this pass's press and moves did. There is none in a
|
||||
/// measuring run, so the host and what the app does with its response never run in one.
|
||||
///
|
||||
/// Transitional: a [`CanvasOutput`](super::CanvasOutput) is accepted for the callers that still run the host
|
||||
/// after the canvas; their picture shows the tool's input a pass late. The form goes once the last caller has
|
||||
/// moved into its body. In a measuring run that form returns the canvas's own view and nothing else.
|
||||
pub fn run(self, ui: &Ui, pass: &impl AsRef<ToolPass>, tool: &mut dyn CanvasTool) -> ToolHostResponse {
|
||||
let canvas = pass.as_ref();
|
||||
let ctx = ui.ctx().clone();
|
||||
if measure::active(&ctx) {
|
||||
return ToolHostResponse { view: canvas.view, ..ToolHostResponse::default() };
|
||||
}
|
||||
let ToolHost { disabled, mut momentary, mut command_stand_in, mut alt_stand_in, smart_guide_color } = self;
|
||||
let env = Env::of(ui);
|
||||
let key = state_id(canvas.response.id);
|
||||
// A measuring run delivers no input: the tool is drawn and sized from the state the last real run left, and
|
||||
// the momentary commands and cancelled view wait for the real run.
|
||||
let measuring = measure::active(&ctx);
|
||||
let key = state_id(canvas.id);
|
||||
let mut state = with_session(&ctx, key, |slot: &mut HostState| slot.clone());
|
||||
let cancelled_view = if measuring { None } else { state.cancelled_view.take() };
|
||||
let cancelled_view = state.cancelled_view.take();
|
||||
let space = canvas.space;
|
||||
let viewport = WindowHost::of(&ctx).current();
|
||||
// The keys injected input holds with no key event are down for the pass, and run their momentary command
|
||||
@@ -451,20 +490,15 @@ impl<'m> ToolHost<'m> {
|
||||
let holds = WindowHost::of(&ctx).injected_holds(viewport);
|
||||
let down = keys_down(&ctx, &holds);
|
||||
let caps = lock_keys::caps_lock(&ctx);
|
||||
let rect = canvas.response.rect;
|
||||
let rect = canvas.rect;
|
||||
let input = pointer::frame(&ctx);
|
||||
let fired = std::iter::from_fn(|| {
|
||||
if measuring {
|
||||
return None;
|
||||
}
|
||||
commands::take_around::<MomentaryCommands>(&ctx, canvas.response.id, env.view().command_chain())
|
||||
});
|
||||
let fired = std::iter::from_fn(|| commands::take_around::<MomentaryCommands>(&ctx, canvas.id, env.view().command_chain()));
|
||||
let momentary_tool = match momentary.as_deref_mut() {
|
||||
Some(momentary) => {
|
||||
for (command, event) in fired {
|
||||
momentary.fired(command, event.phase);
|
||||
}
|
||||
let injected = held_momentary(&ctx, canvas.response.id, env.view().command_chain(), &holds, input.mods);
|
||||
let injected = held_momentary(&ctx, canvas.id, env.view().command_chain(), &holds, input.mods);
|
||||
momentary.tool_holding(injected, input.mods, state.gesture.is_some())
|
||||
}
|
||||
None => {
|
||||
@@ -476,13 +510,11 @@ impl<'m> ToolHost<'m> {
|
||||
let pass = run_as(momentary.as_deref_mut(), momentary_tool, &mut *tool);
|
||||
(pass.view_only(), pass.takes(PointerButton::Primary, input.mods))
|
||||
};
|
||||
if !measuring && view_only {
|
||||
sampler::pass_tool_gesture(&ctx, canvas.response.id, CanvasGesture::PRIMARY);
|
||||
if view_only {
|
||||
sampler::pass_tool_gesture(&ctx, canvas.id, CanvasGesture::PRIMARY);
|
||||
}
|
||||
// A momentary tool takes the next press anywhere on the canvas, over the session members on its body.
|
||||
if !measuring {
|
||||
pointer::withhold_members(&ctx, canvas.response.id, momentary_tool.is_some());
|
||||
}
|
||||
pointer::withhold_members(&ctx, canvas.id, momentary_tool.is_some());
|
||||
// With no gesture and no momentary tool, a held stand-in key makes the stand-in the tool of hover, cursor and
|
||||
// paint while the selected tool does not take a primary press.
|
||||
let lent = if state.gesture.is_none() && momentary_tool.is_none() && !takes_primary {
|
||||
@@ -521,30 +553,26 @@ impl<'m> ToolHost<'m> {
|
||||
ending: None,
|
||||
hover_scrolls: false,
|
||||
};
|
||||
if !measuring {
|
||||
run.call(None, |tool, cx| tool.each_pass(cx));
|
||||
run.hover_scrolls = run.pick_as(None).autoscrolls_on_hover();
|
||||
run.lent = lent;
|
||||
run.glide();
|
||||
if disabled {
|
||||
run.cancel(CancelReason::Dismissed);
|
||||
run.state.spent |= canvas.press.phase.is_press();
|
||||
} else {
|
||||
run.phase(&canvas.press, &input);
|
||||
run.hover_outside(canvas.response.id, &canvas.press, &input);
|
||||
run.keys(&ctx, canvas.response.id, env.direction.is_rtl());
|
||||
run.menu_key();
|
||||
}
|
||||
run.call(None, |tool, cx| tool.each_pass(cx));
|
||||
run.hover_scrolls = run.pick_as(None).autoscrolls_on_hover();
|
||||
run.lent = lent;
|
||||
run.glide();
|
||||
if disabled {
|
||||
run.cancel(CancelReason::Dismissed);
|
||||
run.state.spent |= canvas.press.phase.is_press();
|
||||
} else {
|
||||
run.phase(&canvas.press, &input);
|
||||
run.hover_outside(canvas.id, &canvas.press, &input);
|
||||
run.keys(&ctx, canvas.id, env.direction.is_rtl());
|
||||
run.menu_key();
|
||||
}
|
||||
let Run { mut response, requests, momentary: momentary_tool, .. } = run;
|
||||
let before = space.map(|space| space.target());
|
||||
if !measuring {
|
||||
if let Some(past) = response.past_edge {
|
||||
let step = frame_step(input.time - state.time.unwrap_or(input.time));
|
||||
response.scroll = Some(Autoscroll::step(past, step as f32));
|
||||
}
|
||||
state.time = Some(input.time);
|
||||
if let Some(past) = response.past_edge {
|
||||
let step = frame_step(input.time - state.time.unwrap_or(input.time));
|
||||
response.scroll = Some(Autoscroll::step(past, step as f32));
|
||||
}
|
||||
state.time = Some(input.time);
|
||||
if response.view.is_none() {
|
||||
response.view = cancelled_view;
|
||||
}
|
||||
@@ -559,7 +587,7 @@ impl<'m> ToolHost<'m> {
|
||||
(Some(ease), Some(drawn)) => Steer::Ease { from: drawn.view, anchor: ease.anchor },
|
||||
_ => Steer::Stop,
|
||||
};
|
||||
zoom_motion::steer(&ctx, canvas.response.id, steer);
|
||||
zoom_motion::steer(&ctx, canvas.id, steer);
|
||||
}
|
||||
// The canvas's own view, when no tool moved the view: the app copies one view from the host.
|
||||
if response.view.is_none() {
|
||||
@@ -622,10 +650,10 @@ impl<'m> ToolHost<'m> {
|
||||
frame::request_own_repaint(&ctx);
|
||||
}
|
||||
for chord in claimed_chords(state.claims) {
|
||||
keys::claim(&ctx, canvas.response.id, chord);
|
||||
keys::claim(&ctx, canvas.id, chord);
|
||||
}
|
||||
if !state.claims.keyboard && state.gesture.is_some() {
|
||||
keys::claim(&ctx, canvas.response.id, Chord::new(Mods::NONE, Key::Space));
|
||||
keys::claim(&ctx, canvas.id, Chord::new(Mods::NONE, Key::Space));
|
||||
}
|
||||
state.rect = Some(rect);
|
||||
state.space = space;
|
||||
@@ -634,7 +662,7 @@ impl<'m> ToolHost<'m> {
|
||||
response
|
||||
}
|
||||
|
||||
/// Hands an automation input for the canvas `canvas` (`CanvasOutput::response.id`) to the window host's injected
|
||||
/// Hands an automation input for the canvas `canvas` ([`ToolPass::id`]) to the window host's injected
|
||||
/// input of the viewport the canvas last ran in, behind the input injected before it; it runs one step per pass.
|
||||
/// A press, move or release is the pointer's own event with the pen's reading, so it reaches the tool through the
|
||||
/// canvas's press as a hand's would. A key is held from the pass of its [`ToolInput::hold`] to the pass of its
|
||||
@@ -729,8 +757,8 @@ const TOOL_ANCHOR: PartKey = PartKey::new("tool anchor");
|
||||
/// graphics object over the drawing's bounds (no size at the canvas centre without bounds), and a child object of no
|
||||
/// size at each anchor, clamped to the canvas. The object declares Increment and Decrement while the tool claims the
|
||||
/// nudges. The ids derive from the canvas id, so they stay the same from pass to pass.
|
||||
fn report_access(ctx: &Context, canvas: &CanvasOutput, rect: Rect, tool: &ToolAccess, nudges: bool) {
|
||||
let owner = canvas.response.id;
|
||||
fn report_access(ctx: &Context, canvas: &ToolPass, rect: Rect, tool: &ToolAccess, nudges: bool) {
|
||||
let owner = canvas.id;
|
||||
let to_screen = |point: kurbo::Point| canvas.space.map(|space| space.view.to_screen(point));
|
||||
let bounds = tool.bounds.and_then(|bounds| {
|
||||
let corners = [(bounds.x0, bounds.y0), (bounds.x1, bounds.y0), (bounds.x1, bounds.y1), (bounds.x0, bounds.y1)];
|
||||
|
||||
@@ -5,7 +5,7 @@ use craft_behavior::geometry::{
|
||||
};
|
||||
use craft_behavior::kurbo::{Point, Rect};
|
||||
use craft_behavior::{Axis, CancelReason, CanvasView, EditEvent};
|
||||
use craft_ui::canvas::{Canvas, CanvasTool, CropOverlay, Snap, ToolAction, ToolContext, ToolHost, ToolPath, ToolPointer};
|
||||
use craft_ui::canvas::{Canvas, CanvasTool, CropOverlay, Snap, ToolAction, ToolContext, ToolHost, ToolPath, ToolPointer, tool_pass};
|
||||
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
|
||||
use craft_ui::primitives::Draw;
|
||||
use craft_ui::runtime::access::Role;
|
||||
@@ -555,12 +555,15 @@ fn scrolling_host() -> (Fixture, Rc<Cell<Option<Vec2>>>) {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let view = CanvasView::new(room, Point::new(200.0, 150.0), 1.0);
|
||||
let shown = Canvas::new("Document").view(view).show(ui, |ui, body| {
|
||||
let mut reply = None;
|
||||
let _ = Canvas::new("Document").view(view).show(ui, |ui, body| {
|
||||
let out = CropOverlay::new("Crop", Bind::bound(&mut *crop.borrow_mut())).show_at(ui, body.rect);
|
||||
seen.set(out.scrolls_at);
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
reply = Some(ToolHost::new().run(ui, &pass, &mut *tool.borrow_mut()));
|
||||
}
|
||||
});
|
||||
let reply = ToolHost::new().run(ui, &shown.output, &mut *tool.borrow_mut());
|
||||
reported.set(reply.past_edge);
|
||||
reported.set(reply.unwrap_or_default().past_edge);
|
||||
});
|
||||
fixture.settle();
|
||||
(fixture, past_edge)
|
||||
|
||||
@@ -8,6 +8,7 @@ use craft_keymap::Mods;
|
||||
use craft_ui::canvas::{
|
||||
Autoscroll, Canvas, CanvasScrollbars, CanvasTool, ContextRequest, CropOverlay, HandTool, Momentary, MomentaryCommands, MomentaryTool,
|
||||
NavigationOptions, Snap, StandInKey, ToolAction, ToolClaims, ToolContext, ToolHost, ToolInput, ToolKey, ToolPath, ToolPointer, canvas_space,
|
||||
tool_pass,
|
||||
};
|
||||
use craft_ui::contract::{Bind, Lazy, Sampler as _, Show as _, ShowContainer as _};
|
||||
use craft_ui::dialog::Dialog;
|
||||
@@ -213,9 +214,14 @@ fn scroll_canvas<T: CanvasTool + 'static>(tool: Rc<RefCell<T>>, rotation: f64) -
|
||||
start.rotation = rotation;
|
||||
start
|
||||
});
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show_at(ui, room.shrink(40.0), |_, _| {});
|
||||
let reply = ToolHost::new().run(ui, &shown.output, &mut *tool.borrow_mut());
|
||||
let mut next = reply.view.unwrap_or(current);
|
||||
let mut reply = None;
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show_at(ui, room.shrink(40.0), |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
reply = Some(ToolHost::new().run(ui, &pass, &mut *tool.borrow_mut()));
|
||||
}
|
||||
});
|
||||
let reply = reply.unwrap_or_default();
|
||||
let mut next = reply.view.or(shown.output.view).unwrap_or(current);
|
||||
if let Some(step) = reply.scroll
|
||||
&& Autoscroll::scroll(&mut next, step, DOCUMENT).is_some()
|
||||
{
|
||||
@@ -343,7 +349,16 @@ fn crowded_canvas(crowd: Crowd) -> CanvasRig<Rect> {
|
||||
(area, None)
|
||||
};
|
||||
shown_canvas.set(first);
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show_at(ui, first, |_, _| {});
|
||||
let mut reply = None;
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show_at(ui, first, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let mut host = ToolHost::new();
|
||||
if crowd == Crowd::MomentaryHand {
|
||||
host = host.momentary(&mut momentary);
|
||||
}
|
||||
reply = Some(host.run(ui, &pass, &mut tool));
|
||||
}
|
||||
});
|
||||
if let Some(second) = second {
|
||||
Canvas::new("Other").document(DOCUMENT).view(CanvasView::new(second, Point::new(600.0, 450.0), 4.0)).show_at(ui, second, |_, _| {});
|
||||
}
|
||||
@@ -353,12 +368,8 @@ fn crowded_canvas(crowd: Crowd) -> CanvasRig<Rect> {
|
||||
Text::new("Modal").show(ui);
|
||||
});
|
||||
}
|
||||
let mut host = ToolHost::new();
|
||||
if crowd == Crowd::MomentaryHand {
|
||||
host = host.momentary(&mut momentary);
|
||||
}
|
||||
let reply = host.run(ui, &shown.output, &mut tool);
|
||||
let mut next = reply.view.unwrap_or(current);
|
||||
let reply = reply.unwrap_or_default();
|
||||
let mut next = reply.view.or(shown.output.view).unwrap_or(current);
|
||||
if let Some(step) = reply.scroll
|
||||
&& Autoscroll::scroll(&mut next, step, DOCUMENT).is_some()
|
||||
{
|
||||
@@ -555,17 +566,22 @@ impl Rig {
|
||||
if let Some(snap) = &snap {
|
||||
shown = shown.snap(snap.clone());
|
||||
}
|
||||
let shown = shown.show(ui, |_, _| {});
|
||||
let mut reply = None;
|
||||
let shown = shown.show(ui, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let (mut command, mut alt) = (shown_command.borrow_mut(), shown_alt.borrow_mut());
|
||||
let mut host = ToolHost::new()
|
||||
.stand_in(StandInKey::Command, &mut *command)
|
||||
.stand_in(StandInKey::Alt, &mut *alt)
|
||||
.smart_guide_color(setup.smart_color);
|
||||
if let Some(momentary) = setup.momentary.as_mut() {
|
||||
host = host.momentary(momentary);
|
||||
}
|
||||
reply = Some(host.run(ui, &pass, &mut *shown_main.borrow_mut()));
|
||||
}
|
||||
});
|
||||
shown_canvas.set(shown.output.response.id);
|
||||
let (mut command, mut alt) = (shown_command.borrow_mut(), shown_alt.borrow_mut());
|
||||
let mut host = ToolHost::new()
|
||||
.stand_in(StandInKey::Command, &mut *command)
|
||||
.stand_in(StandInKey::Alt, &mut *alt)
|
||||
.smart_guide_color(setup.smart_color);
|
||||
if let Some(momentary) = setup.momentary.as_mut() {
|
||||
host = host.momentary(momentary);
|
||||
}
|
||||
let reply = host.run(ui, &shown.output, &mut *shown_main.borrow_mut());
|
||||
let reply = reply.unwrap_or_default();
|
||||
shown_contexts.borrow_mut().extend(reply.context);
|
||||
shown_snapped.borrow_mut().extend(reply.snapped.iter().copied());
|
||||
if setup.rerun.replace(false) && frame::discardable(ui.ctx()) {
|
||||
@@ -785,9 +801,12 @@ fn an_injected_pen_reading_reaches_the_tool() {
|
||||
let shown_canvas = Rc::clone(&canvas);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(CanvasView::new(room, Point::new(600.0, 450.0), 1.0)).show(ui, |_, _| {});
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(CanvasView::new(room, Point::new(600.0, 450.0), 1.0)).show(ui, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let _ = ToolHost::new().run(ui, &pass, &mut *shown_probe.borrow_mut());
|
||||
}
|
||||
});
|
||||
shown_canvas.set(shown.output.response.id);
|
||||
let _ = ToolHost::new().run(ui, &shown.output, &mut *shown_probe.borrow_mut());
|
||||
});
|
||||
fixture.settle();
|
||||
let at = fixture.by_role(Role::CANVAS)[0].rect.center();
|
||||
@@ -852,9 +871,12 @@ fn a_stroke_runs_no_settled_panel_beside_the_canvas_and_one_pass_per_move() {
|
||||
});
|
||||
in_region(ui, document, |ui| {
|
||||
let view = CanvasView::new(document, Point::new(600.0, 450.0), 1.0);
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(view).show(ui, |_, _| {});
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(view).show(ui, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let _ = ToolHost::new().run(ui, &pass, &mut *shown_painter.borrow_mut());
|
||||
}
|
||||
});
|
||||
shown_canvas.set(shown.output.response.id);
|
||||
let _ = ToolHost::new().run(ui, &shown.output, &mut *shown_painter.borrow_mut());
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
@@ -895,10 +917,16 @@ fn momentary_canvas_with(main: Probe, mut body: impl FnMut(&mut egui::Ui, &mut c
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let current = shown_view.get().unwrap_or_else(|| CanvasView::new(room, Point::new(600.0, 450.0), 4.0));
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show(ui, &mut body);
|
||||
let mut reply = None;
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show(ui, |ui, cx| {
|
||||
body(ui, cx);
|
||||
if let Some(pass) = tool_pass(&cx.env) {
|
||||
reply = Some(ToolHost::new().momentary(&mut set).run(ui, &pass, &mut *main.borrow_mut()));
|
||||
}
|
||||
});
|
||||
shown_canvas.set(shown.output.response.id);
|
||||
let reply = ToolHost::new().momentary(&mut set).run(ui, &shown.output, &mut *main.borrow_mut());
|
||||
shown_view.set(Some(reply.view.unwrap_or(current)));
|
||||
let reply = reply.unwrap_or_default();
|
||||
shown_view.set(Some(reply.view.or(shown.output.view).unwrap_or(current)));
|
||||
});
|
||||
fixture.settle();
|
||||
(fixture, view, canvas)
|
||||
@@ -1002,7 +1030,12 @@ fn kept_canvas<T: CanvasTool + 'static>(tool: Rc<RefCell<T>>) -> CanvasRig<Rect>
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let current = shown_view.get().unwrap_or_else(|| CanvasView::new(room, Point::new(600.0, 450.0), 1.0));
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show_at(ui, room, |_, _| {});
|
||||
let mut reply = None;
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show_at(ui, room, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
reply = Some(ToolHost::new().run(ui, &pass, &mut *tool.borrow_mut()));
|
||||
}
|
||||
});
|
||||
shown_rect.set(room);
|
||||
let id = shown.output.response.id;
|
||||
let mut open = true;
|
||||
@@ -1010,8 +1043,8 @@ fn kept_canvas<T: CanvasTool + 'static>(tool: Rc<RefCell<T>>) -> CanvasRig<Rect>
|
||||
cx.keep_view_open(id);
|
||||
Text::new("Pick").show(ui);
|
||||
});
|
||||
let reply = ToolHost::new().run(ui, &shown.output, &mut *tool.borrow_mut());
|
||||
shown_view.set(Some(reply.view.unwrap_or(current)));
|
||||
let reply = reply.unwrap_or_default();
|
||||
shown_view.set(Some(reply.view.or(shown.output.view).unwrap_or(current)));
|
||||
});
|
||||
fixture.settle();
|
||||
(fixture, view, rect)
|
||||
@@ -1202,6 +1235,7 @@ fn menu_canvas(momentary: bool, bars: bool) -> MenuRig {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let current = shown_view.get().unwrap_or_else(|| CanvasView::new(room, Point::new(600.0, 450.0), 4.0));
|
||||
shown_rect.set(room);
|
||||
let mut reply = None;
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show_at(ui, room, |ui, body| {
|
||||
if bars {
|
||||
let mut bar_view = current;
|
||||
@@ -1212,14 +1246,17 @@ fn menu_canvas(momentary: bool, bars: bool) -> MenuRig {
|
||||
)
|
||||
.show_at(ui, body.rect);
|
||||
}
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let mut host = ToolHost::new();
|
||||
if momentary {
|
||||
host = host.momentary(&mut set);
|
||||
}
|
||||
reply = Some(host.run(ui, &pass, &mut hand));
|
||||
}
|
||||
});
|
||||
let mut host = ToolHost::new();
|
||||
if momentary {
|
||||
host = host.momentary(&mut set);
|
||||
}
|
||||
let reply = host.run(ui, &shown.output, &mut hand);
|
||||
let reply = reply.unwrap_or_default();
|
||||
shown_contexts.borrow_mut().extend(reply.context);
|
||||
shown_view.set(Some(reply.view.unwrap_or(current)));
|
||||
shown_view.set(Some(reply.view.or(shown.output.view).unwrap_or(current)));
|
||||
});
|
||||
fixture.settle();
|
||||
(fixture, contexts, view, rect)
|
||||
@@ -1254,8 +1291,11 @@ fn window_reading_canvas(tool: impl CanvasTool + 'static) -> (Fixture, Rc<Cell<u
|
||||
let seen = Rc::clone(&cancels);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(CanvasView::new(room, Point::new(600.0, 450.0), 1.0)).show(ui, |_, _| {});
|
||||
let _ = ToolHost::new().run(ui, &shown.output, &mut *tool.borrow_mut());
|
||||
let _ = Canvas::new("Document").document(DOCUMENT).view(CanvasView::new(room, Point::new(600.0, 450.0), 1.0)).show(ui, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let _ = ToolHost::new().run(ui, &pass, &mut *tool.borrow_mut());
|
||||
}
|
||||
});
|
||||
craft_ui::runtime::keys::with_window_intents(ui.ctx(), false, |window| {
|
||||
let took = window.take(craft_behavior::Intent::Cancel);
|
||||
seen.set(seen.get() + u32::from(took));
|
||||
@@ -1316,10 +1356,12 @@ fn the_tool_and_the_canvas_controls_read_one_precise_cursor_from_the_canvas() {
|
||||
let (tool_seen, body_seen) = (Rc::clone(&tool), Rc::clone(&body));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let view = CanvasView::new(ui.available_rect_before_wrap(), Point::new(600.0, 450.0), 1.0);
|
||||
let canvas = Canvas::new("Document").document(DOCUMENT).view(view).precise_cursors(preference).show(ui, |_, cx| {
|
||||
let _ = Canvas::new("Document").document(DOCUMENT).view(view).precise_cursors(preference).show(ui, |ui, cx| {
|
||||
body_seen.set(canvas_space(&cx.env).map(|space| space.precise()));
|
||||
if let Some(pass) = tool_pass(&cx.env) {
|
||||
let _ = ToolHost::new().run(ui, &pass, &mut PreciseProbe(Rc::clone(&tool_seen)));
|
||||
}
|
||||
});
|
||||
let _ = ToolHost::new().run(ui, &canvas.output, &mut PreciseProbe(Rc::clone(&tool_seen)));
|
||||
});
|
||||
let ctx = fixture.ctx().clone();
|
||||
for caps_lock in [false, true] {
|
||||
@@ -1366,8 +1408,13 @@ fn readout_during_a_drag(hud: bool, shown: Option<bool>) -> (bool, bool) {
|
||||
let tool = RefCell::new(Measuring { hud });
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let shown = Canvas::new("Document").document(DOCUMENT).view(CanvasView::new(room, Point::new(600.0, 450.0), 0.5)).show(ui, |_, _| {});
|
||||
let reply = ToolHost::new().run(ui, &shown.output, &mut *tool.borrow_mut());
|
||||
let mut reply = None;
|
||||
let _ = Canvas::new("Document").document(DOCUMENT).view(CanvasView::new(room, Point::new(600.0, 450.0), 0.5)).show(ui, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
reply = Some(ToolHost::new().run(ui, &pass, &mut *tool.borrow_mut()));
|
||||
}
|
||||
});
|
||||
let reply = reply.unwrap_or_default();
|
||||
seen.set(reply.readout.is_some_and(|readout| readout.rows.iter().any(|(_, value)| value == "123 px")));
|
||||
});
|
||||
if let Some(shown) = shown {
|
||||
@@ -1395,3 +1442,62 @@ fn show_transformation_values_gates_the_readouts_paint_and_never_the_hud() {
|
||||
);
|
||||
assert_eq!(readout_during_a_drag(true, Some(false)), (true, true), "a hud paints with Show Transformation Values off");
|
||||
}
|
||||
|
||||
/// A tool that keeps the points of its gesture: the press and every drag.
|
||||
#[derive(Default)]
|
||||
struct Trail {
|
||||
points: Vec<Pos2>,
|
||||
}
|
||||
|
||||
impl CanvasTool for Trail {
|
||||
fn press(&mut self, pointer: &ToolPointer, _: &mut ToolContext<'_>) -> ToolAction {
|
||||
self.points.push(pointer.pos);
|
||||
ToolAction::Claimed
|
||||
}
|
||||
|
||||
fn drag(&mut self, pointer: &ToolPointer, _: &mut ToolContext<'_>) {
|
||||
self.points.push(pointer.pos);
|
||||
}
|
||||
|
||||
fn release(&mut self, _: &ToolPointer, _: &mut ToolContext<'_>) {}
|
||||
|
||||
fn cancel(&mut self, _: CancelReason, _: &mut ToolContext<'_>) {}
|
||||
|
||||
fn paint(&self, _: &Draw<'_>, _: &ToolContext<'_>) {}
|
||||
|
||||
fn command_form(&self) -> ToolPath {
|
||||
ToolPath::Freehand("a test tool")
|
||||
}
|
||||
}
|
||||
|
||||
/// Input, tool, picture: the host runs in the canvas's body before the picture, so with one pass a frame the picture
|
||||
/// of each frame already holds that frame's press and moves.
|
||||
#[trial]
|
||||
fn the_picture_of_a_pass_shows_that_passes_press_and_moves() {
|
||||
let tool = Rc::new(RefCell::new(Trail::default()));
|
||||
// How many of the tool's points the picture of the last pass drew.
|
||||
let drawn = Rc::new(Cell::new(0_usize));
|
||||
let (held, picture) = (Rc::clone(&tool), Rc::clone(&drawn));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let _ = Canvas::new("Document").document(DOCUMENT).show(ui, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let _ = ToolHost::new().run(ui, &pass, &mut *held.borrow_mut());
|
||||
}
|
||||
picture.set(held.borrow().points.len());
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
fixture.ctx().options_mut(|options| options.max_passes = std::num::NonZeroUsize::MIN);
|
||||
let centre = fixture.by_role(Role::CANVAS).into_iter().next().expect("a canvas").center();
|
||||
fixture.frame(vec![pointer::moved(centre)]);
|
||||
let mut frames = vec![vec![pointer::press(centre)]];
|
||||
frames.extend((1..=4).map(|step| vec![pointer::moved(centre + BY * (step as f32 / 4.0))]));
|
||||
for events in frames {
|
||||
let before = tool.borrow().points.len();
|
||||
fixture.frame(events);
|
||||
assert_eq!(fixture.passes(), 1, "one pass a frame");
|
||||
let points = tool.borrow().points.len();
|
||||
assert!(points > before, "the frame's press or move reached the tool ({before} -> {points})");
|
||||
assert_eq!(drawn.get(), points, "the picture of the frame holds every point the tool took in it");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@ use craft_behavior::CanvasView;
|
||||
use craft_behavior::canvas::zoom;
|
||||
use craft_behavior::kurbo::Vec2 as DocVec;
|
||||
use craft_behavior::kurbo::{Point, Rect, Size};
|
||||
use craft_ui::canvas::{Canvas, CanvasTool, HandTool, LayerBounds, NavigationOptions, RotateViewTool, ToolHost, ViewCommands, ViewTarget, ZoomTool};
|
||||
use craft_ui::canvas::{
|
||||
Canvas, CanvasTool, HandTool, LayerBounds, NavigationOptions, RotateViewTool, ToolHost, ViewCommands, ViewTarget, ZoomTool, tool_pass,
|
||||
};
|
||||
use craft_ui::contract::{Bind, ShowContainer as _};
|
||||
use craft_ui::runtime::access::Role;
|
||||
use craft_ui::runtime::commands::{self, FireSource};
|
||||
@@ -41,12 +43,17 @@ impl Rig {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let current = shown_view.get().unwrap_or_else(|| start(room));
|
||||
let shown = Canvas::new("Document").document(ViewTarget::from(DOCUMENT)).navigation(navigation).view(current).show(ui, |_, _| {});
|
||||
let reply = ToolHost::new().run(ui, &shown.output, &mut *shown_tool.borrow_mut());
|
||||
let mut reply = None;
|
||||
let shown = Canvas::new("Document").document(ViewTarget::from(DOCUMENT)).navigation(navigation).view(current).show(ui, |ui, body| {
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
reply = Some(ToolHost::new().run(ui, &pass, &mut *shown_tool.borrow_mut()));
|
||||
}
|
||||
});
|
||||
let reply = reply.unwrap_or_default();
|
||||
if let Some(pan) = reply.pan {
|
||||
shown_pans.borrow_mut().push((pan.delta, pan.scroll_all));
|
||||
}
|
||||
shown_view.set(Some(reply.view.unwrap_or(current)));
|
||||
shown_view.set(Some(reply.view.or(shown.output.view).unwrap_or(current)));
|
||||
});
|
||||
fixture.ctx().set_pixels_per_point(ppp);
|
||||
fixture.settle();
|
||||
|
||||
@@ -9,6 +9,7 @@ use craft_behavior::kurbo::Rect as DocRect;
|
||||
use craft_behavior::{ColumnKey, ColumnWidth, PixelSize, RulerUnits, Ticks, keyed};
|
||||
use craft_ui::canvas::{
|
||||
Canvas, CanvasScrollbars, CanvasSpace, NoTool, PageFrame, Pasteboard, PixelRect, RasterDisplay, RasterSource, RulerText, Rulers, ToolHost,
|
||||
tool_pass,
|
||||
};
|
||||
use craft_ui::canvas_controls::{Guides, Marquee};
|
||||
use craft_ui::contract::SizeLimits;
|
||||
@@ -120,7 +121,7 @@ fn ruler_text(kind: UnitKind) -> RulerText {
|
||||
pub(super) fn show_layers(ui: &mut Ui, at: egui::Rect, layers: &[Layer], size: PixelSize) {
|
||||
for layer in layers.iter().rev().filter(|layer| layer.visible) {
|
||||
let source = LayerFill { layer, size };
|
||||
let _ = RasterDisplay::new(layer.name.clone(), &source).show_at(ui, at);
|
||||
let _ = RasterDisplay::new(layer.name.clone(), &source).picture().show_at(ui, at);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,14 +159,6 @@ impl RasterSource for LayerFill<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// What the canvas body found out in a pass.
|
||||
struct BodyResult {
|
||||
/// The view the scroll bars set.
|
||||
scrolled: Option<craft_behavior::CanvasView>,
|
||||
/// The pointer is over a scroll bar, so the tool leaves the press alone.
|
||||
over_bars: bool,
|
||||
}
|
||||
|
||||
/// The document area: the pasteboard, the visible layers, the selection, the rulers with their guides and the scroll
|
||||
/// bars on a canvas whose tool host runs the current tool and reports the one view.
|
||||
fn canvas(ui: &mut Ui, document: &mut Document, tool: Tool, tools: &mut Tools) {
|
||||
@@ -176,51 +169,56 @@ fn canvas(ui: &mut Ui, document: &mut Document, tool: Tool, tools: &mut Tools) {
|
||||
let (view, show_rulers) = (document.view, document.rulers);
|
||||
let editing = matches!(tool, Tool::Move | Tool::Marquee);
|
||||
let canvas_id = ui.scope_id().with("mock document canvas");
|
||||
let shown = {
|
||||
let Document { layers, guides, guide, marquee, zero, units: unit, .. } = &mut *document;
|
||||
Canvas::new("Document").id(canvas_id).view(view).document(page.size()).show_rulers(show_rulers).rulers(units, units, *zero).show(
|
||||
ui,
|
||||
|ui, body| {
|
||||
let _ = Pasteboard::new("Pasteboard", &pages).show_at(ui, body.rect);
|
||||
show_layers(ui, body.rect, layers, pixels);
|
||||
if let Some(rect) = *marquee {
|
||||
let _ = Marquee::rect("Selection", rect).show_at(ui, body.rect);
|
||||
// What the host reported; none in a measuring run, where it does not run.
|
||||
let mut reported = None;
|
||||
let mut scrolled = None;
|
||||
let _ = Canvas::new("Document").id(canvas_id).view(view).document(page.size()).show_rulers(show_rulers).rulers(units, units, document.zero).show(
|
||||
ui,
|
||||
|ui, body| {
|
||||
let _ = Pasteboard::new("Pasteboard", &pages).show_at(ui, body.rect);
|
||||
let mut content = body.rect;
|
||||
let mut pending = None;
|
||||
let Document { guides, guide, zero, units: unit, .. } = &mut *document;
|
||||
if show_rulers {
|
||||
let rulers = Rulers::new("Rulers", Bind::bound(zero)).units(&UNITS).show_at(ui, body.rect);
|
||||
content = rulers.content_rect();
|
||||
pending = rulers.guide;
|
||||
if let Some(picked) = rulers.unit_picked {
|
||||
*unit = picked;
|
||||
}
|
||||
let mut content = body.rect;
|
||||
let mut pending = None;
|
||||
if show_rulers {
|
||||
let rulers = Rulers::new("Rulers", Bind::bound(zero)).units(&UNITS).show_at(ui, body.rect);
|
||||
content = rulers.content_rect();
|
||||
pending = rulers.guide;
|
||||
if let Some(picked) = rulers.unit_picked {
|
||||
*unit = picked;
|
||||
}
|
||||
}
|
||||
let mut controls = Guides::new("Guides", canvas_id, Bind::bound(guides)).interactive(editing);
|
||||
if let Some(selected) = *guide {
|
||||
controls = controls.selected_guide(selected);
|
||||
}
|
||||
if let Some(drag) = pending {
|
||||
controls = controls.pending(drag);
|
||||
}
|
||||
if let Some(select) = controls.show_at(ui, body.rect).select {
|
||||
*guide = select;
|
||||
}
|
||||
let bars = CanvasScrollbars::new("Scroll bars", Bind::given(view), page).show_at(ui, content);
|
||||
BodyResult { scrolled: bars.view.value().copied(), over_bars: bars.over }
|
||||
},
|
||||
)
|
||||
};
|
||||
let result = shown.inner;
|
||||
let host = ToolHost::new().momentary(&mut tools.momentary).enabled(!result.as_ref().is_some_and(|result| result.over_bars));
|
||||
let reported = match tool {
|
||||
Tool::Hand => host.run(ui, &shown.output, &mut tools.hand),
|
||||
Tool::Zoom => host.run(ui, &shown.output, &mut tools.zoom),
|
||||
Tool::Move => host.run(ui, &shown.output, &mut MoveTool::new(document, Directions::Four)),
|
||||
Tool::Marquee => host.run(ui, &shown.output, &mut MarqueeTool::new(document)),
|
||||
Tool::Brush | Tool::Pencil | Tool::Eraser | Tool::Stamp => host.run(ui, &shown.output, &mut NoTool),
|
||||
};
|
||||
if let Some(next) = result.and_then(|result| result.scrolled).or(reported.view) {
|
||||
}
|
||||
let mut controls = Guides::new("Guides", canvas_id, Bind::bound(guides)).interactive(editing);
|
||||
if let Some(selected) = *guide {
|
||||
controls = controls.selected_guide(selected);
|
||||
}
|
||||
if let Some(drag) = pending {
|
||||
controls = controls.pending(drag);
|
||||
}
|
||||
if let Some(select) = controls.show_at(ui, body.rect).select {
|
||||
*guide = select;
|
||||
}
|
||||
let bars = CanvasScrollbars::new("Scroll bars", Bind::given(view), page).show_at(ui, content);
|
||||
scrolled = bars.view.value().copied();
|
||||
// The tool runs after the controls, which take a press first, and before the picture and the selection,
|
||||
// which show what it did with this pass's input. The tool leaves the press alone over a scroll bar.
|
||||
if let Some(pass) = tool_pass(&body.env) {
|
||||
let host = ToolHost::new().momentary(&mut tools.momentary).enabled(!bars.over);
|
||||
reported = Some(match tool {
|
||||
Tool::Hand => host.run(ui, &pass, &mut tools.hand),
|
||||
Tool::Zoom => host.run(ui, &pass, &mut tools.zoom),
|
||||
Tool::Move => host.run(ui, &pass, &mut MoveTool::new(document, Directions::Four)),
|
||||
Tool::Marquee => host.run(ui, &pass, &mut MarqueeTool::new(document)),
|
||||
Tool::Brush | Tool::Pencil | Tool::Eraser | Tool::Stamp => host.run(ui, &pass, &mut NoTool),
|
||||
});
|
||||
}
|
||||
show_layers(ui, body.rect, &document.layers, pixels);
|
||||
if let Some(rect) = document.marquee {
|
||||
let _ = Marquee::rect("Selection", rect).show_at(ui, body.rect);
|
||||
}
|
||||
},
|
||||
);
|
||||
let reported: craft_ui::canvas::ToolHostResponse = reported.unwrap_or_default();
|
||||
if let Some(next) = scrolled.or(reported.view) {
|
||||
document.view = next;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
use craft_behavior::CanvasView;
|
||||
use craft_behavior::kurbo::{Point, Size};
|
||||
use craft_ui::canvas::{Canvas, HandTool, NavigationOptions, ToolHost, canvas_space};
|
||||
use craft_ui::canvas::{Canvas, HandTool, NavigationOptions, ToolHost, canvas_space, tool_pass};
|
||||
use craft_ui::contract::{Bind, ShowContainer as _};
|
||||
use craft_ui::patterns::canvas::NavigatorPanel;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
@@ -48,13 +48,22 @@ impl Document {
|
||||
NavigatorPanel::new("Navigator", Bind::bound(&mut *held.borrow_mut()), DOC).navigation(navigation).show(ui);
|
||||
}
|
||||
let given = *held.borrow();
|
||||
let shown =
|
||||
Canvas::new("Document").view(given).document(DOC).navigation(navigation).show_rulers(ruled.get()).show_at(ui, at.get(), |_, body| {
|
||||
let mut hosted = None;
|
||||
let shown = Canvas::new("Document").view(given).document(DOC).navigation(navigation).show_rulers(ruled.get()).show_at(
|
||||
ui,
|
||||
at.get(),
|
||||
|ui, body| {
|
||||
if let Some(space) = canvas_space(&body.env) {
|
||||
saw.borrow_mut().push((body.rect, space.view));
|
||||
}
|
||||
});
|
||||
let next = if options.hand { ToolHost::new().run(ui, &shown.output, &mut hand).view } else { shown.output.view };
|
||||
if options.hand
|
||||
&& let Some(pass) = tool_pass(&body.env)
|
||||
{
|
||||
hosted = Some(ToolHost::new().run(ui, &pass, &mut hand));
|
||||
}
|
||||
},
|
||||
);
|
||||
let next = hosted.and_then(|hosted| hosted.view).or(shown.output.view);
|
||||
if let Some(next) = next {
|
||||
*held.borrow_mut() = next;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user