use gpui::{
App, Div, ElementId, FocusHandle, InteractiveElement as _, IntoElement, MouseButton,
ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
StyleRefinement, Styled, Window, div, prelude::FluentBuilder as _,
};
use crate::{TestSupportExt as _, a11y_disabled::A11yDisabledExt as _};
#[derive(IntoElement)]
pub struct Label {
base: crate::ObservedElement<gpui::Stateful<Div>>,
target: FocusHandle,
text: SharedString,
disabled: bool,
}
impl Label {
pub fn new(
id: impl Into<ElementId>,
target: &FocusHandle,
text: impl Into<SharedString>,
) -> Self {
Self {
base: div().id(id).test_support(),
target: target.clone(),
text: text.into(),
disabled: false,
}
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl Styled for Label {
fn style(&mut self) -> &mut StyleRefinement {
self.base.style()
}
}
impl RenderOnce for Label {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let disabled = self.disabled;
let target = self.target;
self.base
.role(Role::Label)
.aria_label(self.text.clone())
.child(self.text)
.when(disabled, |label| {
label.on_mouse_down(MouseButton::Left, |_, _, cx| {
cx.stop_propagation();
})
})
.when(!disabled, |label| {
label.on_click(move |_, window, cx| {
target.focus(window, cx);
cx.stop_propagation();
})
})
.a11y_disabled(disabled)
}
}
#[cfg(test)]
mod tests {
use gpui::{Element as _, TestAppContext, accesskit};
use super::*;
#[gpui::test]
fn native_label_has_a_name_and_only_an_enabled_click_action(cx: &mut TestAppContext) {
let window = cx.add_empty_window();
window.update(|window, cx| {
let target = cx.focus_handle();
for disabled in [false, true] {
let element = Label::new("label", &target, "Drawing tool")
.disabled(disabled)
.render(window, cx)
.into_element();
assert_eq!(element.a11y_role(), Some(Role::Label));
let mut node = accesskit::Node::new(Role::Label);
element.write_a11y_info(&mut node);
assert_eq!(node.label(), Some("Drawing tool"));
assert_eq!(node.is_disabled(), disabled);
assert_eq!(node.supports_action(accesskit::Action::Click), !disabled);
assert!(!node.supports_action(accesskit::Action::Focus));
}
});
}
}