KAS GUI

repository·master·Indexed 21 days ago

https://github.com/kas-gui/kas

A high-performance, pure-Rust GUI toolkit featuring stateful widgets, automatic layout, and pixel-perfect scaling. Version 0.17.0 provides a modular ecosystem including kas-core, kas-widgets, kas-view for virtual scrolling, kas-image for vector and raster graphics, and multiple rendering backends such as kas-wgpu (GPU) and kas-soft (CPU via softbuffer). It supports complex text rendering with BiDi and glyph fallbacks, custom theming, and keyboard accessibility.

Tokens
52.4K
Snippets
153
Records
222
Agent score
76%

What's inside kas

  1. Overview of KAS Image

    master

    The kas-image crate provides widgets and utilities for handling both scalable (vector) and raster images within the KAS ecosystem. It relies on the following core libraries for image processing and rendering:

    • tiny-skia: For 2D graphics rendering.
    • resvg: For SVG rendering.

    For specific feature flag configurations, refer to the Cargo.toml file in the crate root.

  2. Overview of KAS GUI

    master

    KAS GUI is a fast, Rust-based GUI system designed to balance simplicity for basic tasks with power for complex custom widgets. It focuses on automatic layout, pixel-perfect scaling, and high-performance monolithic binaries.

    Key Capabilities

    • Accessibility: Fully keyboard-accessible with partial screen reader support.
    • Text Rendering: Complex text support including system fonts with glyph fallbacks, BiDi, and common text effects.
    • Layout: Automatic margins and layout with pixel-perfect scaling.
    • Theming: Support for custom themes, including theme-driven animations and sizing.
    • Performance: Virtual scrolling (lists/grids) with support for async data access.

    Important Considerations

    • Stability: The project is currently in development; interfaces are not yet stable.
    • State Management: Stateful widgets are supported, which means developers must be mindful of potential invalid-state bugs.
    • Macros: Custom widget definitions use macro-enhanced Rust, which provides full Rust power but may cause issues with some tooling like rustfmt or rust-analyzer.
  3. Use the Kas CPU-rendering backend (kas-soft)

    master

    The kas-soft crate provides a CPU-rendering backend for KAS using softbuffer. It is intended as an alternative to the kas-wgpu GPU-accelerated backend.

    Current Limitations

    When using kas-soft, be aware of the following constraints:

    • Theme Support: Only the SimpleTheme theme is currently supported.
    • Visual Quality: Lines may lack anti-aliasing and rounding, making them appear less smooth than in kas-wgpu.
    • Font Rendering: Fonts may appear slightly darker due to differences in blending compared to kas-wgpu.
    • Performance: While performance is generally acceptable, RGBA texture blending is relatively slow.
  4. Use the KAS Widgets library

    master
    KAS Widgets is the dedicated widget library for the KAS ecosystem. It provides UI components designed to work within KAS applications. For specific feature availability and configuration, refer to the feature flags defined in the project's Cargo.toml.
  5. Use View Widgets for shared data

    master
    Introduced in version 0.7.0, view widgets provide a framework for creating interactive views over shared data. They allow synchronized access to the same data from multiple locations in the UI. This approach is scalable because it avoids the need to create dedicated, unique widgets for every individual item in a large dataset.
  6. Understand the KAS crate ecosystem

    master

    The project is organized into several specialized crates. While the kas meta-package provides a convenient driver for most use cases, you can use specific crates for granular control:

    • kas: Meta-package and convenient driver.
    • kas-core: The core library.
    • kas-macros: Proc-macro crate used by kas-core.
    • kas-widgets: The main widget library.
    • kas-view: View widgets supporting virtual scrolling.
    • kas-image: Scalable and raster image widgets and utilities.
    • kas-wgpu: Rendering backend over wgpu.
    • kas-soft: Basic CPU rendering backend over softbuffer.
    • kas-dylib: Helper crate for dynamic linking.
  7. Understand the KAS Input Data model

    master

    As of version 0.14.0, KAS uses an Input Data model to enable declarative widget trees. Instead of manually updating widgets when state changes, widgets now have a Data associated type. This data is passed by reference to event handlers and a new update method.

    Key benefits:

    • Declarative Trees: A widget (like a label) can be defined as a function of the data it displays. For example, a label can automatically reflect a counter's value without the parent needing to explicitly call an update method on the label.
    • Decoupled Hierarchy: Parent nodes no longer need to refer to children by specific names or fields, reducing the need for complex structs with named fields.
    • Integrated View Widgets: The SingleView widget is now redundant as view widgets are tightly integrated into this data-driven flow.
  8. Emit warnings using the `nightly` feature

    master
    Procedural macros in KAS can emit error messages on stable Rust. However, if you want to emit warnings via the compiler, you must use a nightly rustc and enable the nightly feature. On stable Rust, these warning-specific capabilities are not available.
  9. How to use KAS procedural macros

    master

    To use the procedural macros provided by KAS, do not depend on the kas-macros crate directly. Instead, depend on the main kas crate, which re-exports these macros in its public API. This ensures you are using the version of the macros intended for the KAS ecosystem.

    # Do this
    [dependencies]
    kas = "0.17.0"
    
    # Avoid this
    [dependencies]
    kas-macros = "0.17.0"
  10. Explore KAS UI examples

    master

    KAS provides a variety of examples categorized by complexity to demonstrate different UI capabilities, ranging from simple message boxes to complex GPU-accelerated graphics.

    Simple Examples

    • Hello: A basic custom message box.
    • Counter: An interactive example using push-buttons. The sync-counter variant demonstrates synchronizing state across two windows.
    • Cursors: Displays available mouse cursors provided via winit.
    • Layout: Demonstrates complex layouts and multi-paragraph text rendering.
    • Splitter: Shows how to implement resizable panes.

    Intermediate Examples

    • Calculator: Showcases grid layouts and keyboard support.
    • Stopwatch: A timer-based UI.
    • Sync-counter: Uses the SingleView widget to share data between multiple windows.
    • Clock: Demonstrates mid-level drawing routines rendered over a transparent window.
    • Times-tables: Demonstrates the GridView widget.
    • Data list: Shows two ways to handle lists:
      • data-list: Allocates a widget for every entry (scales to hundreds/thousands of entries).
      • data-list-view: Uses a dynamic view over a lazily-allocated structure for performance independent of list size.
    • Proxy: Demonstrates how to update the UI in response to events coming from a background thread.

    Advanced Examples

    • Gallery: A comprehensive testbed covering most widgets, animations, data models, canvas, and configuration.
    • Mandlebrot: Demonstrates GPU-accelerated fractals using a custom embedded WGPU graphics pipeline.
  11. Compile GLSL shaders for KAS WGPU

    master

    KAS WGPU uses GLSL shaders. While pre-compiled SPIR-V modules are provided for standard use, you will need a compiler if you intend to work on or modify the GLSL shaders yourself.

    To compile shaders manually, use a tool like glslc (from the shaderc project). Alternatively, you can use web-based tools like glslang.js.

    To enable automatic re-compilation during the build process, set the SHADERC environment variable to point to your compiler (e.g., glslc).

    # Enable automatic shader re-compilation
    export SHADERC=glslc
    # Then run your build command
    cargo build