component_test.rs

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