#![forbid(unsafe_code)]
use std::{
any::{Any, TypeId},
collections::HashMap,
fmt,
sync::Arc,
};
pub mod app;
pub mod composites;
pub mod containers;
pub mod controls;
#[cfg(any(feature = "desktop", feature = "web", feature = "mobile"))]
pub use app::UiHandle;
pub use app::{App, ResourceRegistry, Window};
pub use composites::{
ActionTilePaint, BottomSheet, BrowserTabBar, BusyIndicator, CalloutPaint, CodePanelPaint,
CodeTextLine, CodeTextPaint, CodeTextSpan, CommandButtonFill, CommandButtonPaint, CommandGroup,
CommandPalette, ContextMenu, Dialog, DisclosureButtonPaint, DockPanel, Drawer, EmptyState,
EmptyStatePaint, FieldGroup, FormRow, FormSection, FramedField, HairlineEdge, Menu, MenuItem,
Modal, PanelSection, Popover, PopoverAlignment, PresetStrip, ProgressBar, PropertyRow,
PropertyRowLayout, SectionLabel, SectionLabelPaint, SectionPanelGeometry, SectionPanelPaint,
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,
RebuildOnChange, RebuildOnConstraints, ScrollAxes, ScrollBar, ScrollState, ScrollView,
SemanticRegion, SizedBox, Stack, SwitchView, TrailingSlotRow, VirtualScrollView,
};
pub use controls::{
BUILTIN_ICON_GLYPHS, Button, ButtonAppearance, Checkbox, CheckboxIndicatorState,
ChoiceAppearance, ComboBox, DateTimeInput, Divider, FieldAppearance, Icon, IconButton,
IconButtonPaint, IconGlyph, Label, Link, MultilineTextInput, NumberInput, PasswordInput,
RadioButton, RadioGroup, Select, Separator, Slider, SpinBox, Switch, TextArea, TextInput,
draw_glyph, paint_checkbox_indicator, paint_icon_button, register_builtin_icon_resources,
};
pub use sui_core::{
AsyncWakeToken, Clipboard, ClipboardBackend, Color, ColorSpace, CursorGrabMode, CustomEvent,
DirtyRegion, DpiInfo, DragDropScope, DragEvent, DragEventKind, DragOutcome, DragPayload,
DragPreview, DragScopeId, DragSessionId, DropEffect, Error, Event, FontHandle, ImageHandle,
ImeEvent, InvalidationKind, InvalidationRequest, InvalidationTarget, KeyState, KeyboardEvent,
LocalClipboardBackend, Modifiers, 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, 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,
Padding, arrange_flex, flex_layout, grid_layout,
};
#[cfg(all(target_os = "android", feature = "mobile"))]
pub use sui_platform::AndroidApp;
#[cfg(any(feature = "desktop", feature = "web", feature = "mobile"))]
pub use sui_platform::{
AccessibilityIssue, AccessibilityIssueSeverity, AccessibilityIssueTarget,
AccessibilitySnapshot, DesktopAutomationAction, DesktopAutomationConfig, DesktopExtension,
DesktopExtensionContext, DesktopPlatform, DesktopWindow, FileDialogFilter, FileDialogFuture,
FileDialogMode, FileDialogRequest, FileDialogSelection, FileDialogService, HeadlessPlatform,
NativeFileDialogs, PlatformFile, PlatformWindow, Waker, WindowOutputDiagnostics,
show_file_dialog, validate_accessibility_snapshot, window_output_diagnostics,
};
pub use sui_reactive::{
Change as ObservableChange, Observable, Observer, Selector, Signal, SourceId, Subscription,
WeakObserver,
};
#[cfg(feature = "wgpu")]
pub use sui_render_wgpu::{
RendererCapabilities, RendererInterop, StemDarkening, TextCoveragePolicy, TextHinting,
WgpuExternalTextureContext, WgpuExternalTextureRegistry, WgpuRenderer,
};
pub use sui_runtime::{
Application as RuntimeApplication, ArrangeCtx, AsyncTaskInspectorSnapshot, CacheMetrics,
CacheMetricsDelta, Command, CommandController, CommandCtx, CommandDelivery,
CommandDispatchSample, CommandKey, CommandSender, CommandTarget, DEFAULT_SUI_LOGO_SVG,
EXTERNAL_WAKE_KIND, EmbeddedSvgImageResource, EventCtx, EventPhase, EventRoutePhase,
EventRouteStep, EventRouteTraceSample, FocusRestorePolicy, FocusScope, FocusScopeState,
FocusState, FramePhase, FramePhaseSample, FrameSchedule, InspectorHistorySnapshot,
InvalidationTraceSample, KeyedChildren, KeyedReconcile, LayerOptions, MeasureCtx,
OVERLAY_DISMISS_REQUEST, OverlayDismissPolicy, OverlayDismissReason, OverlayDismissRequest,
OverlayFocusBehavior, OverlayInitialFocus, OverlayKind, OverlayManagerSnapshot,
OverlayModality, OverlayOptions, OverlaySnapshot, OverlayTraceKind, OverlayTraceSample,
PaintBoundaryMode, PaintCtx, PresentationLatencyDiagnostics, REACTIVE_CHANGED,
ReactiveInvalidationSample, RenderDiagnostics, RenderOutput, RendererSubmissionDiagnostics,
RetainedPacketRebuildDiagnostics, Runtime, SceneStatistics, SceneStatisticsDetailMode,
SchedulerInspectorSnapshot, SemanticsCtx, SingleChild, StackHostOptions, StackOrderPolicy,
StackSurfaceOptions, TextCacheDeltaDiagnostics, TextCacheDiagnostics, TimerInspectorSnapshot,
Widget, WidgetChildren, WidgetDiagnostic, WidgetDiagnosticsCtx, WidgetDiagnosticsSnapshot,
WidgetGeometrySnapshot, WidgetGraphSnapshot, WidgetNodeSnapshot, WidgetPod,
WidgetPodMutVisitor, WidgetPodVisitor, WidgetRebuildSample, WindowBuilder,
WindowColorManagementMode, WindowCursorState, WindowDynamicRangeMode, WindowIcon,
WindowInspectorSnapshot, WindowOutputColorPrimaries, WindowPerformanceSnapshot,
WindowPerformanceSummary, WindowRenderOptions, WindowStemDarkening, WindowTextCoveragePolicy,
WindowTextHinting, WindowTextSubpixelOrder, WindowToneMappingMode, clear_window_render_options,
default_sui_logo_image, set_window_render_options, set_window_scene_statistics_detail_mode,
window_performance_snapshot, window_performance_summary, window_render_options,
window_scene_statistics_detail_mode,
};
pub use sui_scene::{
Border, Brush, GradientStop, ImageRegistry, ImageSource, RegisteredExternalImage,
RegisteredImage, RegisteredImageFormat, Scene, SceneCommand, SceneFrame, ShadowParams,
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,
AnimationTargetId, AnimationTick, AnimationValue, AnimationValueKind, Blink, Clip,
CompiledClip, CompiledTimeline, CompiledTrack, Easing, Interpolate, Keyframe,
KeyframeSelection, LoopMode, MotionScalar, PlaybackState, Pulse, SampleBatch, SampleBuffer,
SampledAnimationValue, SharedCompiledTimeline, SpringF32, Timeline, TimelineBindingSink,
TimelinePlayer, TimelineSnap, TimelineTick, Track, Transition,
invalidation_for_animation_property,
};
pub use sui_widgets::{
ActionCard, AdaptiveBreakpoints, AdaptiveClass, AdaptiveView, AspectRatio, AspectRatioFit,
BasicSyntaxHighlighter, Breadcrumb, BreadcrumbItem, BrushPreview, BrushPreviewShape,
BrushPreviewSpec, Canvas, CanvasAppearance, CanvasGridStyle, CanvasRuler,
CanvasRulerAppearance, CanvasRulerAxis, CanvasShape, CanvasStroke, CanvasSurface,
CanvasViewport, CanvasZoomBehavior, CanvasZoomContext, CollectionAnchor,
CollectionAnchorGravity, CollectionChange, CollectionDelta, CollectionExtentIndex,
CollectionModelError, CollectionSync, CollectionWindow, ColorPalette, ColorPaletteSwatch,
ColorPicker, ColorPickerAppearance, ColorSwatch, ConstraintOrientation, ConstraintQuery,
ConstraintView, ControlMetrics, ControlPalette, ControlSize, ControlTypography, CoverageDots,
CoverageDotsConfig, DataGrid, DefaultTheme, DetailRow, DockFloatingGroup, DockLayoutError,
DockNode, DockPanelId, DockWorkspace, DockWorkspaceSnapshot, DockWorkspaceState, DockZone,
DragDropHost, Draggable, DropTarget, EffectToken, FloatingStack, FloatingViewConfig,
FloatingViewSnapshot, FloatingWorkspace, FloatingWorkspaceState, Grid, GridCell, HdrColorRoles,
HdrEffectTokens, HdrLuminanceTokens, HdrMaterialTokens, HdrPolicyTokens, HdrThemeMode,
HdrThemeTokens, Image, ImageFit, LayerList, LayerListItem, LayerListReorderChange,
LayoutTransition, LeadingLabelCellPaint, ListItem, ListView, MasterDetail, MasterDetailRoute,
MasterDetailState, MaterialToken, NotificationCenter, NotificationHost, NotificationId,
NotificationUrgency, OverlayAlignment, OverlayCollisionPolicy, OverlayHost, OverlayPlacement,
OverlayPlacementRequest, OverlayPlacementResult, OverlaySide, PathBar, PixelCanvas,
PixelCanvasAppearance, PixelCanvasBlendMode, PixelCanvasBrushShape, PixelCanvasExportSnapshot,
PixelCanvasState, PixelCanvasTool, PlacementBadge, PlacementBadgePaint, ReorderableList,
ReorderableListChange, ResizablePane, 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, TextSurfaceStyleOverlay, TextSurfaceStyleSpan,
ThemeAspectRatios, ThemeBlurScale, ThemeBreakpoints, ThemeColorScheme, ThemeColors,
ThemeContainers, ThemeDensity, ThemeFontFamilies, ThemeFontStack, ThemeFontWeights,
ThemeLeading, ThemeMotion, ThemePerspective, ThemeRadii, ThemeShadow, ThemeShadowLayer,
ThemeShadows, ThemeTextScale, ThemeTextToken, ThemeTracking, 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_aligned_text,
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_single_line_aligned_text, paint_status_badge, paint_text_block, paint_text_cell,
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 trait ThemeExtension: Any + Send + Sync {}
impl<T> ThemeExtension for T where T: Any + Send + Sync {}
#[derive(Clone, Default)]
pub struct ThemeExtensions {
values: HashMap<TypeId, Arc<dyn Any + Send + Sync>>,
}
impl ThemeExtensions {
pub fn new() -> Self {
Self::default()
}
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())
}
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>())
}
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())
}
pub fn contains<T>(&self) -> bool
where
T: ThemeExtension,
{
self.values.contains_key(&TypeId::of::<T>())
}
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())
}
pub fn is_empty(&self) -> bool {
self.values.is_empty()
}
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()
}
}
#[derive(Debug, Clone)]
pub struct Theme {
pub background: Color,
pub foreground: Color,
pub default_widgets: DefaultTheme,
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 {
pub fn new() -> Self {
Self::default()
}
pub fn with_default_widgets(mut self, theme: DefaultTheme) -> Self {
self.default_widgets = theme;
self
}
pub fn with_extension<T>(mut self, value: T) -> Self
where
T: ThemeExtension,
{
self.extensions.insert(value);
self
}
pub fn insert_extension<T>(&mut self, value: T) -> Option<Arc<T>>
where
T: ThemeExtension,
{
self.extensions.insert(value)
}
pub fn extension<T>(&self) -> Option<&T>
where
T: ThemeExtension,
{
self.extensions.get::<T>()
}
pub fn extension_arc<T>(&self) -> Option<Arc<T>>
where
T: ThemeExtension,
{
self.extensions.get_arc::<T>()
}
pub fn has_extension<T>(&self) -> bool
where
T: ThemeExtension,
{
self.extensions.contains::<T>()
}
pub fn remove_extension<T>(&mut self) -> Option<Arc<T>>
where
T: ThemeExtension,
{
self.extensions.remove::<T>()
}
}
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 {
let mut inner = RuntimeApplication::default();
sui_widgets::register_builtin_icon_resources(&mut inner)
.expect("built-in Lucide icon resources should be valid");
Self {
inner,
#[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 {
pub fn new() -> Self {
Self::default()
}
pub fn window(mut self, window: WindowBuilder) -> Self {
self.inner = self.inner.window(window);
self
}
pub fn controller(mut self, controller: impl CommandController + 'static) -> Self {
self.inner = self.inner.controller(controller);
self
}
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")]
pub fn with_feathering_enabled(mut self, enabled: bool) -> Self {
self.feathering_enabled = enabled;
self
}
#[cfg(feature = "wgpu")]
pub fn with_feather_width(mut self, feather_width: f32) -> Self {
self.feather_width = feather_width.max(0.0);
self
}
#[cfg(feature = "wgpu")]
pub fn with_external_texture_registry(mut self, registry: WgpuExternalTextureRegistry) -> Self {
self.external_texture_registry = Some(registry);
self
}
#[cfg(feature = "wgpu")]
pub fn feathering_enabled(&self) -> bool {
self.feathering_enabled
}
#[cfg(feature = "wgpu")]
pub fn feather_width(&self) -> f32 {
self.feather_width
}
#[cfg(feature = "wgpu")]
pub fn external_texture_registry(&self) -> Option<&WgpuExternalTextureRegistry> {
self.external_texture_registry.as_ref()
}
pub fn with_window_render_options(mut self, options: WindowRenderOptions) -> Self {
self.initial_window_render_options = Some(options.clamped());
self
}
pub fn initial_window_render_options(&self) -> Option<WindowRenderOptions> {
self.initial_window_render_options
}
pub fn register_font(&mut self, handle: FontHandle, font: RegisteredFont) -> Result<()> {
self.inner.register_font(handle, font)
}
pub fn register_font_bytes(&mut self, data: impl Into<Vec<u8>>) -> Result<FontHandle> {
self.inner.register_font_bytes(data)
}
pub fn register_image(&mut self, handle: ImageHandle, image: RegisteredImage) -> Result<()> {
self.inner.register_image(handle, image)
}
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)
}
pub fn register_svg_image(&mut self, data: impl AsRef<[u8]>) -> Result<ImageHandle> {
self.inner.register_svg_image(data)
}
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)
}
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)
}
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)
}
pub fn register_embedded_svg_image(
&mut self,
resource: EmbeddedSvgImageResource,
) -> Result<()> {
self.inner.register_embedded_svg_image(resource)
}
pub fn register_embedded_svg_images(
&mut self,
resources: impl IntoIterator<Item = EmbeddedSvgImageResource>,
) -> Result<()> {
self.inner.register_embedded_svg_images(resources)
}
pub fn build(self) -> Result<Runtime> {
self.inner.build()
}
#[cfg(any(feature = "desktop", feature = "web"))]
pub fn run(self) -> Result<()> {
self.run_with_platform(DesktopPlatform::new())
}
#[cfg(any(feature = "desktop", feature = "web"))]
pub fn run_with_platform(self, platform: DesktopPlatform) -> Result<()> {
self.run_with_platform_and_handle(platform, |_| {})
}
#[cfg(any(feature = "desktop", feature = "web"))]
pub fn run_with_platform_and_handle(
self,
mut platform: DesktopPlatform,
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()?;
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);
}
}
let _ = platform.run_with(runtime, on_ready)?;
Ok(())
}
#[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"))]
pub fn run_android(self, android_app: AndroidApp) -> Result<()> {
self.run_android_with(android_app, |_| {})
}
#[cfg(all(target_os = "android", feature = "mobile"))]
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")))]
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",
))
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Style {
pub foreground: Brush,
pub padding: Padding,
}
impl Default for Style {
fn default() -> Self {
let theme = Theme::default();
Self {
foreground: Brush::Solid(theme.foreground),
padding: Padding::all(0.0),
}
}
}
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, NativeFileDialogs, PlatformFile, show_file_dialog,
};
pub use crate::{
ActionCard, ActionTilePaint, AdaptiveBreakpoints, AdaptiveClass, AdaptiveView, Align,
Alignment, AnimatedValue, AnimationBinding, AnimationDocument,
AnimationDocumentFormatError, AnimationEditorCommand, AnimationEditorState,
AnimationPlayer, AnimationProperty, AnimationPropertyPath, AnimationSelection,
AnimationTargetId, AnimationTick, AnimationValue, AnimationValueKind, App, Application,
ArrangeCtx, AspectRatio, AspectRatioFit, AsyncWakeToken, Axis, Background,
BasicSyntaxHighlighter, Blink, BottomSheet, Breadcrumb, BreadcrumbItem, BrowserTabBar,
Brush, BrushPreview, BrushPreviewShape, BrushPreviewSpec, BusyIndicator, Button,
ButtonAppearance, CalloutPaint, Canvas, CanvasAppearance, CanvasGridStyle, CanvasRuler,
CanvasRulerAppearance, CanvasRulerAxis, CanvasShape, CanvasStroke, CanvasSurface,
CanvasViewport, CanvasZoomBehavior, CanvasZoomContext, Checkbox, CheckboxIndicatorState,
ChoiceAppearance, Clip, CodePanelPaint, CodeTextLine, CodeTextPaint, CodeTextSpan,
CollectionAnchor, CollectionAnchorGravity, CollectionChange, CollectionDelta,
CollectionExtentIndex, CollectionModelError, CollectionSync, CollectionWindow, Color,
ColorPalette, ColorPaletteSwatch, ColorPicker, ColorPickerAppearance, ColorSwatch,
ComboBox, Command, CommandButtonFill, CommandButtonPaint, CommandController, CommandCtx,
CommandDelivery, CommandGroup, CommandKey, CommandPalette, CommandSender, CommandTarget,
CompiledClip, CompiledTimeline, CompiledTrack, ConstraintOrientation, ConstraintQuery,
ConstraintView, Constraints, ContentExtent, ContextMenu, ControlMetrics, ControlPalette,
ControlSize, ControlTypography, CoverageDots, CoverageDotsConfig, CursorGrabMode, DataGrid,
DateTimeInput, DefaultTheme, DetailRow, Dialog, DisclosureButtonPaint, Divider, Dock,
DockFloatingGroup, DockLayoutError, DockNode, DockPanel, DockPanelId, DockWorkspace,
DockWorkspaceSnapshot, DockWorkspaceState, DockZone, DragDropHost, DragDropScope,
DragEvent, DragEventKind, DragOutcome, DragPayload, DragPreview, DragScopeId,
DragSessionId, Draggable, Drawer, DropEffect, DropTarget, Easing, EmptyState,
EmptyStatePaint, Event, EventCtx, FieldAppearance, FieldGroup, FixedPaneSplit, Flex,
FlexAlignContent, FlexBasis, FlexItem, FlexItemLayout, FlexJustify, FlexLayout,
FlexLineLayout, FlexStyle, FlexWrap, FloatingViewConfig, FloatingViewSnapshot,
FloatingWorkspace, FloatingWorkspaceState, FocusRestorePolicy, FocusScope, FocusScopeState,
FontFeature, FontFeatures, FontHandle, FontStretch, FontStyle, FontWeight, FormRow,
FormSection, FramedField, Grid, GridCell, GridItem, GridItemLayout, GridLayout,
GridPlacement, GridStyle, GridTrack, GridTrackMax, HairlineEdge, Icon, IconButton,
IconButtonPaint, IconGlyph, Image, ImageFit, ImageHandle, ImeEvent, Insets, Interpolate,
IntrinsicSize, KeyboardEvent, KeyedChildren, KeyedReconcile, Keyframe, KeyframeSelection,
Label, LayerList, LayerListItem, LayerListReorderChange, LayoutTransition,
LeadingLabelCellPaint, Link, ListItem, ListView, LoopMode, MasterDetail, MasterDetailRoute,
MasterDetailState, MeasureCtx, MeasuredBottomDock, Menu, MenuItem, Modal, MotionScalar,
MultilineTextInput, NotificationCenter, NotificationHost, NotificationId,
NotificationUrgency, NumberInput, Observable, Overflow, OverlayAlignment,
OverlayCollisionPolicy, OverlayDismissPolicy, OverlayDismissReason, OverlayFocusBehavior,
OverlayHost, OverlayKind, OverlayOptions, OverlayPlacement, OverlayPlacementRequest,
OverlayPlacementResult, OverlaySide, PaintCtx, PanelSection, PasswordInput, Path, PathBar,
PathBuilder, PixelCanvas, PixelCanvasAppearance, PixelCanvasBlendMode,
PixelCanvasBrushShape, PixelCanvasExportSnapshot, PixelCanvasState, PixelCanvasTool,
PlacementBadgePaint, PlaybackState, Point, PointerEvent, Popover, PresetStrip, ProgressBar,
PropertyRow, PropertyRowLayout, Pulse, RadioButton, RadioGroup, RawMouseMotionEvent,
RebuildOnChange, RebuildOnConstraints, Rect, RegisteredFont, RegisteredImage,
ReorderableList, ReorderableListChange, ResizablePane, 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, SampleBatch, SampleBuffer, SampledAnimationValue,
ScrollAlignment, ScrollAxes, ScrollBar, ScrollState, ScrollView, SectionLabel,
SectionLabelPaint, SectionPanelGeometry, SectionPanelPaint, SegmentedControl,
SegmentedControlItem, Select, SelectionChange, SelectionClipboardBehavior, SelectionEntry,
SelectionIntent, SelectionOrder, SelectionOwnerId, SelectionPayload, SelectionPoint,
SelectionScope, Selector, SemanticsCtx, Separator, ShapedText, SharedCompiledTimeline,
SheetState, SideSheet, SideSheetPlacement, Signal, SimpleColorPicker,
SimpleColorPickerMode, SingleChild, Size, SizedBox, Slider, SourceId, SpinBox, Spinner,
SplitExtent, SplitPaneSide, SplitState, SplitStateSnapshot, SplitView, SpringF32, Stack,
StatusBar, StatusBarHost, StatusBarSegment, StrokeStyle, Style, Surface, SurfaceAppearance,
SurfaceBorder, SurfaceElevation, SurfacePalette, SurfaceRole, Switch, SwitchView,
TEXT_COMMAND, TabBar, TabBarItem, Table, TableColumn, TableColumnAlignment, TableRow, Tabs,
TextArea, TextBlockPaint, TextCellPaint, TextCommand, TextDocument, TextInput, TextLayout,
TextMeasurement, TextParagraph, TextParagraphStyle, TextRenderCoveragePolicy,
TextRenderHinting, TextRenderMode, TextRenderPolicy, TextRenderStemDarkening,
TextSelectionInfo, TextSpan, TextSpanId, TextStyle, TextSubpixelOrder, TextWrap, Theme,
ThemeAspectRatios, ThemeBlurScale, ThemeBreakpoints, ThemeColorScheme, ThemeColors,
ThemeContainers, ThemeDensity, ThemeExtension, ThemeExtensions, ThemeFontFamilies,
ThemeFontStack, ThemeFontWeights, ThemeLeading, ThemeMotion, ThemePerspective, ThemeRadii,
ThemeShadow, ThemeShadowLayer, ThemeShadows, ThemeTextScale, ThemeTextToken, ThemeTracking,
Timeline, TimelineBindingSink, TimelinePlayer, TimelineSnap, TimelineTick, TimerToken,
ToolPalette, ToolPaletteItem, Toolbar, Tooltip, TooltipAlignment, TooltipPlacement, Track,
TrailingSlotRow, Transform, TransientNotification, Transition, TreeItem, TreeView,
VirtualCollectionModel, VirtualCollectionSource, VirtualList, VirtualListChrome,
VirtualListSelectionMode, VirtualListState, VirtualScrollView, VirtualTable,
VirtualTableColumn, VirtualTableRowActivationKind, VirtualTableRowContext,
VirtualTableSortDirection, VirtualTableState, VirtualViewportSnapshot, WakeEvent, Widget,
WidgetChildren, WidgetDiagnosticsCtx, WidgetPod, WidgetShader, Window, WindowBuilder,
WindowCursorState, WindowRenderOptions, arrange_flex, containers::Padding,
detail_row_height_for_value, flex_layout, grid_layout, invalidation_for_animation_property,
paint_action_tile, paint_border, paint_callout, paint_canvas_grid,
paint_checkbox_indicator, paint_code_lines, paint_code_panel, paint_command_button,
paint_coverage_dots, paint_coverage_dots_with_config, paint_detail_row_at,
paint_disclosure_button, paint_empty_state, paint_hairline, paint_icon_button,
paint_leading_label_cell, paint_placement_badge_with, paint_rounded_panel,
paint_rounded_rect, paint_section_label, paint_section_label_detail, paint_section_panel,
paint_text_block, paint_text_cell, place_overlay, register_builtin_icon_resources,
set_window_render_options, wrap_text_lines,
};
}
#[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());
}
}