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