mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Keep the Scale Ruler gesture registered by the open dialog
This commit is contained in:
1 parent
8fb77b7634
commit
fffecde82a
3 files changed
+27
-14
No files matched your search
@@ -9,11 +9,10 @@ use craft::behavior::{CancelReason, CountMark, Cursor, CursorBadge, CursorIcon,
|
||||
use craft::color_ui::ui::{ColorSamplerMarks, ColorSamplerTool, ColorSamplers, SamplerEvent};
|
||||
use craft::draw::ui::{RulerLine, RulerTool};
|
||||
use craft::ui::canvas::{CanvasTool, NoTool, ToolAction, ToolClaims, ToolContext, ToolHostResponse, ToolKey, ToolPath, ToolPointer, canvas_space};
|
||||
use craft::ui::contract::{Access, CanvasGesture, Common, Component, Cx, MeasureCx, PaintUi, Props as _, Role, Show as _};
|
||||
use craft::ui::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, Props as _, Role, Show as _};
|
||||
use craft::ui::fonts::FontRef;
|
||||
use craft::ui::primitives::{DocRun, DocText, Draw, PaintShape, StrokeWidth};
|
||||
use craft::ui::runtime::env::Label;
|
||||
use craft::ui::runtime::sampler;
|
||||
use craft::ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx};
|
||||
use egui::{Color32, CornerRadius, Pos2, Rect, Response, Vec2, vec2};
|
||||
use serde_json::json;
|
||||
@@ -141,11 +140,7 @@ pub(crate) fn tool<'a>(scope: &'a ToolScope<'a>, state: &'a mut AnalysisTools, h
|
||||
let tool = scope.with_app(|app| app.ui.tool);
|
||||
let AnalysisTools { ruler, scale, sampler, samplers, count, note } = state;
|
||||
match tool {
|
||||
Tool::Ruler if scope.with_app(|app| scale_ruler_on(app, host)) => {
|
||||
// The modal's ParamDialog keeps this canvas open for navigation. Pass the ruler's primary drag too.
|
||||
sampler::pass_tool_gesture(scope.ctx(), host.canvas_id(), CanvasGesture::PRIMARY);
|
||||
Box::new(scale.tool.pass(scale.line))
|
||||
}
|
||||
Tool::Ruler if scope.with_app(|app| scale_ruler_on(app, host)) => Box::new(scale.tool.pass(scale.line)),
|
||||
Tool::Ruler => {
|
||||
let line = scope.with_app(|app| ruler_line(app));
|
||||
Box::new(ruler.pass(line))
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
use craft::behavior::IconId;
|
||||
use craft::ui::choice::Checkbox;
|
||||
use craft::ui::contract::{Bind, Props as _, Scroll, Show as _, ShowContainer as _};
|
||||
use craft::ui::contract::{Bind, CanvasGesture, Props as _, Sampler as _, Scroll, Show as _, ShowContainer as _};
|
||||
use craft::ui::dialog::{DialogId, dialog_id};
|
||||
use craft::ui::display::{Divider, Elide, Icon, Text};
|
||||
use craft::ui::layout_components::{Row, Stack};
|
||||
@@ -15,6 +15,7 @@ use photocraft_engine::analysis_cmds;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::canvas::document_display::CanvasHost;
|
||||
use crate::dialogs::params;
|
||||
use crate::state::Tool;
|
||||
|
||||
@@ -97,7 +98,8 @@ pub(crate) fn open(app: &mut PhotocraftApp, kind: Kind) -> Result<DialogId, Stri
|
||||
fields.set("color", "black");
|
||||
}
|
||||
}
|
||||
Ok(params::open_param(app, kind.command(), fields, captured, move |f: &mut DialogFields| form(kind, f)))
|
||||
let scale_canvas = app.session.active_index().filter(|_| kind == Kind::Scale).map(|index| CanvasHost::primary(app, index).canvas_id());
|
||||
Ok(params::open_param(app, kind.command(), fields, captured, move |f: &mut DialogFields| form(kind, f, scale_canvas)))
|
||||
}
|
||||
|
||||
fn scale_id() -> DialogId {
|
||||
@@ -208,8 +210,12 @@ pub(crate) fn scale_readout(f: &DialogFields, decimal: char) -> String {
|
||||
format!("{} pixel(s) = {} {units}", mark(pixels.to_string()), mark(format!("{length:.4}")))
|
||||
}
|
||||
|
||||
fn scale_ruler_note(ui: &mut egui::Ui) {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
fn scale_ruler_note(ui: &mut egui::Ui, canvas: Option<egui::Id>) {
|
||||
Row::new().show(ui, |ui, cx| {
|
||||
// The dialog runs every pass, even when the canvas replays its cached draw.
|
||||
if let Some(canvas) = canvas {
|
||||
cx.pass_canvas_gesture(canvas, CanvasGesture::PRIMARY);
|
||||
}
|
||||
Icon::new(IconSpec::svg(IconId::new("info"))).show(ui);
|
||||
let note = tl!("The Ruler tool has been activated while this dialog is open. You may use it to measure the pixel length for use in your scale.");
|
||||
let sample: String = note.chars().take(READOUT_CHARS).collect();
|
||||
@@ -222,7 +228,7 @@ fn options(pairs: &[(&str, &str)]) -> Vec<(String, Label)> {
|
||||
}
|
||||
|
||||
/// The dialog of `kind` over `fields`.
|
||||
fn form(kind: Kind, fields: &mut DialogFields) -> ParamDialog<'_> {
|
||||
fn form(kind: Kind, fields: &mut DialogFields, scale_canvas: Option<egui::Id>) -> ParamDialog<'_> {
|
||||
let key = kind.command();
|
||||
let dialog = match kind {
|
||||
Kind::Scale => {
|
||||
@@ -233,7 +239,7 @@ fn form(kind: Kind, fields: &mut DialogFields) -> ParamDialog<'_> {
|
||||
Param::new("logicalLength", tl!("Logical Length:"), ParamKind::number(Some(0.0001), Some(1e7), 1.0, 1.0, 4)).not_remembered(),
|
||||
Param::new("units", tl!("Logical Units:"), ParamKind::text("pixels")).not_remembered(),
|
||||
];
|
||||
ParamDialog::command(key, tl!("Measurement Scale"), rows).below(|ui, f| {
|
||||
ParamDialog::command(key, tl!("Measurement Scale"), rows).below(move |ui, f| {
|
||||
// One line: units too long for it are cut with "…", so the dialog keeps its width (#17414). The decimal
|
||||
// mark is the one the fields above write: the system's (any mark, not only `.` or `,`) or the host's.
|
||||
let text = scale_readout(f, Env::of(ui).format().separators().0);
|
||||
@@ -241,7 +247,7 @@ fn form(kind: Kind, fields: &mut DialogFields) -> ParamDialog<'_> {
|
||||
// and long units are cut with "…" instead of widening the dialog.
|
||||
let sample: String = text.chars().take(READOUT_CHARS).collect();
|
||||
Text::new(Label::from(text)).elide(Elide::End).sample(Label::from(sample)).show(ui);
|
||||
scale_ruler_note(ui);
|
||||
scale_ruler_note(ui, scale_canvas);
|
||||
})
|
||||
}
|
||||
Kind::DataPoints => {
|
||||
|
||||
@@ -86,8 +86,11 @@ fn an_exposed_drag_updates_only_pixel_length_and_ok_restores_the_tool() {
|
||||
let primary = CanvasHost::primary(h.state(), 0);
|
||||
assert!(scale_ruler_on(h.state(), primary));
|
||||
assert!(!scale_ruler_on(h.state(), CanvasHost { idx: 0, window: Some(99) }));
|
||||
// Let the canvas settle past the lifetime of any registration made when the form opened.
|
||||
h.run_steps(4);
|
||||
let span = points(&h);
|
||||
drag(&mut h, span);
|
||||
assert!(crate::canvas::tools::in_gesture(h.state()), "the visible canvas claims the modal ruler drag");
|
||||
let f = fields(&h);
|
||||
assert_eq!((f.number("pixelLength"), f.number("logicalLength"), f.text("units")), (Some(200.0), Some(3.5), Some("cm")));
|
||||
assert!(length_has_focus(&h), "the admitted canvas press leaves keyboard focus in the form");
|
||||
@@ -114,6 +117,7 @@ fn a_nested_prompt_blocks_measurement_and_escape_discards_the_scale_draft() {
|
||||
let alert = crate::dialogs::alerts::alert(h.state_mut(), "Test prompt over Measurement Scale");
|
||||
h.run_steps(4);
|
||||
drag(&mut h, span);
|
||||
assert!(!crate::canvas::tools::in_gesture(h.state()), "the nested prompt owns the pointer");
|
||||
button(&mut h, span[1], false);
|
||||
assert_eq!(fields(&h).number("pixelLength"), Some(42.0));
|
||||
let ctx = h.ctx.clone();
|
||||
@@ -123,7 +127,15 @@ fn a_nested_prompt_blocks_measurement_and_escape_discards_the_scale_draft() {
|
||||
assert_eq!(fields(&h).number("pixelLength"), Some(200.0));
|
||||
h.key_press(Key::Escape);
|
||||
h.run_steps(4);
|
||||
assert_eq!(fields(&h).number("pixelLength"), Some(42.0), "Escape cancels the held canvas drag before dismissing the form");
|
||||
button(&mut h, span[1], false);
|
||||
|
||||
let span = points(&h);
|
||||
drag(&mut h, span);
|
||||
button(&mut h, span[1], false);
|
||||
assert_eq!(fields(&h).number("pixelLength"), Some(200.0));
|
||||
h.key_press(Key::Escape);
|
||||
h.run_steps(4);
|
||||
assert!(!h.state().dialog_open());
|
||||
assert_eq!(h.state().ui.tool, Tool::Brush);
|
||||
assert!(h.state().canvas_tools.analysis.scale.line.is_none());
|
||||
|
||||
Reference in new issue
Block a user