Skip to main content

strata/runtime/
edit.rs

1//! Text input editing commands and command queue management.
2
3use 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
10/// Queues an editing command to be applied to the currently focused input.
11pub fn queue_edit_command(cmd: EditCommand) {
12    EDIT_COMMAND_QUEUE.with(|q| q.borrow_mut().push(cmd));
13}
14
15/// Drains all pending edit commands from the thread-local queue.
16pub fn drain_edit_commands_queue() -> Vec<EditCommand> {
17    EDIT_COMMAND_QUEUE.with(|q| q.borrow_mut().drain(..).collect())
18}
19
20impl AppRuntime {
21    /// Applies one editing command to the focused text input. Returns true when something changed.
22    pub fn apply_edit_command(&mut self, cmd: EditCommand) -> bool {
23        let is_password = self
24            .active_focus
25            .as_ref()
26            .is_some_and(|a| self.active_input_is_password(a));
27        if is_password && matches!(cmd, EditCommand::Copy | EditCommand::Cut) {
28            return false;
29        }
30        let Some(active) = self.active_focus.as_mut() else {
31            return false;
32        };
33        let mut text_changed = false;
34        let mut handled = true;
35        match cmd {
36            EditCommand::SelectAll => active.state.select_all(),
37            EditCommand::Copy => {
38                let text = active
39                    .state
40                    .selected_text()
41                    .unwrap_or_else(|| active.state.text.get());
42                #[cfg(target_os = "linux")]
43                strata_platform::gtk_set_clipboard_text(&text);
44                FALLBACK_CLIPBOARD.with(|cb| *cb.borrow_mut() = text.clone());
45                let _ = text;
46            }
47            EditCommand::Cut => {
48                let text = active.state.selected_text();
49                if let Some(sel) = text {
50                    #[cfg(target_os = "linux")]
51                    strata_platform::gtk_set_clipboard_text(&sel);
52                    FALLBACK_CLIPBOARD.with(|cb| *cb.borrow_mut() = sel.clone());
53                    let _ = sel;
54                    active.state.delete_selection();
55                } else {
56                    let cur_text = active.state.text.get();
57                    #[cfg(target_os = "linux")]
58                    strata_platform::gtk_set_clipboard_text(&cur_text);
59                    FALLBACK_CLIPBOARD.with(|cb| *cb.borrow_mut() = cur_text.clone());
60                    active.state.replace_all_text("");
61                }
62                text_changed = true;
63            }
64            EditCommand::Paste => {
65                #[cfg(target_os = "linux")]
66                let clip = strata_platform::gtk_get_clipboard_text();
67                #[cfg(not(target_os = "linux"))]
68                let clip: Option<String> = None;
69                let clip = clip.or_else(|| {
70                    let t = FALLBACK_CLIPBOARD.with(|cb| cb.borrow().clone());
71                    if t.is_empty() {
72                        None
73                    } else {
74                        Some(t)
75                    }
76                });
77                if let Some(clip_text) = clip {
78                    active.state.insert_text(&clip_text);
79                    text_changed = true;
80                }
81            }
82            EditCommand::Undo => text_changed = active.state.undo(),
83            EditCommand::Redo => text_changed = active.state.redo(),
84        }
85        let _ = &mut handled;
86        self.caret_blink_epoch = std::time::Instant::now();
87        if text_changed {
88            let val = active.state.text.get();
89            if let Some(ref on_input) = active.on_input {
90                on_input(strata_dom::Event::with_value(
91                    strata_dom::EventType::Input,
92                    val,
93                ));
94            }
95            self.sync_active_input_layout();
96        }
97        true
98    }
99
100    /// Runs every edit command queued through [`queue_edit_command`] (e.g. from a context menu).
101    pub fn drain_edit_commands(&mut self) -> bool {
102        let cmds: Vec<EditCommand> =
103            EDIT_COMMAND_QUEUE.with(|q| std::mem::take(&mut *q.borrow_mut()));
104        let mut changed = false;
105        for c in cmds {
106            if self.apply_edit_command(c) {
107                changed = true;
108            }
109        }
110        changed
111    }
112}