use std::marker::PhantomData;
pub struct Dialog<Node, Message> {
pub content: Node,
_marker: PhantomData<Message>,
}
impl<Node, Message> Dialog<Node, Message> {
pub fn new(content: Node) -> Self {
Self {
content,
_marker: PhantomData,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SheetHeight {
OneThird,
Half,
TwoThirds,
Ratio(f32),
Pixels(f32),
}
impl SheetHeight {
pub const DEFAULT: SheetHeight = SheetHeight::OneThird;
#[must_use]
pub fn as_ratio(self) -> Option<f32> {
match self {
SheetHeight::OneThird => Some(1.0 / 3.0),
SheetHeight::Half => Some(0.5),
SheetHeight::TwoThirds => Some(2.0 / 3.0),
SheetHeight::Ratio(r) => Some(r.clamp(0.0, 1.0)),
SheetHeight::Pixels(_) => None,
}
}
#[must_use]
pub fn as_pixels(self) -> Option<f32> {
match self {
SheetHeight::Pixels(p) => Some(p),
_ => None,
}
}
}
pub struct BottomSheet<Node, Message> {
pub content: Node,
pub height: SheetHeight,
_marker: PhantomData<Message>,
}
impl<Node, Message> BottomSheet<Node, Message> {
pub fn new(content: Node) -> Self {
Self {
content,
height: SheetHeight::DEFAULT,
_marker: PhantomData,
}
}
#[must_use]
pub fn with_height(mut self, height: SheetHeight) -> Self {
self.height = height;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ratio_resolves_correctly() {
assert_eq!(SheetHeight::OneThird.as_ratio(), Some(1.0 / 3.0));
assert_eq!(SheetHeight::Half.as_ratio(), Some(0.5));
assert_eq!(SheetHeight::TwoThirds.as_ratio(), Some(2.0 / 3.0));
assert_eq!(SheetHeight::Ratio(0.25).as_ratio(), Some(0.25));
assert_eq!(SheetHeight::Pixels(240.0).as_ratio(), None);
}
#[test]
fn ratio_is_clamped() {
assert_eq!(SheetHeight::Ratio(1.5).as_ratio(), Some(1.0));
assert_eq!(SheetHeight::Ratio(-0.1).as_ratio(), Some(0.0));
}
}