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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use std::collections::{HashMap, VecDeque};

use druid::{piet::*, *};

type InnerBuilder<T> = HashMap<&'static str, Box<dyn Fn() -> Box<dyn Widget<T>>>>;
type Inner<T> = HashMap<&'static str, WidgetPod<T, Box<dyn Widget<T>>>>;

const ANIMATION_TIME: u64 = 300_000000;

/// 当分页组件跳转到指定页面时抛出的 [`druid::Notification`]
///
/// 参数是跳转到的当前页面 ID
pub const ON_PAGE: Selector<&'static str> = Selector::new("net.stevexmh.scl.on-page");
/// 使分页组件跳转到指定页面时的 [`druid::Command`]
///
/// 参数是需要跳转到的页面 ID
pub const PUSH_PAGE: Selector<&str> = Selector::new("net.stevexmh.scl.push-page");
/// 当分页组件即将离开指定页面时抛出的 [`druid::Notification`]
///
/// 参数是即将离开的页面 ID
pub const POP_PAGE: Selector<&str> = Selector::new("net.stevexmh.scl.pop-page");
/// 使分页组件回到上一个或指定页面时的 [`druid::Command`]
///
/// 字符串为需要返回的页面,如果留空则回到上一个页面,如果非空则一直返回到该页面或回到第一页
pub const QUERY_POP_PAGE: Selector<&str> = Selector::new("net.stevexmh.scl.query-pop-page");
/// 使分页组件使用滑动动画而非缩放渐变动画的 [`druid::Command`]
///
/// 参数为是否使用滑动动画
pub const SET_SLIDE_PAGE_ANIMATION: Selector<bool> =
    Selector::new("net.stevexmh.scl.set-slide-page-animation");

#[derive(Debug)]
enum AnimationType {
    PushZoom(&'static str),
    PopZoom(&'static str),
    PushSlide(&'static str),
    PopSlide(&'static str),
    // Used when first started
    PushMoveUp(&'static str),
}

/// 一个分页组件,提供类似 WinUI 3 的缩放/滑动切换动画来切换所选页面
///
/// 且看不到的页面会被释放,不会占用过多内存
///
/// 每个页面必须提供一个静态的页面 ID [`str`],作为唯一标识以进行跳转等操作
///
/// 默认情况下,第一个被注册的页面将会作为首页被显示出来
///
/// 之后通过发送 [`PUSH_PAGE`] 或 [`QUERY_POP_PAGE`] 来跳转/退出指定页面
///
/// 推荐配合 [`crate::widgets::WindowWidget`] 使用,配合其返回按钮的 [`crate::widgets::BACK_PAGE_CLICKED`] 通知来更好地返回页面
pub struct PageSwitcher<T> {
    page_anime_timer: u64,
    active_page: &'static str,
    page_chain: Vec<&'static str>,
    page_anime_queue: VecDeque<AnimationType>,
    inner_builder: InnerBuilder<T>,
    inner: Inner<T>,
    slide_page_animation: bool,
    skip_first_anime_frame: u8,
}

impl<T> PageSwitcher<T> {
    /// 创建一个空白的分页组件
    pub fn new() -> Self {
        PageSwitcher::default()
    }
}

impl<T> Default for PageSwitcher<T> {
    fn default() -> Self {
        Self {
            page_anime_timer: 0,
            active_page: "",
            page_anime_queue: VecDeque::with_capacity(16),
            page_chain: Vec::with_capacity(8),
            inner_builder: HashMap::with_capacity(1),
            inner: HashMap::with_capacity(1),
            slide_page_animation: false,
            skip_first_anime_frame: 0,
        }
    }
}

impl<T: Data> PageSwitcher<T> {
    fn clean_unused_page(&mut self) {
        self.inner.retain(|k, _| self.page_chain.contains(k));
    }

    fn load_page(&mut self, key: &'static str) {
        if !self.inner.contains_key(key) {
            if let Some(page_builder) = self.inner_builder.get(key) {
                let w = WidgetPod::new(page_builder()).boxed();
                self.inner.insert(key, w);
            }
        }
    }

    /// 增加一个页面,需要一个页面 ID 和一个生成该页面组件的回调函数
    pub fn add_page(
        &mut self,
        key: &'static str,
        page_widget: Box<dyn Fn() -> Box<dyn Widget<T> + 'static>>,
    ) {
        if self.page_chain.is_empty() && self.inner_builder.is_empty() {
            self.page_anime_queue
                .push_back(AnimationType::PushMoveUp(key));
            self.load_page(key);
        }
        #[cfg(debug_assertions)]
        {
            if self.inner_builder.contains_key(key) {
                panic!("Page {key} has already registered");
            }
        }
        self.inner_builder.insert(key, page_widget);
    }

    /// 以 Builder 模式增加一个页面,需要一个页面 ID 和一个生成该页面组件的回调函数
    pub fn with_page(
        mut self,
        key: &'static str,
        page_widget: Box<dyn Fn() -> Box<dyn Widget<T> + 'static>>,
    ) -> Self {
        self.add_page(key, page_widget);
        self
    }
}

impl<T: Data> Widget<T> for PageSwitcher<T> {
    fn event(
        &mut self,
        ctx: &mut druid::EventCtx,
        event: &druid::Event,
        data: &mut T,
        env: &druid::Env,
    ) {
        if let Event::AnimFrame(i) = event {
            let anime_queue_is_empty = self.page_anime_queue.is_empty();
            if !anime_queue_is_empty {
                if self.skip_first_anime_frame == 0 {
                    if self.page_anime_timer == 0 {
                        self.page_anime_timer += i;
                        let preload_page = match self.page_anime_queue.front() {
                            Some(AnimationType::PushZoom(page))
                            | Some(AnimationType::PushMoveUp(page)) => *page,
                            _ => "",
                        };
                        if !preload_page.is_empty() {
                            self.load_page(preload_page);
                            ctx.children_changed();
                        }
                    } else {
                        self.page_anime_timer += i;
                        if self.page_anime_timer > ANIMATION_TIME {
                            self.page_anime_timer = 0;
                            match self.page_anime_queue.pop_front() {
                                Some(AnimationType::PushZoom(page))
                                | Some(AnimationType::PushSlide(page))
                                | Some(AnimationType::PushMoveUp(page)) => {
                                    self.load_page(page);
                                    ctx.children_changed();
                                    ctx.request_update();
                                    self.active_page = page;
                                    ctx.submit_notification_without_warning(
                                        ON_PAGE.with(page).to(Target::Global),
                                    );
                                    self.page_chain.push(page);
                                }
                                Some(AnimationType::PopZoom(page))
                                | Some(AnimationType::PopSlide(page)) => {
                                    if !page.is_empty() && self.page_chain.contains(&page) {
                                        ctx.submit_notification_without_warning(
                                            POP_PAGE.with(self.active_page),
                                        );
                                        self.active_page = page;
                                        ctx.submit_notification_without_warning(
                                            ON_PAGE.with(page).to(Target::Global),
                                        );
                                        while self.page_chain.last().unwrap() != &page {
                                            self.page_chain.pop();
                                        }
                                        self.clean_unused_page();
                                        ctx.children_changed();
                                        ctx.request_update();
                                    }
                                }
                                _ => {}
                            }
                        }
                    }
                } else {
                    self.skip_first_anime_frame -= 1;
                }
                ctx.request_paint();
                ctx.request_anim_frame();
            }
        } else if let Event::Command(cmd) = event {
            if let Some(&page) = cmd.get(PUSH_PAGE) {
                self.load_page(page);
                ctx.children_changed();
                if self.inner.contains_key(page) {
                    if self.slide_page_animation {
                        self.page_anime_queue
                            .push_back(AnimationType::PushSlide(page));
                    } else {
                        self.page_anime_queue
                            .push_back(AnimationType::PushZoom(page));
                    }
                    ctx.request_update();
                    ctx.request_layout();
                    #[cfg(target_os = "macos")]
                    {
                        self.skip_first_anime_frame = 12;
                    }
                    #[cfg(not(target_os = "macos"))]
                    {
                        self.skip_first_anime_frame = 1;
                    }
                    ctx.request_anim_frame();
                } else {
                    panic!("Can't find inner page called {page}");
                }
            } else if let Some(to_page) = cmd.get(QUERY_POP_PAGE) {
                if self.page_anime_queue.is_empty() {
                    if to_page == self.page_chain.last().unwrap() {
                    } else if !to_page.is_empty() {
                        if self.page_chain.len() > 1 {
                            if self.slide_page_animation {
                                self.page_anime_queue
                                    .push_back(AnimationType::PopSlide(to_page));
                            } else {
                                self.page_anime_queue
                                    .push_back(AnimationType::PopZoom(to_page));
                            }
                            self.skip_first_anime_frame = 1;
                            ctx.request_anim_frame();
                        } else {
                            tracing::trace!("WARNING: Back page invoked when the page is only one!")
                        }
                    } else if self.page_chain.len() > 1 {
                        let to_page = self.page_chain[self.page_chain.len() - 2];
                        if self.slide_page_animation {
                            self.page_anime_queue
                                .push_back(AnimationType::PopSlide(to_page));
                        } else {
                            self.page_anime_queue
                                .push_back(AnimationType::PopZoom(to_page));
                        }
                        self.skip_first_anime_frame = 1;
                        ctx.request_anim_frame();
                    } else {
                        tracing::trace!("WARNING: Back page invoked when the page is only one!")
                    }
                }
            } else if let Some(&t) = cmd.get(SET_SLIDE_PAGE_ANIMATION) {
                if self.slide_page_animation != t {
                    self.slide_page_animation = t;
                    ctx.request_paint();
                }
            }
        }
        if let Some(last_page) = self.page_chain.last() {
            if last_page == &self.active_page {
                if let Some(inner) = self.inner.get_mut(self.active_page) {
                    if inner.is_initialized() && self.page_anime_queue.is_empty() {
                        inner.event(ctx, event, data, env);
                    }
                }
            }
        }
    }

    fn lifecycle(
        &mut self,
        ctx: &mut druid::LifeCycleCtx,
        event: &druid::LifeCycle,
        data: &T,
        env: &druid::Env,
    ) {
        if let LifeCycle::WidgetAdded = event {
            ctx.request_anim_frame();
        }
        for (_, inner) in self.inner.iter_mut() {
            inner.lifecycle(ctx, event, data, env);
        }
    }

    fn update(&mut self, ctx: &mut druid::UpdateCtx, _old_data: &T, data: &T, env: &druid::Env) {
        for (_, inner) in self.inner.iter_mut() {
            inner.update(ctx, data, env);
        }
    }

    fn layout(
        &mut self,
        ctx: &mut druid::LayoutCtx,
        bc: &druid::BoxConstraints,
        data: &T,
        env: &druid::Env,
    ) -> druid::Size {
        bc.debug_check("PageSwitcher");
        for (_, inner) in self.inner.iter_mut() {
            inner.layout(ctx, bc, data, env);
            inner.set_origin(ctx, druid::Point::ZERO);
        }
        bc.max()
    }

    fn paint(&mut self, ctx: &mut druid::PaintCtx, data: &T, env: &druid::Env) {
        #[cfg(not(feature = "druid-ext"))]
        let page_mask = env.get(druid::theme::WINDOW_BACKGROUND_COLOR);
        let anime_timer = self.page_anime_timer.min(ANIMATION_TIME);
        let size = ctx.size();
        let transparent_rect = ctx.region().bounding_box();
        let transparent_size = transparent_rect.size();
        const SCALE_LEVEL: f64 = 0.15;
        match self.page_anime_queue.front() {
            Some(AnimationType::PushZoom(page)) => {
                let x = (anime_timer as f64) / ANIMATION_TIME as f64;
                let s = scl_gui_animation::tween::ease_out_expo(x);
                if s < 0.5 {
                    let s = s * 2.;
                    // 要退出的页面
                    if let Some(inner) = self.inner.get_mut(self.active_page) {
                        ctx.transform(
                            Affine::scale(1. + s * SCALE_LEVEL)
                                * Affine::translate((
                                    size.width * s * -SCALE_LEVEL / 2.,
                                    size.height * s * -SCALE_LEVEL / 2.,
                                )),
                        );
                        #[cfg(feature = "druid-ext")]
                        ctx.set_global_alpha(1. - s);
                        ctx.save().unwrap();
                        inner.paint(ctx, data, env);
                        #[cfg(not(feature = "druid-ext"))]
                        {
                            ctx.fill(
                                transparent_rect.to_owned(),
                                &PaintBrush::Color(page_mask.with_alpha(s)),
                            );
                        }
                        ctx.restore().unwrap();
                    }
                } else {
                    let s = (s - 0.5) * 2.;
                    // 要进入的页面
                    if let Some(inner) = self.inner.get_mut(page) {
                        ctx.transform(
                            Affine::scale((1. - SCALE_LEVEL) + s * SCALE_LEVEL)
                                * Affine::translate((
                                    size.width * (1. - s) * SCALE_LEVEL / 2.,
                                    size.height * (1. - s) * SCALE_LEVEL / 2.,
                                )),
                        );
                        #[cfg(feature = "druid-ext")]
                        ctx.set_global_alpha(s);
                        ctx.save().unwrap();
                        inner.paint(ctx, data, env);
                        #[cfg(not(feature = "druid-ext"))]
                        ctx.fill(
                            transparent_rect.to_owned(),
                            &PaintBrush::Color(page_mask.with_alpha(1. - s)),
                        );
                        ctx.restore().unwrap();
                    }
                }
            }
            Some(AnimationType::PopZoom(page)) => {
                let x = (anime_timer as f64) / ANIMATION_TIME as f64;
                let s = scl_gui_animation::tween::ease_out_expo(x);
                if s < 0.5 {
                    let s = s * 2.;
                    // 要退出的页面
                    if let Some(inner) = self.inner.get_mut(self.active_page) {
                        ctx.transform(
                            Affine::scale(1. - s * SCALE_LEVEL)
                                * Affine::translate((
                                    size.width * s * SCALE_LEVEL / 2.,
                                    size.height * s * SCALE_LEVEL / 2.,
                                )),
                        );
                        #[cfg(feature = "druid-ext")]
                        ctx.set_global_alpha(1. - s);
                        ctx.save().unwrap();
                        inner.paint(ctx, data, env);
                        #[cfg(not(feature = "druid-ext"))]
                        ctx.fill(
                            transparent_rect.to_owned(),
                            &PaintBrush::Color(page_mask.with_alpha(s)),
                        );
                        ctx.restore().unwrap();
                    }
                } else {
                    let s = (s - 0.5) * 2.;
                    // 要进入的页面
                    if let Some(inner) = self.inner.get_mut(page) {
                        ctx.transform(
                            Affine::scale(1. + (1. - s) * SCALE_LEVEL)
                                * Affine::translate((
                                    transparent_size.width * (1. - s) * -SCALE_LEVEL / 2.,
                                    transparent_size.height * (1. - s) * -SCALE_LEVEL / 2.,
                                )),
                        );
                        #[cfg(feature = "druid-ext")]
                        ctx.set_global_alpha(s);
                        ctx.save().unwrap();
                        inner.paint(ctx, data, env);
                        #[cfg(not(feature = "druid-ext"))]
                        ctx.fill(
                            transparent_rect.to_owned(),
                            &PaintBrush::Color(page_mask.with_alpha(1. - s)),
                        );
                        ctx.restore().unwrap();
                    }
                }
            }
            Some(AnimationType::PushSlide(page)) => {
                let x = (anime_timer as f64) / ANIMATION_TIME as f64;
                let s = scl_gui_animation::tween::ease_out_expo(x);
                if s < 0.5 {
                    let s = s * -2.;
                    // 要退出的页面
                    if let Some(inner) = self.inner.get_mut(self.active_page) {
                        ctx.transform(Affine::translate((size.width * s, 0.)));
                        // ctx.apply_global_transparency(1. - s);
                        inner.paint(ctx, data, env);
                        // ctx.fill(
                        //     transparent_rect.to_owned(),
                        //     &PaintBrush::Color(page_mask.with_alpha(s)),
                        // );
                    }
                } else {
                    let s = (s - 0.5) * 2.;
                    // 要进入的页面
                    if let Some(inner) = self.inner.get_mut(page) {
                        ctx.transform(Affine::translate((size.width * (1. - s), 0.)));
                        // ctx.apply_global_transparency(s);
                        inner.paint(ctx, data, env);
                        // ctx.fill(
                        //     transparent_rect.to_owned(),
                        //     &PaintBrush::Color(page_mask.with_alpha(1. - s)),
                        // );
                    }
                }
            }
            Some(AnimationType::PopSlide(page)) => {
                let x = (anime_timer as f64) / ANIMATION_TIME as f64;
                let s = scl_gui_animation::tween::ease_out_expo(x);
                if s < 0.5 {
                    let s = s * 2.;
                    // 要退出的页面
                    if let Some(inner) = self.inner.get_mut(self.active_page) {
                        ctx.transform(Affine::translate((size.width * s, 0.)));
                        inner.paint(ctx, data, env);
                    }
                } else {
                    let s = (s - 0.5) * 2.;
                    // 要进入的页面
                    if let Some(inner) = self.inner.get_mut(page) {
                        ctx.transform(Affine::translate((size.width * (s - 1.), 0.)));
                        inner.paint(ctx, data, env);
                    }
                }
            }
            Some(AnimationType::PushMoveUp(page)) => {
                if let Some(inner) = self.inner.get_mut(page) {
                    let x = ((anime_timer) as f64) / ANIMATION_TIME as f64;
                    let s = 1. - scl_gui_animation::tween::ease_out_expo(x);
                    ctx.transform(Affine::translate((0., s * size.height.min(100.))));
                    #[cfg(feature = "druid-ext")]
                    ctx.set_global_alpha(1. - s);
                    ctx.save().unwrap();
                    inner.paint(ctx, data, env);
                    #[cfg(not(feature = "druid-ext"))]
                    ctx.fill(
                        transparent_rect.to_owned(),
                        &PaintBrush::Color(page_mask.with_alpha(s)),
                    );
                    ctx.restore().unwrap();
                }
            }
            None => {
                if let Some(inner) = self.inner.get_mut(self.active_page) {
                    inner.paint(ctx, data, env);
                }
            }
        }
    }
}