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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

use cgmath::{Deg, Quaternion, Rotation3, Vector3};
use url::Url;

use crate::APP_VERSION;
use crate::asset::{AssetFileBox, AssetManagerRc};
use crate::audio::{AudioEngineRc, AudioFader, AudioFaderHandle, AudioFile, AudioFileHandle};
use crate::mailbox::{self, Receiver, TryRecvError};
use crate::model::*;
use crate::net::{AssetFileRequest, BeatSaverSearchRequest, ImageRequest, NetManager, SongZipRequest};
use crate::output::OutputInfoRc;
use crate::scene::{GameParam, Scene, SceneFactory, SceneInput, SceneManager, create_floor, create_saber, create_stats_window};
use crate::songdef::{CHAR_STANDARD, SongDifficulty};
use crate::songinfo::{SongInfo, ColorScheme};
use crate::ui::{AboutWindow, PoweredByWindow, SearchWindow, SearchWindowItem, SearchWindowMode, UILoop, VirtualKeyboardWindow};
use crate::ui::slintimpl::{self, ComponentHandle as slintimpl_ComponentHandle, Model as slintimpl_Model, WindowUtil as slintimpl_WindowUtil};
use crate::util::StatsRc;

const POINTER_COLOR: Color = Color([0.4, 0.4, 0.4]);
const FADE_RATE: u8 = 80; // [dB/s]

pub struct MenuParam;

impl MenuParam {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self {
        }
    }
}

impl SceneFactory for MenuParam {
    type Scene = Menu;
    type Error = ();

    fn load(self, asset_mgr: AssetManagerRc, model_reg: &mut ModelRegistry, output_info: OutputInfoRc, stats: StatsRc, audio_engine: AudioEngineRc, ui_loop: &UILoop, net_manager: &NetManager) -> Result<Self::Scene, Self::Error> {
        Menu::new(self, asset_mgr, model_reg, output_info, stats, audio_engine, ui_loop, net_manager)
    }
}

pub struct Menu {
    asset_mgr: AssetManagerRc,
    audio_engine: AudioEngineRc,
    ui_loop: UILoop,
    vkbd_window: Rc<Window>,
    vkbd_window_rx: Receiver<VirtualKeyboardMessage>,
    about_window: Rc<Window>,
    search_window_rx: Receiver<SearchMessage>,
    search_window_state_mutex: Arc<Mutex<SearchState>>,
    search_window: Rc<Window>,
    poweredby_window: Rc<Window>,
    saber_l: Rc<Saber>,
    saber_r: Rc<Saber>,
    pointer: Rc<Pointer>,
    inner: RefCell<Inner>,
}

struct Inner {
    audio_info_opt: Option<AudioInfo>,
    preview_info_opt: Option<PreviewInfo>,
}

enum SearchMessage {
    PreviewStart(AssetFileBox, usize),
    PreviewStop,
    GameStart(AssetManagerRc, SongInfo, usize),
    #[cfg(feature = "test")]
    TestStart,
}

struct SearchState {
    active_info_opt: Option<ActiveInfo>,
    preview_serial: usize,
}

struct ActiveInfo {
    item_index: usize,
    preview_active: bool,
}

struct AudioInfo {
    file_handle: AudioFileHandle,
    fader_handle: AudioFaderHandle,
}

struct PreviewInfo {
    file_handle: AudioFileHandle,
    serial: usize,
}

enum VirtualKeyboardMessage {
    Open,
    Close,
}

type VirtualKeyboardFuncMutex = Arc<Mutex<Option<Arc<dyn Fn(String) + Send + Sync>>>>;

enum UpdateItemOp {
    Clear,
    Active(usize),
    PreviewStop,
}

impl Menu {
    #[allow(clippy::too_many_arguments)]
    fn new(_param: MenuParam, asset_mgr: AssetManagerRc, model_reg: &mut ModelRegistry, output_info: OutputInfoRc, stats: StatsRc, audio_engine: AudioEngineRc, ui_loop: &UILoop, net_manager: &NetManager) -> Result<Self, ()> {
        // Implementation notes:
        // - Weak window references in event handlers (on_*):
        //   - If a weak reference to its parent window is unwrapped (window_weak.unwrap()),
        //     then it is safe.
        //   - Otherwise, if it is a weak reference to a different window, then testing
        //     is needed (window_weak.upgrade()).
        // - For net_manager_exec.submit():
        //   - The done_func closure is executed on the UI thread, if the returned handle
        //     is still alive.
        //   - Don't store strong references (owned, Rc, Arc) of handles in the done_func
        //     closure, as this would make cancelling of requests impossible.
        // TODO: Rework: would be nice if we can replace/simplify Arc/Mutex with Rc/RefCell in the
        // window + net_manager logic.

        // Setup virtual keyboard window.
        // TODO: move into UISubr, since it is common?

        let (vkbd_window_tx, vkbd_window_rx) = mailbox::mailbox();
        let vkbd_window_func_opt_mutex: VirtualKeyboardFuncMutex = Arc::new(Mutex::new(None));

        let window_param = WindowParam::new(700, 350, {
            let vkbd_window_tx = vkbd_window_tx.clone();
            let vkbd_window_func_opt_mutex = Arc::clone(&vkbd_window_func_opt_mutex);
            
            || {
                let window = VirtualKeyboardWindow::new().unwrap();

                window.on_pressed({
                    let window_weak = window.as_weak();
                    
                    move |value| {
                        // Send key events.

                        let window = window_weak.unwrap();
                        window.handle_key(value);
                    }
                });

                window.on_edited({
                    let vkbd_window_func_opt_mutex = Arc::clone(&vkbd_window_func_opt_mutex);

                    move |value| {
                        // Value has been changed.

                        let func_opt = vkbd_window_func_opt_mutex.lock().unwrap().as_ref().map(Arc::clone);

                        // Invoke callback (the lock has been already released).

                        if let Some(func) = func_opt {
                            func(value.into());
                        }
                    }
                });

                window.on_enter(move || {
                    // Remove callback: the remaining keystrokes (if any) will be discarded.

                    let mut vkbd_window_func_opt = vkbd_window_func_opt_mutex.lock().unwrap();
                    *vkbd_window_func_opt = None;

                    // Close keyboard.

                    vkbd_window_tx.send(VirtualKeyboardMessage::Close).unwrap();
                });

                window
            }
        });

        let vkbd_window = model_reg.create(window_param);
        vkbd_window.set_scale(2.5, 0.8);
        vkbd_window.set_pos(&Vector3::new(0.0, 3.0, 0.75));
        vkbd_window.set_rot(&Quaternion::from_angle_x(Deg(-45.0 / 2.0)));

        // Setup about by window. On some platforms, the diagnostic information is
        // a multiline string, so put it into one single line.

        let diags: Vec<_> = output_info.get_diags().iter().map(|diag| diag.replace("\n", ", ").into()).collect();

        let window_param = WindowParam::new(500, 500, move || {
            let window = AboutWindow::new().unwrap();
            window.set_version(APP_VERSION.clone().into());
    
            let diags_model = slintimpl::VecModel::default();
            diags_model.set_vec(diags);

            window.set_diags(slintimpl::ModelRc::new(diags_model));

            window
        });

        let about_window = model_reg.create(window_param);
        about_window.set_visible(true);
        about_window.set_scale(2.0, 2.0);
        about_window.set_pos(&Vector3::new(-5.0, 3.5, 2.0));
        about_window.set_rot(&Quaternion::from_angle_z(Deg(45.0)));

        // Setup search window.

        let (search_window_tx, search_window_rx) = mailbox::mailbox();
        let search_window_state_mutex = Arc::new(Mutex::new(SearchState {
            active_info_opt: None,
            preview_serial: 0,
        }));

        let window_param = WindowParam::new(1200, 750, {
            let net_manager_exec = net_manager.create_executor(ui_loop.clone());
            let vkbd_window_weak = vkbd_window.as_weak::<VirtualKeyboardWindow>();
            let search_window_state_mutex = Arc::clone(&search_window_state_mutex);

            move || {
                let window = SearchWindow::new().unwrap();
                window.set_items(slintimpl::ModelRc::new(slintimpl::VecModel::default()));

                let handles_mutex = Arc::new(Mutex::new(Vec::new()));

                // Construct search method:
                // - Before calling search(), caller should ensure that the SearchWindow is still alive.
                // - The search method (owned by SearchWindow and VirtualKeyboardWindow (via vkbd_window_func_opt_mutex))
                //   has weak reference to handles_mutex. The handles_mutex is having the same lifecycle as the SearchWindow.
                //   If the SearchWindow is dropped, then all in-progress fetches will be terminated as well.

                let search = Arc::new({
                    let net_manager_exec = net_manager_exec.clone();
                    let search_window_state_mutex = Arc::clone(&search_window_state_mutex);
                    let window_weak = window.as_weak();
                    let handles_mutex_weak = Arc::downgrade(&handles_mutex);

                    move || {
                        let window = window_weak.unwrap();

                        let query = window.get_query();
                        let order = window.get_order();
                        let ascending = window.get_ascending();

                        window.set_mode(SearchWindowMode::Message);
                        window.set_show_detail(false);
                        window.set_message("Searching...".into());

                        // Clear active item. Since UI- and main-thread are running parallel, preview_serial is used
                        // to match the actual audio file.

                        {
                            let mut search_window_state = search_window_state_mutex.lock().unwrap();

                            search_window_state.preview_serial += 1;
                            Self::update_item(&window, &mut search_window_state, UpdateItemOp::Clear);
                        }

                        // Terminate in-progress fetches.

                        let handles_mutex = handles_mutex_weak.upgrade().unwrap();
                        let mut handles = handles_mutex.lock().unwrap();
                        handles.clear();

                        // Submit search.

                        let handle = net_manager_exec.submit(BeatSaverSearchRequest::new(query, order, ascending), {
                            let net_manager_exec = net_manager_exec.clone();
                            let window_weak = window_weak.clone();
                            let handles_mutex_weak = handles_mutex_weak.clone();

                            move |r| {
                                let window = window_weak.unwrap();
                                let mut items = Vec::new();

                                match r {
                                    Ok(r) => {
                                        let empty_img = slintimpl::Image::from_rgba8(slintimpl::SharedPixelBuffer::new(1, 1)); // slint does not allow (0, 0) for image dimension.
                                        let songs = r.get_songs();

                                        if !songs.is_empty() {
                                            window.set_mode(SearchWindowMode::Item);
                                            
                                            let handles_mutex = handles_mutex_weak.upgrade().unwrap();
                                            let mut handles = handles_mutex.lock().unwrap();

                                            for (item_index, (song, version)) in songs.iter().filter_map(|song| song.get_published_version().map(|version| (song, version))).enumerate() {
                                                // Map difficulties.

                                                let mut difficulties: Box<_> = version.get_variants().iter().filter_map(|variant| {
                                                    if variant.get_characteristic() == CHAR_STANDARD {
                                                        Some(variant.get_difficulty())
                                                    } else {
                                                        None
                                                    }
                                                }).collect();
                                                difficulties.sort();

                                                let difficulty_ints: Vec<_> = difficulties.iter().map(|difficulty| (*difficulty).into()).collect();
                                                let difficulty_ints_model = slintimpl::VecModel::default();
                                                difficulty_ints_model.set_vec(difficulty_ints);

                                                let difficulty_strs: Vec<_> = difficulties.iter().map(|difficulty| {
                                                    match difficulty {
                                                        SongDifficulty::Easy => "Easy",
                                                        SongDifficulty::Normal => "Normal",
                                                        SongDifficulty::Hard => "Hard",
                                                        SongDifficulty::Expert => "Expert",
                                                        SongDifficulty::ExpertPlus => "Expert Plus",
                                                    }.into()
                                                }).collect();
                                                let difficulty_strs_model = slintimpl::VecModel::default();
                                                difficulty_strs_model.set_vec(difficulty_strs);

                                                // Create item.

                                                let metadata = song.get_metadata();
                                                let duration = metadata.get_duration();

                                                let item = SearchWindowItem {
                                                    name: song.get_name().into(),
                                                    uploader_name: song.get_uploader().get_name().into(),
                                                    cover_img: empty_img.clone(),
                                                    duration: format!("{}:{:02}", duration / 60, duration % 60).into(),
                                                    bpm: format!("{:.0}", metadata.get_bpm()).into(),
                                                    score: format!("{:.2}", song.get_stats().get_score() * 100.0).into(),
                                                    preview_url: version.get_preview_url().as_ref().into(),
                                                    download_url: version.get_download_url().as_ref().into(),
                                                    difficulty_ints: slintimpl::ModelRc::new(difficulty_ints_model),
                                                    difficulty_strs: slintimpl::ModelRc::new(difficulty_strs_model),
                                                    active: false,
                                                    preview_active: false,
                                                };

                                                items.push(item);

                                                // Submit cover image fetch.
                                                
                                                let handle = net_manager_exec.submit(ImageRequest::new(version.get_cover_url().clone()), { // TODO: cache?
                                                    let window_weak = window_weak.clone();

                                                    move |r| {
                                                        if let Ok(img_raw) = r {
                                                            let width = img_raw.get_width();
                                                            let height = img_raw.get_height();

                                                            if width > 0 && height > 0 {
                                                                let mut buf = slintimpl::SharedPixelBuffer::<slintimpl::Rgba8Pixel>::new(width, height);
                                                                let buf_raw = buf.make_mut_bytes();
                                                                buf_raw.copy_from_slice(img_raw.get_data());
                                                                let img = slintimpl::Image::from_rgba8(buf);

                                                                // Update model.

                                                                let window = window_weak.unwrap();
                                                                let model = window.get_items();

                                                                let mut item = model.row_data(item_index).expect("Item expected");
                                                                item.cover_img = img;
                                                                model.set_row_data(item_index, item);
                                                            }
                                                        }
                                                    }
                                                });

                                                handles.push(handle);
                                            }
                                        } else {
                                            window.set_mode(SearchWindowMode::Message);
                                            window.set_message("No results".into());
                                        }
                                    },
                                    Err(e) => {
                                        window.set_mode(SearchWindowMode::Message);
                                        window.set_message(format!("Network error: {}", e).into());
                                    },
                                }

                                // Update model.

                                let model = window.get_items();
                                let model = model.as_any().downcast_ref::<slintimpl::VecModel<SearchWindowItem>>().expect("Model expected");
                                model.set_vec(items);
                            }
                        });

                        handles.push(handle);
                    }
                });

                // TODO: move into UISubr, since it is common?
                let set_input_enabled = Arc::new({
                    // For events which result in scene switch:
                    // - Disable further input on the UI.
                    // - Prevent UI from sending any message to the main thread
                    //   (e.g. via mailbox), since the receiving side (current scene) is
                    //   going to be dropped.

                    let vkbd_window_weak = vkbd_window_weak.clone();
                    let window_weak = window.as_weak();

                    move |enabled| {
                        let vkbd_window_opt = vkbd_window_weak.upgrade();
                        if vkbd_window_opt.is_none() {
                            return;
                        }
                        let vkbd_window = vkbd_window_opt.unwrap();

                        vkbd_window.set_input_enabled(enabled);

                        let window = window_weak.unwrap();
                        window.set_input_enabled(enabled);
                    }
                });

                window.on_change_query({
                    let window_weak = window.as_weak();
                    let search = Arc::clone(&search);
                    
                    move || {
                        let _ = Arc::strong_count(&handles_mutex); // As long as the SearchWindow is alive, handles_mutex is alive as well.

                        // Open keyboard.

                        let vkbd_window_opt = vkbd_window_weak.upgrade();
                        if vkbd_window_opt.is_none() {
                            return;
                        }
                        let vkbd_window = vkbd_window_opt.unwrap();

                        let window = window_weak.unwrap();
                        let query = window.get_query();

                        vkbd_window.set_shift(false);
                        vkbd_window.set_value(query);
                        vkbd_window.handle_key_end(); // Move cursor to the end of the string.

                        let mut vkbd_window_func_opt = vkbd_window_func_opt_mutex.lock().unwrap();
                        *vkbd_window_func_opt = Some(Arc::new({
                            let window_weak = window_weak.clone();
                            let search = Arc::clone(&search);

                            move |query| {
                                let window_opt = window_weak.upgrade();
                                if window_opt.is_none() {
                                    return;
                                }
                                let window = window_opt.unwrap();

                                window.set_query(query.into());
                                search();
                            }
                        }));

                        vkbd_window_tx.send(VirtualKeyboardMessage::Open).unwrap();
                    }
                });

                window.on_change_other({
                    let search = Arc::clone(&search);

                    move || {
                        search();
                    }
                });

                window.on_refresh({
                    let search = Arc::clone(&search);
                    
                    move || {
                        search();
                    }
                });

                window.on_select({
                    let search_window_tx = search_window_tx.clone();
                    let net_manager_exec = net_manager_exec.clone();
                    let search_window_state_mutex = Arc::clone(&search_window_state_mutex);
                    let window_weak = window.as_weak();
                    let mut handle_opt = None;

                    move |item_index_selected| {
                        let window = window_weak.unwrap();
                        let item_index_selected: usize = item_index_selected.try_into().unwrap();
                        let mut search_window_state = search_window_state_mutex.lock().unwrap();
                        let handle_opt_ref = &mut handle_opt; // Suppress "value captured by ... is never read" warnings.

                        // Stop audio.

                        search_window_state.preview_serial += 1;
                        let preview_serial = search_window_state.preview_serial;

                        search_window_tx.send(SearchMessage::PreviewStop).unwrap();

                        // Terminate fetch.

                        *handle_opt_ref = None;

                        // Set active item.
                        // TODO: use window->detail_item instead of active_info.item_index?

                        let model = window.get_items();
                        let mut difficulty_int_active_opt = None;

                        if let Some(active_info) = &search_window_state.active_info_opt {
                            let difficulty_index = window.get_difficulty_index();
                            let item = model.row_data(active_info.item_index).expect("Item expected");
                            let difficulty_ints: Box<_> = item.difficulty_ints.iter().collect();

                            // If the active item doesn't have any difficulty (difficulty_ints), then
                            // difficulty_index is still set. Check if we have at least one difficulty.

                            if !difficulty_ints.is_empty() {
                                difficulty_int_active_opt = Some(difficulty_ints[difficulty_index as usize]);
                            }

                            if item_index_selected == active_info.item_index {
                                if active_info.preview_active {
                                    Self::update_item(&window, &mut search_window_state, UpdateItemOp::PreviewStop);
                                    return;
                                }
                            } else {
                                Self::update_item(&window, &mut search_window_state, UpdateItemOp::Clear);
                            }
                        }

                        Self::update_item(&window, &mut search_window_state, UpdateItemOp::Active(item_index_selected));

                        // Set detail.

                        let item = model.row_data(item_index_selected).expect("Item expected");
                        let preview_url: String = item.preview_url.clone().into();
                        let difficulty_ints: Box<_> = item.difficulty_ints.iter().collect();

                        window.set_show_detail(true);
                        window.set_detail_item(item);
                        window.set_detail_message("".into());

                        let difficulty_index = difficulty_int_active_opt.map_or(0, |difficulty_int_active| {
                            difficulty_ints.iter().position(|difficulty_int| *difficulty_int == difficulty_int_active).unwrap_or(0)
                        });

                        window.set_difficulty_index(difficulty_index.try_into().unwrap());

                        // Submit audio preview fetch.

                        let url = Url::parse(&preview_url).expect("Invalid url");
                        
                        let handle = net_manager_exec.submit(AssetFileRequest::new(url), { // TODO: cache?
                            let search_window_tx = search_window_tx.clone();
                            let search_window_state_mutex = Arc::clone(&search_window_state_mutex);
                            let window_weak = window_weak.clone();

                            move |r| {
                                if let Ok(asset_file) = r {
                                    search_window_tx.send(SearchMessage::PreviewStart(asset_file, preview_serial)).unwrap();
                                } else {
                                    let mut search_window_state = search_window_state_mutex.lock().unwrap();

                                    // Remove play icon.

                                    let window = window_weak.unwrap();
                                    Self::update_item(&window, &mut search_window_state, UpdateItemOp::PreviewStop);
                                }
                            }
                        });
                        
                        *handle_opt_ref = Some(handle);
                    }
                });

                window.on_play({
                    let search_window_tx = search_window_tx.clone();
                    let net_manager_exec = net_manager_exec.clone();
                    let search_window_state_mutex = Arc::clone(&search_window_state_mutex);
                    let window_weak = window.as_weak();
                    let set_input_enabled = Arc::clone(&set_input_enabled);
                    let mut handle_opt = None;

                    move || { // TODO: use window->detail_item instead of active_info.item_index?
                        let window = window_weak.unwrap();
                        let item_index = {
                            let search_window_state = search_window_state_mutex.lock().unwrap();
                            let active_info = search_window_state.active_info_opt.as_ref().expect("Active expected");
                            active_info.item_index
                        };

                        let model = window.get_items();
                        let item = model.row_data(item_index).expect("Item expected");

                        let difficulty_index = window.get_difficulty_index();
                        let difficulty_ints: Box<_> = item.difficulty_ints.iter().collect();
                        let difficulty_int = difficulty_ints[difficulty_index as usize];
                        let difficulty: SongDifficulty = difficulty_int.try_into().unwrap();

                        window.set_mode(SearchWindowMode::Message);
                        window.set_message("Downloading...".into());

                        set_input_enabled(false);

                        // Submit song zip fetch.

                        let download_url: String = item.download_url.clone().into();
                        let url = Url::parse(&download_url).expect("Invalid url");

                        let handle = net_manager_exec.submit(SongZipRequest::new(url), { // TODO: cache?
                            let search_window_tx = search_window_tx.clone();
                            let window_weak = window_weak.clone();
                            let set_input_enabled = Arc::clone(&set_input_enabled);

                            move |r| {
                                let mut e_opt = None;

                                match r {
                                    Ok(asset_mgr) => {
                                        // TODO: On which thread should we do the processing of the song data?
                                        match SongInfo::load(Arc::clone(&asset_mgr)) {
                                            Ok(song_info) => {
                                                let beatmap_infos = song_info.get_beatmap_infos();
                                                
                                                if let Some(beatmap_info_index) = beatmap_infos.iter().position(|beatmap_info| beatmap_info.get_characteristic() == CHAR_STANDARD && beatmap_info.get_difficulty() == difficulty) {
                                                    search_window_tx.send(SearchMessage::GameStart(asset_mgr, song_info, beatmap_info_index)).unwrap();
                                                } else {
                                                    e_opt = Some("No such characteristic/difficulty".to_string());
                                                }
                                            },
                                            Err(e) => {
                                                e_opt = Some(format!("Unable to load song: {:?}", e)); // TODO: instead of debug, use display trait for formatting error msg?
                                            },
                                        }
                                    },
                                    Err(e) => {
                                        e_opt = Some(format!("Network error: {:?}", e)); // TODO: instead of debug, use display trait for formatting error msg?
                                    },
                                }

                                if let Some(e) = e_opt {
                                    let window = window_weak.unwrap();
                                    window.set_mode(SearchWindowMode::Item);
                                    window.set_detail_message(e.into());

                                    set_input_enabled(true);
                                }
                            }
                        });

                        let handle_opt_ref = &mut handle_opt; // Suppress "value captured by ... is never read" warnings.
                        *handle_opt_ref = Some(handle);
                    }
                });

                // Setup test, if configured.
                // TODO: Add support for https://github.com/BeatLeader/BS-Open-Replay ?

                #[cfg(feature = "test")]
                {
                    window.set_test_visible(true);

                    window.on_test(move || {
                        set_input_enabled(false);

                        search_window_tx.send(SearchMessage::TestStart).unwrap();
                    });
                }

                // Execute initial query.

                search();

                window
            }
        });

        let search_window = model_reg.create(window_param);
        search_window.set_visible(true);
        search_window.set_scale(4.8, 3.0);
        search_window.set_pos(&Vector3::new(0.0, 5.0, 2.0));

        // Setup powered by window.

        let window_param = WindowParam::new(500, 500, || {
            PoweredByWindow::new().unwrap()
        });

        let poweredby_window = model_reg.create(window_param);
        poweredby_window.set_visible(true);
        poweredby_window.set_scale(2.0, 2.0);
        poweredby_window.set_pos(&Vector3::new(5.0, 3.5, 2.0));
        poweredby_window.set_rot(&Quaternion::from_angle_z(Deg(-45.0)));

        // Setup floor.

        create_floor(model_reg);
        create_stats_window(model_reg, stats, ui_loop);

        // Setup sabers.

        let color_scheme = ColorScheme::default();
        let color_l = color_scheme.get_color_l();
        let color_r = color_scheme.get_color_r();

        let (saber_l, saber_r) = create_saber(model_reg, color_l, color_r);

        // Setup pointer.

        let pointer_param = PointerParam::new(&POINTER_COLOR);
        let pointer = model_reg.create(pointer_param);
        
        let inner = Inner {
            audio_info_opt: None,
            preview_info_opt: None,
        };

        Ok(Self {
            asset_mgr,
            audio_engine,
            ui_loop: ui_loop.clone(),
            vkbd_window,
            vkbd_window_rx,
            about_window,
            search_window_rx,
            search_window_state_mutex,
            search_window,
            poweredby_window,
            saber_l,
            saber_r,
            pointer,
            inner: RefCell::new(inner),
        })
    }

    fn update_item(window: &SearchWindow, state: &mut SearchState, op: UpdateItemOp) {
        let model = window.get_items();

        match op {
            UpdateItemOp::Clear => {
                if let Some(active_info) = &state.active_info_opt {
                    let item_index = active_info.item_index;
                    let mut item = model.row_data(item_index).expect("Item expected");

                    item.active = false;
                    item.preview_active = false;

                    model.set_row_data(item_index, item);

                    state.active_info_opt = None;
                }
            },
            UpdateItemOp::Active(item_index) => {
                let mut item = model.row_data(item_index).expect("Item expected");

                item.active = true;
                item.preview_active = true;

                model.set_row_data(item_index, item);

                state.active_info_opt = Some(ActiveInfo {
                    item_index,
                    preview_active: true,
                });
            },
            UpdateItemOp::PreviewStop => {
                let active_info = state.active_info_opt.as_mut().expect("Active expected");
                let item_index = active_info.item_index;
                let mut item = model.row_data(item_index).expect("Item expected");

                item.preview_active = false;

                model.set_row_data(item_index, item);

                active_info.preview_active = false;
            },
        }
    }
}

impl Scene for Menu {
    fn update(&self, scene_mgr: &SceneManager, scene_input: &SceneInput) {
        let inner = &mut *self.inner.borrow_mut();

        // Start audio on first update, and when ended.
        // TODO: implement lifecycle methods?

        let mut restart = false;

        if let Some(audio_info) = &inner.audio_info_opt {
            if audio_info.file_handle.at_eof() {
                restart = true;
            }
        } else {
            restart = true;
        }

        if restart {
            let asset_file = self.asset_mgr.open_or_err("/audio/menu.mp3");

            let (file_input, file_handle) = AudioFile::new(asset_file);
            let (fader_input, fader_handle) = AudioFader::new(file_input);
            self.audio_engine.add(fader_input);

            // If preview is active, then silence menu song.

            if inner.preview_info_opt.is_some() {
                fader_handle.silence();
            }

            file_handle.play();

            let audio_info = AudioInfo {
                file_handle,
                fader_handle,
            };

            inner.audio_info_opt = Some(audio_info);
        }

        let audio_info = inner.audio_info_opt.as_ref().unwrap();
        let fader_handle = &audio_info.fader_handle;

        // Handle UI events.

        let windows = &[&self.vkbd_window, &self.about_window, &self.search_window, &self.poweredby_window];
        scene_mgr.get_ui_subr().update(&self.saber_l, &self.saber_r, &self.pointer, windows, scene_input);

        // Handle virtual keyboard.

        match self.vkbd_window_rx.try_recv() {
            Ok(msg) => {
                match msg {
                    VirtualKeyboardMessage::Open => {
                        // Show keyboard.

                        self.vkbd_window.set_visible(true);
                    },
                    VirtualKeyboardMessage::Close => {
                        // Hide keyboard.

                        self.vkbd_window.set_visible(false);
                    },
                }
            },
            Err(e) => {
                assert!(matches!(e, TryRecvError::Empty));
            },
        }

        // Poll for messages from search window.

        match self.search_window_rx.try_recv() {
            Ok(msg) => {
                match msg {
                    SearchMessage::PreviewStart(asset_file, serial) => {
                        fader_handle.fade_out(FADE_RATE);

                        // TODO: At the moment we can't start preview directly on the UI thread,
                        // as the AudioEngineRc is Rc and not Arc. 
                        // TODO: Use Content-Type from response to avoid format guess?

                        let (input, file_handle) = AudioFile::new(asset_file);
                        self.audio_engine.add(input);

                        file_handle.play();

                        let preview_info = PreviewInfo {
                            file_handle,
                            serial,
                        };

                        inner.preview_info_opt = Some(preview_info);
                    },
                    SearchMessage::PreviewStop => {
                        fader_handle.fade_in(FADE_RATE);

                        inner.preview_info_opt = None;
                    },
                    SearchMessage::GameStart(asset_mgr, song_info, beatmap_info_index) => {
                        if let Err(e) = scene_mgr.load(GameParam::new(asset_mgr, song_info, beatmap_info_index, #[cfg(feature = "test")] false)) {
                            self.ui_loop.add_callback({
                                let vkbd_window_weak = self.vkbd_window.as_weak::<VirtualKeyboardWindow>();
                                let search_window_weak = self.search_window.as_weak::<SearchWindow>();

                                move || {
                                    let vkbd_window_opt = vkbd_window_weak.upgrade();
                                    let search_window_opt = search_window_weak.upgrade();
                                    if vkbd_window_opt.is_none() || search_window_opt.is_none() {
                                        return;
                                    }
                                    let vkbd_window = vkbd_window_opt.unwrap();
                                    let search_window = search_window_opt.unwrap();

                                    search_window.set_mode(SearchWindowMode::Item);
                                    search_window.set_detail_message(e.into());

                                    // TODO: Refactor to use a single set_input_enabled implementation.
                                    vkbd_window.set_input_enabled(true);
                                    search_window.set_input_enabled(true);
                                }
                            });
                        }
                    },
                    #[cfg(feature = "test")]
                    SearchMessage::TestStart => {
                        let song_info = SongInfo::test(Arc::clone(&self.asset_mgr));
                        scene_mgr.load(GameParam::new(Arc::clone(&self.asset_mgr), song_info, 0, true)).expect("Unable to load scene");
                    },
                }
            },
            Err(e) => {
                assert!(matches!(e, TryRecvError::Empty));
            },
        }

        // Handle the end of audio preview.

        if let Some(preview_info) = &inner.preview_info_opt && preview_info.file_handle.at_eof() {
            fader_handle.fade_in(FADE_RATE);

            self.ui_loop.add_callback({
                let search_window_state_mutex = Arc::clone(&self.search_window_state_mutex);            
                let window_weak = self.search_window.as_weak::<SearchWindow>();
                let preview_serial = preview_info.serial;

                move || {
                    let window_opt = window_weak.upgrade();
                    if window_opt.is_none() {
                        return;
                    }
                    let window = window_opt.unwrap();

                    let mut search_window_state = search_window_state_mutex.lock().unwrap();
                    if search_window_state.preview_serial == preview_serial {
                        Self::update_item(&window, &mut search_window_state, UpdateItemOp::PreviewStop);
                    }
                }
            });

            inner.preview_info_opt = None;
        }
    }
}