1use std::fmt::{self, Debug};
4use std::rc::Rc;
5
6use waft_embedder::{Offset, Rect, Size, clamp_double};
7use waft_foundation::{App, Handle, Listenable, Listener, Ref, Task};
8use waft_rendering::TextSelectionPoint;
9use waft_services::{AnyTextSelectionDelegate, SelectionChangedCause};
10use waft_widgets::{
11 BuildContext, ClipboardStatus, ClipboardStatusNotifier, IntoWidget, KeyRef, MediaQuery,
12 SizedBox, State, StateData, StatefulWidget, TextSelectionControls, TextSelectionHandleControls,
13 TextSelectionHandleType, WidgetRef, debug_check_has_media_query,
14};
15
16use crate::debug::debug_check_has_material_localizations;
17use crate::desktop_text_selection_toolbar::DesktopTextSelectionToolbar;
18use crate::desktop_text_selection_toolbar_button::DesktopTextSelectionToolbarButton;
19use crate::material_localizations::MaterialLocalizations;
20
21#[derive(Debug)]
26struct DesktopTextSelectionHandleControls;
27
28#[derive(Debug, Default)]
32pub struct DesktopTextSelectionControls;
33
34impl DesktopTextSelectionControls {
35 pub const fn new() -> DesktopTextSelectionControls {
37 DesktopTextSelectionControls
38 }
39}
40
41impl TextSelectionControls for DesktopTextSelectionControls {
42 fn get_handle_size(&self, _text_line_height: f64) -> Size {
44 Size::ZERO
45 }
46
47 #[allow(deprecated, clippy::too_many_arguments)]
49 fn build_toolbar(
50 &self,
51 app: &mut App,
52 _context: BuildContext,
53 global_editable_region: Rect,
54 text_line_height: f64,
55 selection_midpoint: Offset,
56 endpoints: &[TextSelectionPoint],
57 delegate: AnyTextSelectionDelegate,
58 clipboard_status: Option<Ref<ClipboardStatusNotifier>>,
59 last_secondary_tap_down_position: Option<Offset>,
60 ) -> WidgetRef {
61 let handle_cut = self.can_cut(app, delegate).then(|| {
62 Listener::new(move |app| {
63 DesktopTextSelectionControls.handle_cut(app, delegate);
64 })
65 });
66 let handle_copy = self.can_copy(app, delegate).then(|| {
67 Listener::new(move |app| {
68 DesktopTextSelectionControls.handle_copy(app, delegate);
69 })
70 });
71 let handle_paste = self.can_paste(app, delegate).then(|| {
72 Listener::new(move |app| {
73 DesktopTextSelectionControls.handle_paste(app, delegate);
74 })
75 });
76 let handle_select_all = self.can_select_all(app, delegate).then(|| {
77 Listener::new(move |app| {
78 DesktopTextSelectionControls.handle_select_all(app, delegate);
79 })
80 });
81 DesktopTextSelectionControlsToolbar {
82 key: None,
83 clipboard_status,
84 endpoints: endpoints.to_vec(),
85 global_editable_region,
86 handle_cut,
87 handle_copy,
88 handle_paste,
89 handle_select_all,
90 selection_midpoint,
91 last_secondary_tap_down_position,
92 text_line_height,
93 }
94 .into_widget()
95 }
96
97 fn build_handle(
99 &self,
100 _app: &mut App,
101 _context: BuildContext,
102 _handle_type: TextSelectionHandleType,
103 _text_line_height: f64,
104 _on_tap: Option<Listener>,
105 ) -> WidgetRef {
106 SizedBox::shrink().into_widget()
107 }
108
109 fn get_handle_anchor(
111 &self,
112 _handle_type: TextSelectionHandleType,
113 _text_line_height: f64,
114 ) -> Offset {
115 Offset::ZERO
116 }
117
118 #[allow(deprecated)]
119 fn can_select_all(&self, app: &App, delegate: AnyTextSelectionDelegate) -> bool {
120 let value = delegate.text_editing_value(app);
123 delegate.select_all_enabled(app)
124 && !value.text.is_empty()
125 && !(value.selection.start() == 0
126 && value.selection.end() == value.text.encode_utf16().count() as i32)
127 }
128
129 #[allow(deprecated)]
130 fn handle_select_all(&self, app: &mut App, delegate: AnyTextSelectionDelegate) {
131 delegate.select_all(app, SelectionChangedCause::Toolbar);
133 delegate.hide_toolbar(app, true);
134 }
135}
136
137impl TextSelectionControls for DesktopTextSelectionHandleControls {
138 fn as_text_selection_handle_controls(&self) -> Option<&dyn TextSelectionHandleControls> {
139 Some(self)
140 }
141
142 fn get_handle_size(&self, text_line_height: f64) -> Size {
143 DesktopTextSelectionControls.get_handle_size(text_line_height)
144 }
145
146 #[allow(deprecated, clippy::too_many_arguments)]
147 fn build_toolbar(
148 &self,
149 app: &mut App,
150 context: BuildContext,
151 global_editable_region: Rect,
152 text_line_height: f64,
153 selection_midpoint: Offset,
154 endpoints: &[TextSelectionPoint],
155 delegate: AnyTextSelectionDelegate,
156 clipboard_status: Option<Ref<ClipboardStatusNotifier>>,
157 last_secondary_tap_down_position: Option<Offset>,
158 ) -> WidgetRef {
159 TextSelectionHandleControls::build_toolbar(
160 self,
161 app,
162 context,
163 global_editable_region,
164 text_line_height,
165 selection_midpoint,
166 endpoints,
167 delegate,
168 clipboard_status,
169 last_secondary_tap_down_position,
170 )
171 }
172
173 fn build_handle(
174 &self,
175 app: &mut App,
176 context: BuildContext,
177 handle_type: TextSelectionHandleType,
178 text_line_height: f64,
179 on_tap: Option<Listener>,
180 ) -> WidgetRef {
181 DesktopTextSelectionControls.build_handle(
182 app,
183 context,
184 handle_type,
185 text_line_height,
186 on_tap,
187 )
188 }
189
190 fn get_handle_anchor(
191 &self,
192 handle_type: TextSelectionHandleType,
193 text_line_height: f64,
194 ) -> Offset {
195 DesktopTextSelectionControls.get_handle_anchor(handle_type, text_line_height)
196 }
197
198 #[allow(deprecated)]
199 fn can_cut(&self, app: &App, delegate: AnyTextSelectionDelegate) -> bool {
200 TextSelectionHandleControls::can_cut(self, app, delegate)
201 }
202
203 #[allow(deprecated)]
204 fn can_copy(&self, app: &App, delegate: AnyTextSelectionDelegate) -> bool {
205 TextSelectionHandleControls::can_copy(self, app, delegate)
206 }
207
208 #[allow(deprecated)]
209 fn can_paste(&self, app: &App, delegate: AnyTextSelectionDelegate) -> bool {
210 TextSelectionHandleControls::can_paste(self, app, delegate)
211 }
212
213 #[allow(deprecated)]
214 fn can_select_all(&self, app: &App, delegate: AnyTextSelectionDelegate) -> bool {
215 TextSelectionHandleControls::can_select_all(self, app, delegate)
216 }
217
218 #[allow(deprecated)]
219 fn handle_cut(&self, app: &mut App, delegate: AnyTextSelectionDelegate) {
220 TextSelectionHandleControls::handle_cut(self, app, delegate, None);
221 }
222
223 #[allow(deprecated)]
224 fn handle_copy(&self, app: &mut App, delegate: AnyTextSelectionDelegate) {
225 TextSelectionHandleControls::handle_copy(self, app, delegate, None);
226 }
227
228 #[allow(deprecated)]
229 fn handle_paste(&self, app: &mut App, delegate: AnyTextSelectionDelegate) -> Task<()> {
230 TextSelectionHandleControls::handle_paste(self, app, delegate)
231 }
232
233 #[allow(deprecated)]
234 fn handle_select_all(&self, app: &mut App, delegate: AnyTextSelectionDelegate) {
235 TextSelectionHandleControls::handle_select_all(self, app, delegate);
236 }
237}
238
239impl TextSelectionHandleControls for DesktopTextSelectionHandleControls {}
240
241pub fn desktop_text_selection_handle_controls() -> Rc<dyn TextSelectionControls> {
250 thread_local! {
251 static CONTROLS: Rc<dyn TextSelectionControls> = Rc::new(DesktopTextSelectionHandleControls);
252 }
253 CONTROLS.with(Rc::clone)
254}
255
256pub fn desktop_text_selection_controls() -> Rc<dyn TextSelectionControls> {
262 thread_local! {
263 static CONTROLS: Rc<dyn TextSelectionControls> = Rc::new(DesktopTextSelectionControls);
264 }
265 CONTROLS.with(Rc::clone)
266}
267
268struct DesktopTextSelectionControlsToolbar {
270 key: Option<KeyRef>,
271 clipboard_status: Option<Ref<ClipboardStatusNotifier>>,
272 #[allow(dead_code)]
273 endpoints: Vec<TextSelectionPoint>,
274 global_editable_region: Rect,
275 handle_copy: Option<Listener>,
276 handle_cut: Option<Listener>,
277 handle_paste: Option<Listener>,
278 handle_select_all: Option<Listener>,
279 last_secondary_tap_down_position: Option<Offset>,
280 selection_midpoint: Offset,
281 #[allow(dead_code)]
282 text_line_height: f64,
283}
284
285impl Debug for DesktopTextSelectionControlsToolbar {
286 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
287 f.debug_struct("_DesktopTextSelectionControlsToolbar")
288 .finish_non_exhaustive()
289 }
290}
291
292impl StatefulWidget for DesktopTextSelectionControlsToolbar {
293 type State = DesktopTextSelectionControlsToolbarState;
294
295 fn key(&self) -> Option<&KeyRef> {
296 self.key.as_ref()
297 }
298
299 fn create_state(&self) -> DesktopTextSelectionControlsToolbarState {
300 DesktopTextSelectionControlsToolbarState {
301 state: StateData::new(),
302 }
303 }
304}
305
306struct DesktopTextSelectionControlsToolbarState {
307 state: StateData<DesktopTextSelectionControlsToolbar>,
308}
309
310impl DesktopTextSelectionControlsToolbarState {
311 fn on_changed_clipboard_status(self: Handle<Self>, app: &mut App) {
312 self.set_state(app, |_state| {
313 });
315 }
316}
317
318impl State for DesktopTextSelectionControlsToolbarState {
319 type Widget = DesktopTextSelectionControlsToolbar;
320 waft_widgets::state_accessors!();
321
322 fn init_state(self: Handle<Self>, app: &mut App) {
323 if let Some(clipboard_status) = self.widget(app).clipboard_status.clone() {
324 clipboard_status.add_listener(
325 app,
326 Listener::handle_method(self, Self::on_changed_clipboard_status),
327 );
328 }
329 }
330
331 fn did_update_widget(
332 self: Handle<Self>,
333 app: &mut App,
334 old_widget: &DesktopTextSelectionControlsToolbar,
335 ) {
336 if old_widget.clipboard_status != self.widget(app).clipboard_status {
337 if let Some(clipboard_status) = old_widget.clipboard_status.clone() {
338 clipboard_status.remove_listener(
339 app,
340 &Listener::handle_method(self, Self::on_changed_clipboard_status),
341 );
342 }
343 if let Some(clipboard_status) = self.widget(app).clipboard_status.clone() {
344 clipboard_status.add_listener(
345 app,
346 Listener::handle_method(self, Self::on_changed_clipboard_status),
347 );
348 }
349 }
350 }
351
352 fn dispose(self: Handle<Self>, app: &mut App) {
353 if let Some(clipboard_status) = self.widget(app).clipboard_status.clone() {
354 clipboard_status.remove_listener(
355 app,
356 &Listener::handle_method(self, Self::on_changed_clipboard_status),
357 );
358 }
359 }
360
361 fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
362 debug_assert!(debug_check_has_material_localizations(app, context));
363 debug_assert!(debug_check_has_media_query(app, context));
364
365 let widget = self.widget(app);
366 let handle_cut = widget.handle_cut.clone();
367 let handle_copy = widget.handle_copy.clone();
368 let handle_paste = widget.handle_paste.clone();
369 let handle_select_all = widget.handle_select_all.clone();
370 let clipboard_status = widget.clipboard_status.clone();
371 let selection_midpoint = widget.selection_midpoint;
372 let global_editable_region = widget.global_editable_region;
373 let last_secondary_tap_down_position = widget.last_secondary_tap_down_position;
374
375 if handle_paste.is_some()
377 && clipboard_status
378 .as_ref()
379 .is_some_and(|status| status.value(app) == ClipboardStatus::Unknown)
380 {
381 return SizedBox::shrink().into_widget();
382 }
383
384 let media_query_padding = MediaQuery::padding_of(app, context);
385 let midpoint_anchor = Offset::new(
386 clamp_double(
387 selection_midpoint.dx() - global_editable_region.left,
388 media_query_padding.left,
389 MediaQuery::width_of(app, context) - media_query_padding.right,
390 ),
391 selection_midpoint.dy() - global_editable_region.top,
392 );
393
394 let localizations = <dyn MaterialLocalizations>::of(app, context);
395 let mut items: Vec<WidgetRef> = Vec::new();
396
397 let mut add_toolbar_button = |app: &mut App, text: String, on_pressed: Listener| {
398 items.push(
399 DesktopTextSelectionToolbarButton::text(app, context, Some(on_pressed), text)
400 .into_widget(),
401 );
402 };
403
404 if let Some(handle_cut) = handle_cut {
405 add_toolbar_button(app, localizations.cut_button_label(), handle_cut);
406 }
407 if let Some(handle_copy) = handle_copy {
408 add_toolbar_button(app, localizations.copy_button_label(), handle_copy);
409 }
410 if let Some(handle_paste) = handle_paste
411 && clipboard_status
412 .as_ref()
413 .is_some_and(|status| status.value(app) == ClipboardStatus::Pasteable)
414 {
415 add_toolbar_button(app, localizations.paste_button_label(), handle_paste);
416 }
417 if let Some(handle_select_all) = handle_select_all {
418 add_toolbar_button(
419 app,
420 localizations.select_all_button_label(),
421 handle_select_all,
422 );
423 }
424
425 if items.is_empty() {
427 return SizedBox::shrink().into_widget();
428 }
429
430 DesktopTextSelectionToolbar::new(
431 last_secondary_tap_down_position.unwrap_or(midpoint_anchor),
432 items,
433 )
434 .into_widget()
435 }
436}