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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
use std::{
    any::TypeId,
    borrow::Cow,
    collections::{HashMap, HashSet},
    fmt::Debug,
    hash::Hash,
    marker::PhantomData,
    ops::{Deref, DerefMut},
    sync::{
        atomic::{AtomicU32, Ordering},
        Arc, RwLock, Weak,
    },
};

use parking_lot::{lock_api::ArcMutexGuard, Mutex, RawMutex};
use stylecs::{Style, StyleComponent};
use unic_langid::LanguageIdentifier;

use crate::{
    styles::style_sheet::StyleSheet, AnyChannels, AnyFrontend, AnySendSync, AnyTransmogrifier,
    AnyTransmogrifierContext, AnyWidget, Channels, Context, Frontend, ManagedCodeGuard,
    TransmogrifierState, Widget, WidgetId,
};

type WidgetTypeId = TypeId;

#[derive(Clone, Debug)]
/// A graphical user interface.
pub struct Gooey<F: Frontend> {
    data: Arc<GooeyData<F>>,
}

#[derive(Debug)]
struct GooeyData<F: Frontend> {
    transmogrifiers: Arc<Transmogrifiers<F>>,
    root: WidgetRegistration,
    storage: WidgetStorage,
    processing_messages_lock: Mutex<()>,
    inside_event_loop: AtomicU32,
}

impl<F: Frontend> Gooey<F> {
    /// Creates a new instance
    #[must_use]
    pub fn new(
        transmogrifiers: Arc<Transmogrifiers<F>>,
        root: WidgetRegistration,
        storage: WidgetStorage,
    ) -> Self {
        Self {
            data: Arc::new(GooeyData {
                transmogrifiers,
                storage,
                root,
                processing_messages_lock: Mutex::default(),
                inside_event_loop: AtomicU32::default(),
            }),
        }
    }

    /// Returns the root widget.
    pub fn root_widget(&self) -> &WidgetRegistration {
        &self.data.root
    }

    /// Returns the transmogrifier fo the type id
    #[must_use]
    pub fn transmogrifier_for_type_id(
        &self,
        widget_type_id: TypeId,
    ) -> Option<&'_ <F as Frontend>::AnyTransmogrifier> {
        self.data.transmogrifiers.map.get(&widget_type_id)
    }

    /// Executes `callback` with the transmogrifier and transmogrifier state as
    /// parameters.
    #[allow(clippy::missing_panics_doc)]
    pub fn with_transmogrifier<
        R,
        C: FnOnce(&'_ <F as Frontend>::AnyTransmogrifier, AnyTransmogrifierContext<'_, F>) -> R,
    >(
        &self,
        widget_id: &WidgetId,
        frontend: &F,
        callback: C,
    ) -> Option<R> {
        self.data
            .transmogrifiers
            .map
            .get(&widget_id.type_id)
            .and_then(|transmogrifier| {
                let widget_state = self.widget_state(widget_id)?;
                widget_state.with_state(transmogrifier, frontend, |state, widget| {
                    let style = widget_state.style.lock();
                    let channels = widget_state.channels.as_ref();
                    callback(
                        transmogrifier,
                        AnyTransmogrifierContext::new(
                            // unwrap is guranteed because this block can't
                            // execute unless the widget registration is still
                            // alive.
                            widget_state.id.upgrade().unwrap(),
                            state,
                            frontend,
                            widget,
                            channels,
                            &style,
                            &frontend.ui_state_for(widget_id),
                        ),
                    )
                })
            })
    }

    /// Executes `callback` with the transmogrifier and transmogrifier state as
    /// parameters.
    #[allow(clippy::missing_panics_doc)]
    pub fn for_each_widget<
        C: FnMut(&'_ <F as Frontend>::AnyTransmogrifier, &mut dyn AnySendSync, &dyn AnyWidget),
    >(
        &self,
        frontend: &F,
        mut callback: C,
    ) {
        self.data.storage.for_each_widget(|widget_state| {
            // unwrap is guaranteed here because each widget that has been
            // inserted already has interacted with the transmogrifier.
            let transmogrifier = self
                .data
                .transmogrifiers
                .map
                .get(&widget_state.registration().unwrap().id().type_id)
                .unwrap();
            widget_state.with_state(transmogrifier, frontend, |transmogrifier_state, widget| {
                callback(transmogrifier, transmogrifier_state, widget);
            });
        });
    }

    /// Loops over each widget processing its events first, then its commands.
    ///
    /// # Panics
    ///
    /// Panics upon internal locking errors.
    pub fn process_widget_messages(&self, frontend: &F) {
        // If a widget calls code that posts more messages, we don't want this
        // code to re-enter. Instead, the first process_widget_message will loop
        // to catch new widgets that have messages.
        let _guard = match self.data.processing_messages_lock.try_lock() {
            Some(guard) => guard,
            None => return,
        };

        let mut managed_code_guard = self.enter_managed_code(frontend);
        managed_code_guard.allow_process_messages = false;

        // This method will return early if there are no widgets with pending
        // messages. `chatter_limit` is put in place to limit cross-talk betwen
        // widgets. It's possible for one widget to invoke behavior that
        // triggers a message in another widget, which then returns the favor.
        // 10 is an arbitrary number.
        let mut chatter_limit = 10;
        while chatter_limit > 0 {
            chatter_limit -= 1;
            let widgets_with_messages = {
                let mut widgets_with_messages = self.data.storage.data.widgets_with_messages.lock();
                if widgets_with_messages.is_empty() {
                    break;
                }

                std::mem::take(&mut *widgets_with_messages)
            };

            for widget_id in widgets_with_messages {
                self.with_transmogrifier(&widget_id, frontend, |transmogrifier, context| {
                    transmogrifier.process_messages(context);
                });
            }
        }
    }

    pub(crate) fn post_transmogrifier_event<W: Widget>(
        &self,
        widget_id: &WidgetId,
        event: <W as Widget>::Event,
        frontend: &F,
    ) {
        if let Some(state) = self.widget_state(widget_id) {
            let channels = state.channels::<W>().unwrap();
            channels.post_event(event);
            frontend.set_widget_has_messages(widget_id.clone());
        }

        // Process any messages that may have been triggered onto other widgets.
        frontend.gooey().process_widget_messages(frontend);
    }

    pub(crate) fn post_command<W: Widget>(
        &self,
        widget_id: &WidgetId,
        event: <W as Widget>::Command,
        frontend: &F,
    ) {
        if let Some(state) = self.widget_state(widget_id) {
            let channels = state.channels::<W>().unwrap();
            channels.post_command(event);
            frontend.set_widget_has_messages(widget_id.clone());
        }

        // Process any messages that may have been triggered onto other widgets.
        frontend.gooey().process_widget_messages(frontend);
    }

    /// Returns the root widget.
    #[must_use]
    pub fn stylesheet(&self) -> &StyleSheet {
        self.app().stylesheet()
    }

    /// Enters a region of managed code. Automatically exits the region when the returned guard is dropped.
    ///
    /// When the last managed code region is exited, widget messages are processed before returning.
    #[must_use]
    pub fn enter_managed_code(&self, frontend: &F) -> ManagedCodeGuard {
        self.data.inside_event_loop.fetch_add(1, Ordering::SeqCst);
        ManagedCodeGuard {
            frontend: Box::new(frontend.clone()),
            allow_process_messages: true,
        }
    }

    pub(crate) fn exit_managed_code(&self, frontend: &F, allow_process_messages: bool) {
        let previous_value = self.data.inside_event_loop.fetch_sub(1, Ordering::SeqCst);
        if previous_value == 1 && allow_process_messages {
            self.process_widget_messages(frontend);
        }
    }

    /// Returns whether `Gooey` managed code is currently executing.
    #[must_use]
    pub fn is_managed_code(&self) -> bool {
        self.data.inside_event_loop.load(Ordering::SeqCst) > 0
    }

    /// Localizes `key` with `parameters`.
    #[must_use]
    pub fn localize<'a>(
        &self,
        key: &str,
        parameters: impl Into<Option<LocalizationParameters<'a>>>,
    ) -> String {
        self.data.storage.localize(key, parameters)
    }
}

impl<F: Frontend> Deref for Gooey<F> {
    type Target = WidgetStorage;

    fn deref(&self) -> &Self::Target {
        &self.data.storage
    }
}

/// A collection of transmogrifiers to use inside of a frontend.
#[derive(Debug)]
pub struct Transmogrifiers<F: Frontend> {
    map: HashMap<WidgetTypeId, <F as Frontend>::AnyTransmogrifier>,
    _phantom: PhantomData<F>,
}

impl<F: Frontend> Transmogrifiers<F> {
    /// Registers a transmogrifier.
    ///
    /// # Errors
    ///
    /// If an existing transmogrifier is already registered, the transmogrifier
    /// is returned in `Err()`.
    pub fn register_transmogrifier<T: Into<<F as Frontend>::AnyTransmogrifier>>(
        &mut self,
        transmogrifier: T,
    ) -> Result<(), <F as Frontend>::AnyTransmogrifier> {
        let transmogrifier = transmogrifier.into();
        let type_id = <<F as Frontend>::AnyTransmogrifier as AnyTransmogrifier<F>>::widget_type_id(
            &transmogrifier,
        );
        if self.map.contains_key(&type_id) {
            return Err(transmogrifier);
        }

        self.map.insert(type_id, transmogrifier);

        Ok(())
    }
}

impl<F: Frontend> Default for Transmogrifiers<F> {
    fn default() -> Self {
        Self {
            map: HashMap::default(),
            _phantom: PhantomData::default(),
        }
    }
}

/// Generic-type-less widget storage.
#[derive(Clone, Debug)]
pub struct WidgetStorage {
    data: Arc<WidgetStorageData>,
    app: AppContext,
}

#[derive(Default, Debug)]
struct WidgetStorageData {
    widget_id_generator: AtomicU32,
    state: RwLock<HashMap<u32, Option<WidgetState>>>,
    widgets_with_messages: Mutex<HashSet<WidgetId>>,
}

impl WidgetStorage {
    /// Returns a new instance for the window provided.
    #[must_use]
    pub fn new(window: AppContext) -> Self {
        Self {
            data: Arc::default(),
            app: window,
        }
    }

    /// Returns the application.
    #[must_use]
    pub fn app(&self) -> &AppContext {
        &self.app
    }

    /// Register a widget with storage.
    #[allow(clippy::missing_panics_doc)] // The unwrap is unreachable
    pub fn register<W: Widget + AnyWidget>(
        &self,
        styled_widget: StyledWidget<W>,
    ) -> WidgetRegistration {
        let StyledWidget {
            widget,
            style,
            registration,
        } = styled_widget;
        let registration = registration.unwrap_or_else(|| self.allocate::<W>());
        let mut state = self.data.state.write().unwrap();
        let state = state.entry(registration.id().id).or_default();
        assert!(state.is_none(), "widget id in use");

        *state = Some(WidgetState::new(widget, style, &registration));
        registration
    }

    /// Allocates a widget registration. This allows obtaining a registration
    /// before the widget has been created, allowing for a parent widget to pass
    /// its id to its children during widget construction. Attempting to
    /// interact with this widget before calling `register` with the created
    /// widget will panic.
    ///
    /// # Panics
    ///
    /// Panics on internal locking failures.
    pub fn allocate<W: Widget + AnyWidget>(&self) -> WidgetRegistration {
        loop {
            let next_id = self.data.widget_id_generator.fetch_add(1, Ordering::AcqRel);
            // Insert None if the slot is free, which is most likely the case.
            // If it performs the insert, is_new is flagged and the id is
            // returned. If not, the loop repeats until a free entry is found.
            let mut widget_registration = None;
            let mut state = self.data.state.write().unwrap();
            state.entry(next_id).or_insert_with(|| {
                let reg = WidgetRegistration::new::<W>(next_id, self);
                widget_registration = Some(reg);
                None
            });
            if let Some(widget_registration) = widget_registration {
                return widget_registration;
            }
        }
    }

    pub(crate) fn unregister_id(&self, widget_id: u32) {
        // To prevent a deadlock if a widget is being removed that contains
        // other widgets, we need to let drop happen outside of the lock
        // acquisition.
        let removed_value = {
            let mut state = self.data.state.write().unwrap();
            state.remove(&widget_id)
        };
        drop(removed_value);
    }

    /// Returns the state of the widget with id `widget_id`.
    ///
    /// # Panics
    ///
    /// Panics if internal lock handling results in an error.
    #[must_use]
    pub fn widget_state(&self, widget_id: &WidgetId) -> Option<WidgetState> {
        let state = self.data.state.read().unwrap();
        state.get(&widget_id.id).cloned().flatten()
    }

    /// Returns the state of the widget with id `widget_id`.
    ///
    /// # Panics
    ///
    /// Panics if internal lock handling results in an error.
    #[must_use]
    pub fn lock_widget<W: Widget>(
        &self,
        widget_id: &WidgetId,
        frontend: &dyn AnyFrontend,
    ) -> Option<LockedWidget<W>> {
        let state = self.data.state.read().unwrap();
        state
            .get(&widget_id.id)
            .cloned()
            .flatten()
            .and_then(|state| state.lock(frontend))
    }

    /// Executes `callback` with the widget state parameters.
    ///
    /// # Panics
    ///
    /// Panics if internal lock handling results in an error.
    pub fn for_each_widget<C: FnMut(WidgetState)>(&self, mut callback: C) {
        let widgets = {
            let state = self.data.state.read().unwrap();
            state.values().cloned().collect::<Vec<_>>()
        };

        for widget in widgets.into_iter().flatten() {
            callback(widget);
        }
    }

    /// Marks a widget as having messages. If this isn't set, pending messages
    /// will not be received.
    ///
    /// # Panics
    ///
    /// Panics if internal lock handling results in an error.
    pub fn set_widget_has_messages(&self, widget: WidgetId) {
        let mut statuses = self.data.widgets_with_messages.lock();
        statuses.insert(widget);
    }

    /// Localizes `key` with `parameters`.
    #[must_use]
    pub fn localize<'a>(
        &self,
        key: &str,
        parameters: impl Into<Option<LocalizationParameters<'a>>>,
    ) -> String {
        self.app.localize(key, parameters)
    }
}

/// A type that registers widgets with an associated key.
pub trait KeyedStorage<K: Key>: Debug + Send + Sync + 'static {
    /// Register `styled_widget` with `key`.
    fn register<W: Widget + AnyWidget>(
        &mut self,
        key: impl Into<Option<K>>,
        styled_widget: StyledWidget<W>,
    ) -> WidgetRegistration;

    /// Returns the underlying widget storage.
    fn storage(&self) -> &WidgetStorage;

    /// If this storage is representing a component, this returns a weak
    /// registration that can be used to communicate with it.
    fn related_storage(&self) -> Option<Box<dyn RelatedStorage<K>>>;
}

/// A key for a widget.
pub trait Key: Clone + Hash + Debug + Eq + PartialEq + Send + Sync + 'static {}

impl<T> Key for T where T: Clone + Hash + Debug + Eq + PartialEq + Send + Sync + 'static {}

impl<K: Key> KeyedStorage<K> for WidgetStorage {
    fn register<W: Widget + AnyWidget>(
        &mut self,
        _key: impl Into<Option<K>>,
        styled_widget: StyledWidget<W>,
    ) -> WidgetRegistration {
        Self::register(self, styled_widget)
    }

    fn storage(&self) -> &WidgetStorage {
        self
    }

    fn related_storage(&self) -> Option<Box<dyn RelatedStorage<K>>> {
        None
    }
}

/// Related storage enables a widget to communicate in a limited way about
/// widgets being inserted or removed.
pub trait RelatedStorage<K: Key>: Debug + Send + Sync + 'static {
    /// Returns the registration of the widget that this is from.
    fn widget(&self) -> WeakWidgetRegistration;
    /// Removes the widget with `key` from this storage. Returns the removed registration if one was removed.
    fn remove(&self, key: &K) -> Option<WeakWidgetRegistration>;
    /// Registers `widget` with `key`.
    fn register(&self, key: K, widget: &WidgetRegistration);
}

/// A widget and its initial style information.
#[derive(Debug)]
#[must_use]
pub struct StyledWidget<W: Widget> {
    /// The widget.
    pub widget: W,
    /// The style information.
    pub style: Style,
    /// The pre-allocated registration, if any. See [`WidgetStorage::allocate()`] for more information.
    pub registration: Option<WidgetRegistration>,
}

impl<W: Widget + Default> Default for StyledWidget<W> {
    fn default() -> Self {
        Self {
            widget: W::default(),
            style: Style::default(),
            registration: None,
        }
    }
}

impl<W: Widget + From<WidgetRegistration>> From<WidgetRegistration> for StyledWidget<W> {
    fn from(widget: WidgetRegistration) -> Self {
        Self::from(W::from(widget))
    }
}

impl<W: Widget> From<W> for StyledWidget<W> {
    fn from(widget: W) -> Self {
        Self {
            widget,
            style: Style::default(),
            registration: None,
        }
    }
}

impl<W: Widget> StyledWidget<W> {
    /// Returns a new instance.
    pub fn new(widget: W, style: Style, registration: Option<WidgetRegistration>) -> Self {
        Self {
            widget,
            style,
            registration,
        }
    }

    /// Adds `component` to `style` and returns self.
    pub fn with<C: StyleComponent + Clone>(mut self, component: C) -> Self {
        self.style.push(component);
        self
    }
}

/// Generic, clone-able storage for a widget's transmogrifier.
#[derive(Clone, Debug)]
pub struct WidgetState {
    /// The id of the widget.
    pub id: WeakWidgetRegistration,
    /// The widget.
    pub widget: Arc<Mutex<Box<dyn AnyWidget>>>,
    /// The transmogrifier state.
    pub state: Arc<Mutex<Option<TransmogrifierState>>>,

    /// The channels to communicate with the widget.
    pub channels: Arc<dyn AnyChannels>,

    /// The widget's style.
    pub style: Arc<Mutex<Style>>,
}

impl WidgetState {
    /// Initializes a new widget state with `widget`, `id`, and `None` for the
    /// transmogrifier state.
    pub fn new<W: Widget + AnyWidget>(widget: W, style: Style, id: &WidgetRegistration) -> Self {
        Self {
            id: WeakWidgetRegistration::from(id),
            widget: Arc::new(Mutex::new(Box::new(widget))),
            style: Arc::new(Mutex::new(style)),
            state: Arc::default(),
            channels: Arc::new(Channels::<W>::new(id)),
        }
    }

    /// Gets the registration for this widget. Returns None if the widget has
    /// been destroyed.
    #[must_use]
    pub fn registration(&self) -> Option<WidgetRegistration> {
        self.id.upgrade()
    }

    /// Returns the state for this widget. If this is the first call, the state
    /// is initialized with the `Default::default()` implementation for the
    /// `State` associated type on [`Transmogrifier`](crate::Transmogrifier).
    ///
    /// # Panics
    ///
    /// Panics if internal lock poisoning occurs.
    pub fn with_state<
        R,
        T: AnyTransmogrifier<F>,
        F: Frontend,
        C: FnOnce(&mut dyn AnySendSync, &mut dyn AnyWidget) -> R,
    >(
        &self,
        transmogrifier: &T,
        frontend: &F,
        callback: C,
    ) -> Option<R> {
        let mut state = self.state.lock();
        let mut widget = self.widget.lock();
        self.id.upgrade().map(|id| {
            let state = state.get_or_insert_with(|| {
                {
                    let style = self.style.lock();
                    frontend.widget_initialized(id.id(), &style);
                }

                transmogrifier.default_state_for(widget.as_mut(), &id, frontend)
            });

            callback(state.state.as_mut(), widget.as_mut())
        })
    }

    #[must_use]
    pub(crate) fn any_channels(&self) -> &'_ dyn AnyChannels {
        self.channels.as_ref()
    }

    /// Returns the channels used to communicate with this widget.
    #[must_use]
    pub fn channels<W: Widget>(&self) -> Option<&'_ Channels<W>> {
        self.any_channels().as_any().downcast_ref()
    }

    /// Invokes `with_fn` with this `Widget` and a `Context`. Returns the
    /// result.
    ///
    /// Returns None if `W` does not match the type of the widget contained.
    pub fn with_widget<W: Widget, F: FnOnce(&W, &Context<W>) -> R, R>(
        &self,
        frontend: &dyn AnyFrontend,
        with_fn: F,
    ) -> Option<R> {
        let _guard = frontend.enter_managed_code();
        let result = {
            let widget = self.widget.lock();
            let channels = self.channels::<W>()?;
            let context = Context::new(channels, frontend);
            Some(with_fn(widget.as_ref().as_any().downcast_ref()?, &context))
        };
        result
    }

    /// Invokes `with_fn` with this `Widget` and a `Context`. Returns the
    /// result.
    ///
    /// Returns None if `W` does not match the type of the widget contained.
    pub fn with_widget_mut<OW: Widget, F: FnOnce(&mut OW, &Context<OW>) -> R, R>(
        &self,
        frontend: &dyn AnyFrontend,
        with_fn: F,
    ) -> Option<R> {
        let _guard = frontend.enter_managed_code();
        let result = {
            let mut widget = self.widget.lock();
            let channels = self.channels::<OW>()?;
            let context = Context::new(channels, frontend);
            Some(with_fn(
                widget.as_mut().as_mut_any().downcast_mut()?,
                &context,
            ))
        };
        result
    }

    /// Returns a [`WidgetGuard`] for this widget. Returns `None` if `OW` is the wrong type.
    pub fn lock<OW: Widget>(&self, frontend: &dyn AnyFrontend) -> Option<LockedWidget<OW>> {
        let widget = self.widget.lock_arc();
        let channels = self.channels::<OW>()?;
        let context = Context::new(channels, frontend);
        Some(LockedWidget::new(widget, context))
    }
}

/// A locked widget reference. No other threads can operate on the widget while
/// this value is alive.
pub struct LockedWidget<W: Widget> {
    /// The locked widget.
    pub widget: WidgetGuard<W>,
    /// The context that can be used to call methods on `widget`.
    pub context: Context<W>,

    _managed_code_guard: ManagedCodeGuard,
}

impl<W: Widget> LockedWidget<W> {
    pub(crate) fn new(
        widget: ArcMutexGuard<RawMutex, Box<dyn AnyWidget>>,
        context: Context<W>,
    ) -> Self {
        // While the guard is active, we're considered in managed code.
        let managed_code_guard = context.frontend().enter_managed_code();
        Self {
            widget: WidgetGuard {
                guard: widget,
                _widget: PhantomData,
            },
            context,
            _managed_code_guard: managed_code_guard,
        }
    }
}

/// A reference to a locked widget.
pub struct WidgetGuard<W: Widget> {
    guard: ArcMutexGuard<RawMutex, Box<dyn AnyWidget>>,
    _widget: PhantomData<W>,
}

impl<W: Widget> Deref for WidgetGuard<W> {
    type Target = W;

    fn deref(&self) -> &Self::Target {
        self.guard.as_ref().as_any().downcast_ref().unwrap()
    }
}

impl<W: Widget> DerefMut for WidgetGuard<W> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.guard.as_mut().as_mut_any().downcast_mut().unwrap()
    }
}

/// References an initialized widget. On drop, frees the storage and id.
#[derive(Clone, Debug)]
#[must_use]
pub struct WidgetRegistration {
    data: Arc<WidgetRegistrationData>,
}

/// References an initialized widget. These references will not keep a widget
/// from being removed.
#[derive(Clone, Debug)]
pub struct WeakWidgetRegistration {
    data: Weak<WidgetRegistrationData>,
}

impl WeakWidgetRegistration {
    /// Attempt to convert this weak widget registration into a strong one.
    #[must_use]
    pub fn upgrade(&self) -> Option<WidgetRegistration> {
        self.data.upgrade().map(|data| WidgetRegistration { data })
    }
}

impl From<&WidgetRegistration> for WeakWidgetRegistration {
    fn from(reg: &WidgetRegistration) -> Self {
        Self {
            data: Arc::downgrade(&reg.data),
        }
    }
}

#[derive(Debug)]
struct WidgetRegistrationData {
    id: WidgetId,
    storage: WidgetStorage,
}

impl WidgetRegistration {
    pub(crate) fn new<W: Widget>(id: u32, storage: &WidgetStorage) -> Self {
        Self {
            data: Arc::new(WidgetRegistrationData {
                id: WidgetId {
                    id,
                    type_id: TypeId::of::<W>(),
                },
                storage: storage.clone(),
            }),
        }
    }

    /// Returns the unique ID of this widget. IDs are unique per `Gooey`
    /// instance, not across the entire executable.
    #[must_use]
    pub fn id(&self) -> &'_ WidgetId {
        &self.data.id
    }

    /// Sets that this widget has messages. Should not be necessary in normal
    /// usage patterns. This is only needed if you're directly calling send on a
    /// widget's channels.
    pub fn set_has_messages(&self) {
        self.data
            .storage
            .set_widget_has_messages(self.data.id.clone());
    }
}

impl Drop for WidgetRegistrationData {
    fn drop(&mut self) {
        self.storage.unregister_id(self.id.id);
    }
}

/// A widget reference. Does not prevent a widget from being destroyed if
/// removed from an interface.
#[derive(Debug)]
pub struct WidgetRef<W: Widget> {
    registration: WeakWidgetRegistration,
    frontend: Box<dyn AnyFrontend>,
    _phantom: PhantomData<W>,
}

impl<W: Widget> Clone for WidgetRef<W> {
    fn clone(&self) -> Self {
        Self {
            registration: self.registration.clone(),
            frontend: self.frontend.cloned(),
            _phantom: PhantomData::default(),
        }
    }
}

impl<W: Widget> WidgetRef<W> {
    /// Creates a new reference from a [`WidgetRegistration`]. Returns None if
    /// the `W` type doesn't match the type of the widget.
    #[must_use]
    pub fn new<F: Frontend>(registration: &WidgetRegistration, frontend: F) -> Option<Self> {
        if registration.id().type_id == TypeId::of::<W>() {
            Some(Self {
                registration: WeakWidgetRegistration::from(registration),
                frontend: Box::new(frontend),
                _phantom: PhantomData::default(),
            })
        } else {
            None
        }
    }

    /// Creates a new reference from a [`WidgetRegistration`]. Returns None if
    /// the `W` type doesn't match the type of the widget.
    #[must_use]
    pub fn new_with_any_frontend(
        registration: &WidgetRegistration,
        frontend: Box<dyn AnyFrontend>,
    ) -> Option<Self> {
        if registration.id().type_id == TypeId::of::<W>() {
            Some(Self::from_weak_registration(
                WeakWidgetRegistration::from(registration),
                frontend,
            ))
        } else {
            None
        }
    }

    /// Creates a new reference from a [`WidgetRegistration`]. Returns None if
    /// the `W` type doesn't match the type of the widget.
    #[must_use]
    pub fn from_weak_registration(
        registration: WeakWidgetRegistration,
        frontend: Box<dyn AnyFrontend>,
    ) -> Self {
        Self {
            registration,
            frontend,
            _phantom: PhantomData::default(),
        }
    }

    /// Returns the registration record. Returns None if the widget has been
    /// removed from the interface.
    #[must_use]
    pub fn registration(&self) -> Option<WidgetRegistration> {
        self.registration.upgrade()
    }

    /// Posts `event` to a transmogrifier.
    ///
    /// # Panics
    ///
    /// Panics if `F` is not the type of the frontend in use.
    pub fn post_event<F: Frontend>(&self, event: W::Event) {
        let frontend = self.frontend.as_ref().as_any().downcast_ref::<F>().unwrap();
        let _guard = frontend.enter_managed_code();
        if let Some(registration) = self.registration() {
            frontend
                .gooey()
                .post_transmogrifier_event::<W>(registration.id(), event, frontend);
        }
    }

    /// Posts `event` to a transmogrifier.
    ///
    /// # Panics
    ///
    /// Panics if `F` is not the type of the frontend in use.
    pub fn post_command<F: Frontend>(&self, command: W::Command) {
        let frontend = self.frontend.as_ref().as_any().downcast_ref::<F>().unwrap();
        if let Some(registration) = self.registration() {
            frontend
                .gooey()
                .post_command::<W>(registration.id(), command, frontend);
        }
    }
}

/// A type that provides localization (multi-lingual representations of text).
pub trait Localizer: Send + Sync + Debug + 'static {
    /// Localizes `key` with `parameters` and returns a string in the user's
    /// preferred locale.
    fn localize<'a>(
        &self,
        key: &str,
        parameters: Option<LocalizationParameters<'a>>,
        language: &LanguageIdentifier,
    ) -> String;
}

impl Localizer for () {
    /// Returns `key.to_string()`, ignoring the remaining parameters.
    fn localize<'a>(
        &self,
        key: &str,
        _parameters: Option<LocalizationParameters<'a>>,
        _language: &LanguageIdentifier,
    ) -> String {
        key.to_string()
    }
}

/// A context used during initialization of a window or application.
#[derive(Debug, Clone)]
pub struct AppContext {
    stylesheet: Arc<StyleSheet>,
    localizer: Arc<dyn Localizer>,
    language: Arc<RwLock<LanguageIdentifier>>,
}

impl AppContext {
    /// Returns a new context with the language and localizer provided.
    #[must_use]
    pub fn new(
        stylesheet: StyleSheet,
        initial_language: LanguageIdentifier,
        localizer: Arc<dyn Localizer>,
    ) -> Self {
        Self {
            stylesheet: Arc::new(stylesheet),
            language: Arc::new(RwLock::new(initial_language)),
            localizer,
        }
    }

    /// Localizes `key` with `parameters`.
    #[must_use]
    // For this usage of RwLock, panics should not be possible.
    #[allow(clippy::missing_panics_doc)]
    pub fn localize<'a>(
        &self,
        key: &str,
        parameters: impl Into<Option<LocalizationParameters<'a>>>,
    ) -> String {
        let language = self.language.read().unwrap();
        self.localizer.localize(key, parameters.into(), &language)
    }

    /// Returns the stylesheet for the application.
    #[must_use]
    pub fn stylesheet(&self) -> &StyleSheet {
        &self.stylesheet
    }
}

/// A parameter used in localization.
#[derive(Debug, Clone)]
pub enum LocalizationParameter<'a> {
    /// A string value.
    String(Cow<'a, str>),
    /// A numeric value.
    Numeric(f64),
}

impl<'a> From<f64> for LocalizationParameter<'a> {
    fn from(value: f64) -> Self {
        Self::Numeric(value)
    }
}

impl<'a> From<&'a str> for LocalizationParameter<'a> {
    fn from(value: &'a str) -> Self {
        Self::String(Cow::Borrowed(value))
    }
}

impl<'a> From<String> for LocalizationParameter<'a> {
    fn from(value: String) -> Self {
        Self::String(Cow::Owned(value))
    }
}

/// Parameters used in localization strings.
#[derive(Debug, Clone, Default)]
#[must_use]
pub struct LocalizationParameters<'a>(HashMap<String, LocalizationParameter<'a>>);

impl<'a> From<HashMap<String, LocalizationParameter<'a>>> for LocalizationParameters<'a> {
    fn from(parameters: HashMap<String, LocalizationParameter<'a>>) -> Self {
        Self(parameters)
    }
}

impl<'a> LocalizationParameters<'a> {
    /// Returns an empty set of parameters.
    pub fn new() -> Self {
        Self::default()
    }

    /// Builder-style method for adding a new parameter.
    pub fn with(
        mut self,
        name: impl Into<String>,
        parameter: impl Into<LocalizationParameter<'a>>,
    ) -> Self {
        self.insert(name.into(), parameter.into());
        self
    }
}

impl<'a> Deref for LocalizationParameters<'a> {
    type Target = HashMap<String, LocalizationParameter<'a>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a> DerefMut for LocalizationParameters<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<'a> IntoIterator for LocalizationParameters<'a> {
    type IntoIter = std::collections::hash_map::IntoIter<String, LocalizationParameter<'a>>;
    type Item = (String, LocalizationParameter<'a>);

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}