Skip to main content

rustapi/actions/
import_vcode.rs

1use crate::{editor::DOCKMANAGER, prelude::*};
2
3pub struct ImportVCode {
4    id: TheId,
5    nodeui: TheNodeUI,
6}
7
8impl Action for ImportVCode {
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_import_vcode_desc"));
15        nodeui.add_item(item);
16
17        Self {
18            id: TheId::named(&fl!("action_import_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_import_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        ctx.ui.open_file_requester(
56            TheId::named_with_id("actionImportVisualCode", Uuid::new_v4()),
57            "Import Visual Code".into(),
58            TheFileExtension::new(
59                "Eldiron Visual Code".into(),
60                vec!["eldiron_vcode".to_string()],
61            ),
62        );
63    }
64
65    fn params(&self) -> TheNodeUI {
66        self.nodeui.clone()
67    }
68
69    fn handle_event(
70        &mut self,
71        event: &TheEvent,
72        project: &mut Project,
73        ui: &mut TheUI,
74        ctx: &mut TheContext,
75        server_ctx: &mut ServerContext,
76    ) -> bool {
77        match event {
78            TheEvent::FileRequesterResult(id, paths) => {
79                if id.name == "actionImportVisualCode" {
80                    for p in paths {
81                        if let Ok(contents) = std::fs::read_to_string(p) {
82                            DOCKMANAGER
83                                .write()
84                                .unwrap()
85                                .import(contents, ui, ctx, project, server_ctx);
86                            return true;
87                        }
88                    }
89                }
90            }
91            _ => {}
92        }
93        false
94    }
95}