use dioxus::prelude::*;
use std::fmt;
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
pub struct Percent(f64);
impl Percent {
#[must_use]
pub fn new(value: f64) -> Self {
if value.is_nan() {
Self(0.0)
} else {
Self(value.clamp(0.0, 100.0))
}
}
#[must_use]
pub fn of(value: f64, max: f64) -> Self {
if max > 0.0 {
Self::new(value / max * 100.0)
} else {
Self(0.0)
}
}
#[must_use]
pub const fn get(self) -> f64 {
self.0
}
}
impl fmt::Display for Percent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.fract() == 0.0 {
write!(f, "{:.0}", self.0)
} else {
write!(f, "{}", self.0)
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CustomProperty {
ProgressValue,
ToolProgress,
SliderPct,
SplitSize,
SplitBlockSize,
}
impl CustomProperty {
const fn name(self) -> &'static str {
match self {
Self::ProgressValue => "--fc-progress-value",
Self::ToolProgress => "--fc-tool-progress",
Self::SliderPct => "--fc-slider-pct",
Self::SplitSize => "--fc-split-size",
Self::SplitBlockSize => "--fc-split-block-size",
}
}
}
pub(crate) fn custom_property(property: CustomProperty, value: impl fmt::Display) -> Attribute {
Attribute::new(
"style",
format!("{}: {value}", property.name()),
None,
false,
)
}
pub(crate) fn grid_columns(row_headers: bool, columns: usize) -> Attribute {
let lead = if row_headers { "auto " } else { "" };
Attribute::new(
"style",
format!("grid-template-columns: {lead}repeat({columns}, minmax(0, 1fr))"),
None,
false,
)
}