Skip to main content

rustapi/actions/
duplicate_tile.rs

1use crate::editor::DOCKMANAGER;
2use crate::prelude::*;
3
4pub struct DuplicateTile {
5    id: TheId,
6    nodeui: TheNodeUI,
7}
8
9impl Action for DuplicateTile {
10    fn new() -> Self
11    where
12        Self: Sized,
13    {
14        let mut nodeui: TheNodeUI = TheNodeUI::default();
15
16        let item = TheNodeUIItem::Markdown("desc".into(), fl!("action_duplicate_tile_desc"));
17        nodeui.add_item(item);
18
19        Self {
20            id: TheId::named(&fl!("action_duplicate_tile")),
21            nodeui,
22        }
23    }
24
25    fn id(&self) -> TheId {
26        self.id.clone()
27    }
28
29    fn info(&self) -> String {
30        fl!("action_duplicate_tile_desc")
31    }
32
33    fn role(&self) -> ActionRole {
34        ActionRole::Dock
35    }
36
37    fn accel(&self) -> Option<TheAccelerator> {
38        None
39    }
40
41    fn is_applicable(&self, _map: &Map, _ctx: &mut TheContext, server_ctx: &ServerContext) -> bool {
42        DOCKMANAGER.read().unwrap().dock == "Tiles" && server_ctx.curr_tile_id.is_some()
43    }
44
45    fn apply_project(
46        &self,
47        project: &mut Project,
48        _ui: &mut TheUI,
49        ctx: &mut TheContext,
50        server_ctx: &mut ServerContext,
51    ) {
52        if let Some(tile_id) = server_ctx.curr_tile_id {
53            if let Some(mut tile) = project.tiles.get(&tile_id).cloned() {
54                tile.id = Uuid::new_v4();
55                project.tiles.insert(tile.id, tile);
56
57                ctx.ui.send(TheEvent::Custom(
58                    TheId::named("Update Tilepicker"),
59                    TheValue::Empty,
60                ));
61
62                ctx.ui.send(TheEvent::Custom(
63                    TheId::named("Update Tiles"),
64                    TheValue::Empty,
65                ));
66            }
67        }
68    }
69
70    fn params(&self) -> TheNodeUI {
71        self.nodeui.clone()
72    }
73
74    fn handle_event(
75        &mut self,
76        event: &TheEvent,
77        _project: &mut Project,
78        _ui: &mut TheUI,
79        _ctx: &mut TheContext,
80        _server_ctx: &mut ServerContext,
81    ) -> bool {
82        self.nodeui.handle_event(event)
83    }
84}