Uno.Themes

repository·master·Indexed 41 days ago

https://github.com/unoplatform/uno.themes

A collection of NuGet packages providing Material Design 3 and Cupertino design systems for Uno Platform applications. It includes styling libraries such as Uno.Material and Uno.Cupertino, offering comprehensive color systems, design tokens for typography and spacing, and pre-defined styles for WinUI controls across platforms including iOS, Android, WebAssembly, macOS, Linux, and Windows.

Tokens
173.7K
Snippets
221
Records
394
Agent score
48%

What's inside Uno.Themes

  1. Overview of Uno.Themes

    master

    Uno.Themes provides NuGet add-on packages that can be integrated into any new or existing Uno Platform solution to apply specific design systems. It offers two primary styling libraries:

    Both libraries provide a comprehensive color system for Light and Dark themes and include pre-defined styles for standard WinUI controls (e.g., Buttons, TextBox).

  2. Overview of Uno Themes libraries

    master

    Uno.Themes provides add-ons that can be integrated into any new or existing Uno solution. The ecosystem is composed of four primary libraries:

    1. Uno Themes: The core library containing base resources, extensions, and helper classes used by all design system libraries.
    2. Uno Material: Implements the Material 3 Design System.
    3. Uno Cupertino: Implements Apple's Human Interface Guidelines.
    4. Uno Simple: Implements the Figma Simple Design System.

    These libraries provide color systems for both Light and Dark themes, customizable via seed colors or key-by-key overrides, and styles for standard WinUI controls (e.g., Buttons, TextBox).

  3. What is Lightweight Styling in Uno Themes

    master

    Lightweight styling is a method to customize XAML control appearances by overriding their default brushes, fonts, and numeric properties using resource keys. Instead of redefining an entire Style, you provide alternate resources with the same key.

    All Uno Material styles support this capability. This approach is "surgical"—you change one key at a time. Note that lightweight overrides always take precedence over values generated from a seed color.

  4. Overview of Material Button Styles

    master

    Material Design 3 defines 6 button variants used in Uno.Themes. These variants are distinguished by their visual emphasis and purpose:

    1. Filled Button (MaterialFilledButtonStyle): Primary action.
    2. Filled Tonal Button (MaterialFilledTonalButtonStyle): Secondary action.
    3. Elevated Button (MaterialElevatedButtonStyle): Important but not primary.
    4. Outlined Button (MaterialOutlinedButtonStyle): Medium emphasis.
    5. Text Button (MaterialTextButtonStyle): Low emphasis.
    6. Icon Button (MaterialIconButtonStyle): Icon-only actions.

    Reference the source Button.xaml for the full implementation details.

  5. How spacing and density compose in Uno.Themes

    master

    In Uno.Themes, spacing and density are orthogonal axes that compose to determine the effective base spacing unit. Instead of density being a fixed value, it acts as a multiplier (mode) applied to a base spacing value.

    The Composition Formula

    Effective Base = DefaultSpacing × Density factor

    1. DefaultSpacing

    DefaultSpacing is a Dependency Property (DP) on BaseTheme. It defines the base unit for the Space* scale.

    • Default value: 4.
    • Validation: Non-finite (NaN, Infinity) or negative values automatically degrade to the default base of 4 to prevent poisoning the scale.

    2. Density (Mode)

    Density is a mode that scales the DefaultSpacing. It does not have its own underlying base-unit values.

    • Compact: Multiplier of 0.75
    • Regular: Multiplier of 1
    • Comfy: Multiplier of 1.25
    • Undefined/Other: Multiplier of 1

    Example Composition

    If DefaultSpacing is set to 6:

    • Compact: $6 imes 0.75 = 4.5$
    • Regular: $6 imes 1 = 6$
    • Comfy: $6 imes 1.25 = 7.5$

    Invariant Tokens

    Certain tokens are fixed and do not change regardless of DefaultSpacing or Density settings:

    • ControlHeight*
    • IconSize*
    • TouchTargetMinSize
    • Shape tokens (e.g., CornerRadius) are also unaffected by DefaultSpacing.
  6. Understand Material v2 sizing and design tokens

    master

    Material v2 sizing, spacing, and corner-radius resources are no longer hard-coded literals; they are now aliases for global design tokens. This allows the new DefaultSpacing, DefaultDensity (Compact / Regular / Comfy), and DefaultCornerRadius theme properties to reshape all Material controls globally.

    Example of resource aliasing:

    • ButtonMinHeight (40) now aliases ControlHeightMedium.
    • ButtonPadding (16,0) now aliases Space400HorizontalThickness.
    • ButtonCornerRadius (20) now aliases Radius500CornerRadius.
  7. Understand the mapping from Simple SDS brushes to Shared Semantic Brushes

    master

    When migrating or working with the Simple theme, you must understand how the legacy SDS (Simple Design System) brushes map to the new Shared Semantic Brush system.

    Key Mapping Principles

    • Brand maps to Primary (primary action colors).
    • Danger maps to Error (destructive/error actions).
    • Default maps to Surface / OnSurface (neutral UI chrome).
    • Neutral maps to SurfaceInverse / OnSurfaceVariant (muted secondary chrome).
    • Disabled maps to *DisabledBrush variants (uses opacity-based logic).
    • Utilities are generally kept as Simple-specific (non-semantic tools).

    State Mapping: Opacity vs. Explicit Colors

    A major architectural shift is how interaction states (hover/pressed) are handled:

    • SDS Pattern: Uses distinct, hard-coded color values (e.g., Brand800 $\rightarrow$ Brand900 $\rightarrow$ Brand1000).
    • Shared Pattern: Uses a single base color with opacity multipliers (e.g., PrimaryBrush with a hover overlay).

    This change means that while BrandDefault might match a specific color, BrandHover and BrandPressed are now achieved via overlays rather than distinct shade values.

  8. How in-process app hosting works with ALC

    master

    Uno 6.7-dev introduces a secondary-AssemblyLoadContext (ALC) feature that allows one Uno application (the host) to host another Uno application (the guest) in-process. This is achieved through the following mechanism:

    1. Visual Tree Integration: The host places an Uno.UI.Xaml.Controls.AlcContentHost (a ContentControl) in its visual tree and assigns it via Uno.UI.Xaml.WindowHelper.ContentHostOverride.
    2. Assembly Loading: The host loads the guest app's assemblies into a collectible ALC. To maintain type identity, Uno framework assemblies are shared from the default ALC.
    3. Bootstrapping: The host builds a second UnoPlatformHostBuilder around the guest's Application instance and executes it (on desktop, this runs on a dedicated background thread; on WASM, it uses RunAsync).
    4. Redirection: The guest's Window.Content is transparently redirected into the host's AlcContentHost.

    Isolation Guarantees:

    • Resource Scoping: Per-ALC resource-dictionary registration is supported. If the guest project opts in via <UnoEnableAlcAppSupport>true</UnoEnableAlcAppSupport>, all ms-appx:/// dictionaries are scoped to that specific ALC.
    • Theme Isolation: The host-wins resource resolution ensures guest resources do not recolor the host's chrome. The guest's RequestedTheme is pinned at the AlcContentHost boundary.
    • Memory Management: A full static-cache purge (including DP, Style, metadata, and resource-resolver) occurs on unload so the ALC can be collected.
    // Conceptual workflow for a host
    // 1. Place host in tree
    var host = new AlcContentHost();
    
    // 2. Set the override
    WindowHelper.ContentHostOverride = host;
    
    // 3. Build and run guest
    var guestApp = // ... loaded from collectible ALC
    var builder = new UnoPlatformHostBuilder().Configure(guestApp);
    await builder.Build().StartAsync();
  9. Configure Seed Color Generation Modes

    master

    The SeedColorMode property on ThemeColors determines how the palette is generated. There are two modes:

    1. Fidelity (Default): Prioritizes brand accuracy. In Light mode, the PrimaryColor is your seed color verbatim. The supporting palettes match the saturation/character of your seed. Use this when exact brand colors are required.
    2. TonalSpot: Uses the standard Material Design 3 recipe. It enforces a minimum saturation, ensuring the theme remains vibrant even if your seed color is muted or gray. Use this for a classic, colorful Material look.

    To use TonalSpot, set the SeedColorMode attribute in your XAML configuration.

    <MaterialTheme xmlns="using:Uno.Material">
        <MaterialTheme.Colors>
            <ut:ThemeColors xmlns:ut="using:Uno.Themes"
                            PrimarySeed="#6750A4"
                            SeedColorMode="TonalSpot" />
        </MaterialTheme.Colors>
    </MaterialTheme>
  10. How seed-based color generation works

    master

    The color generation follows the Material Design 3 system using the HCT (Hue-Chroma-Tone) color space.

    Palette Derivation

    Using the TonalSpot scheme, the seed color's HCT values are used to derive palettes with fixed chroma values to ensure visual consistency:

    • Primary: Uses seed Hue and max(Chroma, 48).
    • Secondary: Uses seed Hue and Chroma 16.
    • Tertiary: Uses seed Hue + 60 and Chroma 24.
    • Neutral: Uses seed Hue and Chroma 4.
    • Neutral Variant: Uses seed Hue and Chroma 8.

    Accessibility

    Color roles are mapped to specific Tones (perceptual lightness) to guarantee WCAG AA contrast (minimum 4.5:1). For example, a Light Theme Primary role uses Tone 40, while its OnPrimary counterpart uses Tone 100, achieving ~7:1 contrast.