use std::f32::consts::TAU;
use crate::accessibility::Role;
use crate::animation::Tuning;
use crate::color::Color;
use crate::context::{Response, Sense};
use crate::geometry::{Rect, Vec2, vec2};
use crate::material::Corner;
use crate::text::{TextStyle, Weight};
use crate::ui::{Ui, Widget};
pub struct ProgressBar {
value: f32,
height: f32,
tint: Option<Color>,
track: Option<Color>,
}
impl ProgressBar {
pub fn new(value: f32) -> Self {
Self { value, height: 6.0, tint: None, track: None }
}
pub fn height(mut self, height: f32) -> Self {
self.height = height;
self
}
pub fn tint(mut self, color: Color) -> Self {
self.tint = Some(color);
self
}
pub fn track(mut self, color: Color) -> Self {
self.track = Some(color);
self
}
}
impl Widget for ProgressBar {
fn ui(self, ui: &mut Ui) -> Response {
let w = ui.available_width().clamp(60.0, 460.0);
let r = ui.allocate_response(vec2(w, self.height), Sense::Hover);
if let Some(n) = ui.access(r.id, Role::ProgressIndicator, r.rect) {
n.numeric(self.value.clamp(0.0, 1.0) as f64, 0.0, 1.0, None);
}
let v = ui.ctx.animate(r.id, self.value.clamp(0.0, 1.0), Tuning::new(0.5, 1.0));
let fill = self.tint.unwrap_or(ui.ctx.theme.accent);
let track = self.track.unwrap_or(ui.surface.strong_fill_color());
let mut p = ui.painter();
p.fill(r.rect, Corner::Capsule, track);
if v > 0.0005 {
let fw = r.rect.width() * v;
let h = self.height.min(fw.max(self.height * 0.5));
let bar =
Rect::from_min_size(vec2(r.rect.min.x, r.rect.center().y - h * 0.5), vec2(fw.max(h), h));
p.fill(bar, Corner::Capsule, fill);
}
r
}
}
pub struct Gauge<'a> {
value: f32,
size: f32,
label: &'a str,
tint: Color,
thickness: Option<f32>,
label_size: Option<f32>,
padding: Option<f32>,
}
impl<'a> Gauge<'a> {
pub fn new(value: f32, label: &'a str) -> Self {
Self {
value,
size: 64.0,
label,
tint: Color::GREEN,
thickness: None,
label_size: None,
padding: None,
}
}
pub fn size(mut self, size: f32) -> Self {
self.size = size;
self
}
pub fn tint(mut self, color: Color) -> Self {
self.tint = color;
self
}
pub fn thickness(mut self, thickness: f32) -> Self {
self.thickness = Some(thickness);
self
}
pub fn label_size(mut self, size: f32) -> Self {
self.label_size = Some(size);
self
}
pub fn label_padding(mut self, padding: f32) -> Self {
self.padding = Some(padding);
self
}
}
const MIN_GAUGE_LABEL: f32 = 7.0;
impl Widget for Gauge<'_> {
fn ui(self, ui: &mut Ui) -> Response {
let r = ui.allocate_response(Vec2::splat(self.size), Sense::Hover);
if let Some(n) = ui.access(r.id, Role::Meter, r.rect) {
n.label(self.label).numeric(self.value.clamp(0.0, 1.0) as f64, 0.0, 1.0, None);
}
let v = ui.ctx.animate(r.id, self.value.clamp(0.0, 1.0), Tuning::new(0.6, 0.85));
let radius = self.size * 0.5;
let w = self.thickness.unwrap_or(self.size * 0.11).clamp(1.0, radius * 0.6);
let pad = self.padding.unwrap_or(self.size * 0.09).max(0.0);
let room = (radius - w - pad).max(1.0);
let l = fit_label(ui, self.label, self.label_size.unwrap_or(self.size * 0.22), room);
let (track, tc) = (ui.surface.fill_color(), ui.surface.text_color());
let center = r.rect.center();
let mut p = ui.painter();
p.arc(center, radius, w, 0.0, TAU, track, track);
if v > 0.001 {
let cap = w / (radius - w * 0.5);
let sweep = (v * TAU).min(TAU - cap * 0.35);
p.arc(center, radius, w, -TAU / 4.0, sweep, self.tint.lerp(Color::WHITE, 0.2), self.tint);
}
p.text_centered(r.rect, &l, tc, 1.0);
r
}
}
fn fit_label(ui: &mut Ui, text: &str, size: f32, room: f32) -> std::rc::Rc<crate::text::TextLayout> {
let style = |s: f32| TextStyle::new(s).weight(Weight::Semibold);
let l = ui.layout_text(text, &style(size));
let half = vec2(l.size.x, l.size.y * 0.74) * 0.5;
let k = room / half.length().max(0.01);
if k >= 1.0 {
return l;
}
let fitted = ((size * k * 2.0).floor() * 0.5).max(MIN_GAUGE_LABEL);
ui.layout_text(text, &style(fitted))
}
pub struct Spinner {
size: f32,
}
impl Default for Spinner {
fn default() -> Self {
Self::new()
}
}
impl Spinner {
pub fn new() -> Self {
Self { size: 24.0 }
}
pub fn size(mut self, size: f32) -> Self {
self.size = size;
self
}
}
impl Widget for Spinner {
fn ui(self, ui: &mut Ui) -> Response {
let r = ui.allocate_response(Vec2::splat(self.size), Sense::Hover);
let t = ui.ctx.time as f32;
let c = ui.surface.text_color();
let n = 8;
let head = (t * 1.1).fract() * n as f32;
let mut p = ui.painter();
for i in 0..n {
let a = i as f32 / n as f32 * TAU - TAU / 4.0;
let dir = vec2(a.cos(), a.sin());
let age = (head - i as f32).rem_euclid(n as f32) / n as f32;
let k = self.size * 0.5;
p.line(
r.rect.center() + dir * k * 0.46,
r.rect.center() + dir * k * 0.86,
self.size * 0.10,
c.alpha(0.12 + 0.62 * (1.0 - age).powf(1.8)),
);
}
ui.ctx.request_repaint();
r
}
}
impl Ui<'_> {
pub fn progress(&mut self, value: f32) -> Response {
self.add(ProgressBar::new(value))
}
pub fn spinner(&mut self) -> Response {
self.add(Spinner::new())
}
}