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