Skip to main content

ferrum_flow/plugins/
select_all_viewport.rs

1use crate::{
2    Edge, EdgeId, NodeId,
3    plugin::{
4        FlowEvent, Plugin, PluginContext, is_edge_visible, is_node_visible,
5        primary_platform_modifier,
6    },
7};
8
9/// Select every node and edge that intersects the current window viewport (⌘A / Ctrl+A).
10pub struct SelectAllViewportPlugin;
11
12impl SelectAllViewportPlugin {
13    pub fn new() -> Self {
14        Self
15    }
16}
17
18fn select_visible(ctx: &mut PluginContext) {
19    let order: Vec<NodeId> = ctx.graph.node_order().to_vec();
20    let visible_nodes: Vec<NodeId> = order
21        .into_iter()
22        .filter(|id| is_node_visible(ctx.graph, ctx.viewport, id))
23        .collect();
24
25    let edges: Vec<Edge> = ctx.graph.edges.values().cloned().collect();
26    let visible_edges: Vec<EdgeId> = edges
27        .into_iter()
28        .filter(|e| is_edge_visible(ctx.graph, ctx.viewport, &e))
29        .map(|e| e.id)
30        .collect();
31
32    ctx.clear_selected_node();
33    ctx.clear_selected_edge();
34
35    for id in visible_nodes.iter() {
36        ctx.add_selected_node(*id, true);
37    }
38    for id in visible_edges.iter() {
39        ctx.add_selected_edge(*id, true);
40    }
41}
42
43pub(crate) fn select_all_in_viewport(ctx: &mut PluginContext) {
44    select_visible(ctx);
45}
46
47impl Plugin for SelectAllViewportPlugin {
48    fn name(&self) -> &'static str {
49        "select_all_viewport"
50    }
51
52    fn setup(&mut self, _ctx: &mut crate::plugin::InitPluginContext) {}
53
54    fn priority(&self) -> i32 {
55        93
56    }
57
58    fn on_event(
59        &mut self,
60        event: &FlowEvent,
61        ctx: &mut PluginContext,
62    ) -> crate::plugin::EventResult {
63        if let FlowEvent::Input(crate::plugin::InputEvent::KeyDown(ev)) = event {
64            if primary_platform_modifier(ev) && ev.keystroke.key == "a" {
65                select_visible(ctx);
66                ctx.notify();
67                return crate::plugin::EventResult::Stop;
68            }
69        }
70        crate::plugin::EventResult::Continue
71    }
72}