BlueGit

ict rusty-teams

Files

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

window_overlay.rs

window_overlay.rs 2.2 KiB Download Raw
1use gpui::{
2 App, ClipboardItem, Context, IntoElement, ParentElement as _, Render, Subscription, Window, div,
3};
4use gpui_base::TextSelection;
5use rust_i18n::t;
6
7use super::{EditMenuItem, TouchSelectionOverlay};
8
9/// Draws the handles and the edit menu of the window text selection — the one
10/// a [`crate::text::TextView`] takes part in. [`crate::Root`] mounts one per
11/// window, after the content, so the surfaces float above whatever was
12/// selected.
13///
14/// Read-only text offers Copy and Select All.
15pub(crate) struct WindowTouchSelectionOverlay {
16 _subscription: Subscription,
17}
18
19impl WindowTouchSelectionOverlay {
20 pub(crate) fn new(window: &Window, cx: &mut Context<Self>) -> Self {
21 let this = cx.weak_entity();
22 let subscription = TextSelection::observe_touch_selection(window, cx, move |cx| {
23 let _ = this.update(cx, |_, cx| cx.notify());
24 });
25 Self {
26 _subscription: subscription,
27 }
28 }
29
30 fn copy(window: &mut Window, cx: &mut App) {
31 let text = TextSelection::selected_text(window, cx);
32 let text = text.trim();
33 if !text.is_empty() {
34 cx.write_to_clipboard(ClipboardItem::new_string(text.to_string()));
35 }
36 TextSelection::close_edit_menu(window, cx);
37 }
38
39 fn select_all(window: &mut Window, cx: &mut App) {
40 TextSelection::select_all(window, cx);
41 }
42}
43
44impl Render for WindowTouchSelectionOverlay {
45 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
46 if TextSelection::touch_selection(window, cx).is_none() {
47 return div();
48 }
49 // The handles are painted by the text that owns them, where they
50 // are covered by whatever covers the text; only the menu floats.
51 let overlay = TouchSelectionOverlay::new("window-touch-selection", |window, cx| {
52 TextSelection::touch_selection(window, cx)
53 })
54 .items([
55 EditMenuItem::new(t!("Input.Copy"), Self::copy),
56 EditMenuItem::new(t!("Input.Select All"), Self::select_all),
57 ])
58 .on_paint(|bounds, window, cx| TextSelection::register_touch_ui(bounds, window, cx));
59 div().children(overlay.into_elements(window, cx))
60 }
61}