1use super::*;
4
5thread_local! {
6 static EDIT_COMMAND_QUEUE: std::cell::RefCell<Vec<EditCommand>> = const { std::cell::RefCell::new(Vec::new()) };
7}
8
9static CLIPBOARD_BRIDGE_INIT: std::sync::Once = std::sync::Once::new();
10
11pub(crate) fn ensure_clipboard_bridge_registered() {
14 CLIPBOARD_BRIDGE_INIT.call_once(|| {
15 #[cfg(target_os = "linux")]
16 strata_dom::clipboard::register_system_clipboard_bridge(
17 strata_platform::gtk_get_clipboard_text,
18 strata_platform::gtk_set_clipboard_text,
19 );
20 });
21}
22
23pub fn queue_edit_command(cmd: EditCommand) {
25 EDIT_COMMAND_QUEUE.with(|q| q.borrow_mut().push(cmd));
26}
27
28pub fn drain_edit_commands_queue() -> Vec<EditCommand> {
30 EDIT_COMMAND_QUEUE.with(|q| q.borrow_mut().drain(..).collect())
31}
32
33impl AppRuntime {
34 pub fn apply_edit_command(&mut self, cmd: EditCommand) -> bool {
36 ensure_clipboard_bridge_registered();
37 let is_password = self
38 .active_focus
39 .as_ref()
40 .is_some_and(|a| self.active_input_is_password(a));
41 if is_password && matches!(cmd, EditCommand::Copy | EditCommand::Cut) {
42 return false;
43 }
44 let Some(active) = self.active_focus.as_mut() else {
45 return false;
46 };
47 let mut text_changed = false;
48 let mut handled = true;
49 match cmd {
50 EditCommand::SelectAll => active.state.select_all(),
51 EditCommand::Copy => {
52 let text = active
53 .state
54 .selected_text()
55 .unwrap_or_else(|| active.state.text.get());
56 strata_dom::clipboard::set_clipboard_text(&text);
57 }
58 EditCommand::Cut => {
59 let text = active.state.selected_text();
60 if let Some(sel) = text {
61 strata_dom::clipboard::set_clipboard_text(&sel);
62 active.state.delete_selection();
63 } else {
64 let cur_text = active.state.text.get();
65 strata_dom::clipboard::set_clipboard_text(&cur_text);
66 active.state.replace_all_text("");
67 }
68 text_changed = true;
69 }
70 EditCommand::Paste => {
71 if let Some(clip_text) = strata_dom::clipboard::get_clipboard_text() {
72 active.state.insert_text(&clip_text);
73 text_changed = true;
74 }
75 }
76 EditCommand::Undo => text_changed = active.state.undo(),
77 EditCommand::Redo => text_changed = active.state.redo(),
78 }
79 let _ = &mut handled;
80 self.caret_blink_epoch = self.manual_clock.unwrap_or_else(std::time::Instant::now);
81 if text_changed {
82 let val = active.state.text.get();
83 if let Some(ref on_input) = active.on_input {
84 on_input(strata_dom::Event::with_value(
85 strata_dom::EventType::Input,
86 val,
87 ));
88 }
89 self.sync_active_input_layout_and_scroll_caret();
90 }
91 true
92 }
93
94 pub fn drain_edit_commands(&mut self) -> bool {
96 let cmds: Vec<EditCommand> =
97 EDIT_COMMAND_QUEUE.with(|q| std::mem::take(&mut *q.borrow_mut()));
98 let mut changed = false;
99 for c in cmds {
100 if self.apply_edit_command(c) {
101 changed = true;
102 }
103 }
104 changed
105 }
106}