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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ unit = { git = "https://github.com/diaryx-org/unit", branch = "main" }
| 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` (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 |
| 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, on a spring) and `Theme::sharp()` (square and ruled); the gallery switches between them, and `?theme=soft` opens it in one. A theme's `Motion` times its changes: a switch's knob sliding, a hover easing, a menu, dialog, sheet or toast growing in and fading out; `MotionSetting` on the root keeps a page still where the system or the host asks, and a terminal's cells never move |

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
54 changes: 46 additions & 8 deletions gallery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ pub fn Gallery() -> Element {
let appearance = use_signal(|| appearance);
let density = use_signal(|| density);
let theme = use_signal(chosen_theme);
let motion = use_signal(chosen_motion);
rsx! {
Shown { appearance, density, theme, accent: Accent::System }
Shown { appearance, density, theme, motion, accent: Accent::System }
}
}

/// How the page moves: the environment's UNIT_MOTION (full, reduced), or a
/// web page's `?motion=reduced`; the system's otherwise.
fn chosen_motion() -> MotionSetting {
#[cfg(not(target_arch = "wasm32"))]
let asked = std::env::var("UNIT_MOTION").unwrap_or_default();
#[cfg(target_arch = "wasm32")]
let asked = web_query("motion");
match asked.as_str() {
"full" => MotionSetting::Full,
"reduced" => MotionSetting::Reduced,
_ => MotionSetting::System,
}
}

Expand Down Expand Up @@ -84,15 +99,24 @@ fn chosen() -> (Appearance, Density) {

/// The page as [`Gallery`] draws it, told its window is `width` × `height`
/// CSS pixels: what a window's host tells unit ([`Unit::set_viewport`]),
/// for the tests, which lay it out with no window.
/// for the tests, which lay it out with no window. Still unless `moving`:
/// a test's clock stands at 0 unless it moves it, so whatever moved would
/// stand where its motion began.
#[component]
pub fn GallerySized(width: f64, height: f64) -> Element {
pub fn GallerySized(width: f64, height: f64, #[props(default)] moving: bool) -> Element {
let (appearance, density) = chosen();
let appearance = use_signal(|| appearance);
let density = use_signal(|| density);
let theme = use_signal(chosen_theme);
let motion = use_signal(move || {
if moving {
MotionSetting::Full
} else {
MotionSetting::Reduced
}
});
rsx! {
Shown { appearance, density, theme, accent: Accent::Theme, size: (width, height) }
Shown { appearance, density, theme, motion, accent: Accent::Theme, size: (width, height) }
}
}

Expand All @@ -112,8 +136,10 @@ pub fn GalleryAt(
let appearance = use_signal(|| appearance);
let density = use_signal(|| density);
let theme = use_signal(|| theme);
// Still: the audit reads every look as it is at rest.
let motion = use_signal(|| MotionSetting::Reduced);
rsx! {
Shown { appearance, density, theme, accent: Accent::Theme, size }
Shown { appearance, density, theme, motion, accent: Accent::Theme, size }
}
}

Expand All @@ -123,15 +149,16 @@ fn Shown(
density: Signal<Density>,
/// Which of [`Theme::all`].
theme: Signal<usize>,
motion: Signal<MotionSetting>,
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, theme: shown, class: "gallery",
UnitRoot { appearance: appearance(), density: density(), accent, theme: shown, motion: motion(), class: "gallery",
style { {GALLERY_CSS} }
Sized { size }
Page { appearance, density, theme }
Page { appearance, density, theme, motion }
// 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 @@ -237,7 +264,12 @@ html, body { margin: 0; }
"#;

#[component]
fn Page(appearance: Signal<Appearance>, density: Signal<Density>, theme: Signal<usize>) -> Element {
fn Page(
appearance: Signal<Appearance>,
density: Signal<Density>,
theme: Signal<usize>,
motion: Signal<MotionSetting>,
) -> Element {
let unit = use_unit();
#[cfg(not(target_arch = "wasm32"))]
use_hook(move || {
Expand Down Expand Up @@ -432,6 +464,12 @@ fn Page(appearance: Signal<Appearance>, density: Signal<Density>, theme: Signal<
unit.set_keyboard(if i == 2 { 336.0 } else { 0.0 });
},
}
Segmented {
label: "Motion",
options: vec!["System".into(), "Full".into(), "Reduced".into()],
selected: Some(match motion() { MotionSetting::System => 0, MotionSetting::Full => 1, MotionSetting::Reduced => 2 }),
onchange: move |i| motion.clone().set([MotionSetting::System, MotionSetting::Full, MotionSetting::Reduced][i]),
}
Segmented {
label: "Density",
options: vec!["Pointer".into(), "Touch".into()],
Expand Down
2 changes: 1 addition & 1 deletion gallery/tests/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +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),
theme: unit::Theme::all().swap_remove(at.theme), motion: unit::MotionSetting::Reduced,
Stack { gap: 12.0,
Heading { level: 1, "{LONG}" }
Heading { level: 1, "{WORD}" }
Expand Down
132 changes: 132 additions & 0 deletions gallery/tests/blitz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3291,3 +3291,135 @@ fn a_nav_bar_centres_its_title_and_gives_way_to_its_buttons() {
}
}
}

/// The gallery with motion on, as [`page`] is without it.
#[allow(non_snake_case)]
fn Moving(window: Window) -> Element {
rsx! {
unit_gallery::GallerySized { width: window.width, height: window.height, moving: true }
}
}

fn page_moving(width: u32, height: u32) -> DioxusDocument {
let vdom = VirtualDom::new_with_props(
Moving,
Window {
width: width as f64,
height: height as f64,
},
);
let mut doc = DioxusDocument::new(vdom, DocumentConfig::default());
doc.inner
.borrow_mut()
.set_viewport(Viewport::new(width, height, 1.0, ColorScheme::Light));
doc.initial_build();
settle(&mut doc);
doc
}

/// Poll the page and lay it out with the clock at `seconds`.
fn at_time(doc: &mut DioxusDocument, seconds: f64) {
for _ in 0..3 {
Document::poll(doc, Some(Context::from_waker(Waker::noop())));
doc.inner.borrow_mut().resolve(seconds);
}
}

fn opacity(doc: &DioxusDocument, node: NodeId) -> f64 {
doc.inner
.borrow()
.resolved_style_value(node, "opacity")
.parse()
.unwrap_or(f64::NAN)
}

/// With motion on, a menu grows into place as Blitz's clock runs, and
/// once closed is drawn going, then taken away when its motion is over.
#[test]
fn a_menu_comes_and_goes_in_the_themes_motion() {
let mut doc = page_moving(1200, 800);
let edit = find(&doc, "button", "Edit ▾");
click(&mut doc, edit);
let menu = doc
.inner
.borrow()
.query_selector(".unit-menu")
.unwrap()
.unwrap();
at_time(&mut doc, 0.0);
let start = opacity(&doc, menu);
at_time(&mut doc, 0.06);
let during = opacity(&doc, menu);
at_time(&mut doc, 1.0);
let end = opacity(&doc, menu);
assert!(
start < during && during < end && end == 1.0,
"{start} → {during} → {end}"
);

// Closed, the menu is gone at once to whatever reads it, and a drawing
// of it goes.
let primary = find(&doc, "button", "Primary");
click(&mut doc, primary);
at_time(&mut doc, 1.0);
assert!(!displayed(&doc, menu), "the menu itself is closed");
let going = doc
.inner
.borrow()
.query_selector(".unit-menu.unit-leaving")
.unwrap()
.unwrap();
assert!(displayed(&doc, going), "drawn going");
at_time(&mut doc, 1.06);
let fading = opacity(&doc, going);
at_time(&mut doc, 2.0);
let gone = opacity(&doc, going);
assert!(fading < 1.0 && gone == 0.0, "{fading} → {gone}");
// The host's sleep ends its time, and the drawing is taken away.
std::thread::sleep(std::time::Duration::from_millis(300));
at_time(&mut doc, 2.0);
assert!(!displayed(&doc, going), "taken away");
}

/// With motion on, a dismissed toast sinks away and is then taken out; the
/// toasts that read it are told at once that it is gone.
#[test]
fn a_dismissed_toast_sinks_away_then_goes() {
let mut doc = page_moving(1200, 800);
let toast = find(&doc, "button", "Toast");
click(&mut doc, toast);
at_time(&mut doc, 1.0);
let dismiss = {
let inner = doc.inner.borrow();
let all = inner
.query_selector_all(".unit-toast [aria-label=\"Dismiss\"]")
.unwrap();
*all.first().expect("a toast to dismiss")
};
click(&mut doc, dismiss);
at_time(&mut doc, 1.0);
let going = doc
.inner
.borrow()
.query_selector(".unit-toast.unit-leaving")
.unwrap()
.expect("drawn going");
assert_eq!(
doc.inner
.borrow()
.resolved_style_value(going, "pointer-events"),
"none"
);
at_time(&mut doc, 2.0);
assert_eq!(opacity(&doc, going), 0.0, "sunk away");
std::thread::sleep(std::time::Duration::from_millis(300));
at_time(&mut doc, 2.0);
assert!(
doc.inner
.borrow()
.query_selector(".unit-toast")
.unwrap()
.is_none(),
"taken out"
);
}
Loading
Loading