sinomo-ui 0.6.1

Public Rust facade for the SUI workspace.
Documentation
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
#![forbid(unsafe_code)]

//! Public Rust facade for SUI.
//!
//! Most applications can import [`prelude`] and construct an [`App`] with one
//! or more [`Window`] values. Lower-level runtime, scene, renderer, and widget
//! types are re-exported for custom widgets, embedding, tests, and tooling.
//!
//! ```no_run
//! use sui::prelude::*;
//!
//! fn main() -> Result<()> {
//!     App::new()
//!         .main_window("Hello SUI", Label::new("Ready"))
//!         .run()
//! }
//! ```

use std::{
    any::{Any, TypeId},
    collections::HashMap,
    fmt,
    sync::Arc,
};

/// User-facing application, window, resource, and wake-handle builders.
pub mod app;
/// Composite widgets and application-shell building blocks.
pub mod composites;
/// Layout and containment widgets.
pub mod containers;
/// Primitive controls, labels, inputs, and icons.
pub mod controls;

#[cfg(any(feature = "desktop", feature = "web", feature = "mobile"))]
pub use app::UiHandle;
pub use app::{App, ResourceRegistry, Window};

pub use composites::{
    ActionTileGeometry, ActionTilePaint, BottomSheet, BrowserTabBar, CalloutPaint, CodePanelPaint,
    CodeTextLine, CodeTextPaint, CodeTextSpan, CommandButtonFill, CommandButtonPaint, CommandGroup,
    CommandPalette, ContextMenu, ContextMenuHandle, Dialog, DisclosureButtonPaint, DockPanel,
    EmptyState, EmptyStatePaint, FieldGroup, FormRow, FormSection, FramedField, HairlineEdge, Menu,
    MenuItem, PanelSection, Popover, PopoverAlignment, PresetStrip, ProgressBar, PropertyRow,
    PropertyRowLayout, SectionLabel, SectionLabelPaint, SectionPanelGeometry, SectionPanelPaint,
    ShadowBox, SheetState, SideSheet, SideSheetPlacement, Spinner, StatusBar, StatusBarHost,
    StatusBarSegment, Surface, SurfaceAppearance, SurfaceBorder, SurfaceElevation, SurfaceRole,
    TabBar, TabBarItem, Tabs, ToolPalette, ToolPaletteItem, Toolbar, Tooltip, TooltipAlignment,
    TooltipPlacement, paint_action_tile, paint_border, paint_callout, paint_code_lines,
    paint_code_panel, paint_command_button, paint_disclosure_button, paint_empty_state,
    paint_hairline, paint_placement_badge_with, paint_rounded_panel, paint_rounded_rect,
    paint_section_label, paint_section_label_detail, paint_section_panel,
};
pub use containers::{
    Align, Background, ContentExtent, Dock, FixedPaneSplit, Flex, MeasuredBottomDock, Overflow,
    Padding, RebuildOnChange, RebuildOnConstraints, ScrollAxes, ScrollBar, ScrollState, ScrollView,
    SemanticRegion, SizedBox, Stack, SwitchView, TrailingSlotRow, VirtualScrollView,
};
pub use controls::{
    BUILTIN_ICON_GLYPHS, Button, ButtonAppearance, Checkbox, CheckboxIndicatorState,
    ChoiceAppearance, DateTimeInput, FieldAppearance, Icon, IconButton, IconButtonPaint, IconGlyph,
    InteractionPreview, Label, Link, NumberInput, PasswordInput, RadioButton, RadioGroup, Select,
    Separator, Slider, Switch, TextArea, TextInput, draw_glyph, paint_checkbox_indicator,
    paint_icon_button,
};
pub use sui_core::{
    AsyncWakeToken, Clipboard, ClipboardBackend, Color, ColorSpace, CursorGrabMode, CustomEvent,
    DirtyRegion, DpiInfo, DragDropScope, DragEvent, DragEventKind, DragOutcome, DragPayload,
    DragPreview, DragScopeId, DragSessionId, DropEffect, DropEffects, Error, Event, FontHandle,
    ImageHandle, ImeEvent, InvalidationKind, InvalidationRequest, InvalidationTarget, KeyState,
    KeyboardEvent, LocalClipboardBackend, LocalUrlOpenerBackend, Modifiers, MotionPolicy,
    MotionPreference, Oklch, Path, PathBuilder, PathElement, Point, PointerButton, PointerButtons,
    PointerEvent, PointerEventKind, PointerKind, RawMouseMotionEvent, Rect, Result, SafeAreaInsets,
    ScrollDelta, SemanticsAction, SemanticsActionRequest, SemanticsEvent, SemanticsLiveRegion,
    SemanticsNode, SemanticsPopupKind, SemanticsRelations, SemanticsRole, SemanticsState,
    SemanticsTextRange, SemanticsValue, Size, SurfaceId, TimerToken, ToggleState, Transform,
    UrlOpener, UrlOpenerBackend, UrlPolicy, Vector, WakeEvent, WidgetId, WindowEvent, WindowId,
};
pub use sui_layout::Padding as Insets;
pub use sui_layout::{
    Alignment, Axis, Constraints, FlexAlignContent, FlexBasis, FlexItem, FlexItemLayout,
    FlexJustify, FlexLayout, FlexLineLayout, FlexStyle, FlexWrap, GridItem, GridItemLayout,
    GridLayout, GridPlacement, GridStyle, GridTrack, GridTrackMax, IntrinsicSize, LayoutContext,
    arrange_flex, flex_layout, grid_layout,
};
#[cfg(all(target_os = "android", feature = "mobile"))]
pub use sui_platform::AndroidApp;
/// The canvas element a browser app draws a window on.
#[cfg(all(feature = "web", target_arch = "wasm32"))]
pub use sui_platform::WebCanvas;
#[cfg(all(feature = "web", target_arch = "wasm32"))]
pub use sui_platform::WebUrlOpenerBackend;
#[cfg(any(feature = "desktop", feature = "web", feature = "mobile"))]
pub use sui_platform::{
    DesktopExtension, DesktopExtensionContext, DesktopPlatform, DesktopWindow, FileDialogFilter,
    FileDialogFuture, FileDialogMode, FileDialogRequest, FileDialogSelection, FileDialogService,
    FolderEntry, HeadlessPlatform, NativeFileDialogs, PlatformFile, PlatformFolder, PlatformWindow,
    Waker, show_file_dialog,
};
#[cfg(all(
    feature = "desktop",
    any(target_os = "windows", target_os = "macos", target_os = "linux")
))]
pub use sui_platform::{DesktopPumpStatus, PumpedDesktop};
#[cfg(all(
    feature = "desktop",
    any(target_os = "windows", target_os = "macos", target_os = "linux")
))]
pub use sui_platform::{OsClipboardBackend, OsUrlOpenerBackend};
pub use sui_reactive::{
    Change as ObservableChange, Changed, Observable, Observer, Selector, Signal, SourceId,
    Subscription, Task, TaskHandle, TaskState, WeakObserver, Zip, batch, combine, combine_named,
};
#[cfg(feature = "wgpu")]
pub use sui_render_wgpu::{
    DisplayCapabilities, DisplayColorPrimaries, OutputGamut, RendererCapabilities, RendererInterop,
    RequestedToneMappingMode, RgbaImage, StemDarkening, TextCoveragePolicy, TextHinting,
    WgpuExternalTextureContext, WgpuExternalTextureRegistry, WgpuRenderer,
};
pub use sui_runtime::{
    AnimateCtx, Application as RuntimeApplication, ArrangeCtx, Command, CommandController,
    CommandCtx, CommandDelivery, CommandKey, CommandSender, CommandTarget, DEFAULT_SUI_LOGO_SVG,
    EXTERNAL_WAKE_KIND, EmbeddedSvgImageResource, EventCtx, EventPhase, EventRoutePhase,
    EventRouteStep, FocusRestorePolicy, FocusScope, FocusScopeState, FocusState, FrameClock,
    FramePacing, FrameSchedule, KeyedChildren, KeyedReconcile, LayerOptions, MeasureCtx, Motion,
    OVERLAY_DISMISS_REQUEST, OutputColorRange, OverlayDismissPolicy, OverlayDismissReason,
    OverlayDismissRequest, OverlayFocusBehavior, OverlayInitialFocus, OverlayKind, OverlayModality,
    OverlayOptions, PaintBoundaryMode, PaintCtx, REACTIVE_CHANGED, RenderOutput, Runtime,
    SemanticsCtx, SingleChild, StackHostOptions, StackOrderPolicy, StackSurfaceOptions, Widget,
    WidgetChildren, WidgetDiagnosticsCtx, WidgetPod, WidgetPodMutVisitor, WidgetPodVisitor,
    WindowBuilder, WindowColorManagementMode, WindowCursorState, WindowDynamicRangeMode,
    WindowIcon, WindowOutputColorPrimaries, WindowRenderOptions, WindowStemDarkening,
    WindowTextCoveragePolicy, WindowTextHinting, WindowTextSubpixelOrder, WindowToneMappingMode,
    app_motion_preference, clear_window_render_options, default_sui_logo_image, motion_policy,
    motion_time_scale, reset_motion_settings, set_app_motion_preference, set_motion_time_scale,
    set_system_motion_preference, set_window_render_options, system_motion_preference,
    window_output_color_range, window_output_color_range_signal, window_render_options,
};
pub use sui_scene::{
    Border, Brush, GradientStop, ImagePixelSnap, ImageRegistry, ImageSampling, ImageSource,
    LayerCompositionMode, LayerProperties, RegisteredExternalImage, RegisteredImage,
    RegisteredImageFormat, Scene, SceneCommand, SceneFrame, ShadowParams, ShadowPlacement,
    StrokeStyle, TextRenderCoveragePolicy, TextRenderHinting, TextRenderMode, TextRenderPolicy,
    TextRenderStemDarkening, TextSubpixelOrder, WidgetShader,
};
pub use sui_text::{
    FontFamilyStack, FontFeature, FontFeatures, FontRegistry, FontStretch, FontStyle, FontWeight,
    PersistentTextLayout, RegisteredFont, ResolvedTextFace, ShapedGlyph, ShapedText,
    ShapedTextWindow, TextAffinity, TextAlign, TextCluster, TextCursor, TextDirection,
    TextDocument, TextFlowDirection, TextGlyphInstance, TextLayout, TextLayoutHandle, TextLayoutId,
    TextLayoutMetadata, TextLayoutRegistry, TextLayoutRequest, TextLayoutRun, TextLayoutVersion,
    TextLayoutView, TextLine, TextLineWindow, TextMeasurement, TextParagraph, TextParagraphLayout,
    TextParagraphStyle, TextRun, TextRunView, TextSelection, TextSelectionGeometry, TextSpan,
    TextSpanId, TextStyle, TextWrap, TextWritingMode,
};
pub use sui_widgets::SignalMeter;
pub use sui_widgets::animation::{
    ANIMATION_DOCUMENT_VERSION, AnimatedValue, AnimationBinding, AnimationBindingInvalidation,
    AnimationDocument, AnimationDocumentFormatError, AnimationEditorCommand, AnimationEditorState,
    AnimationPlayer, AnimationProperty, AnimationPropertyPath, AnimationSelection, AnimationSpec,
    AnimationTargetId, AnimationTick, AnimationValue, AnimationValueKind, Blink, Clip,
    CompiledClip, CompiledTimeline, CompiledTrack, Easing, Interpolate, Keyframe,
    KeyframeSelection, LoopMode, MotionScalar, MotionValue, PlaybackState, Progress, Pulse,
    SampleBatch, SampleBuffer, SampledAnimationValue, SharedCompiledTimeline, SpringF32,
    SpringSpec, Timeline, TimelineBindingSink, TimelinePlayer, TimelineSnap, TimelineTick, Track,
    Transition, invalidation_for_animation_property,
};
pub use sui_widgets::animation::{
    MIN_ANIMATION_DOCUMENT_VERSION, PlaybackSpan, Stagger, StaggerOrigin, TimelineMarker,
};
pub use sui_widgets::{
    ActionCard, AdaptiveBreakpoints, AdaptiveClass, AdaptiveView, AspectRatio, AspectRatioFit,
    BasicSyntaxHighlighter, Breadcrumb, BreadcrumbItem, BrushPreview, BrushPreviewShape,
    BrushPreviewSpec, Canvas, CanvasColors, CanvasGridStyle, CanvasRuler, CanvasRulerAxis,
    CanvasRulerColors, CanvasShape, CanvasStroke, CanvasSurface, CanvasViewport,
    CanvasZoomBehavior, CanvasZoomContext, CollectionAnchor, CollectionAnchorGravity,
    CollectionChange, CollectionDelta, CollectionExtentIndex, CollectionModelError, CollectionSync,
    CollectionWindow, ColorPalette, ColorPaletteSwatch, ColorPicker, ColorPickerColors,
    ColorSwatch, ConstraintOrientation, ConstraintQuery, ConstraintView, ControlMetrics,
    ControlPalette, ControlSize, ControlTypography, CoverageDots, CoverageDotsConfig,
    DecorativeColors, DecorativeHue, DecorativePalette, DefaultTheme, DetailRow, DockFloatingGroup,
    DockLayoutError, DockNode, DockPanelId, DockWorkspace, DockWorkspaceSnapshot,
    DockWorkspaceState, DockZone, DragDropHost, Draggable, DropHover, DropTarget, EffectToken,
    FloatingStack, FloatingViewConfig, FloatingViewSnapshot, FloatingWorkspace,
    FloatingWorkspaceState, GlowTone, Grid, GridCell, HdrColorRoles, HdrEffectTokens,
    HdrLuminanceTokens, HdrMaterialTokens, HdrPolicyTokens, HdrThemeMode, HdrThemeTokens, Image,
    ImageFit, LayerList, LayerListItem, LayerListReorderChange, LayoutTransition,
    LeadingLabelCellPaint, ListItem, ListView, MasterDetail, MasterDetailRoute, MasterDetailState,
    MaterialToken, NeutralRamp, NotificationCenter, NotificationHost, NotificationId,
    NotificationUrgency, OverlayAlignment, OverlayCollisionPolicy, OverlayHost, OverlayPlacement,
    OverlayPlacementRequest, OverlayPlacementResult, OverlaySide, PixelCanvas,
    PixelCanvasBlendMode, PixelCanvasBrushShape, PixelCanvasColors, PixelCanvasExportSnapshot,
    PixelCanvasState, PixelCanvasTool, PlacementBadge, PlacementBadgePaint, ReorderableList,
    ReorderableListChange, ResolvedEffectStyle, ResolvedHdrStyle, ResolvedMaterialStyle,
    ResponsiveSidebar, ResponsiveSidebarMode, ResponsiveSidebarSnapshot, ResponsiveSidebarState,
    RichAttachment, RichBlockId, RichDocumentBlock, RichDocumentBlockKind, RichDocumentModel,
    RichDocumentRenderContext, RichDocumentRendererRegistry, RichDocumentSnapshot,
    RichDocumentSpan, RichDocumentStatus, RichDocumentUpdate, RichDocumentView,
    RichDocumentViewState, RichExtensionBlock, RichInlineImage, RichInlineKind, RichInlineStyle,
    RichLink, RichListItem, RichSyntaxHighlighter, RichSyntaxSpan, RichSyntaxTokenKind, RichText,
    RichTextSourceMap, RichTextSourceSpan, SafeArea, SafeAreaEdges, ScrollAlignment,
    SegmentedControl, SegmentedControlItem, SelectionChange, SelectionClipboardBehavior,
    SelectionEntry, SelectionIntent, SelectionOrder, SelectionOwnerId, SelectionPayload,
    SelectionPoint, SelectionScope, SemanticColorToken, SemanticTone, SimpleColorPicker,
    SimpleColorPickerMode, SplitExtent, SplitPaneSide, SplitState, SplitStateSnapshot, SplitView,
    StatusBadge, SurfacePalette, TEXT_COMMAND, Table, TableColumn, TableColumnAlignment, TableRow,
    TextBlockPaint, TextCellPaint, TextCommand, TextSelectionInfo, TextSurface,
    TextSurfaceOverlayKind, TextSurfaceStatus, TextSurfaceStyleOverlay, TextSurfaceStyleSpan,
    ThemeAspectRatios, ThemeBlurScale, ThemeBreakpoints, ThemeColorScheme, ThemeColors,
    ThemeContainers, ThemeDensity, ThemeFontFamilies, ThemeFontStack, ThemeFontWeights, ThemeGlows,
    ThemeLeading, ThemeMotion, ThemePerspective, ThemeRadii, ThemeShadow, ThemeShadowLayer,
    ThemeShadows, ThemeTextScale, ThemeTextToken, ThemeTracking, ToneRoles, TransientNotification,
    TreeItem, TreeView, VirtualCollectionModel, VirtualCollectionSource, VirtualList,
    VirtualListChrome, VirtualListSelectionMode, VirtualListState, VirtualTable,
    VirtualTableColumn, VirtualTableRowActivationKind, VirtualTableRowContext,
    VirtualTableSortDirection, VirtualTableState, VirtualViewportSnapshot, WidgetColorRole,
    WidgetEffectRole, WidgetLuminanceRole, WidgetMaterialRole, detail_row_height_for_value,
    paint_canvas_grid, paint_coverage_dots, paint_coverage_dots_with_config, paint_detail_row_at,
    paint_leading_label_cell, paint_placement_badge, paint_progress_bar, paint_status_badge,
    paint_text, paint_text_block, paint_text_cell, paint_text_line, paint_text_line_elided,
    paint_theme_glow, paint_theme_inset_shadow, paint_theme_shadow, place_overlay,
    resolve_effect_role, resolve_luminance_role, resolve_material_role, resolve_semantic_color,
    resolve_widget_hdr_style, wrap_text_lines,
};
pub use sui_widgets::{
    ElidedLine, Paragraph, TextOverflow, TextPlacement, TextShaper, VerticalAlign,
};
pub use sui_widgets::{KeyedStack, Presence, PresenceTransition};

/// Marker trait for type-indexed application theme extensions.
pub trait ThemeExtension: Any + Send + Sync {}

impl<T> ThemeExtension for T where T: Any + Send + Sync {}

/// Type-indexed storage for application-specific theme values.
#[derive(Clone, Default)]
pub struct ThemeExtensions {
    values: HashMap<TypeId, Arc<dyn Any + Send + Sync>>,
}

impl ThemeExtensions {
    /// Create empty extension storage.
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert or replace an extension, returning the previous value when present.
    pub fn insert<T>(&mut self, value: T) -> Option<Arc<T>>
    where
        T: ThemeExtension,
    {
        self.values
            .insert(TypeId::of::<T>(), Arc::new(value))
            .and_then(|previous| Arc::downcast::<T>(previous).ok())
    }

    /// Borrow an extension by its concrete type.
    pub fn get<T>(&self) -> Option<&T>
    where
        T: ThemeExtension,
    {
        self.values
            .get(&TypeId::of::<T>())
            .and_then(|value| value.as_ref().downcast_ref::<T>())
    }

    /// Clone the shared pointer for an extension by its concrete type.
    pub fn get_arc<T>(&self) -> Option<Arc<T>>
    where
        T: ThemeExtension,
    {
        self.values
            .get(&TypeId::of::<T>())
            .and_then(|value| Arc::clone(value).downcast::<T>().ok())
    }

    /// Return whether an extension of type `T` is registered.
    pub fn contains<T>(&self) -> bool
    where
        T: ThemeExtension,
    {
        self.values.contains_key(&TypeId::of::<T>())
    }

    /// Remove and return an extension by its concrete type.
    pub fn remove<T>(&mut self) -> Option<Arc<T>>
    where
        T: ThemeExtension,
    {
        self.values
            .remove(&TypeId::of::<T>())
            .and_then(|value| Arc::downcast::<T>(value).ok())
    }

    /// Return whether no extensions are registered.
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// Return the number of registered extension types.
    pub fn len(&self) -> usize {
        self.values.len()
    }
}

impl fmt::Debug for ThemeExtensions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ThemeExtensions")
            .field("len", &self.values.len())
            .finish()
    }
}

/// Application-level colors, built-in widget theme, and custom extensions.
#[derive(Debug, Clone)]
pub struct Theme {
    /// Default application background color.
    pub background: Color,
    /// Default application foreground color.
    pub foreground: Color,
    /// Theme used by SUI's built-in widgets.
    pub default_widgets: DefaultTheme,
    /// Type-indexed application theme extensions.
    pub extensions: ThemeExtensions,
}

impl Default for Theme {
    fn default() -> Self {
        let default_widgets = DefaultTheme::default();
        Self {
            background: default_widgets.palette.surface,
            foreground: default_widgets.palette.text,
            default_widgets,
            extensions: ThemeExtensions::default(),
        }
    }
}

impl Theme {
    /// Create the default application theme.
    pub fn new() -> Self {
        Self::default()
    }

    /// Replace the theme used by built-in widgets.
    pub fn with_default_widgets(mut self, theme: DefaultTheme) -> Self {
        self.default_widgets = theme;
        self
    }

    /// Add a typed extension using builder syntax.
    pub fn with_extension<T>(mut self, value: T) -> Self
    where
        T: ThemeExtension,
    {
        self.extensions.insert(value);
        self
    }

    /// Insert or replace a typed theme extension.
    pub fn insert_extension<T>(&mut self, value: T) -> Option<Arc<T>>
    where
        T: ThemeExtension,
    {
        self.extensions.insert(value)
    }

    /// Borrow a typed theme extension.
    pub fn extension<T>(&self) -> Option<&T>
    where
        T: ThemeExtension,
    {
        self.extensions.get::<T>()
    }

    /// Clone the shared pointer for a typed theme extension.
    pub fn extension_arc<T>(&self) -> Option<Arc<T>>
    where
        T: ThemeExtension,
    {
        self.extensions.get_arc::<T>()
    }

    /// Return whether a typed theme extension is present.
    pub fn has_extension<T>(&self) -> bool
    where
        T: ThemeExtension,
    {
        self.extensions.contains::<T>()
    }

    /// Remove and return a typed theme extension.
    pub fn remove_extension<T>(&mut self) -> Option<Arc<T>>
    where
        T: ThemeExtension,
    {
        self.extensions.remove::<T>()
    }
}

/// Lower-level application builder for runtime and platform integration.
///
/// Regular applications should prefer [`App`]. This type remains public for
/// custom embedding, debug tooling, and direct [`Runtime`] construction.
pub struct Application {
    inner: RuntimeApplication,
    #[cfg(feature = "wgpu")]
    feathering_enabled: bool,
    #[cfg(feature = "wgpu")]
    feather_width: f32,
    #[cfg(feature = "wgpu")]
    external_texture_registry: Option<WgpuExternalTextureRegistry>,
    initial_window_render_options: Option<WindowRenderOptions>,
}

impl Default for Application {
    fn default() -> Self {
        Self {
            inner: RuntimeApplication::default(),
            #[cfg(feature = "wgpu")]
            feathering_enabled: sui_render_wgpu::FeatheringOptions::default().enabled,
            #[cfg(feature = "wgpu")]
            feather_width: sui_render_wgpu::FeatheringOptions::default().width,
            #[cfg(feature = "wgpu")]
            external_texture_registry: None,
            initial_window_render_options: None,
        }
    }
}

impl Application {
    /// Create an empty application.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a lower-level window builder.
    pub fn window(mut self, window: WindowBuilder) -> Self {
        self.inner = self.inner.window(window);
        self
    }

    /// Attach an application-scoped controller outside the widget tree.
    pub fn controller(mut self, controller: impl CommandController + 'static) -> Self {
        self.inner = self.inner.controller(controller);
        self
    }

    /// Decide which URLs `EventCtx::open_url` hands to the platform, in place
    /// of the default that opens only `http`, `https`, and `mailto` URLs. See
    /// [`UrlPolicy`].
    pub fn url_policy(mut self, policy: UrlPolicy) -> Self {
        self.inner = self.inner.url_policy(policy);
        self
    }

    /// Subscribe an application-scoped typed command handler.
    pub fn on_command<T, F>(mut self, key: CommandKey<T>, handler: F) -> Self
    where
        T: Send + Sync + 'static,
        F: FnMut(&mut CommandCtx, &T) + 'static,
    {
        self.inner = self.inner.on_command(key, handler);
        self
    }

    #[cfg(feature = "wgpu")]
    /// Enable or disable analytic-edge feathering in the WGPU renderer.
    pub fn with_feathering_enabled(mut self, enabled: bool) -> Self {
        self.feathering_enabled = enabled;
        self
    }

    #[cfg(feature = "wgpu")]
    /// Set the analytic-edge feather width in logical pixels.
    pub fn with_feather_width(mut self, feather_width: f32) -> Self {
        self.feather_width = feather_width.max(0.0);
        self
    }

    #[cfg(feature = "wgpu")]
    /// Attach an app-owned external texture registry.
    pub fn with_external_texture_registry(mut self, registry: WgpuExternalTextureRegistry) -> Self {
        self.external_texture_registry = Some(registry);
        self
    }

    #[cfg(feature = "wgpu")]
    /// Return whether renderer feathering is enabled.
    pub fn feathering_enabled(&self) -> bool {
        self.feathering_enabled
    }

    #[cfg(feature = "wgpu")]
    /// Return the configured renderer feather width.
    pub fn feather_width(&self) -> f32 {
        self.feather_width
    }

    #[cfg(feature = "wgpu")]
    /// Borrow the configured external texture registry, if present.
    pub fn external_texture_registry(&self) -> Option<&WgpuExternalTextureRegistry> {
        self.external_texture_registry.as_ref()
    }

    /// Set initial render options applied to every window at startup.
    pub fn with_window_render_options(mut self, options: WindowRenderOptions) -> Self {
        self.initial_window_render_options = Some(options.clamped());
        self
    }

    /// Return the initial render options, if configured.
    pub fn initial_window_render_options(&self) -> Option<WindowRenderOptions> {
        self.initial_window_render_options
    }

    /// Register a font with an explicit handle.
    pub fn register_font(&mut self, handle: FontHandle, font: RegisteredFont) -> Result<()> {
        self.inner.register_font(handle, font)
    }

    /// Register a TrueType, OpenType, WOFF, or WOFF2 font and allocate a
    /// handle. Fails when the data is not such a font.
    pub fn register_font_bytes(&mut self, data: impl Into<Vec<u8>>) -> Result<FontHandle> {
        self.inner.register_font_bytes(data)
    }

    /// Register an image with an explicit handle.
    pub fn register_image(&mut self, handle: ImageHandle, image: RegisteredImage) -> Result<()> {
        self.inner.register_image(handle, image)
    }

    /// Register RGBA8 pixels and allocate an image handle.
    pub fn register_rgba_image(
        &mut self,
        width: u32,
        height: u32,
        data: impl Into<Vec<u8>>,
    ) -> Result<ImageHandle> {
        self.inner.register_rgba_image(width, height, data)
    }

    /// Register SVG bytes at their intrinsic size and allocate an image handle.
    pub fn register_svg_image(&mut self, data: impl AsRef<[u8]>) -> Result<ImageHandle> {
        self.inner.register_svg_image(data)
    }

    /// Register SVG bytes at their intrinsic size with an explicit handle.
    pub fn register_svg_image_with_handle(
        &mut self,
        handle: ImageHandle,
        data: impl AsRef<[u8]>,
    ) -> Result<()> {
        self.inner.register_svg_image_with_handle(handle, data)
    }

    /// Rasterize SVG bytes at an explicit size and allocate an image handle.
    pub fn register_svg_image_at_size(
        &mut self,
        width: u32,
        height: u32,
        data: impl AsRef<[u8]>,
    ) -> Result<ImageHandle> {
        self.inner.register_svg_image_at_size(width, height, data)
    }

    /// Rasterize SVG bytes at an explicit size and handle.
    pub fn register_svg_image_at_size_with_handle(
        &mut self,
        handle: ImageHandle,
        width: u32,
        height: u32,
        data: impl AsRef<[u8]>,
    ) -> Result<()> {
        self.inner
            .register_svg_image_at_size_with_handle(handle, width, height, data)
    }

    /// Register one embedded SVG resource.
    pub fn register_embedded_svg_image(
        &mut self,
        resource: EmbeddedSvgImageResource,
    ) -> Result<()> {
        self.inner.register_embedded_svg_image(resource)
    }

    /// Register multiple embedded SVG resources.
    pub fn register_embedded_svg_images(
        &mut self,
        resources: impl IntoIterator<Item = EmbeddedSvgImageResource>,
    ) -> Result<()> {
        self.inner.register_embedded_svg_images(resources)
    }

    /// Build the retained runtime without starting a platform event loop.
    pub fn build(self) -> Result<Runtime> {
        self.inner.build()
    }

    #[cfg(any(feature = "desktop", feature = "web"))]
    /// Build and run the application on the default desktop or web platform.
    pub fn run(self) -> Result<()> {
        self.run_with_platform(DesktopPlatform::new())
    }

    #[cfg(any(feature = "desktop", feature = "web"))]
    /// Build and run the application with a configured desktop platform host.
    ///
    /// This is the integration point for optional native-surface libraries.
    /// Renderer settings already configured on the application are applied to
    /// the supplied platform before the event loop starts.
    pub fn run_with_platform(self, platform: DesktopPlatform) -> Result<()> {
        self.run_with_platform_and_handle(platform, |_| {})
    }

    /// Like [`run_with_platform`](Self::run_with_platform) but invokes
    /// `on_ready` with the application's typed command sender.
    #[cfg(any(feature = "desktop", feature = "web"))]
    pub fn run_with_platform_and_handle(
        self,
        platform: DesktopPlatform,
        on_ready: impl FnOnce(CommandSender),
    ) -> Result<()> {
        let (platform, runtime) = self.prepare_platform(platform)?;
        let _ = platform.run_with(runtime, on_ready)?;
        Ok(())
    }

    /// Start the application on the desktop without blocking, invoking
    /// `on_ready` with the application's typed command sender. The caller
    /// drives the event loop with [`PumpedDesktop::pump`].
    #[cfg(all(
        feature = "desktop",
        any(target_os = "windows", target_os = "macos", target_os = "linux")
    ))]
    pub fn start_pumped_with(self, on_ready: impl FnOnce(CommandSender)) -> Result<PumpedDesktop> {
        let (platform, runtime) = self.prepare_platform(DesktopPlatform::new())?;
        platform.start_pumped(runtime, on_ready)
    }

    /// Start the application in the browser, drawing the first window on
    /// `canvas` (or the page's `sui-main-canvas` element). `on_ready`
    /// receives the command sender; `on_exit` runs when the app exits.
    #[cfg(all(feature = "web", target_arch = "wasm32"))]
    pub fn start_web_with(
        self,
        canvas: Option<WebCanvas>,
        on_ready: impl FnOnce(CommandSender),
        on_exit: impl FnOnce(Result<()>) + 'static,
    ) -> Result<()> {
        let mut platform = DesktopPlatform::new();
        if let Some(canvas) = canvas {
            platform = platform.with_web_canvas(canvas);
        }
        let (platform, runtime) = self.prepare_platform(platform)?;
        platform.start_web(runtime, on_ready, on_exit)
    }

    /// Build the runtime and apply this application's renderer settings to
    /// `platform`.
    #[cfg(any(feature = "desktop", feature = "web"))]
    fn prepare_platform(self, mut platform: DesktopPlatform) -> Result<(DesktopPlatform, Runtime)> {
        let feathering_enabled = self.feathering_enabled;
        let feather_width = self.feather_width;
        let external_texture_registry = self.external_texture_registry.clone();
        let initial_window_render_options = self.initial_window_render_options;
        let runtime = self.build()?;
        platform.set_feathering_enabled(feathering_enabled);
        platform.set_feather_width(feather_width);
        if let Some(registry) = external_texture_registry {
            platform.set_external_texture_registry(registry);
        }
        if let Some(options) = initial_window_render_options {
            for window_id in runtime.window_ids() {
                set_window_render_options(window_id, options);
            }
        }
        Ok((platform, runtime))
    }

    /// Like [`run`](Self::run) but invokes `on_ready` with a typed,
    /// thread-safe [`CommandSender`] once the event loop is created.
    #[cfg(any(feature = "desktop", feature = "web"))]
    pub fn run_with(self, on_ready: impl FnOnce(CommandSender)) -> Result<()> {
        self.run_with_platform_and_handle(DesktopPlatform::new(), on_ready)
    }

    #[cfg(all(target_os = "android", feature = "mobile"))]
    /// Build and run the application in an Android native activity.
    pub fn run_android(self, android_app: AndroidApp) -> Result<()> {
        self.run_android_with(android_app, |_| {})
    }

    #[cfg(all(target_os = "android", feature = "mobile"))]
    /// Run on Android and invoke `on_ready` with a cross-thread command handle.
    pub fn run_android_with(
        self,
        android_app: AndroidApp,
        on_ready: impl FnOnce(CommandSender),
    ) -> Result<()> {
        let feathering_enabled = self.feathering_enabled;
        let feather_width = self.feather_width;
        let external_texture_registry = self.external_texture_registry.clone();
        let initial_window_render_options = self.initial_window_render_options;
        let runtime = self.build()?;
        let mut platform = DesktopPlatform::new()
            .with_feathering_enabled(feathering_enabled)
            .with_feather_width(feather_width);
        if let Some(registry) = external_texture_registry {
            platform.set_external_texture_registry(registry);
        }
        if let Some(options) = initial_window_render_options {
            for window_id in runtime.window_ids() {
                set_window_render_options(window_id, options);
            }
        }
        let _ = platform.run_android_with(runtime, android_app, on_ready)?;
        Ok(())
    }

    #[cfg(not(any(feature = "desktop", feature = "web", feature = "mobile")))]
    /// Return an error when no platform event-loop feature is enabled.
    pub fn run(self) -> Result<()> {
        let _ = self;
        Err(Error::new(
            "Application::run requires the `desktop`, `web`, or `mobile` feature to provide a platform event loop",
        ))
    }
}

/// Common imports for ordinary SUI application and widget code.
/// Tooling plumbing for inspectors, tests, and benchmarks: frame timings and
/// scene statistics, inspector snapshots and traces, renderer debug captures,
/// desktop automation, and accessibility audits. Applications rarely need it.
pub mod diagnostics {
    #[cfg(any(feature = "desktop", feature = "web", feature = "mobile"))]
    pub use sui_platform::{
        AccessibilityIssue, AccessibilityIssueSeverity, AccessibilityIssueTarget,
        AccessibilitySnapshot, DebugCaptureTicket, DesktopAutomationAction,
        DesktopAutomationConfig, WindowOutputDiagnostics, request_window_debug_capture,
        take_window_debug_capture, validate_accessibility_snapshot, window_output_diagnostics,
        window_output_diagnostics_signal,
    };
    #[cfg(feature = "wgpu")]
    pub use sui_render_wgpu::{
        DebugCaptureArtifact, DebugCaptureEncoding, DebugCaptureId, DebugCaptureRequest,
        DebugCaptureStage, DebugSdrVisualization, HdrRgbaImage, OutputStrategy, fit_to_sdr,
    };
    pub use sui_runtime::{
        AsyncTaskInspectorSnapshot, COMMAND_HISTORY_LENGTH, CacheMetrics, CacheMetricsDelta,
        CommandDispatchSample, EventRouteTraceSample, FramePhase, FramePhaseSample,
        InspectorHistorySnapshot, InvalidationTraceSample, OverlayManagerSnapshot, OverlaySnapshot,
        OverlayTraceKind, OverlayTraceSample, PresentationLatencyDiagnostics,
        ReactiveInvalidationSample, RenderDiagnostics, RendererSubmissionDiagnostics,
        RetainedPacketRebuildDiagnostics, SceneStatistics, SceneStatisticsDetailMode,
        SchedulerInspectorSnapshot, TextCacheDeltaDiagnostics, TextCacheDiagnostics,
        TimerInspectorSnapshot, WidgetDiagnostic, WidgetDiagnosticsSnapshot,
        WidgetGeometrySnapshot, WidgetGraphSnapshot, WidgetNodeSnapshot, WidgetRebuildSample,
        WindowInspectorSnapshot, WindowPerformanceSnapshot, WindowPerformanceSummary,
        set_window_scene_statistics_detail_mode, window_command_dispatches_signal,
        window_performance_snapshot, window_performance_summary,
        window_scene_statistics_detail_mode,
    };
}

pub mod prelude {
    #[cfg(any(feature = "desktop", feature = "web"))]
    pub use crate::UiHandle;
    #[cfg(any(feature = "desktop", feature = "web", feature = "mobile"))]
    pub use crate::{
        FileDialogFilter, FileDialogMode, FileDialogRequest, FileDialogSelection,
        FileDialogService, FolderEntry, NativeFileDialogs, PlatformFile, PlatformFolder,
        show_file_dialog,
    };

    pub use crate::{
        ActionCard, AdaptiveBreakpoints, AdaptiveClass, AdaptiveView, Align, Alignment, AnimateCtx,
        AnimatedValue, AnimationBinding, AnimationDocument, AnimationDocumentFormatError,
        AnimationPlayer, AnimationProperty, AnimationPropertyPath, AnimationSpec,
        AnimationTargetId, AnimationTick, AnimationValue, AnimationValueKind, App, ArrangeCtx,
        AspectRatio, AspectRatioFit, AsyncWakeToken, Axis, Background, BasicSyntaxHighlighter,
        Blink, BottomSheet, Breadcrumb, BreadcrumbItem, BrowserTabBar, Brush, BrushPreview,
        BrushPreviewShape, BrushPreviewSpec, Button, ButtonAppearance, Canvas, CanvasColors,
        CanvasGridStyle, CanvasRuler, CanvasRulerAxis, CanvasRulerColors, CanvasShape,
        CanvasStroke, CanvasSurface, CanvasViewport, CanvasZoomBehavior, CanvasZoomContext,
        Checkbox, ChoiceAppearance, Clip, CollectionAnchor, CollectionAnchorGravity,
        CollectionChange, CollectionDelta, CollectionExtentIndex, CollectionModelError,
        CollectionSync, CollectionWindow, Color, ColorPalette, ColorPaletteSwatch, ColorPicker,
        ColorPickerColors, ColorSwatch, Command, CommandController, CommandCtx, CommandDelivery,
        CommandGroup, CommandKey, CommandPalette, CommandSender, CommandTarget,
        ConstraintOrientation, ConstraintQuery, ConstraintView, Constraints, ContentExtent,
        ContextMenu, ContextMenuHandle, ControlMetrics, ControlPalette, ControlSize,
        ControlTypography, CoverageDots, CoverageDotsConfig, CursorGrabMode, DateTimeInput,
        DecorativeHue, DefaultTheme, DetailRow, Dialog, Dock, DockFloatingGroup, DockLayoutError,
        DockNode, DockPanel, DockPanelId, DockWorkspace, DockWorkspaceSnapshot, DockWorkspaceState,
        DockZone, DragDropHost, DragDropScope, DragEvent, DragEventKind, DragOutcome, DragPayload,
        DragPreview, DragScopeId, DragSessionId, Draggable, DropEffect, DropEffects, DropHover,
        DropTarget, Easing, EmptyState, Error, Event, EventCtx, EventPhase, FieldAppearance,
        FieldGroup, FixedPaneSplit, Flex, FlexAlignContent, FlexBasis, FlexItem, FlexJustify,
        FlexStyle, FlexWrap, FloatingViewConfig, FloatingViewSnapshot, FloatingWorkspace,
        FloatingWorkspaceState, FocusRestorePolicy, FocusScope, FocusScopeState, FontFeature,
        FontFeatures, FontHandle, FontStretch, FontStyle, FontWeight, FormRow, FormSection,
        FrameClock, FramedField, GlowTone, Grid, GridCell, GridItem, GridPlacement, GridStyle,
        GridTrack, GridTrackMax, Icon, IconButton, IconGlyph, Image, ImageFit, ImageHandle,
        ImeEvent, Insets, InteractionPreview, Interpolate, IntrinsicSize, KeyState, KeyboardEvent,
        KeyedChildren, KeyedReconcile, KeyedStack, Keyframe, Label, LayerList, LayerListItem,
        LayerListReorderChange, LayoutTransition, Link, ListItem, ListView, LoopMode, MasterDetail,
        MasterDetailRoute, MasterDetailState, MeasureCtx, MeasuredBottomDock, Menu, MenuItem,
        Modifiers, Motion, MotionPolicy, MotionPreference, MotionScalar, MotionValue, NeutralRamp,
        NotificationCenter, NotificationHost, NotificationId, NotificationUrgency, NumberInput,
        Observable, Observer, Overflow, OverlayAlignment, OverlayCollisionPolicy,
        OverlayDismissPolicy, OverlayDismissReason, OverlayFocusBehavior, OverlayHost, OverlayKind,
        OverlayOptions, OverlayPlacement, OverlayPlacementRequest, OverlayPlacementResult,
        OverlaySide, Padding, PaintCtx, PanelSection, PasswordInput, Path, PathBuilder,
        PixelCanvas, PixelCanvasBlendMode, PixelCanvasBrushShape, PixelCanvasColors,
        PixelCanvasExportSnapshot, PixelCanvasState, PixelCanvasTool, PlacementBadge,
        PlaybackState, Point, PointerButton, PointerEvent, Popover, Presence, PresenceTransition,
        PresetStrip, Progress, ProgressBar, PropertyRow, PropertyRowLayout, Pulse, RadioButton,
        RadioGroup, RawMouseMotionEvent, RebuildOnChange, RebuildOnConstraints, Rect,
        RegisteredFont, RegisteredImage, ReorderableList, ReorderableListChange, ResourceRegistry,
        ResponsiveSidebar, ResponsiveSidebarMode, ResponsiveSidebarSnapshot,
        ResponsiveSidebarState, Result, RichAttachment, RichBlockId, RichDocumentBlock,
        RichDocumentBlockKind, RichDocumentModel, RichDocumentRenderContext,
        RichDocumentRendererRegistry, RichDocumentSnapshot, RichDocumentSpan, RichDocumentStatus,
        RichDocumentUpdate, RichDocumentView, RichDocumentViewState, RichExtensionBlock,
        RichInlineImage, RichInlineKind, RichInlineStyle, RichLink, RichListItem,
        RichSyntaxHighlighter, RichSyntaxSpan, RichSyntaxTokenKind, RichText, RichTextSourceMap,
        RichTextSourceSpan, SafeArea, SafeAreaEdges, SafeAreaInsets, ScrollAlignment, ScrollAxes,
        ScrollBar, ScrollState, ScrollView, SectionLabel, SegmentedControl, SegmentedControlItem,
        Select, SelectionChange, SelectionClipboardBehavior, SelectionEntry, SelectionIntent,
        SelectionOrder, SelectionOwnerId, SelectionPayload, SelectionPoint, SelectionScope,
        Selector, SemanticRegion, SemanticTone, SemanticsCtx, Separator, ShadowBox, ShadowParams,
        ShadowPlacement, ShapedText, SheetState, SideSheet, SideSheetPlacement, Signal,
        SimpleColorPicker, SimpleColorPickerMode, SingleChild, Size, SizedBox, Slider, SourceId,
        Spinner, SplitExtent, SplitPaneSide, SplitState, SplitStateSnapshot, SplitView, SpringF32,
        SpringSpec, Stack, Stagger, StaggerOrigin, StatusBar, StatusBarHost, StatusBarSegment,
        StrokeStyle, Subscription, Surface, SurfaceAppearance, SurfaceBorder, SurfaceElevation,
        SurfacePalette, SurfaceRole, Switch, SwitchView, TabBar, TabBarItem, Table, TableColumn,
        TableColumnAlignment, TableRow, Tabs, Task, TaskHandle, TaskState, TextArea, TextCommand,
        TextDocument, TextInput, TextLayout, TextMeasurement, TextParagraph, TextParagraphStyle,
        TextSelectionInfo, TextSpan, TextSpanId, TextStyle, TextWrap, Theme, ThemeAspectRatios,
        ThemeBlurScale, ThemeBreakpoints, ThemeColorScheme, ThemeColors, ThemeContainers,
        ThemeDensity, ThemeExtension, ThemeExtensions, ThemeFontFamilies, ThemeFontStack,
        ThemeFontWeights, ThemeGlows, ThemeLeading, ThemeMotion, ThemePerspective, ThemeRadii,
        ThemeShadow, ThemeShadowLayer, ThemeShadows, ThemeTextScale, ThemeTextToken, ThemeTracking,
        Timeline, TimelineMarker, TimelinePlayer, TimelineSnap, TimelineTick, TimerToken,
        ToolPalette, ToolPaletteItem, Toolbar, Tooltip, TooltipAlignment, TooltipPlacement, Track,
        TrailingSlotRow, Transform, TransientNotification, Transition, TreeItem, TreeView,
        UrlPolicy, VirtualCollectionModel, VirtualCollectionSource, VirtualList, VirtualListChrome,
        VirtualListSelectionMode, VirtualListState, VirtualScrollView, VirtualTable,
        VirtualTableColumn, VirtualTableRowActivationKind, VirtualTableRowContext,
        VirtualTableSortDirection, VirtualTableState, VirtualViewportSnapshot, WakeEvent,
        WeakObserver, Widget, WidgetChildren, WidgetPod, WidgetShader, Window, WindowCursorState,
        WindowRenderOptions, paint_text, paint_text_line, paint_text_line_elided,
    };
    pub use crate::{
        ElidedLine, Paragraph, TextAlign, TextOverflow, TextPlacement, TextShaper, VerticalAlign,
    };
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::{DefaultTheme, HdrThemeMode, HdrThemeTokens, Theme};
    #[cfg(feature = "wgpu")]
    use crate::{
        Application, WgpuExternalTextureRegistry, WindowColorManagementMode,
        WindowDynamicRangeMode, WindowOutputColorPrimaries, WindowRenderOptions,
        WindowToneMappingMode,
    };

    #[derive(Debug, PartialEq)]
    struct CustomWidgetTheme {
        radius: f32,
        density: u8,
    }

    #[test]
    fn theme_stores_default_widget_theme_separately_from_extensions() {
        let mut defaults = DefaultTheme::default();
        defaults.metrics.min_height = 24.0;

        let theme = Theme::new()
            .with_default_widgets(defaults)
            .with_extension(CustomWidgetTheme {
                radius: 6.0,
                density: 2,
            });

        assert_eq!(theme.default_widgets.metrics.min_height, 24.0);
        assert!(theme.has_extension::<CustomWidgetTheme>());
        assert_eq!(
            theme.extension::<CustomWidgetTheme>(),
            Some(&CustomWidgetTheme {
                radius: 6.0,
                density: 2,
            })
        );
    }

    #[test]
    fn theme_extensions_round_trip_hdr_theme_tokens() {
        let mut defaults = DefaultTheme::dark();
        defaults.metrics.min_height = 30.0;
        defaults.hdr.mode = HdrThemeMode::WideGamutOnly;

        let hdr_tokens = HdrThemeTokens {
            mode: HdrThemeMode::FullHdr,
            ..HdrThemeTokens::default()
        };

        let theme = Theme::new()
            .with_default_widgets(defaults)
            .with_extension(hdr_tokens);

        assert_eq!(theme.default_widgets.metrics.min_height, 30.0);
        assert_eq!(theme.default_widgets.hdr.mode, HdrThemeMode::WideGamutOnly);
        assert!(theme.has_extension::<HdrThemeTokens>());
        assert_eq!(theme.extension::<HdrThemeTokens>(), Some(&hdr_tokens));
        assert_eq!(
            Arc::as_ref(
                &theme
                    .extension_arc::<HdrThemeTokens>()
                    .expect("hdr theme tokens extension present")
            ),
            &hdr_tokens
        );
    }

    #[test]
    fn theme_extensions_support_arc_access_and_removal() {
        let mut theme = Theme::new();
        theme.insert_extension(CustomWidgetTheme {
            radius: 12.0,
            density: 3,
        });

        let extension = theme
            .extension_arc::<CustomWidgetTheme>()
            .expect("custom widget theme present");
        assert_eq!(
            Arc::as_ref(&extension),
            &CustomWidgetTheme {
                radius: 12.0,
                density: 3,
            }
        );

        let removed = theme
            .remove_extension::<CustomWidgetTheme>()
            .expect("custom widget theme removed");
        assert_eq!(
            Arc::as_ref(&removed),
            &CustomWidgetTheme {
                radius: 12.0,
                density: 3,
            }
        );
        assert!(!theme.has_extension::<CustomWidgetTheme>());
    }

    #[test]
    fn prelude_exports_rich_text_document_types() {
        use crate::prelude::*;

        let style = TextStyle::new(Color::WHITE);
        let document = TextDocument {
            paragraphs: vec![TextParagraph::from_spans(vec![TextSpan::new(
                "Hello rich text",
                style,
            )])],
        };
        let _widget = RichText::new(document);
        let _theme = DefaultTheme::default().with_density(ThemeDensity::Compact);
    }

    #[cfg(feature = "wgpu")]
    #[test]
    fn application_feather_width_is_configurable() {
        let options = WindowRenderOptions::new(true, 1.5)
            .with_color_management_mode(WindowColorManagementMode::PreferHdr)
            .with_output_color_primaries(WindowOutputColorPrimaries::DisplayP3)
            .with_dynamic_range_mode(WindowDynamicRangeMode::HighDynamicRange)
            .with_tone_mapping_mode(WindowToneMappingMode::Reinhard);
        let app = Application::new()
            .with_feathering_enabled(false)
            .with_feather_width(2.25)
            .with_window_render_options(options);
        let clamped = Application::new().with_feather_width(-1.0);

        assert!(!app.feathering_enabled());
        assert_eq!(app.feather_width(), 2.25);
        assert_eq!(clamped.feather_width(), 0.0);
        assert_eq!(app.initial_window_render_options(), Some(options));
    }

    #[cfg(feature = "wgpu")]
    #[test]
    fn application_accepts_external_texture_registry() {
        let registry = WgpuExternalTextureRegistry::new();
        let app = Application::new().with_external_texture_registry(registry.clone());

        assert!(app.external_texture_registry().is_some());
        assert!(registry.context().is_none());
    }
}