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
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ unit = { git = "https://github.com/diaryx-org/unit", branch = "main" }
| 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), `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 |
| 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` |
| Collections | `TileGrid` (a folder's icons, a library of photos: tiles of a fixed size or stretched to `fill` the row, with or without `labels`, under `section` headings, some not to be chosen, each with a button of its own) and `Table` (rows under column titles that sort), each choosing as a file browser does into a `Selection` the page keeps: a press alone, ⌘ or Ctrl to add, Shift for a range, the arrows, ⌘A, Space, Return or a double click to open, a letter to jump, a menu on the selection; on a phone a tap opens, or puts in and out while it `toggles` |
| Browsing | `Breadcrumbs` (the folders down to where the page is), `SearchField` (a magnifier, and a button and Escape to empty it), `EmptyState` (nothing here, and what to do), `PhotoViewer` (one photo over the window, by the arrows, its buttons or a swipe; zoomed as a phone's Photos zooms, by a pinch, a double tap or the wheel with ⌘ or Ctrl, on `unit::zoom`'s model) |
| Overlays | `Unit::open_dialog` (alerts, confirmations, a prompt), `Unit::open_actions` (an action sheet), `Modal` and its `Sheet` placement (a sheet along the bottom on a phone held upright, with Cancel and Done and dragged down to close, and a card in the middle of a wider window; `BottomSheet` for a sheet everywhere), `Unit::toast` (until dismissed, or for as long as it `lasts`) |
| Gestures | `LongPress` and `use_hold`: a finger held still, timed by the host's sleep (`Unit::set_timer`) |
| Layout | `Stack`, `Spacer`, `Card`, `Divider`, `Field` (a form's row), `Heading`, `Text`, `Badge`, `Progress`, `Callout` |
| Theme | `Theme`: a light and a dark `Palette`, a pointer's and a finger's `Metrics`, and a text and a heading font, written as CSS custom properties; `Appearance`, `Density` and `Accent` on the root |
| Theme | `Theme`: a light and a dark `Palette`, a pointer's and a finger's `Metrics` (corners among them), a `Shape` (edges, pills, shadows, how a menu's rows sit), and a text and a heading font, written as CSS custom properties; `Appearance`, `Density` and `Accent` on the root. Three ship: `Theme::default()` (Unit), `Theme::soft()` (Diaryx's: warm, round, faces by fill) and `Theme::sharp()` (square and ruled); the gallery switches between them, and `?theme=soft` opens it in one |

Its component list and its theme of tokens take after
[Libero](https://github.com/tdymel/libero)'s; how it is built does not.
Expand Down
4 changes: 4 additions & 0 deletions gallery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ blitz-traits = "=0.3.0-beta.4"
accesskit = "0.25"
atomic_refcell = "0.1"
keyboard-types = "0.8"

# The page's address on the web, for a theme asked for in it (`?theme=soft`).
[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3"
12 changes: 6 additions & 6 deletions gallery/audit.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ edge-contrast light/touch/390 unit-input.unit-textarea
edge-contrast light/touch/390 unit-radio-ring
edge-contrast light/touch/390 unit-slider-line
edge-contrast light/touch/390 unit-switch-track
focus-ring dark/regular/1200 RadioGroup:Appearance
focus-ring dark/touch/320 RadioGroup:Appearance
focus-ring dark/touch/390 RadioGroup:Appearance
focus-ring light/regular/1200 RadioGroup:Appearance
focus-ring light/touch/320 RadioGroup:Appearance
focus-ring light/touch/390 RadioGroup:Appearance
focus-ring dark/regular/1200 RadioGroup:Theme
focus-ring dark/touch/320 RadioGroup:Theme
focus-ring dark/touch/390 RadioGroup:Theme
focus-ring light/regular/1200 RadioGroup:Theme
focus-ring light/touch/320 RadioGroup:Theme
focus-ring light/touch/390 RadioGroup:Theme
keyboard dark/regular/1200 Button:Italic
keyboard dark/touch/320 Button:Italic
keyboard dark/touch/390 Button:Italic
Expand Down
74 changes: 62 additions & 12 deletions gallery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,49 @@ const DELETE: usize = 12;
const OPEN: usize = 20;
const RENAME: usize = 21;
const TRASH: usize = 22;
const COPY_FILE: usize = 23;
const SHARE_FILE: usize = 24;

/// The page, at the appearance and density the page itself chooses.
#[allow(non_snake_case)]
pub fn Gallery() -> Element {
let (appearance, density) = chosen();
let appearance = use_signal(|| appearance);
let density = use_signal(|| density);
let theme = use_signal(chosen_theme);
rsx! {
Shown { appearance, density, accent: Accent::System }
Shown { appearance, density, theme, accent: Accent::System }
}
}

/// Which of unit's themes the page starts in: the environment's
/// UNIT_THEME (unit, soft, sharp), or a web page's `?theme=soft`.
fn chosen_theme() -> usize {
#[cfg(not(target_arch = "wasm32"))]
let asked = std::env::var("UNIT_THEME").unwrap_or_default();
#[cfg(target_arch = "wasm32")]
let asked = web_query("theme");
Theme::all()
.iter()
.position(|t| t.name.eq_ignore_ascii_case(&asked))
.unwrap_or(0)
}

/// A query parameter of the page's address, on the web.
#[cfg(target_arch = "wasm32")]
fn web_query(name: &str) -> String {
let search = js_sys::eval("location.search")
.ok()
.and_then(|v| v.as_string())
.unwrap_or_default();
search
.trim_start_matches('?')
.split('&')
.find_map(|kv| kv.strip_prefix(name)?.strip_prefix('='))
.unwrap_or_default()
.to_string()
}

/// The appearance and density the environment asks for: a window can start
/// dark, or at a finger's density, for a look at each:
/// UNIT_APPEARANCE=dark, UNIT_DENSITY=touch.
Expand Down Expand Up @@ -59,8 +90,9 @@ pub fn GallerySized(width: f64, height: f64) -> Element {
let (appearance, density) = chosen();
let appearance = use_signal(|| appearance);
let density = use_signal(|| density);
let theme = use_signal(chosen_theme);
rsx! {
Shown { appearance, density, accent: Accent::Theme, size: (width, height) }
Shown { appearance, density, theme, accent: Accent::Theme, size: (width, height) }
}
}

Expand All @@ -73,26 +105,33 @@ pub fn GalleryAt(
density: Density,
/// The window's size, as a host would tell unit.
size: Option<(f64, f64)>,
/// Which of [`Theme::all`]: unit's own unless given.
#[props(default)]
theme: usize,
) -> Element {
let appearance = use_signal(|| appearance);
let density = use_signal(|| density);
let theme = use_signal(|| theme);
rsx! {
Shown { appearance, density, accent: Accent::Theme, size }
Shown { appearance, density, theme, accent: Accent::Theme, size }
}
}

#[component]
fn Shown(
appearance: Signal<Appearance>,
density: Signal<Density>,
/// Which of [`Theme::all`].
theme: Signal<usize>,
accent: Accent,
size: Option<(f64, f64)>,
) -> Element {
let shown = Theme::all().swap_remove(theme().min(Theme::all().len() - 1));
rsx! {
UnitRoot { appearance: appearance(), density: density(), accent, class: "gallery",
UnitRoot { appearance: appearance(), density: density(), accent, theme: shown, class: "gallery",
style { {GALLERY_CSS} }
Sized { size }
Page { appearance, density }
Page { appearance, density, theme }
// The page's own element, outside any component, as a host's
// editor is: unit's styles leave its box as the page drew it.
div { class: "gallery-own" }
Expand Down Expand Up @@ -198,7 +237,7 @@ html, body { margin: 0; }
"#;

#[component]
fn Page(appearance: Signal<Appearance>, density: Signal<Density>) -> Element {
fn Page(appearance: Signal<Appearance>, density: Signal<Density>, theme: Signal<usize>) -> Element {
let unit = use_unit();
#[cfg(not(target_arch = "wasm32"))]
use_hook(move || {
Expand Down Expand Up @@ -299,18 +338,21 @@ fn Page(appearance: Signal<Appearance>, density: Signal<Density>) -> Element {
let shown: Vec<String> = files.iter().map(|f| f.name.clone()).collect();
let tiles: Vec<Tile> = files.iter().map(File::tile).collect();
let rows: Vec<TableRow> = files.iter().map(File::row).collect();
// A file's menu, condensed: what is done most in a palette at its
// top, the rest in rows under it.
let file_menu = vec![
Entry::header(match chosen().len() {
1 => "1 file".to_string(),
n => format!("{n} files"),
}),
Item::new(OPEN, "Open").into(),
Item::new(RENAME, "Rename…").into(),
Entry::palette(vec![
Item::new(OPEN, "Open").icon(paths::ARROW_UP_RIGHT),
Item::new(RENAME, "Rename").icon(paths::PENCIL),
Item::new(COPY_FILE, "Duplicate").icon(paths::COPY),
Item::new(TRASH, "Trash").icon(paths::TRASH).destructive(),
]),
Entry::Separator,
Item::new(TRASH, "Move to Trash")
.icon(paths::TRASH)
.destructive()
.into(),
Item::new(SHARE_FILE, "Share…").icon(paths::SHARE).into(),
];
let on_file = move |id: usize| {
let n = chosen.peek().len();
Expand All @@ -322,6 +364,8 @@ fn Page(appearance: Signal<Appearance>, density: Signal<Density>) -> Element {
said.set(match id {
OPEN => format!("Open {what}."),
RENAME => format!("Rename {what}."),
COPY_FILE => format!("Duplicate {what}."),
SHARE_FILE => format!("Share {what}."),
_ => format!("Move {what} to the Trash."),
});
};
Expand Down Expand Up @@ -365,6 +409,12 @@ fn Page(appearance: Signal<Appearance>, density: Signal<Density>) -> Element {
Stack { axis: Axis::Horizontal, align: Align::Center, wrap: true,
Heading { level: 1, "unit" }
Spacer {}
Segmented {
label: "Theme",
options: Theme::all().iter().map(|t| t.name.to_string()).collect::<Vec<String>>(),
selected: Some(theme()),
onchange: move |i| theme.clone().set(i),
}
Segmented {
label: "Appearance",
options: vec!["System".into(), "Light".into(), "Dark".into()],
Expand Down
59 changes: 47 additions & 12 deletions gallery/tests/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,25 @@ struct At {
appearance: Appearance,
density: Density,
size: (f64, f64),
/// Which of `Theme::all()`.
theme: usize,
}

impl At {
fn of(m: Mode) -> At {
fn of(m: Mode, theme: usize) -> At {
At {
appearance: m.appearance,
density: m.density,
size: (m.width as f64, m.height as f64),
theme,
}
}
}

#[allow(non_snake_case)]
fn WholeGallery(at: At) -> Element {
rsx! {
GalleryAt { appearance: at.appearance, density: at.density, size: at.size }
GalleryAt { appearance: at.appearance, density: at.density, size: at.size, theme: at.theme }
}
}

Expand All @@ -197,6 +200,7 @@ fn LongText(at: At) -> Element {
let options: Vec<String> = vec![LONG.into(), WORD.into()];
rsx! {
UnitRoot { appearance: at.appearance, density: at.density,
theme: unit::Theme::all().swap_remove(at.theme),
Stack { gap: 12.0,
Heading { level: 1, "{LONG}" }
Heading { level: 1, "{WORD}" }
Expand Down Expand Up @@ -242,12 +246,15 @@ fn laid_out(vdom: VirtualDom, m: Mode) -> DioxusDocument {
doc
}

fn gallery(m: Mode) -> DioxusDocument {
laid_out(VirtualDom::new_with_props(WholeGallery, At::of(m)), m)
fn gallery(m: Mode, theme: usize) -> DioxusDocument {
laid_out(
VirtualDom::new_with_props(WholeGallery, At::of(m, theme)),
m,
)
}

fn long_text(m: Mode) -> DioxusDocument {
laid_out(VirtualDom::new_with_props(LongText, At::of(m)), m)
fn long_text(m: Mode, theme: usize) -> DioxusDocument {
laid_out(VirtualDom::new_with_props(LongText, At::of(m, theme)), m)
}

fn rect(doc: &DioxusDocument, node: NodeId) -> (f64, f64, f64, f64) {
Expand Down Expand Up @@ -773,16 +780,16 @@ impl Audit {
}
}

/// Every check in every mode.
fn audit() -> Audit {
/// Every check in every mode, in the `theme`th of `Theme::all()`.
fn audit(theme: usize) -> Audit {
let mut a = Audit::default();
for m in MODES {
let mut doc = gallery(m);
let mut doc = gallery(m, theme);
a.page(&doc, m, "page");
a.keyboard(&mut doc, m);

for (scene, button, scrolled) in SCENES {
let mut doc = gallery(m);
let mut doc = gallery(m, theme);
let target = {
let inner = doc.inner.borrow();
let all = inner.query_selector_all("button").unwrap();
Expand Down Expand Up @@ -811,7 +818,7 @@ fn audit() -> Audit {
}
}

let doc = long_text(m);
let doc = long_text(m, theme);
a.page(&doc, m, "long-text");
}
a
Expand Down Expand Up @@ -862,7 +869,7 @@ fn report(a: &Audit) -> String {
/// The list of failures only shrinks. See the top of this file.
#[test]
fn the_audit_holds() {
let a = audit();
let a = audit(0);
// A file, as corgi shows nothing a passing test prints.
std::fs::create_dir_all("../target").unwrap();
std::fs::write(REPORT, report(&a)).unwrap();
Expand Down Expand Up @@ -901,6 +908,34 @@ fn the_audit_holds() {
);
}

/// Every other theme unit ships fails no cell its own theme passes: a
/// theme may round, recolour and unedge a page, and still never take a
/// check away from it. Its failures, all of them on the baseline, are in
/// `target/audit-<theme>.txt`.
#[test]
fn every_theme_fails_only_what_units_own_does() {
let known = listed(Path::new(BASELINE));
for (i, theme) in unit::Theme::all().iter().enumerate().skip(1) {
let a = audit(i);
let name = theme.name.to_lowercase();
std::fs::create_dir_all("../target").unwrap();
std::fs::write(format!("../target/audit-{name}.txt"), report(&a)).unwrap();
let new: Vec<String> = a
.failed
.iter()
.filter(|(c, _)| !known.contains(*c))
.map(|(c, why)| format!(" {c} # {why}"))
.collect();
assert!(
new.is_empty(),
"{} fails {} cells unit's own theme passes:\n{}",
theme.name,
new.len(),
new.join("\n")
);
}
}

/// A component the gallery does not show is one the audit cannot see.
#[test]
fn the_gallery_shows_every_component() {
Expand Down
Loading
Loading