Skip to main content

editor/
block_menu.rs

1//! The menu a block's gutter handle opens: the rows the app installed with
2//! [`crate::AppExt::set_block_menu_items`], or [`defaults`] until it does.
3
4use std::rc::Rc;
5
6use gpui::{App, Global};
7use ui::icons::glyph;
8
9use crate::slash::{SlashAction, SlashAt, SlashItem, SlashRow};
10
11/// The slash menu's default blocks under "Turn into", then Duplicate and
12/// Delete under "Block".
13pub fn defaults() -> Vec<SlashItem> {
14    let mut items = vec![SlashItem::Heading("Turn into".into())];
15    items.extend(crate::slash::turns(crate::slash::defaults()));
16    items.push(SlashItem::Heading("Block".into()));
17    items.push(SlashItem::Row(SlashRow {
18        label: "Duplicate".into(),
19        icon: Some(glyph::CopyPlus.into()),
20        action: SlashAction::Run(Rc::new(|at: SlashAt, _, cx| {
21            at.editor
22                .update(cx, |editor, cx| editor.duplicate_block(at.block, cx))
23                .ok();
24        })),
25    }));
26    items.push(SlashItem::Row(SlashRow {
27        label: "Delete".into(),
28        icon: Some(glyph::Trash.into()),
29        action: SlashAction::Run(Rc::new(|at: SlashAt, _, cx| {
30            at.editor
31                .update(cx, |editor, cx| editor.remove_block(at.block, cx))
32                .ok();
33        })),
34    }));
35    items
36}
37
38/// What the app installed.
39pub(crate) struct Installed(pub Vec<SlashItem>);
40
41impl Global for Installed {}
42
43/// The items the app installed, or [`defaults`].
44pub(crate) fn installed(cx: &App) -> Vec<SlashItem> {
45    cx.try_global::<Installed>()
46        .map_or_else(defaults, |Installed(items)| items.clone())
47}