From 20e0bcf93b490664836a4534bced7fea3d07ce72 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 15 Nov 2026 04:46:07 +0000 Subject: [PATCH] dialogs-w2: net change of 3 commits - dialogs-w2: written by an agent - Integration: ParamRows keeps dialogs-w1's preview_colors beside the patterns rows (dialogs-w1 7a195af4a) - dialogs-w2: review fixes (review-1) --- crates/dialogs/README.md | 6 +- crates/dialogs/src/portal/tests.rs | 26 ++ crates/dialogs/src/probe.rs | 3 + crates/dialogs/src/probe/tests.rs | 60 +++ crates/dialogs/src/ui/file_list.rs | 399 ------------------ crates/dialogs/src/ui/mod.rs | 4 +- crates/dialogs/src/ui/stories.rs | 37 +- crates/dialogs/tests/it/files.rs | 40 +- .../src/patterns/form_dialogs/file_list.rs | 7 +- .../src/patterns/form_dialogs/param.rs | 157 +++++-- .../src/patterns/form_dialogs/rows.rs | 216 +++++++++- .../src/patterns/form_dialogs/rows/stories.rs | 31 +- .../tests/it/form_dialogs/files.rs | 91 ++++ .../tests/it/form_dialogs/params.rs | 111 ++++- crates/ui-widgets/src/text/selectable_text.rs | 17 +- docs/craft-framework.md | 15 +- .../automation-update-quit-dialogs.toml | 10 +- 17 files changed, 667 insertions(+), 563 deletions(-) create mode 100644 crates/dialogs/src/probe/tests.rs delete mode 100644 crates/dialogs/src/ui/file_list.rs create mode 100644 crates/ui-patterns/tests/it/form_dialogs/files.rs diff --git a/crates/dialogs/README.md b/crates/dialogs/README.md index b2410f8e3..8d17b8970 100644 --- a/crates/dialogs/README.md +++ b/crates/dialogs/README.md @@ -7,10 +7,8 @@ is any `raw-window-handle` 0.6 window, and the app passes a closure that wakes i ## egui parts (feature `ui`) -`craft_dialogs::ui` holds the egui widgets over `host.files()`: `PathField` (a path text field with a -Browse button) and `FileListPicker` with `FileList` (Photoshop's Load Files into Stack list: -Files or Folder mode, Browse…, Remove, Add Open Files, Sort By Name; a 300 × 160 list box and -120-wide buttons from the `file_list.*` tokens). +`craft_dialogs::ui` holds the egui widget over `host.files()`: `PathField` (a path text field with a +Browse button). ## Save flow diff --git a/crates/dialogs/src/portal/tests.rs b/crates/dialogs/src/portal/tests.rs index 9468c2a69..9aa038a2c 100644 --- a/crates/dialogs/src/portal/tests.rs +++ b/crates/dialogs/src/portal/tests.rs @@ -9,3 +9,29 @@ fn file_uris_become_local_paths() { assert_eq!(file_uri_path("sftp://host/x.png"), None); assert_eq!(file_uri_path("file://host/x.png"), None); } + +/// The name the Save dialog's answer ends up with: the picked type (`picked`, an index into `filters`) decides the +/// extension a bare name takes, and an extension the user typed wins unless it is the one the dialog opened with. +#[trial] +fn the_picked_file_type_gives_a_bare_name_its_extension_and_a_typed_extension_wins() { + let two = [Filter::new("Photoshop", &["psd", "psb"]), Filter::new("PNG", &["png"])]; + let only = [Filter::new("Swatches", &["aco"])]; + // (path, opened with, filters, picked type, expected path) + let table: [(&str, Option<&str>, &[Filter], Option, &str); 8] = [ + ("/s/Untitled", Some("Untitled.psd"), &two, Some(1), "/s/Untitled.png"), + ("/s/Untitled.psd", Some("Untitled.psd"), &two, Some(1), "/s/Untitled.png"), + ("/s/art.png", Some("art.psd"), &two, Some(0), "/s/art.png"), + ("/s/art.PSB", Some("art.psd"), &two, Some(0), "/s/art.PSB"), + ("/s/scan.2024", None, &two, Some(1), "/s/scan.2024.png"), + ("/s/Swatches", None, &only, None, "/s/Swatches.aco"), + ("/s/Swatches.aco", None, &only, None, "/s/Swatches.aco"), + ("/s/free", None, &two, None, "/s/free"), + ]; + for (path, opened, filters, picked, expected) in table { + let chosen: &[String] = match chosen_type(filters, picked) { + Some(at) => &filters[at].extensions, + None => &[], + }; + assert_eq!(with_chosen_type(Path::new(path), opened, chosen, filters), PathBuf::from(expected), "{path} with type {picked:?}"); + } +} diff --git a/crates/dialogs/src/probe.rs b/crates/dialogs/src/probe.rs index 1da8fb20e..c06691b42 100644 --- a/crates/dialogs/src/probe.rs +++ b/crates/dialogs/src/probe.rs @@ -184,3 +184,6 @@ fn classify_error(e: &zbus::Error) -> Option { _ => None, } } + +#[cfg(test)] +mod tests; diff --git a/crates/dialogs/src/probe/tests.rs b/crates/dialogs/src/probe/tests.rs new file mode 100644 index 000000000..6a5b2b9da --- /dev/null +++ b/crates/dialogs/src/probe/tests.rs @@ -0,0 +1,60 @@ +use super::*; +use craft_ui::testing::trial; + +fn findings(libdbus: Option, file_chooser_portal: Option, zenity: bool) -> Findings { + Findings { libdbus, file_chooser_portal, zenity } +} + +/// The probe speaks only when no backend can show a dialog: anything it could not tell counts as present, so a +/// working system stays silent. +#[trial] +fn a_dialog_is_explained_only_when_no_backend_can_show_one() { + let silent = [ + findings(Some(true), Some(true), false), + findings(Some(false), Some(true), true), + findings(Some(true), Some(false), true), + findings(None, None, false), + findings(Some(true), None, false), + findings(None, Some(true), false), + ]; + for found in silent { + assert_eq!(problem(found), None, "{found:?}"); + } + assert_eq!(problem(findings(Some(false), Some(true), false)).as_deref(), Some("libdbus is missing")); + assert_eq!(problem(findings(Some(true), Some(false), false)).as_deref(), Some("no desktop portal offers a file chooser")); + assert_eq!(problem(findings(None, Some(false), false)).as_deref(), Some("no desktop portal offers a file chooser")); +} + +#[cfg(target_os = "linux")] +#[trial] +fn the_interface_list_says_whether_a_file_chooser_is_offered() { + let xml = |name: &str, quote: char| format!(""); + assert!(interface_listed(&xml("org.freedesktop.portal.FileChooser", '"'))); + assert!(interface_listed(&xml("org.freedesktop.portal.FileChooser", '\''))); + assert!(!interface_listed(&xml("org.freedesktop.portal.FileChooser2", '"')), "a longer name is another interface"); + assert!(!interface_listed(&xml("org.freedesktop.portal.Settings", '"'))); + assert!(!interface_listed("")); +} + +#[cfg(target_os = "linux")] +#[trial] +fn dbus_errors_for_a_missing_interface_mean_absent() { + use zbus::fdo::Error as Fdo; + let absent = [ + zbus::Error::FDO(Box::new(Fdo::ServiceUnknown("gone".into()))), + zbus::Error::FDO(Box::new(Fdo::NameHasNoOwner("gone".into()))), + zbus::Error::FDO(Box::new(Fdo::UnknownObject("gone".into()))), + zbus::Error::FDO(Box::new(Fdo::InvalidArgs("No such interface".into()))), + ]; + for error in &absent { + assert_eq!(classify_error(error), Some(false), "{error}"); + } + let method = |name: &'static str| { + let name = zbus::names::OwnedErrorName::try_from(name).expect("a valid error name"); + zbus::Error::MethodError(name, None, zbus::message::Message::method_call("/", "Ping").expect("a call").build(&()).expect("a message")) + }; + assert_eq!(classify_error(&method("org.freedesktop.DBus.Error.UnknownInterface")), Some(false)); + // Anything else says nothing about the interface, so the probe counts it as present. + assert_eq!(classify_error(&zbus::Error::FDO(Box::new(Fdo::AccessDenied("no".into())))), None); + assert_eq!(classify_error(&method("org.freedesktop.DBus.Error.AccessDenied")), None); +} diff --git a/crates/dialogs/src/ui/file_list.rs b/crates/dialogs/src/ui/file_list.rs deleted file mode 100644 index e4a50ee5e..000000000 --- a/crates/dialogs/src/ui/file_list.rs +++ /dev/null @@ -1,399 +0,0 @@ -//! [`FileListPicker`]: a list of chosen files (or one folder) with Browse, Remove and the other list buttons, inside a -//! dialog that loads several files. - -use craft_behavior::{DisplayOrder, EditEvent, ItemSource, Selection}; -use craft_host::{PickId, PickKind, PickRequest}; -use craft_ui::buttons::Button; -use craft_ui::contract::{ - Bind, BodyCx, CollectionEvent, Entry, Held, NoValue, Outcome, Pad, Props as _, Remember as _, ReorderRule, SelectMode, Show as _, - ShowCollection as _, ShowContainer as _, Sizing, clicked, -}; -use craft_ui::display::{Shade, Text}; -use craft_ui::form::Field; -use craft_ui::layout_components::Stack; -use craft_ui::list::{List, ListRow}; -use craft_ui::pickers::Select; -use craft_ui::runtime::env::{Env, Label}; -use craft_ui::style::SurfaceKind; -use egui::{Response, Ui}; -use std::borrow::Cow; -use std::cell::RefCell; -use std::path::{Path, PathBuf}; - -craft_ui::words! { - /// The words of the file list picker. - pub struct FileListWords { - use_label: "fileListPicker.use" = "Use:"; - files: "fileListPicker.files" = "Files"; - folder: "fileListPicker.folder" = "Folder"; - browse: "fileListPicker.browse" = "Browse…"; - remove: "fileListPicker.remove" = "Remove"; - add_open_files: "fileListPicker.addOpenFiles" = "Add Open Files"; - sort_by_name: "fileListPicker.sortByName" = "Sort By Name"; - add_files: "fileListPicker.addFiles" = "Add the files to load"; - choose_folder: "fileListPicker.chooseFolder" = "Choose a folder to load"; - } -} - -/// Files picked one by one, or one folder whose files the app lists. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -#[non_exhaustive] -pub enum FileListMode { - /// Individual files, listed in the user's order. - #[default] - Files, - /// One folder; the app lists its files. - Folder, -} - -/// The picker's value; the dialog keeps it. -#[derive(Clone, Debug, Default, PartialEq)] -#[non_exhaustive] -pub struct FileList { - /// Whether files or a folder are picked. - pub mode: FileListMode, - /// The chosen files in Files mode, in the user's order. The paths are distinct: they are the list's row keys, so - /// add files through [`FileList::add`]. - pub files: Vec, - /// The folder in Folder mode. - pub folder: Option, - /// The selected file in Files mode, one of `files`. - pub selected: Option, -} - -impl FileList { - /// Adds paths not in the list yet, in order (Browse, Add Open Files, a drop). - pub fn add(&mut self, paths: impl IntoIterator) { - for path in paths { - if !self.files.contains(&path) { - self.files.push(path); - } - } - } - - /// Whether OK may run: Files mode needs a file, Folder mode a folder. - pub fn is_valid(&self) -> bool { - match self.mode { - FileListMode::Files => !self.files.is_empty(), - FileListMode::Folder => self.folder.as_ref().is_some_and(|folder| !folder.as_os_str().is_empty()), - } - } - - /// Why OK can't run, for the dialog's message: "Add the files to load" or "Choose a folder to load"; `None` when - /// [`is_valid`](Self::is_valid). - pub fn problem<'l>(&self, words: &'l FileListWords) -> Option<&'l str> { - if self.is_valid() { - return None; - } - Some(match self.mode { - FileListMode::Files => words.add_files.as_str(), - FileListMode::Folder => words.choose_folder.as_str(), - }) - } -} - -/// The second line of a file's row. -type InfoLine<'a> = &'a dyn Fn(&Path) -> Option; - -/// The pick the picker is waiting for and what it asked for, kept in the picker's container while it is shown. -#[derive(Clone, Copy, Default)] -struct Ticket(Option<(PickId, PickKind)>); - -/// What the picker reports. -#[derive(Debug)] -#[non_exhaustive] -pub struct FileListPickerOutput { - /// The picker's response. - pub response: Response, - /// The list, mode or folder changed in this pass. - pub changed: bool, - /// Browse queued a picker; the answer fills the list on a later pass. - pub browsing: Option, -} - -impl Outcome for FileListPickerOutput { - type Value = NoValue; - - fn response(&self) -> &Response { - &self.response - } -} - -/// Photoshop's Load Files into Stack file list, after PhotoCraft's port of it. -/// -/// The user sees "Use:" with Files and Folder (when [`folder_mode`](Self::folder_mode) is on), the list (one row per -/// file showing its name, the full path as tooltip, and the app's info line under the name when given) and a column of -/// buttons beside it, one width: Browse… (an open-many picker in Files mode, a folder picker in Folder mode, both through `host.files()`), -/// Remove (with a row selected), Add Open Files (when the app passed open documents with a path) and Sort By Name (more -/// than one file; sorts by name ignoring case). In Folder mode the list shows the folder's files the app passes and has -/// no selection. -/// -/// Keys: the list is one Tab stop; arrows move the selection; Delete and Backspace remove the selected file; Cmd+] and -/// Cmd+[ move it up and down (Ctrl off macOS; Shift to the ends). Dropped files are added by the dialog with [`FileList::add`]. -#[must_use = "show the picker with `show`"] -#[derive(craft_ui::contract::Pattern, craft_ui::contract::Props)] -pub struct FileListPicker<'a> { - label: Label, - list: Bind<'a, FileList>, - /// Browse's picker in Files mode (purpose, filters); Folder mode asks for a folder with the same purpose. - #[prop] - request: PickRequest, - /// Offers Folder mode. - #[prop] - folder_mode: bool, - /// The open documents' paths, for Add Open Files. - #[prop] - open_files: &'a [PathBuf], - /// The files the app found in the chosen folder, shown in Folder mode. - #[prop] - folder_files: &'a [PathBuf], - /// A second line per file ("1200 × 800 px, 300 ppi, RGB"); none for no line. - #[prop] - info: Option>, -} - -impl<'a> FileListPicker<'a> { - /// The picker named `label` over `list`; Browse asks with `PickRequest::open_many("stack")` until - /// [`request`](Self::request) replaces it. - pub fn new(label: impl Into