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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ trybuild = "1"
# eggshell's own tests drive views through `testing::Driver`.
eggshell-ui = { path = ".", features = ["driver"] }
eggshell-ui-shadcn = { path = "crates/eggshell-ui-shadcn", features = ["extras"] }
eggshell-ui-extras = { path = "crates/eggshell-ui-extras", features = ["form", "hover-card", "markdown", "badge", "spinner", "skeleton", "command"] }
eggshell-ui-extras = { path = "crates/eggshell-ui-extras", features = ["form", "hover-card", "markdown", "badge", "spinner", "skeleton", "command", "notify"] }

# proptest's rng wants a getrandom backend that the browser build does not
# select, and the fuzzers only run natively.
Expand Down
1 change: 1 addition & 0 deletions crates/eggshell-ui-extras/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ form = []
spinner = []
skeleton = []
command = ["dep:unicode-normalization"]
notify = []

[dependencies]
eggshell-ui = { path = "../..", version = "0.1.0" }
Expand Down
2 changes: 2 additions & 0 deletions crates/eggshell-ui-extras/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub mod form;
pub mod hover_card;
#[cfg(feature = "markdown")]
pub mod markdown;
#[cfg(feature = "notify")]
pub mod notify;
#[cfg(feature = "skeleton")]
pub mod skeleton;
#[cfg(feature = "spinner")]
Expand Down
115 changes: 115 additions & 0 deletions crates/eggshell-ui-extras/src/notify/dwell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//! How long a notice has been on screen, on a clock the caller passes in.

use core::time::Duration;

/// Whether the dwell counts the time since the last tick.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Hold {
Running,
Held,
}

/// What a tick found.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Countdown {
/// This much is left.
Left(Duration),
/// The dwell ran out at this tick.
Expires,
/// It ran out at an earlier tick.
Over,
}

/// One notice's dwell.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Dwell {
Counting { elapsed: Duration, seen: Duration },
Over,
}

impl Dwell {
/// A dwell first seen at `now`.
pub(super) fn new(now: Duration) -> Self {
Self::Counting {
elapsed: Duration::ZERO,
seen: now,
}
}

/// Moves the dwell to `now`: the time since the last tick counts unless
/// it is `hold`'s. Reports [`Countdown::Expires`] once.
pub(super) fn tick(&mut self, now: Duration, length: Duration, hold: Hold) -> Countdown {
let Self::Counting { elapsed, seen } = *self else {
return Countdown::Over;
};
let elapsed = match hold {
Hold::Running => elapsed.saturating_add(now.saturating_sub(seen)),
Hold::Held => elapsed,
};
let left = length.saturating_sub(elapsed);
if left.is_zero() {
*self = Self::Over;
Countdown::Expires
} else {
*self = Self::Counting { elapsed, seen: now };
Countdown::Left(left)
}
}
}

#[cfg(test)]
mod tests {
use super::*;

fn ms(ms: u64) -> Duration {
Duration::from_millis(ms)
}

#[test]
fn a_dwell_counts_down_and_expires_once() {
let mut dwell = Dwell::new(ms(1000));
assert_eq!(
dwell.tick(ms(1000), ms(500), Hold::Running),
Countdown::Left(ms(500))
);
assert_eq!(
dwell.tick(ms(1300), ms(500), Hold::Running),
Countdown::Left(ms(200))
);
assert_eq!(
dwell.tick(ms(1500), ms(500), Hold::Running),
Countdown::Expires
);
assert_eq!(
dwell.tick(ms(1600), ms(500), Hold::Running),
Countdown::Over
);
}

#[test]
fn held_time_is_not_counted_and_the_dwell_resumes_where_it_left_off() {
let mut dwell = Dwell::new(ms(0));
dwell.tick(ms(300), ms(500), Hold::Running);
assert_eq!(
dwell.tick(ms(5000), ms(500), Hold::Held),
Countdown::Left(ms(200))
);
assert_eq!(
dwell.tick(ms(5100), ms(500), Hold::Running),
Countdown::Left(ms(100))
);
assert_eq!(
dwell.tick(ms(5200), ms(500), Hold::Running),
Countdown::Expires
);
}

#[test]
fn a_clock_that_goes_back_counts_nothing() {
let mut dwell = Dwell::new(ms(1000));
assert_eq!(
dwell.tick(ms(400), ms(500), Hold::Running),
Countdown::Left(ms(500))
);
}
}
68 changes: 68 additions & 0 deletions crates/eggshell-ui-extras/src/notify/look.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! What a notice looks like until the render round settles its style: the
//! page's popover surface, at sonner's sizes.

use eggshell_ui::{Style, TextSpec, Tw, tw};
use egui::epaint::Shadow;

pub(super) struct Look {
fill: egui::Color32,
border: egui::Color32,
text: egui::Color32,
border_width: f32,
radius: u8,
shadow: Option<Shadow>,
role: TextSpec,
}

const WIDTH: f32 = 356.0;
/// Tailwind spacing units.
const PADDING: f32 = 4.0;
const INNER_GAP: f32 = 1.5;
const STACK_GAP: f32 = 3.5;

impl Look {
pub(super) fn of(style: &Style) -> Self {
let popup = &style.menu.popup;
let colors = popup.paint.states.rest;
Self {
fill: colors.fill,
border: colors.border,
text: colors.text,
border_width: popup.border_width,
radius: style.menu.radius,
shadow: popup.paint.shadow,
role: style
.menu
.text
.clone()
.unwrap_or_else(|| style.text.clone()),
}
}

pub(super) fn stack(&self) -> Tw {
tw().flex().flex_col().gap(STACK_GAP).sense_hover()
}

pub(super) fn card(&self) -> Tw {
let card = tw()
.flex()
.items_center()
.gap(INNER_GAP)
.w_px(WIDTH)
.p(PADDING)
.border(self.border_width)
.border_color(self.border)
.bg(self.fill)
.text(self.text)
.rounded_px(f32::from(self.radius))
.role(self.role.clone());
match self.shadow {
Some(shadow) => card.shadow(shadow),
None => card,
}
}

pub(super) fn sentence(&self) -> Tw {
tw().flex_1().min_w_0()
}
}
Loading
Loading