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