context_menu.rs

 1use gpui::{actions, Action, AnchorCorner, Div, Render, View};
 2use story::Story;
 3
 4use crate::prelude::*;
 5use crate::{right_click_menu, ContextMenu, Label};
 6
 7actions!(PrintCurrentDate, PrintBestFood);
 8
 9fn build_menu(cx: &mut WindowContext, header: impl Into<SharedString>) -> View<ContextMenu> {
10    ContextMenu::build(cx, |menu, _| {
11        menu.header(header)
12            .separator()
13            .entry("Print current time", |cx| {
14                println!("dispatching PrintCurrentTime action");
15                cx.dispatch_action(PrintCurrentDate.boxed_clone())
16            })
17            .entry("Print best foot", |cx| {
18                cx.dispatch_action(PrintBestFood.boxed_clone())
19            })
20    })
21}
22
23pub struct ContextMenuStory;
24
25impl Render for ContextMenuStory {
26    type Element = Div;
27
28    fn render(&mut self, _cx: &mut ViewContext<Self>) -> Self::Element {
29        Story::container()
30            .on_action(|_: &PrintCurrentDate, _| {
31                println!("printing unix time!");
32                if let Ok(unix_time) = std::time::UNIX_EPOCH.elapsed() {
33                    println!("Current Unix time is {:?}", unix_time.as_secs());
34                }
35            })
36            .on_action(|_: &PrintBestFood, _| {
37                println!("burrito");
38            })
39            .flex()
40            .flex_row()
41            .justify_between()
42            .child(
43                div()
44                    .flex()
45                    .flex_col()
46                    .justify_between()
47                    .child(
48                        right_click_menu("test2")
49                            .trigger(Label::new("TOP LEFT"))
50                            .menu(move |cx| build_menu(cx, "top left")),
51                    )
52                    .child(
53                        right_click_menu("test1")
54                            .trigger(Label::new("BOTTOM LEFT"))
55                            .anchor(AnchorCorner::BottomLeft)
56                            .attach(AnchorCorner::TopLeft)
57                            .menu(move |cx| build_menu(cx, "bottom left")),
58                    ),
59            )
60            .child(
61                div()
62                    .flex()
63                    .flex_col()
64                    .justify_between()
65                    .child(
66                        right_click_menu("test3")
67                            .trigger(Label::new("TOP RIGHT"))
68                            .anchor(AnchorCorner::TopRight)
69                            .menu(move |cx| build_menu(cx, "top right")),
70                    )
71                    .child(
72                        right_click_menu("test4")
73                            .trigger(Label::new("BOTTOM RIGHT"))
74                            .anchor(AnchorCorner::BottomRight)
75                            .attach(AnchorCorner::TopRight)
76                            .menu(move |cx| build_menu(cx, "bottom right")),
77                    ),
78            )
79    }
80}