context_menu.rs

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