context_menu.rs

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