Skip to main content

ferrum_flow/canvas/
types.rs

1use gpui::{AnyElement, MouseMoveEvent, MouseUpEvent};
2
3use crate::plugin::{PluginContext, RenderContext};
4
5pub struct InteractionState {
6    pub handler: Option<Box<dyn Interaction>>,
7}
8
9impl InteractionState {
10    pub fn new() -> Self {
11        Self { handler: None }
12    }
13
14    pub fn add(&mut self, handler: impl Interaction + 'static) {
15        self.handler = Some(Box::new(handler));
16    }
17
18    pub fn clear(&mut self) {
19        self.handler = None;
20    }
21
22    pub fn is_some(&self) -> bool {
23        self.handler.is_some()
24    }
25}
26
27pub trait Interaction {
28    fn on_mouse_move(
29        &mut self,
30        event: &MouseMoveEvent,
31        ctx: &mut PluginContext,
32    ) -> InteractionResult;
33
34    fn on_mouse_up(&mut self, event: &MouseUpEvent, ctx: &mut PluginContext) -> InteractionResult;
35
36    fn render(&self, _ctx: &mut RenderContext) -> Option<AnyElement> {
37        None
38    }
39}
40
41pub enum InteractionResult {
42    Continue,
43    End,
44    Replace(Box<dyn Interaction>),
45}
46
47impl InteractionResult {
48    pub fn replace(new_handler: impl Interaction + 'static) -> Self {
49        Self::Replace(Box::new(new_handler))
50    }
51}