From b54195e805572b1a8794bdfd5b54c3faea4f1f68 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 10:29:44 +0000 Subject: [PATCH] One path row: PathRow, a path field and a Browse button that reports the press; ParamRows and Watermark ask with a PathRequest, PathField picks through the file service --- crates/dialogs/src/ui/path_field.rs | 34 +++----- crates/dialogs/tests/it/files.rs | 33 +++++++- .../src/patterns/form_dialogs/mod.rs | 2 + .../src/patterns/form_dialogs/path_row.rs | 80 +++++++++++++++++++ .../patterns/form_dialogs/path_row/stories.rs | 26 ++++++ .../src/patterns/form_dialogs/rows.rs | 34 +++++--- .../src/patterns/form_dialogs/watermark.rs | 20 +++-- .../src/patterns/form_dialogs/words.rs | 1 - docs/components-index.md | 1 + 9 files changed, 182 insertions(+), 49 deletions(-) create mode 100644 crates/ui-patterns/src/patterns/form_dialogs/path_row.rs create mode 100644 crates/ui-patterns/src/patterns/form_dialogs/path_row/stories.rs diff --git a/crates/dialogs/src/ui/path_field.rs b/crates/dialogs/src/ui/path_field.rs index d24460427..f6a880f61 100644 --- a/crates/dialogs/src/ui/path_field.rs +++ b/crates/dialogs/src/ui/path_field.rs @@ -2,12 +2,11 @@ use craft_behavior::{EditEvent, Mixed}; use craft_host::target::Caps; -use craft_host::{PickId, PickKind, PickRequest}; +use craft_host::{PickId, PickRequest}; use craft_ui::contract::{Bind, Held, NoValue, Outcome, Remember as _, ShowContainer as _, Validity}; -use craft_ui::layout_components::Stack; -use craft_ui::patterns::form_dialogs::OutputPathRow; +use craft_ui::form::Field; +use craft_ui::patterns::form_dialogs::PathRow; use craft_ui::runtime::env::{Env, Label, Message}; -use craft_ui::runtime::requests::PathKind; use egui::{Response, Ui}; use std::path::Path; @@ -44,10 +43,10 @@ impl Outcome for PathFieldOutput { } } -/// A label, a text field with the path and a Browse button in one row, over the bound path. Typing commits on Enter or -/// focus loss; Browse queues `request` through `host.files()` and the answer fills the field on a later pass (the field -/// matches tickets, so several fields can browse in turn). Tab order: field, then Browse. With `must_exist` set, a path -/// that does not exist shows the not-found message under the field. +/// A label, then the path row (a text field with the path and a Browse button) over the bound path. Typing commits on +/// Enter or focus loss; Browse queues `request` through `host.files()` and the answer fills the field on a later pass +/// (the field matches tickets, so several fields can browse in turn). Tab order: field, then Browse. With `must_exist` +/// set, a path that does not exist shows the not-found message under the field. #[must_use = "show the field with `show`"] #[derive(craft_ui::contract::Pattern, craft_ui::contract::Props)] pub struct PathField<'a> { @@ -70,11 +69,10 @@ impl<'a> PathField<'a> { pub fn show(self, ui: &mut Ui) -> PathFieldOutput { let PathField { label, value, request, must_exist } = self; let words = Env::of(ui).words::(); - let kind = path_kind(&request); let mut held = Held::new(value); let mut edit = None; let mut browsing = None; - let shown = Stack::new().show(ui, |ui, cx| { + let shown = Field::new(label.clone()).show(ui, |ui, cx| { let before = held.read(cx).unwrap_or_default(); let host = cx.env.host().cloned(); let looks = must_exist && cx.env.shell().caps.contains(Caps::FILE_SYSTEM); @@ -89,8 +87,8 @@ impl<'a> PathField<'a> { cx.memory::().0 = None; answered = picked.first_path().map(|path| path.to_string_lossy().into_owned()); } - let row = OutputPathRow::new(Bind::given(before.clone()).check(check)).label(label.clone()).kind(kind.clone()).key("path").show(ui); - if row.browse.is_some() + let row = PathRow::new(label.clone(), Bind::given(before.clone()).check(check)).show(ui); + if row.browse && let Some(host) = &host { let id = host.files().pick(request.clone()); @@ -123,18 +121,6 @@ impl<'a> PathField<'a> { } } -/// What Browse asks the output row to pick, from the host's request. -fn path_kind(request: &PickRequest) -> PathKind { - match request.kind { - PickKind::Folder => PathKind::Folder, - PickKind::Save => PathKind::SaveFile, - PickKind::Open | PickKind::OpenMany => { - let extensions: Vec<&str> = request.filters.iter().flat_map(|filter| filter.extensions.iter().map(String::as_str)).collect(); - PathKind::open_file(&extensions) - } - } -} - /// Whether a non-empty `path` names nothing on disk. An empty field is not flagged. fn path_is_missing(path: &str) -> bool { !path.trim().is_empty() && !Path::new(path.trim()).exists() diff --git a/crates/dialogs/tests/it/files.rs b/crates/dialogs/tests/it/files.rs index a9a8f702e..8c8faa7de 100644 --- a/crates/dialogs/tests/it/files.rs +++ b/crates/dialogs/tests/it/files.rs @@ -1,6 +1,6 @@ //! The file list, the path field and the picker tickets they keep, against a file service that records its requests. -use craft_dialogs::ui::{FileList, FileListPicker}; +use craft_dialogs::ui::{FileList, FileListPicker, PathField}; use craft_host::{AppInfo, FileError, Files, Host, PickAnswer, PickId, PickRequest, Picked}; use craft_ui::contract::Bind; use craft_ui::testing::{Fixture, trial}; @@ -107,3 +107,34 @@ fn browse_answer_is_taken_and_written_once() { assert_eq!(names(&list.borrow()), ["a.png", "b.png"]); assert_eq!(changes.get(), 1, "the answer is written to the binding once"); } + +/// The path field's Browse queues a pick through the file service, and the answer lands in the field's path once. +#[trial] +fn path_field_browse_fills_the_path() { + let files = Arc::new(StubFiles::default()); + let path = Rc::new(RefCell::new(String::new())); + let sets = Rc::new(Cell::new(0usize)); + let ticket = Rc::new(Cell::new(None::)); + let (held, counted, issued) = (Rc::clone(&path), Rc::clone(&sets), Rc::clone(&ticket)); + let mut fixture = Fixture::new(move |ui| { + let mut path = held.borrow_mut(); + let out = PathField::new("PDF file", Bind::bound(&mut *path), PickRequest::open("import")).show(ui); + if out.browsing.is_some() { + issued.set(out.browsing); + } + if out.edit.is_some() { + counted.set(counted.get() + 1); + } + }); + fixture.host(host_with(&files)); + fixture.settle(); + let browse = fixture.by_label("Browse…").expect("the Browse button"); + fixture.click(&browse); + fixture.settle(); + sets.set(0); + + files.answer(ticket.get().expect("Browse issued a ticket"), Picked::Paths(paths(&["brochure.pdf"]))); + fixture.idle(3); + assert_eq!(*path.borrow(), "/photos/brochure.pdf"); + assert_eq!(sets.get(), 1, "the answer lands once"); +} diff --git a/crates/ui-patterns/src/patterns/form_dialogs/mod.rs b/crates/ui-patterns/src/patterns/form_dialogs/mod.rs index 40389f791..48b087628 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/mod.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/mod.rs @@ -14,6 +14,7 @@ pub use words::ParamWords; mod dialog; mod form; mod host_requests; +mod path_row; mod preview; pub use dialog::{ @@ -21,6 +22,7 @@ pub use dialog::{ }; pub use form::{FormCx, FormDialog, FormOutput}; pub use host_requests::{HostRequests, PreviewRequest}; +pub use path_row::{PathRow, PathRowOutput, PathRowWords}; pub use preview::{ PaneOverlay, PreviewOutput, PreviewPane, PreviewPaneStyle, PreviewState, PreviewView, PreviewWords, PreviewZoom, PreviewZoomOutput, }; diff --git a/crates/ui-patterns/src/patterns/form_dialogs/path_row.rs b/crates/ui-patterns/src/patterns/form_dialogs/path_row.rs new file mode 100644 index 000000000..8573059d5 --- /dev/null +++ b/crates/ui-patterns/src/patterns/form_dialogs/path_row.rs @@ -0,0 +1,80 @@ +//! `PathRow`: a path typed in a text field, with a Browse button that asks the host to pick one. + +#[cfg(feature = "testing")] +mod stories; + +use crate::buttons::Button; +use crate::contract::{Bind, NoValue, Outcome, Props as _, Show as _, ShowContainer as _, clicked}; +use crate::layout_components::Row; +use crate::runtime::env::{Env, Label}; +use crate::text::TextField; +use craft_behavior::EditEvents; +use egui::{Response, Ui}; + +craft_ui::words! { + /// The words of the path row. + pub struct PathRowWords { + browse: "pathRow.browse" = "Browse…"; + } +} + +/// What a path row reports. +#[derive(Debug)] +#[non_exhaustive] +pub struct PathRowOutput { + /// The row's response. + pub response: Response, + /// The text field's edits of the path in this pass; a given value is applied by the caller. + pub edit: EditEvents, + /// Browse was pressed this pass: the host asks for a path by its own route and writes the answer into the value. + pub browse: bool, +} + +impl Outcome for PathRowOutput { + type Value = NoValue; + + fn response(&self) -> &Response { + &self.response + } +} + +/// A text field with the path, taking the row's room, and a Browse button after it, for a row whose enclosing `Field` +/// shows the label. Typing edits the bound path; the binding's check shows under the field (a file that is not there). +/// A press on Browse is reported, and the host files its pick request: a `PathRequest` in a form dialog, or a pick from +/// the host's file service. Tab order: the field, then Browse. +#[must_use = "show the row with `show`"] +#[derive(crate::contract::Pattern, crate::contract::Props)] +pub struct PathRow<'a> { + name: Label, + value: Bind<'a, String>, + /// The button's text; the words' "Browse…" without one. + #[prop(into)] + button: Option