context_menu.rs

 1use gpui::{actions, Corner, 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    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
24        Story::container()
25            .on_action(|_: &PrintCurrentDate, _| {
26                println!("printing unix time!");
27                if let Ok(unix_time) = std::time::UNIX_EPOCH.elapsed() {
28                    println!("Current Unix time is {:?}", unix_time.as_secs());
29                }
30            })
31            .on_action(|_: &PrintBestFood, _| {
32                println!("burrito");
33            })
34            .flex()
35            .flex_row()
36            .justify_between()
37            .child(
38                div()
39                    .flex()
40                    .flex_col()
41                    .justify_between()
42                    .child(
43                        right_click_menu("test2")
44                            .trigger(Label::new("TOP LEFT"))
45                            .menu(move |cx| build_menu(cx, "top left")),
46                    )
47                    .child(
48                        right_click_menu("test1")
49                            .trigger(Label::new("BOTTOM LEFT"))
50                            .anchor(Corner::BottomLeft)
51                            .attach(Corner::TopLeft)
52                            .menu(move |cx| build_menu(cx, "bottom left")),
53                    ),
54            )
55            .child(
56                div()
57                    .flex()
58                    .flex_col()
59                    .justify_between()
60                    .child(
61                        right_click_menu("test3")
62                            .trigger(Label::new("TOP RIGHT"))
63                            .anchor(Corner::TopRight)
64                            .menu(move |cx| build_menu(cx, "top right")),
65                    )
66                    .child(
67                        right_click_menu("test4")
68                            .trigger(Label::new("BOTTOM RIGHT"))
69                            .anchor(Corner::BottomRight)
70                            .attach(Corner::TopRight)
71                            .menu(move |cx| build_menu(cx, "bottom right")),
72                    ),
73            )
74    }
75}