BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-component / src / touch_selection / edit_menu.rs

edit_menu.rs

edit_menu.rs 5.4 KiB Download Raw
1use std::rc::Rc;
2
3use gpui::{
4 App, Bounds, ClickEvent, Corners, ElementId, InteractiveElement as _, IntoElement,
5 ParentElement as _, Pixels, RenderOnce, SharedString, Styled as _, Window, canvas, deferred,
6 div, prelude::FluentBuilder as _, px,
7};
8use gpui_base::{Placement, Positioner};
9
10use super::handle::SurfaceHandler;
11use crate::{
12 ActiveTheme as _, Sizable as _, ThemeStyled as _,
13 button::{Button, ButtonCustomVariant, ButtonVariants as _},
14 h_flex,
15 separator::Separator,
16};
17use gpui_base::TestSupportExt as _;
18
19/// The height of a menu row.
20const ROW_HEIGHT: Pixels = px(32.);
21
22/// One command in the edit menu.
23pub(crate) struct EditMenuItem {
24 label: SharedString,
25 on_click: Rc<dyn Fn(&mut Window, &mut App)>,
26}
27
28impl EditMenuItem {
29 pub(crate) fn new(
30 label: impl Into<SharedString>,
31 on_click: impl Fn(&mut Window, &mut App) + 'static,
32 ) -> Self {
33 Self {
34 label: label.into(),
35 on_click: Rc::new(on_click),
36 }
37 }
38}
39
40/// The row of commands a touch selection offers: Cut, Copy, Paste, Select All
41/// — whichever apply. It floats above the selection, or below it when there
42/// is no room above, and stays out of the way of the handles' knobs.
43///
44/// Every item is a [`Button`] sized for a finger, so the row keeps the button
45/// family's press feedback. It carries no arrow: it belongs to the selection
46/// it sits on, not to a trigger.
47#[derive(IntoElement)]
48pub(crate) struct EditMenu {
49 id: ElementId,
50 /// The selection, including the room its handles take.
51 anchor: Bounds<Pixels>,
52 items: Vec<EditMenuItem>,
53 on_paint: Option<SurfaceHandler>,
54}
55
56impl EditMenu {
57 pub(crate) fn new(id: impl Into<ElementId>, anchor: Bounds<Pixels>) -> Self {
58 Self {
59 id: id.into(),
60 anchor,
61 items: Vec::new(),
62 on_paint: None,
63 }
64 }
65
66 pub(crate) fn items(mut self, items: impl IntoIterator<Item = EditMenuItem>) -> Self {
67 self.items.extend(items);
68 self
69 }
70
71 pub(crate) fn on_paint(mut self, on_paint: SurfaceHandler) -> Self {
72 self.on_paint = Some(on_paint);
73 self
74 }
75}
76
77impl RenderOnce for EditMenu {
78 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
79 let id = self.id;
80 let on_paint = self.on_paint;
81 // A pill-like bar, as iOS draws its edit menu.
82 let radius = cx.theme().radius_lg;
83 // The menu family's hover, not the ghost button's: the ghost's grey
84 // is the bar's own ring color and would blur the edge.
85 let item_style = ButtonCustomVariant::new(cx)
86 .color(cx.theme().transparent)
87 .foreground(cx.theme().popover_foreground)
88 .hover(cx.theme().accent)
89 .active(cx.theme().accent);
90 let last = self.items.len().saturating_sub(1);
91 let items = self.items.into_iter().enumerate().flat_map(|(ix, item)| {
92 let on_click = item.on_click;
93 // The items fill the bar edge to edge, so each one's press
94 // surface takes the bar's own corners: the first the left pair,
95 // the last the right pair, the ones between none.
96 let corners = Corners {
97 top_left: ix == 0,
98 bottom_left: ix == 0,
99 top_right: ix == last,
100 bottom_right: ix == last,
101 };
102 // Observed under its label, so a test can press "Copy".
103 let button = div()
104 .id(item.label.clone())
105 .test_support()
106 .child(
107 Button::new(ix)
108 .custom(item_style)
109 // This is a finger's menu wherever it shows: body
110 // text in a 32px row.
111 .large()
112 .h(ROW_HEIGHT)
113 .px_3()
114 .rounded(radius)
115 .border_corners(corners)
116 .tab_stop(false)
117 .label(item.label)
118 .on_click(move |_: &ClickEvent, window, cx| on_click(window, cx)),
119 )
120 .into_any_element();
121 // A rule between neighbours, none before the first, the full
122 // height of the bar.
123 (ix > 0)
124 .then(|| Separator::vertical().into_any_element())
125 .into_iter()
126 .chain([button])
127 });
128 deferred(
129 Positioner::side(self.anchor)
130 .placement(Placement::Top)
131 .offset(px(8.))
132 .occlude()
133 .child(
134 h_flex()
135 .id(id)
136 .relative()
137 .items_stretch()
138 .overflow_hidden()
139 .popover_style(cx)
140 .rounded(radius)
141 .children(items)
142 .when_some(on_paint, |this, on_paint| {
143 this.child(
144 canvas(
145 move |bounds, window, cx| on_paint(bounds, window, cx),
146 |_, _, _, _| {},
147 )
148 .absolute()
149 .inset_0(),
150 )
151 }),
152 ),
153 )
154 .with_priority(gpui_base::POPUP_PRIORITY)
155 }
156}