Skip to main content

rustapi/actions/
paste_vcode.rs

1use crate::{editor::DOCKMANAGER, prelude::*};
2
3pub struct PasteVCode {
4    id: TheId,
5    nodeui: TheNodeUI,
6}
7
8impl Action for PasteVCode {
9    fn new() -> Self
10    where
11        Self: Sized,
12    {
13        let mut nodeui: TheNodeUI = TheNodeUI::default();
14        let item = TheNodeUIItem::Markdown("desc".into(), fl!("action_paste_vcode_desc"));
15        nodeui.add_item(item);
16
17        Self {
18            id: TheId::named(&fl!("action_paste_vcode")),
19            nodeui,
20        }
21    }
22
23    fn id(&self) -> TheId {
24        self.id.clone()
25    }
26
27    fn info(&self) -> String {
28        fl!("action_paste_vcode_desc")
29    }
30
31    fn role(&self) -> ActionRole {
32        ActionRole::Dock
33    }
34
35    fn accel(&self) -> Option<TheAccelerator> {
36        None
37    }
38
39    fn is_applicable(
40        &self,
41        _map: &Map,
42        _ctx: &mut TheContext,
43        _server_ctx: &ServerContext,
44    ) -> bool {
45        DOCKMANAGER.read().unwrap().dock == "Visual Code"
46    }
47
48    fn apply_project(
49        &self,
50        project: &mut Project,
51        ui: &mut TheUI,
52        ctx: &mut TheContext,
53        server_ctx: &mut ServerContext,
54    ) {
55        let mut content: Option<String> = None;
56        if let Some(TheValue::Text(text)) = &ctx.ui.clipboard {
57            if !text.trim().is_empty() {
58                content = Some(text.clone());
59            }
60        }
61        if content.is_none()
62            && let Ok(mut clipboard) = arboard::Clipboard::new()
63            && let Ok(text) = clipboard.get_text()
64            && !text.trim().is_empty()
65        {
66            content = Some(text);
67        }
68
69        if let Some(text) = content {
70            DOCKMANAGER
71                .write()
72                .unwrap()
73                .import(text.clone(), ui, ctx, project, server_ctx);
74            ctx.ui.clipboard = Some(TheValue::Text(text));
75            ctx.ui.clipboard_app_type = Some("text/plain".to_string());
76        }
77    }
78
79    fn params(&self) -> TheNodeUI {
80        self.nodeui.clone()
81    }
82
83    fn handle_event(
84        &mut self,
85        event: &TheEvent,
86        _project: &mut Project,
87        _ui: &mut TheUI,
88        _ctx: &mut TheContext,
89        _server_ctx: &mut ServerContext,
90    ) -> bool {
91        self.nodeui.handle_event(event)
92    }
93}