Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
2 changes: 2 additions & 0 deletions gallery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
8 changes: 8 additions & 0 deletions gallery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dioxus::html::FileData>| {
let names: Vec<String> = 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" } }
Expand Down
48 changes: 46 additions & 2 deletions gallery/tests/blitz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
87 changes: 87 additions & 0 deletions src/components/file_button.rs
Original file line number Diff line number Diff line change
@@ -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 `<input type=file>`, 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<Vec<FileData>>,
oncancel: Option<EventHandler<()>>,
accept: Option<String>,
#[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(());
}
},
}
}
}
}
1 change: 1 addition & 0 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading