context_menu.rs

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