1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use {
    std::cell::Cell,
    std::rc::Rc,
    crate::{
        makepad_live_id::*,
        makepad_math::*,
        event::{
            KeyModifiers,
            finger::{HitOptions, Margin},
            event::{Event, DragHit}
        },
        cx::Cx,
        area::Area,
    },
};


#[derive(Clone, Debug)]
pub struct DragEvent {
    pub modifiers: KeyModifiers,
    pub handled: Cell<bool>,
    pub abs: DVec2,
    pub items: Rc<Vec<DragItem >>,
    pub response: Rc<Cell<DragResponse >>,
}

#[derive(Clone, Debug)]
pub struct DropEvent {
    pub modifiers: KeyModifiers,
    pub handled: Cell<bool>,
    pub abs: DVec2,
    pub items: Rc<Vec<DragItem >>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct DragHitEvent {
    pub modifiers: KeyModifiers,
    pub abs: DVec2,
    pub rect: Rect,
    pub state: DragState,
    pub items: Rc<Vec<DragItem >>,
    pub response: Rc<Cell<DragResponse >>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct DropHitEvent {
    pub modifiers: KeyModifiers,
    pub abs: DVec2,
    pub rect: Rect,
    pub items: Rc<Vec<DragItem >>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum DragState {
    In,
    Over,
    Out,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DragResponse {
    None,
    Copy,
    Link,
    Move,
}

#[derive(Clone, Debug, PartialEq)]
pub enum DragItem {
    FilePath {path: String, internal_id: Option<LiveId>},
    String {value: String, internal_id: Option<LiveId>}
}

/*
pub enum HitTouch {
    Single,
    Multi
}*/


// Status


#[derive(Default)]
pub struct CxDragDrop {
    drag_area: Area,
    next_drag_area: Area,
}

impl CxDragDrop {
    #[allow(dead_code)]
    pub (crate) fn cycle_drag(&mut self) {
        self.drag_area = self.next_drag_area;
        self.next_drag_area = Area::Empty;
    }
    
    pub (crate) fn update_area(&mut self, old_area: Area, new_area: Area) {
        if self.drag_area == old_area {
            self.drag_area = new_area;
        }
    }
}

impl Event {
    
    pub fn drag_hits(&self, cx: &mut Cx, area: Area) -> DragHit {
        self.drag_hits_with_options(cx, area, HitOptions::default())
    }
    
    pub fn drag_hits_with_options(&self, cx: &mut Cx, area: Area, options: HitOptions) -> DragHit {
        match self {
            Event::Drag(event) => {
                let rect = area.get_clipped_rect(cx);
                if area == cx.drag_drop.drag_area {
                    if !event.handled.get() && Margin::rect_contains_with_margin(&rect, event.abs, &options.margin) {
                        //log!("drag_hist_with_options: Drag, in drag area, event handled and rect ({:?}) contains ({},{}) with margin {:?}",rect,event.abs.x,event.abs.y,options.margin);
                        cx.drag_drop.next_drag_area = area;
                        event.handled.set(true);
                        DragHit::Drag(DragHitEvent {
                            rect,
                            modifiers: event.modifiers,
                            abs: event.abs,
                            items: event.items.clone(),
                            state: DragState::Over,
                            response: event.response.clone()
                        })
                    } else {
                        //log!("drag_hist_with_options: Drag, in drag area, event not handled or rect ({:?}) doesn't contain ({},{}) with margin {:?}",rect,event.abs.x,event.abs.y,options.margin);
                        DragHit::Drag(DragHitEvent {
                            rect,
                            modifiers: event.modifiers,
                            state: DragState::Out,
                            items: event.items.clone(),
                            abs: event.abs,
                            response: event.response.clone()
                        })
                    }
                } else {
                    if !event.handled.get() && Margin::rect_contains_with_margin(&rect, event.abs, &options.margin) {
                        //log!("drag_hits_with_options: Drag, not in drag_area, event not handled and rect ({:?}) contains ({},{}) with margin {:?}",rect,event.abs.x,event.abs.y,options.margin);
                        cx.drag_drop.next_drag_area = area;
                        event.handled.set(true);
                        DragHit::Drag(DragHitEvent {
                            modifiers: event.modifiers,
                            rect,
                            state: DragState::In,
                            items: event.items.clone(),
                            abs: event.abs,
                            response: event.response.clone()
                        })
                    } else {
                        //log!("drag_hits_with_options: Drag, not in drag_area, event handled or rect ({:?}) doesn't contain ({},{}) with margin {:?}",rect,event.abs.x,event.abs.y,options.margin);
                        DragHit::NoHit
                    }
                }
            }
            Event::Drop(event) => {
                let rect = area.get_clipped_rect(cx);
                if !event.handled.get() && Margin::rect_contains_with_margin(&rect, event.abs, &options.margin) {
                    //log!("drag_hits_with_options: Drop, event not handled and rect {:?} contains ({},{}) in margin {:?}",rect,event.abs.x,event.abs.y,options.margin);
                    cx.drag_drop.next_drag_area = Area::default();
                    event.handled.set(true);
                    DragHit::Drop(DropHitEvent {
                        modifiers: event.modifiers,
                        rect,
                        abs: event.abs,
                        items: event.items.clone()
                    })
                } else {
                    //log!("drag_hits_with_options: Drop, event handled or rect {:?} doesn't contain ({},{}) in margin {:?}",rect,event.abs.x,event.abs.y,options.margin);
                    DragHit::NoHit
                }
            }
            _ => DragHit::NoHit,
        }
    }
    
}