Skip to main content

gpui_base/
component_traits.rs

1/// An element or component that exposes controlled selected state.
2#[allow(patterns_in_fns_without_body)]
3pub trait Selectable: Sized {
4    fn selected(mut self, selected: bool) -> Self;
5    fn is_selected(&self) -> bool;
6
7    fn secondary_selected(self, _: bool) -> Self {
8        self
9    }
10}
11
12/// An element or component that can be disabled.
13#[allow(patterns_in_fns_without_body)]
14pub trait Disableable {
15    fn disabled(mut self, disabled: bool) -> Self;
16}
17
18/// A component that exposes whether its UI layer should draw a focus ring.
19///
20/// This trait carries state only. Focus-ring geometry and presentation belong
21/// to the component's visual layer.
22pub trait FocusableExt: Sized {
23    fn focus_ring(self, enabled: bool) -> Self;
24    fn is_focus_ring_enabled(&self) -> bool;
25}
26
27/// An element or component that exposes collapsed state.
28pub trait Collapsible {
29    fn collapsed(self, collapsed: bool) -> Self;
30    fn is_collapsed(&self) -> bool;
31}
32
33#[cfg(test)]
34mod tests {
35    use super::FocusableExt;
36
37    struct CustomControl {
38        focus_ring_enabled: bool,
39    }
40
41    impl FocusableExt for CustomControl {
42        fn focus_ring(mut self, enabled: bool) -> Self {
43            self.focus_ring_enabled = enabled;
44            self
45        }
46
47        fn is_focus_ring_enabled(&self) -> bool {
48            self.focus_ring_enabled
49        }
50    }
51
52    #[test]
53    fn focus_ring_api_carries_state_without_visuals() {
54        let control = CustomControl {
55            focus_ring_enabled: true,
56        }
57        .focus_ring(false);
58
59        assert!(!control.is_focus_ring_enabled());
60    }
61}