Bubble menu
Show a contextual menu above a selection.
Try it
Section titled “Try it”Drag across “the headline” to show the menu above the selection. Click a word to hide it.
Install
Section titled “Install”cargo add gpdoc-plugin-bubble-menu gpdoc-plugin-boldExample
Section titled “Example”Attach a renderer to show a menu above selected text in the Quickstart editor.
use gpdoc::{EditorConfig, EditorView};use gpdoc_plugin_bold::bold;use gpdoc_plugin_bubble_menu::bubble_menu;use gpdoc_plugin_paragraph::paragraph;use gpui::{App, Div, Entity, Window, div, prelude::*};
let config = EditorConfig::builder() .plugin(paragraph()) .plugin(bold()) .plugin(bubble_menu().with_view(format_menu)) .build() .expect("configure editor");
fn format_menu(_: Entity<EditorView>, _: &mut Window, _: &mut App) -> Div { div().child("Selected text")}Select a word to see Selected text.
Compose a bubble menu
Section titled “Compose a bubble menu”The plugin positions the menu above the selection and keeps focus in the editor while you click its controls.
Anatomy
Section titled “Anatomy”bubble_menu().with_view(...) Positions the menu above selected text└── div().id(...) Runs an editor command on clickFormat the selected text
Section titled “Format the selected text”Replace format_menu with a button that runs the Bold plugin’s command on the same editor.
use gpdoc_plugin_bold::Toggle;use gpui::Stateful;
fn format_menu(editor: Entity<EditorView>, _: &mut Window, cx: &mut App) -> Stateful<Div> { let active = editor.read(cx).editor().mark_active("bold").unwrap_or(false); div() .id("bubble_menu-bold") .child(if active { "Remove bold" } else { "Bold" }) .on_click(move |_, window, cx| { editor.update(cx, |view, cx| { view.run(Toggle, window, cx).expect("toggle bold"); }); })}Select a word and click Bold. The button now reads Remove bold. Collapse the selection to hide the menu.
Use .named("linkMenu").show_when(|editor| editor.editor().mark_active("link").unwrap_or(false)) before .with_view(...) to register a second menu for links. Each menu needs a unique name. See Floating menu for a caret in an empty paragraph.