Skip to main content

gpui_deck/
component_traits.rs

1// Modified for GPUI Deck: keep the selected-state contract used by Button.
2/// An element or component that exposes controlled selected state.
3///
4/// `selected` is the application's own meaning of selection: the current
5/// view, the active item, the chosen option. `open` is a separate state a
6/// popover, menu or dropdown puts on its trigger for as long as it is open.
7/// The two coincide on a plain button, which paints both the same way, so
8/// `open` falls back to `selected` by default and a trigger that only
9/// implements `selected` keeps working unchanged. A trigger whose selection
10/// means something else, such as a sidebar row that is selected when it is
11/// the current view, overrides `open` and `is_open` to keep the two apart.
12#[allow(patterns_in_fns_without_body)]
13pub trait Selectable: Sized {
14    fn selected(mut self, selected: bool) -> Self;
15    fn is_selected(&self) -> bool;
16
17    fn secondary_selected(self, _: bool) -> Self {
18        self
19    }
20
21    /// Sets the open state a popover, menu or dropdown holds on its trigger
22    /// while it is open.
23    ///
24    /// Defaults to `selected`, so a trigger only overrides this when its
25    /// selected state means something other than "my popup is open".
26    fn open(self, open: bool) -> Self {
27        self.selected(open)
28    }
29
30    /// Whether the trigger is currently marked open.
31    ///
32    /// Defaults to `is_selected`, matching the default of [`Self::open`].
33    fn is_open(&self) -> bool {
34        self.is_selected()
35    }
36}