context_menu.rs

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