use gpui::{
AnyElement, App, Div, ElementId, InteractiveElement, Interactivity, IntoElement, ParentElement,
RenderOnce, Role, SharedString, StatefulInteractiveElement, StyleRefinement, Styled, Window,
div, prelude::FluentBuilder as _,
};
use smallvec::SmallVec;
use crate::{StyledExt as _, TestSupportExt as _};
#[derive(IntoElement)]
pub struct Progress {
base: crate::ObservedElement<gpui::Stateful<Div>>,
style: StyleRefinement,
value: f32,
indeterminate: bool,
accessibility_label: Option<SharedString>,
children: SmallVec<[AnyElement; 2]>,
}
impl Progress {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
base: div().id(id).test_support(),
style: StyleRefinement::default(),
value: 0.,
indeterminate: false,
accessibility_label: None,
children: SmallVec::new(),
}
}
pub fn value(mut self, value: f32) -> Self {
assert!(value.is_finite(), "Progress value must be finite");
self.value = value.clamp(0., 100.);
self
}
pub fn indeterminate(mut self, indeterminate: bool) -> Self {
self.indeterminate = indeterminate;
self
}
pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
self.accessibility_label = Some(label.into());
self
}
}
impl Styled for Progress {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl ParentElement for Progress {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl InteractiveElement for Progress {
fn interactivity(&mut self) -> &mut Interactivity {
self.base.interactivity()
}
}
impl StatefulInteractiveElement for Progress {}
impl RenderOnce for Progress {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
self.base
.role(Role::ProgressIndicator)
.when_some(self.accessibility_label, |this, label| {
this.aria_label(label)
})
.aria_min_numeric_value(0.)
.aria_max_numeric_value(100.)
.when(!self.indeterminate, |this| {
this.aria_numeric_value(self.value as f64)
})
.children(self.children)
.refine_style(&self.style)
}
}
#[cfg(test)]
mod tests {
use gpui::{Element as _, TestAppContext, accesskit};
use super::*;
#[gpui::test]
fn known_and_unknown_progress_expose_the_numeric_contract(cx: &mut TestAppContext) {
let window = cx.add_empty_window();
window.update(|window, cx| {
for (value, indeterminate, expected) in [
(-10., false, Some(0.)),
(42.5, false, Some(42.5)),
(120., false, Some(100.)),
(42.5, true, None),
] {
let element = Progress::new("progress")
.value(value)
.indeterminate(indeterminate)
.accessibility_label("Preview progress")
.render(window, cx)
.into_element();
assert_eq!(element.a11y_role(), Some(Role::ProgressIndicator));
let mut node = accesskit::Node::new(Role::ProgressIndicator);
element.write_a11y_info(&mut node);
assert_eq!(node.label(), Some("Preview progress"));
assert_eq!(node.min_numeric_value(), Some(0.));
assert_eq!(node.max_numeric_value(), Some(100.));
assert_eq!(node.numeric_value(), expected);
}
});
}
}