use std::fmt;
use crate::any_value::Opaque;
use crate::custom::CustomProps;
use crate::draw::DisplayList;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NodeId(pub(crate) u32);
impl NodeId {
pub fn raw(self) -> u32 {
self.0
}
pub fn from_raw(raw: u32) -> NodeId {
NodeId(raw)
}
}
impl fmt::Display for NodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "#{}", self.0)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum WidgetKind {
Window,
Container,
Fragment,
Text,
Button,
TextInput,
Checkbox,
Switch,
ScrollView,
Custom(&'static str),
Native,
}
impl WidgetKind {
pub fn is_native(self) -> bool {
self != WidgetKind::Fragment
}
pub fn is_container(self) -> bool {
matches!(self, WidgetKind::Window | WidgetKind::Container | WidgetKind::ScrollView)
}
pub fn name(self) -> &'static str {
match self {
WidgetKind::Window => "Window",
WidgetKind::Container => "Container",
WidgetKind::ScrollView => "ScrollView",
WidgetKind::Fragment => "Fragment",
WidgetKind::Text => "Text",
WidgetKind::Button => "Button",
WidgetKind::TextInput => "TextInput",
WidgetKind::Checkbox => "Checkbox",
WidgetKind::Switch => "Switch",
WidgetKind::Custom(name) => name,
WidgetKind::Native => "Native",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TextStyle {
LargeTitle,
Title,
Headline,
#[default]
Body,
Callout,
Caption,
Monospace,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ButtonVariant {
#[default]
Default,
Primary,
Destructive,
Plain,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ScrollAxes {
#[default]
Vertical,
Horizontal,
Both,
}
impl ScrollAxes {
pub fn horizontal(self) -> bool {
matches!(self, ScrollAxes::Horizontal | ScrollAxes::Both)
}
pub fn vertical(self) -> bool {
matches!(self, ScrollAxes::Vertical | ScrollAxes::Both)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Prop {
Title(String),
Text(String),
Label(String),
Value(String),
Placeholder(String),
Checked(bool),
Enabled(bool),
TextStyle(TextStyle),
Variant(ButtonVariant),
ScrollAxes(ScrollAxes),
Custom(CustomProps),
Drawing(DisplayList),
Native(Opaque),
}
impl Prop {
pub fn key(&self) -> std::mem::Discriminant<Prop> {
std::mem::discriminant(self)
}
pub fn affects_measure(&self) -> bool {
matches!(
self,
Prop::Text(_)
| Prop::Label(_)
| Prop::Placeholder(_)
| Prop::TextStyle(_)
| Prop::Variant(_)
| Prop::Custom(_)
| Prop::Native(_)
)
}
}
#[macro_export]
macro_rules! find_prop {
($props:expr, $variant:ident) => {
$props.iter().find_map(|p| match p {
$crate::Prop::$variant(v) => Some(v.clone()),
_ => None,
})
};
}
macro_rules! static_value {
($($t:ty),*) => {$(
impl mitsuami_reactive::IntoValue<$t> for $t {
fn into_value(self) -> mitsuami_reactive::Value<$t> {
mitsuami_reactive::Value::Static(self)
}
}
)*};
}
static_value!(TextStyle, ButtonVariant, ScrollAxes);