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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use crate::{
    makepad_derive_widget::*,
    makepad_draw::*,
    widget::*,
};

live_design!{
    DrawSplitter= {{DrawSplitter}} {}
    SplitterBase = {{Splitter}} {}
}


#[derive(Live, LiveHook)]
#[repr(C)]
pub struct DrawSplitter {
    #[deref] draw_super: DrawQuad,
    #[live] is_vertical: f32,
}

#[derive(Live)]
pub struct Splitter {
    #[live(Axis::Horizontal)] pub axis: Axis,
    #[live(SplitterAlign::Weighted(0.5))] pub align: SplitterAlign,
    #[rust] rect: Rect,
    #[rust] position: f64,
    #[rust] drag_start_align: Option<SplitterAlign>,
    #[rust] area_a: Area,
    #[rust] area_b: Area,
    #[animator] animator: Animator,
    
    #[live] min_vertical: f64,
    #[live] max_vertical: f64,
    #[live] min_horizontal: f64,
    #[live] max_horizontal: f64,
    
    #[live] draw_splitter: DrawSplitter,
    #[live] split_bar_size: f64,
    
    // framecomponent mode
    #[rust] draw_state: DrawStateWrap<DrawState>,
    #[live] a: WidgetRef,
    #[live] b: WidgetRef,
    #[walk] walk: Walk,
}

impl LiveHook for Splitter{
    fn before_live_design(cx:&mut Cx){
        register_widget!(cx,Splitter)
    }
}

#[derive(Clone)]
enum DrawState {
    DrawA,
    DrawSplit,
    DrawB
}

impl Widget for Splitter {
   fn handle_widget_event_with(
        &mut self,
        cx: &mut Cx,
        event: &Event,
        dispatch_action: &mut dyn FnMut(&mut Cx, WidgetActionItem)
    ) {
        let mut redraw = false;
        let uid = self.widget_uid();
        self.handle_event_with(cx, event, &mut | cx, action | {
            dispatch_action(cx, WidgetActionItem::new(action.into(), uid));
            redraw = true;
        });
        self.a.handle_widget_event_with(cx, event, dispatch_action);
        self.b.handle_widget_event_with(cx, event, dispatch_action);
        if redraw {
            self.a.redraw(cx);
            self.b.redraw(cx);
        }
    }
    
    fn walk(&mut self, _cx:&mut Cx) -> Walk {
        self.walk
    }
    
    fn redraw(&mut self, cx:&mut Cx){
        self.draw_splitter.redraw(cx)
    }
    
    fn find_widgets(&mut self, path: &[LiveId], cached: WidgetCache, results:&mut WidgetSet) {
        self.a.find_widgets(path, cached, results);
        self.b.find_widgets(path, cached, results);
    }
    
    fn draw_walk_widget(&mut self, cx: &mut Cx2d, walk: Walk) -> WidgetDraw {
        if self.draw_state.begin(cx, DrawState::DrawA) {
            self.begin(cx, walk);
        }
        if let Some(DrawState::DrawA) = self.draw_state.get() {
            self.a.draw_widget(cx) ?;
            self.draw_state.set(DrawState::DrawSplit);
        }
        if let Some(DrawState::DrawSplit) = self.draw_state.get() {
            self.middle(cx);
            self.draw_state.set(DrawState::DrawB)
        }
        if let Some(DrawState::DrawB) = self.draw_state.get() {
            self.b.draw_widget(cx) ?;
            self.end(cx);
            self.draw_state.end();
        }
        WidgetDraw::done()
    }
}

impl Splitter {
    pub fn begin(&mut self, cx: &mut Cx2d, walk: Walk) {
        // we should start a fill turtle in the layout direction of choice
        match self.axis {
            Axis::Horizontal => {
                cx.begin_turtle(walk, Layout::flow_right());
            }
            Axis::Vertical => {
                cx.begin_turtle(walk, Layout::flow_down());
            }
        }
        
        self.rect = cx.turtle().padded_rect();
        self.position = self.align.to_position(self.axis, self.rect);
        
        let walk = match self.axis {
            Axis::Horizontal => Walk::size(Size::Fixed(self.position), Size::Fill),
            Axis::Vertical => Walk::size(Size::Fill, Size::Fixed(self.position)),
        };
        cx.begin_turtle(walk, Layout::flow_down());
    }
    
    pub fn middle(&mut self, cx: &mut Cx2d) {
        cx.end_turtle_with_area(&mut self.area_a);
        match self.axis {
            Axis::Horizontal => {
                self.draw_splitter.is_vertical = 1.0;
                self.draw_splitter.draw_walk(cx, Walk::size(Size::Fixed(self.split_bar_size), Size::Fill));
            }
            Axis::Vertical => {
                self.draw_splitter.is_vertical = 0.0;
                self.draw_splitter.draw_walk(cx, Walk::size(Size::Fill, Size::Fixed(self.split_bar_size)));
            }
        }
        cx.begin_turtle(Walk::default(), Layout::flow_down());
    }
    
    pub fn end(&mut self, cx: &mut Cx2d) {
        cx.end_turtle_with_area(&mut self.area_b);
        cx.end_turtle();
    }
    
    pub fn axis(&self) -> Axis {
        self.axis
    }

    pub fn area_a(&self) -> Area {
        self.area_a
    }
    
    pub fn area_b(&self) -> Area {
        self.area_b
    }
    
    pub fn set_axis(&mut self, axis: Axis) {
        self.axis = axis;
    }
    
    pub fn align(&self) -> SplitterAlign {
        self.align
    }
    
    pub fn set_align(&mut self, align: SplitterAlign) {
        self.align = align;
    }
    
    pub fn handle_event_with(
        &mut self,
        cx: &mut Cx,
        event: &Event,
        dispatch_action: &mut dyn FnMut(&mut Cx, SplitterAction),
    ) {
        self.animator_handle_event(cx, event);
        match event.hits_with_options(cx, self.draw_splitter.area(), HitOptions::new().with_margin(self.margin())) {
        Hit::FingerHoverIn(_) => {
            match self.axis {
                Axis::Horizontal => cx.set_cursor(MouseCursor::ColResize),
                Axis::Vertical => cx.set_cursor(MouseCursor::RowResize),
            }
            self.animator_play(cx, id!(hover.on));
        }
        Hit::FingerHoverOut(_) => {
            self.animator_play(cx, id!(hover.off));
        },
        Hit::FingerDown(_) => {
            match self.axis {
                Axis::Horizontal => cx.set_cursor(MouseCursor::ColResize),
                Axis::Vertical => cx.set_cursor(MouseCursor::RowResize),
            }
            self.animator_play(cx, id!(hover.pressed));
            self.drag_start_align = Some(self.align);
        }
        Hit::FingerUp(f) => {
            self.drag_start_align = None;
            if f.is_over && f.device.has_hovers() {
                self.animator_play(cx, id!(hover.on));
            }
            else {
                self.animator_play(cx, id!(hover.off));
            }
        }
        Hit::FingerMove(f) => {
            if let Some(drag_start_align) = self.drag_start_align {
                let delta = match self.axis {
                    Axis::Horizontal => f.abs.x - f.abs_start.x,
                    Axis::Vertical => f.abs.y - f.abs_start.y,
                };
                let new_position =
                drag_start_align.to_position(self.axis, self.rect) + delta;
                self.align = match self.axis {
                    Axis::Horizontal => {
                        let center = self.rect.size.x / 2.0;
                        if new_position < center - 30.0 {
                            SplitterAlign::FromA(new_position.max(self.min_vertical))
                        } else if new_position > center + 30.0 {
                            SplitterAlign::FromB((self.rect.size.x - new_position).max(self.max_vertical))
                        } else {
                            SplitterAlign::Weighted(new_position / self.rect.size.x)
                        }
                    }
                    Axis::Vertical => {
                        let center = self.rect.size.y / 2.0;
                        if new_position < center - 30.0 {
                            SplitterAlign::FromA(new_position.max(self.min_horizontal))
                        } else if new_position > center + 30.0 {
                            SplitterAlign::FromB((self.rect.size.y - new_position).max(self.max_horizontal))
                        } else {
                            SplitterAlign::Weighted(new_position / self.rect.size.y)
                        }
                    }
                };
                self.draw_splitter.redraw(cx);
                dispatch_action(cx, SplitterAction::Changed {axis: self.axis, align: self.align});
            }
        }
        _ => {}
    }
}

fn margin(&self) -> Margin {
    match self.axis {
        Axis::Horizontal => Margin {
            left: 3.0,
            top: 0.0,
            right: 3.0,
            bottom: 0.0,
        },
        Axis::Vertical => Margin {
            left: 0.0,
            top: 3.0,
            right: 0.0,
            bottom: 3.0,
        },
    }
}
}

#[derive(Clone, Copy, Debug, Live, LiveHook)]
#[live_ignore]
pub enum SplitterAlign {
    #[live(50.0)] FromA(f64),
    #[live(50.0)] FromB(f64),
    #[pick(0.5)] Weighted(f64),
}

impl SplitterAlign {
    fn to_position(self, axis: Axis, rect: Rect) -> f64 {
        match axis {
            Axis::Horizontal => match self {
                Self::FromA(position) => position,
                Self::FromB(position) => rect.size.x - position,
                Self::Weighted(weight) => weight * rect.size.x,
            },
            Axis::Vertical => match self {
                Self::FromA(position) => position,
                Self::FromB(position) => rect.size.y - position,
                Self::Weighted(weight) => weight * rect.size.y,
            },
        }
    }
}

#[derive(Clone, WidgetAction)]
pub enum SplitterAction {
    None,
    Changed {axis: Axis, align: SplitterAlign},
}