mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
Merge branch 'sync/next2' into sync/delta11
This commit is contained in:
@@ -8,7 +8,7 @@ mod view;
|
||||
pub mod zoom;
|
||||
|
||||
pub use history::{ViewHistory, ViewPlace};
|
||||
pub use units::{RulerUnits, Ticks};
|
||||
pub use units::{RulerUnits, TickLabels, Ticks};
|
||||
pub use view::{CanvasView, resize_cursor};
|
||||
pub use zoom::{ZoomFloor, ZoomScheme};
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
//! lives here; the text of a distance or a tick is written by craft-ui over `Env::units`, so no second number
|
||||
//! formatter exists.
|
||||
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
/// How a ruler divides its unit at a zoom.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[non_exhaustive]
|
||||
@@ -19,8 +21,20 @@ pub enum Ticks {
|
||||
Custom(fn(f64) -> (f64, u32)),
|
||||
}
|
||||
|
||||
/// The ruler unit of one document axis: its size and its ticks. Rulers label their ticks with it; guides use it for
|
||||
/// the readout of a drag, their AccessKit values and Shift's snap to the ticks.
|
||||
/// How a ruler writes the number at a labelled tick.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum TickLabels {
|
||||
/// PhotoCraft's: the distance from the zero point with no minus sign, a step under 1 with at least two decimals
|
||||
/// (`2.50`, `0.30`, `1.0000` on a sixteenth-inch ruler).
|
||||
#[default]
|
||||
Distance,
|
||||
/// VectorCraft's: the signed position, with only the decimals the tick needs (`-2.5`, `0.3`, `1`).
|
||||
Signed,
|
||||
}
|
||||
|
||||
/// The ruler unit of one document axis: its size, its ticks and how it labels them. Rulers label their ticks with it;
|
||||
/// guides use it for the readout of a drag, their AccessKit values and Shift's snap to the ticks.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[non_exhaustive]
|
||||
pub struct RulerUnits {
|
||||
@@ -28,6 +42,8 @@ pub struct RulerUnits {
|
||||
pub per_doc: f64,
|
||||
/// How the ticks are spaced.
|
||||
pub ticks: Ticks,
|
||||
/// How the labelled ticks are written.
|
||||
pub labels: TickLabels,
|
||||
}
|
||||
|
||||
impl Ticks {
|
||||
@@ -38,9 +54,16 @@ impl Ticks {
|
||||
}
|
||||
|
||||
impl RulerUnits {
|
||||
/// Ruler units of `per_doc` per document unit, spaced by `ticks`.
|
||||
/// Ruler units of `per_doc` per document unit, spaced by `ticks`, labelled as [`TickLabels::Distance`].
|
||||
pub const fn new(per_doc: f64, ticks: Ticks) -> Self {
|
||||
Self { per_doc, ticks }
|
||||
Self { per_doc, ticks, labels: TickLabels::Distance }
|
||||
}
|
||||
|
||||
/// The units with their ticks written as `labels`.
|
||||
#[must_use]
|
||||
pub const fn with_labels(mut self, labels: TickLabels) -> Self {
|
||||
self.labels = labels;
|
||||
self
|
||||
}
|
||||
|
||||
/// Whole document pixels.
|
||||
@@ -78,22 +101,31 @@ impl RulerUnits {
|
||||
(position - zero) * self.per_doc
|
||||
}
|
||||
|
||||
/// The decimals a ruler writes a tick of labelled step `step` with. Whole ticks are whole numbers; a step of at
|
||||
/// least 1 takes the decimals it has (none for 5, one for 2.5), a smaller one at least two (PhotoCraft's
|
||||
/// `0.50`), more when it has them (0.025).
|
||||
pub fn label_decimals(&self, step: f64) -> usize {
|
||||
match self.ticks {
|
||||
/// The least and most decimals a ruler writes a tick of labelled step `step` with. Whole ticks are whole numbers;
|
||||
/// a step of at least 1 takes the decimals it has (none for 5, one for 2.5). Under [`TickLabels::Distance`] a
|
||||
/// smaller step takes at least two (PhotoCraft's `0.50`), more when it has them (0.025), and every tick takes them
|
||||
/// all; under [`TickLabels::Signed`] a tick drops the trailing zeros (`1` on a sixteenth-inch ruler).
|
||||
pub fn label_decimals(&self, step: f64) -> RangeInclusive<usize> {
|
||||
let most = match self.ticks {
|
||||
Ticks::Decimal { whole: true } => 0,
|
||||
_ if step.abs() < 1.0 => step_decimals(step).max(2),
|
||||
_ if step.abs() < 1.0 && self.labels == TickLabels::Distance => step_decimals(step).max(2),
|
||||
_ => step_decimals(step),
|
||||
};
|
||||
match self.labels {
|
||||
TickLabels::Distance => most..=most,
|
||||
TickLabels::Signed => 0..=most,
|
||||
}
|
||||
}
|
||||
|
||||
/// The number a ruler prints at the tick `value` (ruler units from the zero point) for a labelled step `step`:
|
||||
/// the distance without a minus sign left of or above the zero point (PhotoCraft's `ruler_label`), rounded when
|
||||
/// the ticks are whole.
|
||||
/// under [`TickLabels::Distance`] the distance without a minus sign left of or above the zero point (PhotoCraft's
|
||||
/// `ruler_label`), under [`TickLabels::Signed`] the value itself; rounded when the ticks are whole.
|
||||
pub fn label_value(&self, value: f64, step: f64) -> f64 {
|
||||
if self.label_decimals(step) == 0 { value.abs().round() } else { value.abs() }
|
||||
let value = match self.labels {
|
||||
TickLabels::Distance => value.abs(),
|
||||
TickLabels::Signed => value,
|
||||
};
|
||||
if *self.label_decimals(step).end() == 0 { value.round() } else { value }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ pub use anchor::Anchor9;
|
||||
pub use arrangement::Arrangement;
|
||||
pub use canvas::CanvasTransform;
|
||||
|
||||
pub use canvas::{CanvasView, RulerUnits, Ticks, ViewHistory, ViewPlace, ZoomFloor, ZoomScheme, resize_cursor};
|
||||
pub use canvas::{CanvasView, RulerUnits, TickLabels, Ticks, ViewHistory, ViewPlace, ZoomFloor, ZoomScheme, resize_cursor};
|
||||
/// The f64 geometry crate the canvas view and its callers share.
|
||||
pub use kurbo;
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ repository.workspace = true
|
||||
reads = [
|
||||
"crates/ui-contract/fonts/inter/Inter-Regular.ttf",
|
||||
"crates/ui-contract/fonts/inter/Inter-SemiBold.ttf",
|
||||
"crates/font-subset/tests/data/sarabun/Sarabun-Regular.ttf",
|
||||
"crates/ui-contract/tests/fonts/NotoSansCJKsc-test.otf",
|
||||
]
|
||||
|
||||
|
||||
@@ -155,17 +155,24 @@ impl Momentary {
|
||||
/// the Hand into Zoom In, and Alt turns either into Zoom Out. While `in_gesture` (a drag runs), the tool of the
|
||||
/// pass before keeps the drag, whatever the keys do.
|
||||
pub fn tool(&mut self, mods: Mods, in_gesture: bool) -> Option<MomentaryTool> {
|
||||
self.tool_holding(None, mods, in_gesture)
|
||||
self.tool_holding(None, false, mods, in_gesture)
|
||||
}
|
||||
|
||||
/// [`tool`](Self::tool) with `injected`, the command that the keys injected input holds with no key event
|
||||
/// ([`ToolInput::hold`](super::ToolInput::hold)) run through the keymap: it counts before the command whose window
|
||||
/// key is down, as the real key would.
|
||||
pub(crate) fn tool_holding(&mut self, injected: Option<MomentaryCommands>, mods: Mods, in_gesture: bool) -> Option<MomentaryTool> {
|
||||
/// key is down, as the real key would. With `navigates` ([`ToolHost::navigates`](super::ToolHost::navigates)) the
|
||||
/// Hand is in effect whatever the keys hold.
|
||||
pub(crate) fn tool_holding(
|
||||
&mut self,
|
||||
injected: Option<MomentaryCommands>,
|
||||
navigates: bool,
|
||||
mods: Mods,
|
||||
in_gesture: bool,
|
||||
) -> Option<MomentaryTool> {
|
||||
if in_gesture {
|
||||
return self.latched;
|
||||
}
|
||||
let tool = self.pick(injected.or(self.held), mods);
|
||||
let tool = if navigates { Some(MomentaryTool::Hand) } else { self.pick(injected.or(self.held), mods) };
|
||||
self.latched = tool;
|
||||
tool
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::runtime::env::{Message, UnitKind};
|
||||
use crate::testing::{Drive, Fixture, StoryData, StoryOutput, story};
|
||||
use crate::testing::{pointer, stage};
|
||||
use craft_behavior::kurbo::Point;
|
||||
use craft_behavior::{Mixed, RulerUnits};
|
||||
use craft_behavior::{Mixed, RulerUnits, TickLabels, Ticks};
|
||||
use egui::{Rect, Ui, Vec2};
|
||||
|
||||
/// How far the corner is dragged, in drag thresholds: out of the corner and onto the document.
|
||||
@@ -74,9 +74,8 @@ fn configured<'a>(rulers: Rulers<'a>, settings: Settings) -> Rulers<'a> {
|
||||
rulers.interactive(settings.interactive).origin_locked(settings.locked)
|
||||
}
|
||||
|
||||
/// A canvas that shows `rulers` and whose view lies inside the strips, counting from their zero in pixels.
|
||||
fn rulers_canvas<'a>(room: Rect, pixels_per_point: f32, settings: Settings, rulers: Rulers<'a>) -> Canvas<'a> {
|
||||
let text = pixels();
|
||||
/// A canvas that shows `rulers` and whose view lies inside the strips, counting from their zero in `text`.
|
||||
fn rulers_canvas<'a>(room: Rect, pixels_per_point: f32, settings: Settings, text: RulerText, rulers: Rulers<'a>) -> Canvas<'a> {
|
||||
let view =
|
||||
StoryDocument::STANDARD.view(room, pixels_per_point, settings.zoom).rotation(settings.rotation).flip_x(settings.flip).aspect(settings.aspect);
|
||||
Canvas::new("Document").view(view).with_rulers(Some(configured(rulers, settings))).rulers(text, text)
|
||||
@@ -87,7 +86,7 @@ fn zero_point(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let settings = settings(data);
|
||||
let (zero, zero_at) = data.value_at("zero", Point::ZERO);
|
||||
let room = stage(ui);
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, Rulers::new("Rulers", zero_at.bind(&mut *zero)));
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, pixels(), Rulers::new("Rulers", zero_at.bind(&mut *zero)));
|
||||
on_ruled_canvas(ui, room, canvas, Some(&StoryDocument::STANDARD))
|
||||
}
|
||||
|
||||
@@ -96,7 +95,7 @@ fn units_menu(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let settings = settings(data);
|
||||
let (zero, zero_at) = data.value_at("zero", Point::ZERO);
|
||||
let room = stage(ui);
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, Rulers::new("Rulers", zero_at.bind(&mut *zero)).units(&UNITS));
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, pixels(), Rulers::new("Rulers", zero_at.bind(&mut *zero)).units(&UNITS));
|
||||
on_ruled_canvas(ui, room, canvas, Some(&StoryDocument::STANDARD))
|
||||
}
|
||||
|
||||
@@ -105,7 +104,7 @@ fn zero_points_that_differ(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let settings = settings(data);
|
||||
let (zero, zero_at) = data.value_at("zero", Mixed::<Point>::Mixed);
|
||||
let room = stage(ui);
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, Rulers::new("Rulers", zero_at.bind_mixed(&mut *zero)));
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, pixels(), Rulers::new("Rulers", zero_at.bind_mixed(&mut *zero)));
|
||||
on_ruled_canvas(ui, room, canvas, Some(&StoryDocument::STANDARD))
|
||||
}
|
||||
|
||||
@@ -116,7 +115,7 @@ fn zero_point_left_of_the_document(ui: &mut Ui, data: &mut StoryData) -> StoryOu
|
||||
let room = stage(ui);
|
||||
let inside =
|
||||
|zero: &Point| if zero.x < LEAST_X { Validity::Refuse(Message::new("The zero point is left of the document")) } else { Validity::Ok };
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, Rulers::new("Rulers", zero_at.bind(&mut *zero).check(inside)));
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, pixels(), Rulers::new("Rulers", zero_at.bind(&mut *zero).check(inside)));
|
||||
on_ruled_canvas(ui, room, canvas, Some(&StoryDocument::STANDARD))
|
||||
}
|
||||
|
||||
@@ -129,7 +128,7 @@ fn in_face(ui: &mut Ui, data: &mut StoryData, face: RulerFace) -> StoryOutput {
|
||||
let (width, height) = (f64::from(size.width), f64::from(size.height));
|
||||
let margins = RulerMargins::new([0.0..MARGIN, width - MARGIN..width], [0.0..MARGIN, height - MARGIN..height]);
|
||||
let rulers = Rulers::new("Rulers", zero_at.bind(&mut *zero)).margins(margins).face(face);
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, rulers);
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, pixels(), rulers);
|
||||
on_ruled_canvas(ui, room, canvas, Some(&StoryDocument::STANDARD))
|
||||
}
|
||||
|
||||
@@ -146,3 +145,17 @@ fn edge_face(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
fn centred_face(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
in_face(ui, data, RulerFace::Centred)
|
||||
}
|
||||
|
||||
/// Signed labels (VectorCraft's) on an inch ruler counting from the document's centre: ticks left of and above the zero
|
||||
/// point carry a minus sign, and a tick writes only the decimals it needs (`-0.5`, `1`).
|
||||
#[story(Rulers<'static>, covers = [], drive = Drive::Never)]
|
||||
fn signed_labels(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let settings = settings(data);
|
||||
let size = StoryDocument::STANDARD.size();
|
||||
let centre = Point::new(f64::from(size.width) / 2.0, f64::from(size.height) / 2.0);
|
||||
let (zero, zero_at) = data.value_at("zero", centre);
|
||||
let room = stage(ui);
|
||||
let inches = RulerText::new(RulerUnits::new(1.0 / 72.0, Ticks::decimal(false)).with_labels(TickLabels::Signed), UnitKind::Inches);
|
||||
let canvas = rulers_canvas(room, ui.pixels_per_point(), settings, inches, Rulers::new("Rulers", zero_at.bind(&mut *zero)));
|
||||
on_ruled_canvas(ui, room, canvas, Some(&StoryDocument::STANDARD))
|
||||
}
|
||||
|
||||
@@ -445,7 +445,7 @@ impl PartialEq for RulerText {
|
||||
(Ticks::Custom(a), Ticks::Custom(b)) => std::ptr::fn_addr_eq(a, b),
|
||||
_ => false,
|
||||
};
|
||||
self.kind == other.kind && self.units.per_doc == other.units.per_doc && same_ticks
|
||||
self.kind == other.kind && self.units.per_doc == other.units.per_doc && same_ticks && self.units.labels == other.units.labels
|
||||
}
|
||||
}
|
||||
|
||||
@@ -469,10 +469,9 @@ impl RulerText {
|
||||
}
|
||||
|
||||
/// The number a ruler prints at the tick `value` (ruler units from the zero point) of labelled step `step`, with
|
||||
/// no unit.
|
||||
/// no unit, as the units' [`TickLabels`](craft_behavior::TickLabels) write it.
|
||||
pub fn tick_label(&self, numbers: &Units, value: f64, step: f64) -> String {
|
||||
let digits = self.units.label_decimals(step);
|
||||
numbers.number_ungrouped(self.units.label_value(value, step), digits..=digits)
|
||||
numbers.number_ungrouped(self.units.label_value(value, step), self.units.label_decimals(step))
|
||||
}
|
||||
|
||||
/// The unit's short name: `px`.
|
||||
|
||||
@@ -460,6 +460,7 @@ pub fn tool_pass(env: &EnvView<'_>) -> Option<ToolPass> {
|
||||
pub struct ToolHost<'m> {
|
||||
disabled: bool,
|
||||
momentary: Option<&'m mut Momentary>,
|
||||
navigates: bool,
|
||||
command_stand_in: Option<&'m mut (dyn CanvasTool + 'm)>,
|
||||
alt_stand_in: Option<&'m mut (dyn CanvasTool + 'm)>,
|
||||
smart_guide_color: Option<DataColor>,
|
||||
@@ -490,6 +491,16 @@ impl<'m> ToolHost<'m> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether the pointer of this pass navigates instead of using the tool: a press then runs the momentary Hand of
|
||||
/// [`momentary`](Self::momentary), latched for its gesture, whatever tool is selected or key is held. The host
|
||||
/// says so for the input its platform reports, such as a finger once a pen has been seen (palm rejection);
|
||||
/// without a momentary set it changes nothing.
|
||||
#[must_use]
|
||||
pub fn navigates(mut self, navigates: bool) -> Self {
|
||||
self.navigates = navigates;
|
||||
self
|
||||
}
|
||||
|
||||
/// The tool a press runs when `key` is held at the press and the selected tool does not take it, latched for the
|
||||
/// gesture. Order: `Momentary` first, then Command, then Alt.
|
||||
#[must_use]
|
||||
@@ -517,7 +528,7 @@ impl<'m> ToolHost<'m> {
|
||||
/// measuring run, so the host and what the app does with its response never run in one.
|
||||
pub fn run(self, ui: &Ui, pass: &ToolPass, tool: &mut dyn CanvasTool) -> ToolHostResponse {
|
||||
let ctx = ui.ctx().clone();
|
||||
let ToolHost { disabled, mut momentary, mut command_stand_in, mut alt_stand_in, smart_guide_color } = self;
|
||||
let ToolHost { disabled, mut momentary, navigates, mut command_stand_in, mut alt_stand_in, smart_guide_color } = self;
|
||||
let env = Env::of(ui);
|
||||
let key = state_id(pass.id);
|
||||
let mut state = with_session(&ctx, key, |slot: &mut HostState| slot.clone());
|
||||
@@ -539,7 +550,7 @@ impl<'m> ToolHost<'m> {
|
||||
momentary.fired(command, event.phase);
|
||||
}
|
||||
let injected = held_momentary(&ctx, pass.id, env.view().command_chain(), &holds, input.mods);
|
||||
momentary.tool_holding(injected, input.mods, state.gesture.is_some())
|
||||
momentary.tool_holding(injected, navigates, input.mods, state.gesture.is_some())
|
||||
}
|
||||
None => {
|
||||
fired.for_each(drop);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use craft_behavior::geometry::{SnapOptions, SnapTargets};
|
||||
use craft_behavior::kurbo::{Point, Rect as DocRect};
|
||||
use craft_behavior::{CancelReason, CanvasView, RulerUnits, Ticks};
|
||||
use craft_behavior::{CancelReason, CanvasView, RulerUnits, TickLabels, Ticks};
|
||||
use craft_ui::canvas::{
|
||||
Canvas, CanvasTool, INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, PageFrame, Pasteboard, RulerText, Rulers, Snap, StepLadder, TickRule,
|
||||
ToolAction, ToolContext, ToolPath, ToolPointer,
|
||||
@@ -106,6 +106,55 @@ fn ticks_stay_level_and_the_zero_label_sits_at_the_zero_point_in_a_turned_view()
|
||||
assert!(turned.is_subset(&level), "tick marks keep the lengths and cross coordinates of the level view: {turned:?} against {level:?}");
|
||||
}
|
||||
|
||||
/// The labels of the top strip, left to right, over an inch ruler written as `labels` whose zero point is the middle of
|
||||
/// a view at 4 points per document point (72 to the inch), so the step is a fraction of an inch.
|
||||
fn top_inch_labels(labels: TickLabels) -> Vec<String> {
|
||||
let room = Vec2::new(800.0, 600.0);
|
||||
let middle = Point::new(400.0, 300.0);
|
||||
let view = CanvasView::new(Rect::from_min_size(Pos2::ZERO, room), middle, 4.0);
|
||||
let text = RulerText::new(RulerUnits::new(1.0 / 72.0, Ticks::decimal(false)).with_labels(labels), UnitKind::Inches);
|
||||
let content = Rc::new(Cell::new(Rect::NOTHING));
|
||||
let seen = Rc::clone(&content);
|
||||
let mut zero = middle;
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let strips = Rulers::new("Rulers", Bind::bound(&mut zero));
|
||||
let shown = Canvas::new("Document").view(view).with_rulers(Some(strips)).rulers(text, text).show(ui, |_, _| {});
|
||||
if let Some(rulers) = shown.output.rulers {
|
||||
seen.set(rulers.content_rect());
|
||||
}
|
||||
});
|
||||
fixture.room(room);
|
||||
fixture.settle();
|
||||
let content = content.get();
|
||||
let mut runs: Vec<(f32, String)> = fixture
|
||||
.texts()
|
||||
.iter()
|
||||
.filter(|run| run.rect.max.y <= content.min.y + 0.5 && run.rect.min.x >= content.min.x - 0.5)
|
||||
.map(|run| (run.rect.min.x, run.text.clone()))
|
||||
.collect();
|
||||
runs.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
runs.into_iter().map(|(_, text)| text).collect()
|
||||
}
|
||||
|
||||
/// VectorCraft's signed labels write the same ticks as the default distance labels, with a minus sign left of the zero
|
||||
/// point and only the decimals each tick needs; the default keeps PhotoCraft's unsigned, padded form.
|
||||
#[trial]
|
||||
fn signed_tick_labels_carry_the_sign_and_drop_trailing_zeros() {
|
||||
let distance = top_inch_labels(TickLabels::Distance);
|
||||
let signed = top_inch_labels(TickLabels::Signed);
|
||||
assert_eq!(distance.len(), signed.len(), "both styles label the same ticks: {distance:?} against {signed:?}");
|
||||
let fractional = |labels: &[String]| labels.iter().any(|label| label.contains('.'));
|
||||
assert!(fractional(&distance) && fractional(&signed), "the step is a fraction of an inch: {signed:?}");
|
||||
assert!(!distance.iter().any(|label| label.starts_with('-')), "distance labels carry no sign: {distance:?}");
|
||||
assert!(distance.iter().any(|label| label.contains('.') && label.ends_with('0')), "distance labels pad the decimals: {distance:?}");
|
||||
assert!(signed.iter().any(|label| label.starts_with('-')), "labels left of the zero point are negative: {signed:?}");
|
||||
assert!(!signed.iter().any(|label| label.contains('.') && label.ends_with('0')), "signed labels drop trailing zeros: {signed:?}");
|
||||
for (padded, plain) in distance.iter().zip(&signed) {
|
||||
let (padded, plain) = (padded.parse::<f64>(), plain.parse::<f64>().map(f64::abs));
|
||||
assert_eq!(padded, plain, "each tick is the same distance from the zero point in both styles");
|
||||
}
|
||||
}
|
||||
|
||||
/// How far past the canvas the tool's fill and the marquee reach.
|
||||
const PAST: f32 = 60.0;
|
||||
|
||||
|
||||
@@ -1184,12 +1184,14 @@ fn momentary_canvas(main: Probe) -> CanvasRig<Id> {
|
||||
|
||||
/// `momentary_canvas` with `body` as the canvas's body.
|
||||
fn momentary_canvas_with(main: Probe, body: impl FnMut(&mut egui::Ui, &mut craft_ui::contract::BodyCx<'_>) + 'static) -> CanvasRig<Id> {
|
||||
momentary_canvas_under(NavigationOptions::default(), main, body)
|
||||
momentary_canvas_under(NavigationOptions::default(), Rc::default(), main, body)
|
||||
}
|
||||
|
||||
/// `momentary_canvas_with` under the navigation choices `options`.
|
||||
/// `momentary_canvas_with` under the navigation choices `options`, its host told in each pass whether the pointer
|
||||
/// `navigates`.
|
||||
fn momentary_canvas_under(
|
||||
options: NavigationOptions,
|
||||
navigates: Rc<Cell<bool>>,
|
||||
main: Probe,
|
||||
mut body: impl FnMut(&mut egui::Ui, &mut craft_ui::contract::BodyCx<'_>) + 'static,
|
||||
) -> CanvasRig<Id> {
|
||||
@@ -1204,7 +1206,7 @@ fn momentary_canvas_under(
|
||||
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()));
|
||||
reply = Some(ToolHost::new().momentary(&mut set).navigates(navigates.get()).run(ui, &pass, &mut *main.borrow_mut()));
|
||||
}
|
||||
});
|
||||
shown_canvas.set(shown.output.response.id);
|
||||
@@ -1240,6 +1242,28 @@ fn a_queued_space_held_over_a_press_pans_with_the_hand_and_the_press_after_its_r
|
||||
assert!(!calls.iter().any(|call| call == "main drag"), "the Hand's drag never reaches the selected tool: {calls:?}");
|
||||
}
|
||||
|
||||
/// Palm rejection: a press the host says navigates (a finger once a pen has been seen) pans with the Hand to its
|
||||
/// release, even once the host stops saying so mid-drag, and never reaches the selected tool; the next press that does
|
||||
/// not navigate is the tool's.
|
||||
#[trial]
|
||||
fn a_press_the_host_says_navigates_pans_with_the_hand_to_its_release() {
|
||||
let log = Log::default();
|
||||
let navigates = Rc::new(Cell::new(true));
|
||||
let (mut fixture, view, _) = momentary_canvas_under(NavigationOptions::default(), Rc::clone(&navigates), Probe::new("main", &log), |_, _| {});
|
||||
let at = fixture.by_role(Role::CANVAS)[0].rect.center();
|
||||
let before = view.get().expect("the canvas ran").center;
|
||||
fixture.frame(vec![pointer::moved(at)]).frame(vec![pointer::press(at)]).frame(vec![pointer::moved(at + BY * 0.5)]);
|
||||
navigates.set(false);
|
||||
fixture.frame(vec![pointer::moved(at + BY)]).frame(vec![pointer::release(at + BY)]).settle();
|
||||
assert_ne!(view.get().expect("the canvas ran").center, before, "the navigating press pans the view");
|
||||
let calls = log.borrow().clone();
|
||||
let pressed = |call: &String| ["main press", "main drag", "main release"].contains(&call.as_str());
|
||||
assert!(!calls.iter().any(pressed), "the navigating press never reaches the selected tool: {calls:?}");
|
||||
fixture.frame(vec![pointer::press(at)]).frame(vec![pointer::release(at)]).settle();
|
||||
let calls = log.borrow().clone();
|
||||
assert_eq!(calls.iter().filter(|call| *call == "main press").count(), 1, "the next press is the selected tool's: {calls:?}");
|
||||
}
|
||||
|
||||
/// The zoom rectangle of a momentary Zoom that Cmd+Space started keeps being sized by the drag once Cmd is let go, and
|
||||
/// moves with Space once Space is let go and pressed again, as a marquee does: the release zooms to the moved
|
||||
/// rectangle. Space held since the press sizes it until let go.
|
||||
@@ -1249,7 +1273,7 @@ fn space_pressed_again_moves_a_cmd_space_zoom_rectangle() {
|
||||
let mut options = NavigationOptions::default();
|
||||
options.scrubby_zoom = false;
|
||||
options.animated_zoom = false;
|
||||
let (mut fixture, view, _) = momentary_canvas_under(options, Probe::new("main", &log), |_, _| {});
|
||||
let (mut fixture, view, _) = momentary_canvas_under(options, Rc::default(), Probe::new("main", &log), |_, _| {});
|
||||
let room = fixture.by_role(Role::CANVAS)[0].rect;
|
||||
let at = room.center() - Vec2::new(180.0, 90.0);
|
||||
let before = view.get().expect("the canvas ran").zoom;
|
||||
|
||||
Reference in New Issue
Block a user