component_test.rs

  1use gpui::{
  2    actions,
  3    elements::{Component, Flex, ParentElement, SafeStylable},
  4    AppContext, Element, Entity, ModelHandle, Task, View, ViewContext, ViewHandle, WeakViewHandle,
  5};
  6use project::Project;
  7use theme::components::{action_button::Button, label::Label, ComponentExt};
  8use workspace::{
  9    item::Item, register_deserializable_item, ItemId, Pane, PaneBackdrop, Workspace, WorkspaceId,
 10};
 11
 12pub fn init(cx: &mut AppContext) {
 13    cx.add_action(ComponentTest::toggle_disclosure);
 14    cx.add_action(ComponentTest::toggle_toggle);
 15    cx.add_action(ComponentTest::deploy);
 16    register_deserializable_item::<ComponentTest>(cx);
 17}
 18
 19actions!(
 20    test,
 21    [NoAction, ToggleDisclosure, ToggleToggle, NewComponentTest]
 22);
 23
 24struct ComponentTest {
 25    disclosed: bool,
 26    toggled: bool,
 27}
 28
 29impl ComponentTest {
 30    fn new() -> Self {
 31        Self {
 32            disclosed: false,
 33            toggled: false,
 34        }
 35    }
 36
 37    fn deploy(workspace: &mut Workspace, _: &NewComponentTest, cx: &mut ViewContext<Workspace>) {
 38        workspace.add_item(Box::new(cx.add_view(|_| ComponentTest::new())), cx);
 39    }
 40
 41    fn toggle_disclosure(&mut self, _: &ToggleDisclosure, cx: &mut ViewContext<Self>) {
 42        self.disclosed = !self.disclosed;
 43        cx.notify();
 44    }
 45
 46    fn toggle_toggle(&mut self, _: &ToggleToggle, cx: &mut ViewContext<Self>) {
 47        self.toggled = !self.toggled;
 48        cx.notify();
 49    }
 50}
 51
 52impl Entity for ComponentTest {
 53    type Event = ();
 54}
 55
 56impl View for ComponentTest {
 57    fn ui_name() -> &'static str {
 58        "Component Test"
 59    }
 60
 61    fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> gpui::AnyElement<Self> {
 62        let theme = theme::current(cx);
 63
 64        PaneBackdrop::new(
 65            cx.view_id(),
 66            Flex::column()
 67                .with_spacing(10.)
 68                .with_child(
 69                    Button::action(NoAction)
 70                        .with_tooltip("Here's what a tooltip looks like", theme.tooltip.clone())
 71                        .with_contents(Label::new("Click me!"))
 72                        .with_style(theme.component_test.button.clone())
 73                        .element(),
 74                )
 75                .with_child(
 76                    Button::action(ToggleToggle)
 77                        .with_tooltip("Here's what a tooltip looks like", theme.tooltip.clone())
 78                        .with_contents(Label::new("Toggle me!"))
 79                        .toggleable(self.toggled)
 80                        .with_style(theme.component_test.toggle.clone())
 81                        .element(),
 82                )
 83                .with_child(
 84                    Label::new("A disclosure")
 85                        .disclosable(Some(self.disclosed), Box::new(ToggleDisclosure))
 86                        .with_style(theme.component_test.disclosure.clone())
 87                        .element(),
 88                )
 89                .constrained()
 90                .with_width(200.)
 91                .aligned()
 92                .into_any(),
 93        )
 94        .into_any()
 95    }
 96}
 97
 98impl Item for ComponentTest {
 99    fn tab_content<V: 'static>(
100        &self,
101        _: Option<usize>,
102        style: &theme::Tab,
103        _: &AppContext,
104    ) -> gpui::AnyElement<V> {
105        gpui::elements::Label::new("Component test", style.label.clone()).into_any()
106    }
107
108    fn serialized_item_kind() -> Option<&'static str> {
109        Some("ComponentTest")
110    }
111
112    fn deserialize(
113        _project: ModelHandle<Project>,
114        _workspace: WeakViewHandle<Workspace>,
115        _workspace_id: WorkspaceId,
116        _item_id: ItemId,
117        cx: &mut ViewContext<Pane>,
118    ) -> Task<anyhow::Result<ViewHandle<Self>>> {
119        Task::ready(Ok(cx.add_view(|_| Self::new())))
120    }
121}