Skip to main content

rustapi/actions/
copy_vcode.rs

1use crate::{editor::DOCKMANAGER, prelude::*};
2
3pub struct CopyVCode {
4    id: TheId,
5    nodeui: TheNodeUI,
6}
7
8impl Action for CopyVCode {
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_copy_vcode_desc"));
15        nodeui.add_item(item);
16
17        Self {
18            id: TheId::named(&fl!("action_copy_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_copy_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        if let Some(json) = DOCKMANAGER.read().unwrap().export() {
56            ctx.ui.clipboard = Some(TheValue::Text(json.clone()));
57            ctx.ui.clipboard_app_type = Some("text/plain".to_string());
58            if let Ok(mut clipboard) = arboard::Clipboard::new() {
59                let _ = clipboard.set_text(json);
60            }
61        }
62    }
63
64    fn params(&self) -> TheNodeUI {
65        self.nodeui.clone()
66    }
67
68    fn handle_event(
69        &mut self,
70        event: &TheEvent,
71        _project: &mut Project,
72        _ui: &mut TheUI,
73        _ctx: &mut TheContext,
74        _server_ctx: &mut ServerContext,
75    ) -> bool {
76        self.nodeui.handle_event(event)
77    }
78}