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