diff --git a/Cargo.lock b/Cargo.lock index 6bc6aab..3398a76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,7 +80,7 @@ dependencies = [ [[package]] name = "accesskit_xplat" version = "0.2.1" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "accesskit", "accesskit_android", @@ -216,7 +216,7 @@ dependencies = [ [[package]] name = "anyrender_vello_hybrid" version = "0.11.0" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "anyrender", "debug_timer", @@ -368,7 +368,7 @@ checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" [[package]] name = "blitz-dom" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "accesskit", "anyrender", @@ -412,12 +412,13 @@ dependencies = [ "usvg", "web-time", "writeable", + "zeroize", ] [[package]] name = "blitz-net" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "blitz-traits", "data-url", @@ -429,7 +430,7 @@ dependencies = [ [[package]] name = "blitz-paint" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "anyrender", "anyrender_svg", @@ -451,7 +452,7 @@ dependencies = [ [[package]] name = "blitz-shell" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "accesskit", "accesskit_xplat", @@ -461,18 +462,27 @@ dependencies = [ "blitz-dom", "blitz-paint", "blitz-traits", + "block2 0.6.2", "futures-util", + "js-sys", "keyboard-types", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-foundation 0.3.2", + "objc2-ui-kit", + "objc2-uniform-type-identifiers", "wasm-bindgen", + "wasm-bindgen-futures", "web-sys", "web-time", + "windows-sys 0.61.2", "winit", ] [[package]] name = "blitz-traits" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "atomic_refcell", "bitflags", @@ -918,7 +928,7 @@ checksum = "be1e0bca6c3637f992fc1cc7cbc52a78c1ef6db076dbf1059c4323d6a2048376" [[package]] name = "debug_timer" version = "0.1.3" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" [[package]] name = "derive_more" @@ -1227,11 +1237,11 @@ dependencies = [ [[package]] name = "dioxus-native-dom" version = "0.8.0-alpha.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "200d283bd0e6ecdd4a7542e081e44c1bf83f8aba7bc478837b918ff8dc9c0c5f" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "blitz-dom", "blitz-traits", + "bytes", "dioxus-core", "dioxus-html", "futures-util", @@ -1773,7 +1783,7 @@ dependencies = [ [[package]] name = "glifo" version = "0.4.0" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "bytemuck", "foldhash 0.2.0", @@ -2992,6 +3002,7 @@ dependencies = [ "objc2 0.6.5", "objc2-core-foundation", "objc2-foundation 0.3.2", + "objc2-uniform-type-identifiers", ] [[package]] @@ -3164,9 +3175,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d87d638e33c06f577498cbcc50491496a3ed4246998a7fbba7ccb98b1e7eab22" dependencies = [ "bitflags", + "block2 0.6.2", "objc2 0.6.5", "objc2-core-foundation", "objc2-foundation 0.3.2", + "objc2-uniform-type-identifiers", +] + +[[package]] +name = "objc2-uniform-type-identifiers" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7902ac02859fc1f7045f8b598c63f1ae0cc7efeaa06a9bc9f3d9a3c955974fa4" +dependencies = [ + "objc2 0.6.5", + "objc2-foundation 0.3.2", ] [[package]] @@ -4494,7 +4517,7 @@ dependencies = [ [[package]] name = "stylo_taffy" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "bitflags", "stylo", @@ -4633,7 +4656,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.3", + "getrandom 0.3.4", "once_cell", "rustix", "windows-sys 0.61.2", @@ -5162,7 +5185,7 @@ dependencies = [ [[package]] name = "vello_gpu" version = "0.3.0" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "bytemuck", "glifo", @@ -5177,7 +5200,7 @@ dependencies = [ [[package]] name = "vello_gpu_shaders" version = "0.3.0" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#91ee344f520265c2836b294e2aec12295b399b75" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#8c0270d75ccf163581ebf63c31c51d489d796b01" dependencies = [ "naga 30.0.1", "wesl", diff --git a/Cargo.toml b/Cargo.toml index 5d52697..59cb441 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,6 +82,10 @@ anyrender_vello_hybrid = { git = "https://github.com/diaryx-org/blitz", branch = glifo = { git = "https://github.com/diaryx-org/blitz", branch = "leaf" } vello_gpu = { git = "https://github.com/diaryx-org/blitz", branch = "leaf" } vello_gpu_shaders = { git = "https://github.com/diaryx-org/blitz", branch = "leaf" } +# dioxus-native-dom, which turns Blitz's events into Dioxus's, from the same +# fork: leaf's events (a file input's change and cancel) do not compile with +# the published one, and its files() is the files chosen (PATCH.md there). +dioxus-native-dom = { git = "https://github.com/diaryx-org/blitz", branch = "leaf", version = "0.8.0-alpha.3" } [profile.dev] debug = "line-tables-only" diff --git a/README.md b/README.md index aa23db6..387f874 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,10 @@ unit = { git = "https://github.com/diaryx-org/unit", branch = "main" } | | | |---|---| -| Buttons | `Button` (default, primary, ghost, danger), `ToggleButton`, each with an `icon`; `Pressable`, a page's own row or tile that Tab reaches and Return and Space press | +| Buttons | `Button` (default, primary, ghost, danger), `ToggleButton`, each with an `icon`; `Pressable`, a page's own row or tile that Tab reaches and Return and Space press; `FileButton`, the system's file picker (an `accept`, `multiple`, a `directory`), a browser's or, on Blitz, the platform's | | Icons | `Icon`: a path on a 24-unit grid stroked in `currentColor`, the text's or an `Ink` of the theme's; `icon::paths` | | Choices | `Checkbox`, `Switch`, `RadioGroup`, `Segmented`, `Slider`, `Chips` (each with a colour's dot and a count, if it has them; or `single`, a radio group drawn as pills) | -| Text | `TextField` (text, search, email, URL, password, number; with an `action` at its end, pressed by Return too; a password `revealable` by an eye, and drawn as bullets on Blitz too with diaryx-org/blitz's `feat/password-input`), `TextArea`, `CodeField` (a one-time code: a cell for each digit, one field to assistive tech, a whole code pasted or filled in at once) | +| Text | `TextField` (text, search, email, URL, password, number; with an `action` at its end, pressed by Return too; a password `revealable` by an eye, and drawn as bullets on Blitz too), `TextArea`, `CodeField` (a one-time code: a cell for each digit, one field to assistive tech, a whole code pasted or filled in at once) | | Menus | `MenuButton`, `Select`, `ContextMenu` (the secondary button, or a finger held); items with shortcuts, ticks, icons, separators, submenus, and destructive ones; a `palette`, a few items side by side in one row (Copy, Share, Delete; or Reflow, Pages, Source, ticked where they stand), which keeps a long menu short | | Lists | `Section` (rows under a caption), `Row` (icon, title, subtitle, a note under it, value, a chevron or a tick, a control at the right, set in by a `depth`) | | Navigation | `Tabs`, `ListBox` (arrows, Home, End, typeahead), `Disclosure`, `NavBar` (back, forward, a title to press), `TabBar` (with a button of its own at the end), `Tree` and `TreeRow` | diff --git a/gallery/Cargo.toml b/gallery/Cargo.toml index 1c1a743..99c127a 100644 --- a/gallery/Cargo.toml +++ b/gallery/Cargo.toml @@ -32,6 +32,8 @@ dioxus = { version = "0.8.0-alpha.3", default-features = false, features = [ # the renderer patched from the fork (the root manifest's [patch]). dioxus-native = { version = "0.8.0-alpha.3", optional = true, default-features = false, features = [ "net", "svg", "system-fonts", "vello-hybrid", "accessibility", + # The system's picker for a file input (unit's FileButton). + "file-dialog", ] } dioxus-web = { version = "=0.8.0-alpha.3", optional = true } diff --git a/gallery/src/lib.rs b/gallery/src/lib.rs index 0e451c1..ff3b973 100644 --- a/gallery/src/lib.rs +++ b/gallery/src/lib.rs @@ -500,6 +500,14 @@ fn Page( Button { variant: Variant::Danger, onpress: move |_| said.set("Delete pressed.".into()), "Delete" } Button { disabled: true, onpress: move |_| said.set("Disabled pressed!".into()), "Disabled" } Button { icon: paths::PLUS, onpress: move |_| said.set("Add pressed.".into()), "Add" } + // The system's picker, by a press or Return and Space. + FileButton { label: "Choose Pictures…", icon: paths::PHOTO, accept: "image/*", + multiple: true, + onchoose: move |files: Vec| { + let names: Vec = files.iter().map(|f| f.name()).collect(); + said.set(format!("Chose {}.", names.join(", "))); + }, + oncancel: move |_| said.set("Chose nothing.".into()) } Button { icon: paths::SHARE, label: "Share", variant: Variant::Ghost, class: "unit-icon", onpress: move |_| said.set("Share pressed.".into()) } ToggleButton { pressed: bold(), label: "Bold", onchange: move |v| bold.set(v), b { "B" } } diff --git a/gallery/tests/blitz.rs b/gallery/tests/blitz.rs index 4e91661..14b4bce 100644 --- a/gallery/tests/blitz.rs +++ b/gallery/tests/blitz.rs @@ -1480,6 +1480,49 @@ fn a_password_is_shown_by_its_eye_and_hidden_again() { assert!(said.contains("8 characters"), "{said}"); } +#[test] +fn a_file_button_opens_the_picker_and_hears_the_files_chosen() { + let mut doc = page(1200, 800); + let input = labelled(&doc, "input", "Choose Pictures…"); + assert_eq!(attr(&doc, input, "type").as_deref(), Some("file")); + assert_eq!(attr(&doc, input, "accept").as_deref(), Some("image/*")); + // The face is drawn as a button's, out of assistive tech's way; the + // input, over it, is what is pressed and read. + let face = find(&doc, ".unit-file-button > .unit-button", "Choose Pictures…"); + assert_eq!(attr(&doc, face, "aria-hidden").as_deref(), Some("true")); + let (fx, fy, fw, fh) = rect(&doc, face); + let (ix, iy, iw, ih) = rect(&doc, input); + assert!( + (ix - fx).abs() < 0.5 + && (iy - fy).abs() < 0.5 + && (iw - fw).abs() < 0.5 + && (ih - fh).abs() < 0.5, + "the input covers the face: {:?} over {:?}", + (ix, iy, iw, ih), + (fx, fy, fw, fh) + ); + // Pressed here, with no system to show a picker, the press is answered + // as a picker dismissed. + click(&mut doc, input); + assert_eq!(said(&doc), "Chose nothing."); + // The system's answer, as a window gives it to the page. + let files = vec![ + blitz_traits::files::ChosenFile::from_bytes("a.png", vec![1, 2, 3].into(), 0), + blitz_traits::files::ChosenFile::from_bytes("b.jpg", vec![4].into(), 0), + blitz_traits::files::ChosenFile::from_bytes("notes.txt", vec![5].into(), 0), + ]; + doc.handle_ui_event(UiEvent::FilesChosen { + target: input, + files: Some(files), + }); + settle(&mut doc); + assert_eq!( + said(&doc), + "Chose a.png, b.jpg.", + "what accept does not take is left out" + ); +} + #[test] fn a_brief_toast_goes_by_itself() { let mut doc = page(1200, 800); @@ -1677,8 +1720,9 @@ fn tab_reaches_a_pressable_and_return_presses_it() { let other = find(&doc, "button", "Default"); click(&mut doc, other); assert_eq!(said(&doc), "Default pressed."); - // Primary, Ghost, Delete, Add, Share, Bold: Disabled and Italic passed by. - for _ in 0..7 { + // Primary, Ghost, Delete, Add, Choose Pictures… (its file input), Share, + // Bold: Disabled and Italic passed by. + for _ in 0..8 { key(&mut doc, Key::Named(NamedKey::Tab)); } assert_eq!(doc.inner.borrow().get_focussed_node_id(), Some(tile)); diff --git a/src/components/file_button.rs b/src/components/file_button.rs new file mode 100644 index 0000000..48e7aed --- /dev/null +++ b/src/components/file_button.rs @@ -0,0 +1,87 @@ +//! A button that chooses files: the system's picker, by a press or the +//! keys, in a browser and on Blitz alike. +//! +//! It is the renderer's own ``, unseen, laid over a +//! button's face: a press, a tap, Tab, Return and Space all reach the +//! input, and the renderer opens the system's picker for it (a browser's, +//! or on Blitz the platform's, from diaryx-org/blitz's `leaf`) and reads +//! the files back. No JavaScript, no picker of unit's own. + +use dioxus::html::FileData; +use dioxus::prelude::*; + +use crate::components::button::Variant; +use crate::icon::Icon; +use crate::unit::{Kind, use_focus}; + +/// A button that opens the system's file picker, and gives the files +/// chosen to `onchoose`: a database to open, a picture to insert. +/// +/// - `accept`: the types it takes, as an input's `accept` says them +/// (`.kdbx`, `image/*`, `application/pdf`); any file without. +/// - `multiple`: several at once; `directory`: a folder, its files every +/// one under it. +/// - `oncancel`: the picker was dismissed with nothing chosen. +/// +/// Each file is Dioxus's `FileData`: its name, size and type, and its +/// contents read when asked (`read_bytes`). On Blitz a file chosen from +/// disk has its path (`path()`); one a browser or a phone handed over has +/// only its name there. +#[component] +pub fn FileButton( + /// What the button says, and what assistive tech calls it. + label: String, + onchoose: EventHandler>, + oncancel: Option>, + accept: Option, + #[props(default)] multiple: bool, + #[props(default)] directory: bool, + #[props(default)] variant: Variant, + #[props(default)] disabled: bool, + /// An icon before the label: a path for [`Icon`]. + icon: Option<&'static str>, + #[props(default)] class: String, +) -> Element { + // The input takes its own keys, as a text field does: Return and Space + // open its picker, the renderer's default for a file input. + let focus = use_focus(Kind::Text, |_, _| false); + let dim = if disabled { " unit-face-disabled" } else { "" }; + rsx! { + span { class: "unit-file-button {class}", + // The face: drawn as a button is, but not one, so that neither + // presses, Tab nor assistive tech find it; the input over it is + // the control. + span { + class: "unit-button{variant.class()}{focus.class()}{dim}", + "aria-hidden": "true", + if let Some(d) = icon { + Icon { d, ink: variant.ink(disabled) } + } + "{label}" + } + input { + class: "unit-file-input", + r#type: "file", + accept, + multiple, + "webkitdirectory": directory.then_some("true"), + disabled, + "aria-label": "{label}", + onpointerdown: move |_| focus.press(), + onfocus: move |_| focus.dom_focus(), + onblur: move |_| focus.dom_blur(), + onchange: move |e: FormEvent| { + let files = e.files(); + if !files.is_empty() { + onchoose.call(files); + } + }, + oncancel: move |_| { + if let Some(h) = oncancel { + h.call(()); + } + }, + } + } + } +} diff --git a/src/components/mod.rs b/src/components/mod.rs index 6d36721..d1b931d 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -6,6 +6,7 @@ pub mod button; pub mod choice; pub mod code_field; pub mod collection; +pub mod file_button; pub mod hold; pub mod layout; pub mod list; diff --git a/src/lib.rs b/src/lib.rs index 63e7014..fc6a462 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,6 +81,7 @@ pub use components::choice::{ }; pub use components::code_field::CodeField; pub use components::collection::{Column, Sort, Table, TableRow, Tile, TileGrid}; +pub use components::file_button::FileButton; pub use components::hold::{Hold, LongPress, use_hold}; pub use components::layout::{ Align, Axis, Badge, Callout, Card, Divider, EmptyState, Field, Heading, Progress, Spacer, @@ -114,13 +115,13 @@ pub mod prelude { pub use crate::{ Accent, Accessory, ActionSheet, Align, Answer, Appearance, Axis, Badge, BarItem, Breadcrumbs, Button, Callout, Card, Checkbox, Chips, Choice, CodeField, Column, - ContextMenu, Density, Dialog, Disclosure, Divider, EmptyState, Entry, Field, Heading, Icon, - Ink, InputKind, Item, Keys, ListBox, LongPress, MenuButton, Modal, MotionSetting, NavBar, - Orientation, Photo, PhotoViewer, Pick, Placement, Pressable, Progress, RadioGroup, Row, - ScrollBar, SearchField, Section, Segmented, Select, Selection, SheetAction, Slider, Sort, - Spacer, Spot, Stack, Stepper, Swatch, SwatchAction, Swatches, Switch, TabBar, Table, - TableRow, Tabs, Text, TextArea, TextField, Theme, Tile, TileGrid, Toast, ToggleButton, - Tone, Tree, TreeRow, Unit, UnitRoot, Variant, use_unit, + ContextMenu, Density, Dialog, Disclosure, Divider, EmptyState, Entry, Field, FileButton, + Heading, Icon, Ink, InputKind, Item, Keys, ListBox, LongPress, MenuButton, Modal, + MotionSetting, NavBar, Orientation, Photo, PhotoViewer, Pick, Placement, Pressable, + Progress, RadioGroup, Row, ScrollBar, SearchField, Section, Segmented, Select, Selection, + SheetAction, Slider, Sort, Spacer, Spot, Stack, Stepper, Swatch, SwatchAction, Swatches, + Switch, TabBar, Table, TableRow, Tabs, Text, TextArea, TextField, Theme, Tile, TileGrid, + Toast, ToggleButton, Tone, Tree, TreeRow, Unit, UnitRoot, Variant, use_unit, }; } @@ -274,6 +275,39 @@ mod tests { assert!(html.contains("aria-pressed=\"false\""), "{html}"); } + #[test] + fn a_file_button_is_a_file_input_over_a_buttons_face() { + fn Page() -> Element { + rsx! { + UnitRoot { + FileButton { label: "Open Database…", accept: ".kdbx", onchoose: |_| {} } + FileButton { label: "Choose a Folder", directory: true, multiple: true, + disabled: true, onchoose: |_| {} } + } + } + } + let html = render(Page); + for needle in [ + "type=\"file\"", + "accept=\".kdbx\"", + "aria-label=\"Open Database…\"", + "webkitdirectory=\"true\"", + "class=\"unit-file-input\"", + ] { + assert!(html.contains(needle), "{needle} missing in {html}"); + } + // Each face drawn as a button, not one: out of Tab's and assistive + // tech's way. + assert_eq!( + html.matches(" Element { diff --git a/src/style.rs b/src/style.rs index bae7bfb..ce4a2d3 100644 --- a/src/style.rs +++ b/src/style.rs @@ -189,6 +189,19 @@ pub const CSS: &str = r#" .unit-ink-accent-text { color: var(--unit-accent-text); } .unit-ink-danger { color: var(--unit-danger); } .unit-button[disabled] { color: var(--unit-dim); box-shadow: none; } +.unit-file-button { position: relative; display: inline-flex; vertical-align: middle; } +.unit-file-button > .unit-button { pointer-events: none; } +.unit-file-button:hover > .unit-button:where(:not(.unit-face-disabled)) { background: var(--unit-control-hover); } +.unit-file-button:hover > .unit-button.unit-primary:where(:not(.unit-face-disabled)) { background: var(--unit-accent); opacity: 0.86; } +.unit-file-button:hover > .unit-button.unit-danger:where(:not(.unit-face-disabled)) { background: var(--unit-danger); opacity: 0.86; } +.unit-file-button:hover > .unit-button.unit-ghost:where(:not(.unit-face-disabled)) { background: var(--unit-control-hover); } +.unit-button.unit-face-disabled { color: var(--unit-dim); box-shadow: none; } +/* The renderer's own file input, unseen over the face: a press lands on it. + Blitz draws its own parts inside it, which are hidden. */ +.unit-file-input { position: absolute; left: 0; top: 0; right: 0; bottom: 0; width: 100%; height: 100%; + margin: 0; padding: 0; border: 0; opacity: 0; cursor: pointer; font-size: 0; } +.unit-file-input:disabled { cursor: default; } +.unit-file-input > * { display: none; } /* A toggle is often one glyph, B or I: never narrower than it is tall. */ .unit-toggle { min-width: var(--unit-control-height); } .unit-toggle.on { background: var(--unit-selection); border-color: var(--unit-accent); }