use egui::{Response, Ui, Widget, WidgetText};
use crate::metric_slider::MetricSlider;
use crate::slider::SliderHandle;
use crate::theme::Accent;
#[must_use = "Call `ui.add(...)` to render the widget."]
pub struct PercentSlider<'a> {
inner: MetricSlider<'a>,
}
impl<'a> std::fmt::Debug for PercentSlider<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PercentSlider")
.field("inner", &self.inner)
.finish()
}
}
impl<'a> PercentSlider<'a> {
pub fn new(value: &'a mut f32) -> Self {
Self {
inner: MetricSlider::new(value, 0.0..=100.0)
.suffix("%")
.tick_fmt(|v| {
if v == 0.0 {
"0".to_string()
} else {
format!("{}%", v.round() as i32)
}
}),
}
}
#[inline]
pub fn label(mut self, label: impl Into<WidgetText>) -> Self {
self.inner = self.inner.label(label);
self
}
#[inline]
pub fn accent(mut self, accent: Accent) -> Self {
self.inner = self.inner.accent(accent);
self
}
#[inline]
pub fn show_ticks(mut self, show: bool) -> Self {
self.inner = self.inner.show_ticks(show);
self
}
#[inline]
pub fn step(mut self, step: f32) -> Self {
self.inner = self.inner.step(step);
self
}
#[inline]
pub fn steps(mut self, n: usize) -> Self {
self.inner = self.inner.steps(n);
self
}
#[inline]
pub fn stops(mut self, positions: impl IntoIterator<Item = f32>) -> Self {
self.inner = self.inner.stops(positions);
self
}
#[inline]
pub fn decimals(mut self, n: usize) -> Self {
self.inner = self.inner.decimals(n);
self
}
#[inline]
pub fn callout_fmt(mut self, fmt: impl Fn(f32) -> String + 'a) -> Self {
self.inner = self.inner.callout_fmt(fmt);
self
}
#[inline]
pub fn desired_width(mut self, width: f32) -> Self {
self.inner = self.inner.desired_width(width);
self
}
#[inline]
pub fn handle(mut self, handle: SliderHandle) -> Self {
self.inner = self.inner.handle(handle);
self
}
}
impl<'a> Widget for PercentSlider<'a> {
fn ui(self, ui: &mut Ui) -> Response {
self.inner.ui(ui)
}
}