deft 0.15.0

Cross platform ui framework
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate as deft;
use crate::animation::actor::AnimationActor;
use crate::animation::{AnimationInstance, WindowAnimationController};
use crate::base::{EventContext, Rect};
use crate::element::common::ScrollBar;
use crate::element::scroll::Scroll;
use crate::element::{Element, ElementWeak};
use crate::event::{Event, TouchCancelEvent, TouchEndEvent, TouchMoveEvent, TouchStartEvent};
use crate::number::DeNan;
use crate::render::RenderFn;
use crate::style::ResolvedStyleProp;
use crate::{is_mobile_platform, some_or_return};
use bezier_rs::{Bezier, TValue};
use deft_macros::mrc_object;
use log::debug;
use std::cell::Cell;
use std::collections::HashMap;
use std::time::Instant;
use yoga::Direction::LTR;

thread_local! {
    static CONSUMED_EVENT_ID: Cell<u64> = Cell::new(0);
}

#[mrc_object]
pub struct Scrollable {
    pub vertical_bar: ScrollBar,
    pub horizontal_bar: ScrollBar,
    momentum_info: Option<crate::element::scroll::MomentumInfo>,
    momentum_animation_instance: Option<AnimationInstance>,
    vertical_move_begin: Option<(f32, f32)>,
    /// (mouse_offset, scroll_offset)
    horizontal_move_begin: Option<(f32, f32)>,
    auto_scroll_callback: Option<Box<dyn FnOnce() -> Option<Rect>>>,
}

impl Scrollable {
    pub fn new() -> Self {
        let vertical_bar = ScrollBar::new_vertical();
        let horizontal_bar = ScrollBar::new_horizontal();
        ScrollableData {
            vertical_bar,
            horizontal_bar,
            momentum_animation_instance: None,
            momentum_info: None,
            vertical_move_begin: None,
            horizontal_move_begin: None,
            auto_scroll_callback: None,
        }
        .to_ref()
    }

    /// Scroll rect into view.
    /// Returns true if scroll occurs
    pub fn scroll_into_view(&mut self, rect: &Rect) -> bool {
        let scrolled_y = self.vertical_bar.scroll_into_view(rect.y, rect.height);
        let scrolled_x = self.horizontal_bar.scroll_into_view(rect.x, rect.width);
        scrolled_x || scrolled_y
    }

    pub fn execute_auto_scroll_callback(&mut self) {
        if let Some(auto_scroll_callback) = self.auto_scroll_callback.take() {
            if let Some(rect) = auto_scroll_callback() {
                self.scroll_into_view(&rect);
            }
        }
    }

    pub fn render(&mut self) -> RenderFn {
        let vertical_bar = self.vertical_bar.render();
        let horizontal_bar = self.horizontal_bar.render();
        RenderFn::merge(vec![vertical_bar, horizontal_bar])
    }

    pub fn scroll_offset(&self) -> (f32, f32) {
        let offset_y = self.vertical_bar.scroll_offset();
        let offset_x = self.horizontal_bar.scroll_offset();
        (offset_x, offset_y)
    }

    pub fn set_autoscroll_callback<F: FnOnce() -> Option<Rect> + 'static>(
        &mut self,
        autoscroll_callback: F,
    ) {
        self.auto_scroll_callback = Some(Box::new(autoscroll_callback));
    }

    pub fn is_scrollable(&self) -> bool {
        self.vertical_bar.is_scrollable() || self.horizontal_bar.is_scrollable()
    }

    pub fn on_event(
        &mut self,
        event: &Event,
        ctx: &mut EventContext<ElementWeak>,
        element: &Element,
    ) -> bool {
        let event_id = ctx.get_id();
        if !self.is_scrollable() || CONSUMED_EVENT_ID.get() == event_id {
            return false;
        }
        let accepted =
            self.vertical_bar.on_event(&event, ctx) || self.horizontal_bar.on_event(&event, ctx);
        if accepted {
            CONSUMED_EVENT_ID.set(event_id);
        } else if event_id != CONSUMED_EVENT_ID.get() {
            if let Some(e) = TouchStartEvent::cast(event) {
                // debug!("touch start: {:?}", e.0);
                let d = &e.0;
                let touch = unsafe { d.touches.get_unchecked(0) };
                let (window_x, window_y) =
                    match Scroll::map_element_window_xy(element, touch.window_x, touch.window_y) {
                        None => return false,
                        Some(v) => v,
                    };
                self.begin_scroll_x(-window_x);
                self.begin_scroll_y(-window_y);
                debug!("touch start: pos {:?}", (window_x, window_y));
                self.momentum_info = Some(crate::element::scroll::MomentumInfo {
                    start_time: Instant::now(),
                    start_left: self.horizontal_bar.scroll_offset,
                    start_top: self.vertical_bar.scroll_offset,
                });
                self.momentum_animation_instance = None;
                CONSUMED_EVENT_ID.set(event_id);
                return false;
            } else if let Some(e) = TouchMoveEvent::cast(event) {
                // debug!("touch move: {:?}", e.0);
                let d = &e.0;
                let touch = unsafe { d.touches.get_unchecked(0) };
                let (window_x, window_y) =
                    match Scroll::map_element_window_xy(element, touch.window_x, touch.window_y) {
                        None => return false,
                        Some(v) => v,
                    };
                self.update_scroll_x(-window_x);
                self.update_scroll_y(-window_y);
                let left = self.horizontal_bar.scroll_offset;
                let top = self.vertical_bar.scroll_offset;
                // debug!("touch updated: {:?}", (window_x, window_y));
                if let Some(momentum_info) = &mut self.momentum_info {
                    if momentum_info.start_time.elapsed().as_millis() as f32
                        > crate::element::scroll::MOMENTUM_DURATION
                    {
                        momentum_info.start_time = Instant::now();
                        momentum_info.start_left = left;
                        momentum_info.start_top = top;
                    }
                }
                CONSUMED_EVENT_ID.set(event_id);
                return false;
            } else if let Some(e) = TouchEndEvent::cast(event) {
                debug!("touch end: {:?} {}", e.0, self.momentum_info.is_some());
                if let Some(momentum_info) = &self.momentum_info {
                    let duration =
                        momentum_info.start_time.elapsed().as_nanos() as f32 / 1000_000.0;
                    let horizontal_distance = self.scroll_offset().0 - momentum_info.start_left;
                    let vertical_distance = self.scroll_offset().1 - momentum_info.start_top;
                    let max_distance = f32::max(horizontal_distance.abs(), vertical_distance.abs());
                    debug!("touch end: info{:?}", (duration, vertical_distance));
                    if duration < crate::element::scroll::MOMENTUM_DURATION
                        && max_distance > crate::element::scroll::MOMENTUM_DISTANCE
                    {
                        let horizontal_speed =
                            crate::element::scroll::calculate_speed(horizontal_distance, duration);
                        let vertical_speed =
                            crate::element::scroll::calculate_speed(vertical_distance, duration);
                        debug!("speed: {} {}", horizontal_speed, vertical_speed);
                        let (old_left, old_top) = self.scroll_offset();
                        let left_dist = horizontal_speed / 0.003;
                        let top_dist = vertical_speed / 0.003;
                        debug!(
                            "scroll params: {} {} {} {} {}",
                            old_left, old_top, left_dist, top_dist, duration
                        );
                        let actor = ScrollAnimationActor::new(
                            self.clone(),
                            old_left,
                            old_top,
                            left_dist,
                            top_dist,
                        );
                        let window = some_or_return!(element.get_window(), false);
                        let fc = WindowAnimationController::new(window);
                        let mut ai =
                            AnimationInstance::new(actor, 1000.0 * 1000000.0, 1.0, Box::new(fc));
                        ai.run();
                        self.momentum_animation_instance = Some(ai);
                    }
                }
                self.momentum_info = None;
                self.end_scroll();
                CONSUMED_EVENT_ID.set(event_id);
                return false;
            } else if let Some(_e) = TouchCancelEvent::cast(event) {
                self.end_scroll();
                self.momentum_info = None;
                return false;
            }
        }
        accepted
    }

    pub fn is_mouse_over_bar(&self, x: f32, y: f32) -> bool {
        self.vertical_bar.is_mouse_over(x, y) || self.horizontal_bar.is_mouse_over(x, y)
    }

    pub fn accept_css_style(&mut self, styles: &HashMap<String, Vec<ResolvedStyleProp>>) -> bool {
        let mut accepted = false;
        if let Some(scrollbar_styles) = styles.get("scrollbar") {
            for style in scrollbar_styles {
                match style {
                    ResolvedStyleProp::BackgroundColor(color) => {
                        self.vertical_bar.set_track_background_color(*color);
                        self.horizontal_bar.set_track_background_color(*color);
                        accepted = true;
                    }
                    _ => {}
                }
            }
        }
        if let Some(thumb_styles) = styles.get("scrollbar-thumb") {
            for style in thumb_styles {
                match style {
                    ResolvedStyleProp::BackgroundColor(color) => {
                        self.vertical_bar.set_thumb_background_color(*color);
                        self.horizontal_bar.set_thumb_background_color(*color);
                        accepted = true;
                    }
                    _ => {}
                }
            }
        }
        accepted
    }

    pub fn update_size(&mut self, size: (f32, f32), content_size: (f32, f32)) {
        let box_width = size.0.de_nan(f32::INFINITY);
        let box_height = size.1.de_nan(f32::INFINITY);
        let content_height = content_size.1.de_nan(0.0);
        let content_width = content_size.0.de_nan(0.0);

        self.vertical_bar
            .set_length(box_height, content_height, box_width);
        self.horizontal_bar
            .set_length(box_width, content_width, box_height);
    }

    pub fn update_layout(&mut self, element: &mut Element) {
        let bounds = element.get_bounds();
        //TODO avoid recalculate
        // if size == self.last_layout_size {
        //     return;
        // }
        let border = element.get_border_width();
        self.do_layout_content(
            element,
            bounds.width - border.1 - border.3,
            bounds.height - border.0 - border.2,
        );
    }

    fn do_layout_content(&mut self, element: &mut Element, bounds_width: f32, bounds_height: f32) {
        // print_time!("scroll layout content time");
        self.layout_content(element, bounds_width, bounds_height);

        // let (mut body_width, body_height) = self.get_body_view_size(bounds_width, bounds_height);
        let (mut real_content_width, mut real_content_height) = element.get_real_content_size();

        //TODO optimize, do not emit events
        let old_vertical_bar_visible = self.vertical_bar.visible_thickness() > 0.0;
        let old_horizontal_bar_visible = self.horizontal_bar.visible_thickness() > 0.0;

        self.vertical_bar
            .set_length(bounds_height, real_content_height, bounds_width);
        let new_vertical_bar_visible = self.vertical_bar.visible_thickness() > 0.0;

        if old_vertical_bar_visible != new_vertical_bar_visible {
            if !is_mobile_platform() {
                self.layout_content(element, bounds_width, bounds_height);
            }
            // (body_width, _) = self.get_body_view_size(bounds_width, bounds_height);
            (real_content_width, real_content_height) = element.get_real_content_size();
            self.vertical_bar
                .set_length(bounds_height, real_content_height, bounds_width);
        }

        self.horizontal_bar
            .set_length(bounds_width, real_content_width, bounds_height);
        let new_horizontal_bar_visible = self.horizontal_bar.visible_thickness() > 0.0;
        if old_horizontal_bar_visible != new_horizontal_bar_visible {
            if !is_mobile_platform() {
                self.layout_content(element, bounds_width, bounds_height);
            }
            (real_content_width, real_content_height) = element.get_real_content_size();
        }
        self.vertical_bar
            .set_length(bounds_height, real_content_height, bounds_width);
        self.horizontal_bar
            .set_length(bounds_width, real_content_width, bounds_height);

        let vbw = self.vertical_bar.visible_thickness();
        let hbw = self.horizontal_bar.visible_thickness();
        element.set_child_decoration((0.0, vbw, hbw, 0.0));
    }

    pub fn layout_content(&mut self, element: &mut Element, bounds_width: f32, bounds_height: f32) {
        let (width, height) = self.get_body_view_size(bounds_width, bounds_height);
        //TODO fix ltr
        // self.element.style.calculate_shadow_layout(width, height, LTR);
        // let layout_width = width;
        let layout_height = height;
        // self.element.style.calculate_shadow_layout(f32::NAN, f32::NAN, LTR);
        element.before_layout_recurse();
        element
            .style
            .calculate_shadow_layout(width, layout_height, LTR);

        for child in &mut element.get_children().clone() {
            //TODO remove?
            child.on_layout_update();
        }
    }

    fn get_body_view_size(&self, mut width: f32, mut height: f32) -> (f32, f32) {
        if !is_mobile_platform() {
            width -= self.vertical_bar.visible_thickness();
            height -= self.horizontal_bar.visible_thickness();
        }

        width = f32::max(0.0, width);
        height = f32::max(0.0, height);

        (width, height)
    }

    fn begin_scroll_y(&mut self, y: f32) {
        self.vertical_move_begin = Some((y, self.vertical_bar.scroll_offset));
    }

    fn begin_scroll_x(&mut self, x: f32) {
        self.horizontal_move_begin = Some((x, self.horizontal_bar.scroll_offset));
    }

    fn update_scroll_y(&mut self, y: f32) {
        if let Some((begin_y, begin_top)) = self.vertical_move_begin {
            let mouse_move_distance = y - begin_y;
            let distance = mouse_move_distance;
            self.vertical_bar.update_scroll_offset(begin_top + distance);
        }
    }

    fn update_scroll_x(&mut self, x: f32) {
        if let Some((begin_x, begin_left)) = self.horizontal_move_begin {
            let mouse_move_distance = x - begin_x;
            let distance = mouse_move_distance;
            self.horizontal_bar
                .update_scroll_offset(begin_left + distance)
        }
    }

    fn end_scroll(&mut self) {
        self.vertical_move_begin = None;
        self.horizontal_move_begin = None;
    }
}

pub struct ScrollAnimationActor {
    old_left: f32,
    left_dist: f32,
    old_top: f32,
    top_dist: f32,
    //TODO use weak
    element: Scrollable,
    timing_func: Bezier,
}

impl ScrollAnimationActor {
    pub fn new(
        scrollable: Scrollable,
        old_left: f32,
        old_top: f32,
        left_dist: f32,
        top_dist: f32,
    ) -> Self {
        let timing_func = Bezier::from_cubic_coordinates(0.0, 0.0, 0.17, 0.89, 0.45, 1.0, 1.0, 1.0);
        Self {
            old_left,
            left_dist,
            old_top,
            top_dist,
            element: scrollable,
            timing_func,
        }
    }
}

impl AnimationActor for ScrollAnimationActor {
    fn apply_animation(&mut self, position: f32, stop: &mut bool) {
        let mut left_stopped = self.left_dist == 0.0;
        let mut top_stooped = self.top_dist == 0.0;

        if !left_stopped {
            let new_left = self.old_left
                + self.left_dist
                    * self
                        .timing_func
                        .evaluate(TValue::Parametric(position as f64))
                        .y as f32;
            self.element.horizontal_bar.update_scroll_offset(new_left);
            // ele.set_scroll_left(new_left);
            left_stopped =
                new_left < 0.0 || new_left > self.element.horizontal_bar.get_max_scroll_offset();
        }
        if !top_stooped {
            let new_top = self.old_top
                + self.top_dist
                    * self
                        .timing_func
                        .evaluate(TValue::Parametric(position as f64))
                        .y as f32;
            self.element.vertical_bar.update_scroll_offset(new_top);
            top_stooped =
                new_top < 0.0 || new_top > self.element.vertical_bar.get_max_scroll_offset();
        }
        if left_stopped && top_stooped {
            debug!("animation stopped: {} {}", left_stopped, top_stooped);
            *stop = true;
        }
    }
}