1use gpui::{div, Element, ParentElement};
2
3use crate::{Icon, IconElement, IconSize, TextColor};
4
5/// Whether the entry is toggleable, and if so, whether it is currently toggled.
6///
7/// To make an element toggleable, simply add a `Toggle::Toggled(_)` and handle it's cases.
8///
9/// You can check if an element is toggleable with `.is_toggleable()`
10///
11/// Possible values:
12/// - `Toggle::NotToggleable` - The entry is not toggleable
13/// - `Toggle::Toggled(true)` - The entry is toggleable and toggled
14/// - `Toggle::Toggled(false)` - The entry is toggleable and not toggled
15#[derive(Debug, Copy, Clone, PartialEq, Eq)]
16pub enum Toggle {
17 NotToggleable,
18 Toggled(bool),
19}
20
21impl Toggle {
22 /// Returns true if the entry is toggled (or is not toggleable.)
23 ///
24 /// As element that isn't toggleable is always "expanded" or "enabled"
25 /// returning true in that case makes sense.
26 pub fn is_toggled(&self) -> bool {
27 match self {
28 Self::Toggled(false) => false,
29 _ => true,
30 }
31 }
32
33 pub fn is_toggleable(&self) -> bool {
34 match self {
35 Self::Toggled(_) => true,
36 _ => false,
37 }
38 }
39}
40
41impl From<bool> for Toggle {
42 fn from(toggled: bool) -> Self {
43 Toggle::Toggled(toggled)
44 }
45}
46
47pub fn disclosure_control(toggle: Toggle) -> impl Element {
48 match (toggle.is_toggleable(), toggle.is_toggled()) {
49 (false, _) => div(),
50 (_, true) => div().child(
51 IconElement::new(Icon::ChevronDown)
52 .color(TextColor::Muted)
53 .size(IconSize::Small),
54 ),
55 (_, false) => div().child(
56 IconElement::new(Icon::ChevronRight)
57 .color(TextColor::Muted)
58 .size(IconSize::Small),
59 ),
60 }
61}