Skip to main content

ferrum_flow/plugins/
clipboard.rs

1use crate::plugin::{FlowEvent, Plugin, PluginContext, primary_platform_modifier};
2
3use super::clipboard_ops::{extract_subgraph, paste_subgraph};
4
5/// Copy / paste selected nodes, their ports, and edges **between** those ports (one undo on paste).
6pub struct ClipboardPlugin;
7
8impl ClipboardPlugin {
9    pub fn new() -> Self {
10        Self
11    }
12}
13
14impl Plugin for ClipboardPlugin {
15    fn name(&self) -> &'static str {
16        "clipboard"
17    }
18
19    fn setup(&mut self, _ctx: &mut crate::plugin::InitPluginContext) {}
20
21    fn priority(&self) -> i32 {
22        95
23    }
24
25    fn on_event(
26        &mut self,
27        event: &FlowEvent,
28        ctx: &mut PluginContext,
29    ) -> crate::plugin::EventResult {
30        if let FlowEvent::Input(crate::plugin::InputEvent::KeyDown(ev)) = event {
31            if !primary_platform_modifier(ev) {
32                return crate::plugin::EventResult::Continue;
33            }
34            match ev.keystroke.key.as_str() {
35                "c" => {
36                    if let Some(sub) = extract_subgraph(ctx.graph) {
37                        *ctx.clipboard_subgraph = Some(sub);
38                    }
39                    return crate::plugin::EventResult::Stop;
40                }
41                "v" => {
42                    if let Some(sub) = ctx.clipboard_subgraph.clone() {
43                        paste_subgraph(ctx, &sub);
44                    }
45                    return crate::plugin::EventResult::Stop;
46                }
47                _ => {}
48            }
49        }
50        crate::plugin::EventResult::Continue
51    }
52}