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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use std::sync::Mutex;

use smallvec::smallvec;

use crate::draw::Primitive;
use crate::event::{Event, Key, NodeEvent};
use crate::layout::{Rectangle, Size};
use crate::stylesheet::{StyleState, Stylesheet};
use crate::widget::{Context, Frame, IntoNode, Node, StateVec, Widget};

/// Message type for communicating between `Drag` and `Drop` widgets
pub trait DragDropId: 'static + Copy + Send {}

/// Context for `Drag` and `Drop` widgets. Only `Drag` and `Drop` widgets that share the same `DragDropContext` can
/// interact with each other.
//#[derive(Clone)]
pub struct DragDropContext<T: DragDropId> {
    data: Mutex<Option<(T, (f32, f32))>>,
}

/// A draggable item that can be dropped in `Drop` zones.
pub struct Drag<'a, T: DragDropId, Message> {
    state: &'a mut DragState<T>,
    context: &'a DragDropContext<T>,
    data: T,
    content: Frame<'a, Message>,
}

/// State for `Drag`
pub struct DragState<T> {
    dragging: Option<T>,
    origin: (f32, f32),
    offset: (f32, f32),
}

/// A drop zone where draggable `Drag` items may be dropped
pub struct Drop<'a, T: DragDropId, Message, OnAccept, OnDrop> {
    state: &'a mut DropState<T>,
    context: &'a DragDropContext<T>,
    accept: OnAccept,
    drop: OnDrop,
    content: Frame<'a, Message>,
}

/// State for `Drop`
pub struct DropState<T> {
    hovering: Option<(T, (f32, f32))>,
}

impl<'a, T: DragDropId, Message: 'a> Drag<'a, T, Message> {
    /// Construct a new `Drag` widget, with some data that is to be dragged through the context.
    pub fn new(
        state: &'a mut DragState<T>,
        context: &'a DragDropContext<T>,
        data: T,
        content: impl IntoNode<'a, Message>,
    ) -> Self {
        Self {
            state,
            context,
            data,
            content: Frame::new(content),
        }
    }
}

impl<'a, T: DragDropId, Message: 'a, OnAccept: 'a, OnDrop: 'a> Drop<'a, T, Message, OnAccept, OnDrop>
where
    OnAccept: Fn(T) -> bool,
    OnDrop: Fn(T, (f32, f32)) -> Message,
{
    /// Construct a new `Drop` widget
    pub fn new(
        state: &'a mut DropState<T>,
        context: &'a DragDropContext<T>,
        accept: OnAccept,
        drop: OnDrop,
        content: impl IntoNode<'a, Message>,
    ) -> Self {
        Self {
            state,
            context,
            accept,
            drop,
            content: Frame::new(content),
        }
    }
}

impl<'a, T: DragDropId + Send, Message: 'a> Widget<'a, Message> for Drag<'a, T, Message> {
    fn widget(&self) -> &'static str {
        "drag"
    }

    fn state(&self) -> StateVec {
        if self.state.dragging.is_some() {
            smallvec![StyleState::Drag]
        } else {
            smallvec![]
        }
    }

    fn len(&self) -> usize {
        self.content.len()
    }

    fn visit_children(&mut self, visitor: &mut dyn FnMut(&mut Node<'a, Message>)) {
        self.content.visit_children(visitor);
    }

    fn size(&self, style: &Stylesheet) -> (Size, Size) {
        self.content.size(style)
    }

    fn event(
        &mut self,
        layout: Rectangle,
        clip: Rectangle,
        style: &Stylesheet,
        event: Event,
        context: &mut Context<Message>,
    ) {
        match event {
            Event::Cursor(x, y) if self.state.dragging.is_some() => {
                self.state.offset = (x - self.state.origin.0, y - self.state.origin.1);
                context.redraw();
            }

            Event::Release(Key::LeftMouseButton) if self.state.dragging.is_some() => {
                self.state.dragging.take();
                self.context.data.lock().unwrap().take();
                context.redraw();
            }

            _ => (),
        }

        self.content.event(layout, clip, style, event, context);
    }

    fn node_event(&mut self, layout: Rectangle, _: &Stylesheet, event: NodeEvent, context: &mut Context<Message>) {
        match event {
            NodeEvent::MouseDown(Key::LeftMouseButton) => {
                self.context.data.lock().unwrap().replace((
                    self.data,
                    (context.cursor.0 - layout.left, context.cursor.1 - layout.top),
                ));
                self.state.origin = context.cursor;
                self.state.offset = (0.0, 0.0);
                self.state.dragging = Some(self.data);
                context.redraw();
            }

            _ => (),
        }
    }

    fn draw(&mut self, layout: Rectangle, clip: Rectangle, style: &Stylesheet) -> Vec<Primitive<'a>> {
        if self.state.dragging.is_some() {
            let (dx, dy) = self.state.offset;
            let mut result = Vec::new();
            result.push(Primitive::LayerUp);
            result.extend(self.content.draw(layout.translate(dx, dy), clip, style));
            result.push(Primitive::LayerDown);
            result
        } else {
            self.content.draw(layout, clip, style)
        }
    }
}

impl<'a, T: DragDropId, Message: 'a, OnAccept, OnDrop> Widget<'a, Message> for Drop<'a, T, Message, OnAccept, OnDrop>
where
    OnAccept: Send + Fn(T) -> bool,
    OnDrop: Send + Fn(T, (f32, f32)) -> Message,
{
    fn widget(&self) -> &'static str {
        "drop"
    }

    fn state(&self) -> StateVec {
        if self.state.hovering.is_some() {
            smallvec![StyleState::Drop]
        } else {
            smallvec![]
        }
    }

    fn len(&self) -> usize {
        self.content.len()
    }

    fn visit_children(&mut self, visitor: &mut dyn FnMut(&mut Node<'a, Message>)) {
        self.content.visit_children(visitor);
    }

    fn size(&self, style: &Stylesheet) -> (Size, Size) {
        self.content.size(style)
    }

    fn event(
        &mut self,
        layout: Rectangle,
        clip: Rectangle,
        style: &Stylesheet,
        event: Event,
        context: &mut Context<Message>,
    ) {
        self.content.event(layout, clip, style, event, context)
    }

    fn node_event(&mut self, layout: Rectangle, _: &Stylesheet, event: NodeEvent, context: &mut Context<Message>) {
        match event {
            NodeEvent::MouseEnter => {
                if let Some(data) = self.context.data.lock().unwrap().clone() {
                    if (self.accept)(data.0) {
                        self.state.hovering = Some(data);
                    }
                }
            }
            NodeEvent::MouseLeave => {
                self.state.hovering = None;
            }
            NodeEvent::MouseUp(Key::LeftMouseButton) => {
                if let Some(data) = self.state.hovering.take() {
                    context.push((self.drop)(
                        data.0,
                        (
                            context.cursor.0 - (data.1).0 - layout.left,
                            context.cursor.1 - (data.1).1 - layout.top,
                        ),
                    ));
                }
            }

            _ => (),
        }
    }

    fn draw(&mut self, layout: Rectangle, clip: Rectangle, style: &Stylesheet) -> Vec<Primitive<'a>> {
        self.content.draw(layout, clip, style)
    }
}

impl<'a, T: DragDropId, Message: 'a> IntoNode<'a, Message> for Drag<'a, T, Message> {
    fn into_node(self) -> Node<'a, Message> {
        Node::new(self)
    }
}

impl<'a, T: DragDropId, Message: 'a, OnAccept: 'a, OnDrop: 'a> IntoNode<'a, Message>
    for Drop<'a, T, Message, OnAccept, OnDrop>
where
    OnAccept: Send + Fn(T) -> bool,
    OnDrop: Send + Fn(T, (f32, f32)) -> Message,
{
    fn into_node(self) -> Node<'a, Message> {
        Node::new(self)
    }
}

impl<T: 'static + Copy + Send> DragDropId for T {}

impl<T: DragDropId> Default for DragDropContext<T> {
    fn default() -> Self {
        Self { data: Mutex::new(None) }
    }
}

impl<T: DragDropId> Default for DragState<T> {
    fn default() -> Self {
        Self {
            dragging: None,
            origin: (0.0, 0.0),
            offset: (0.0, 0.0),
        }
    }
}

impl<T: DragDropId> Default for DropState<T> {
    fn default() -> Self {
        Self { hovering: None }
    }
}