Druid Documentation

repository·master·Indexed 27 days ago

https://github.com/linebender/druid

A data-oriented, Rust-native UI design toolkit for high-performance desktop applications. Druid utilizes a widget-based architecture and a two-way dataflow system centered around the Data, Widget, and Lens traits. It integrates with piet for 2D graphics and kurbo for geometry. Note: The project is currently unmaintained and discontinued, with development having moved to Xilem.

Tokens
13.3K
Snippets
28
Records
99
Agent score
94%

What's inside druid

  1. Important: Druid is discontinued

    master
    The Druid project has been discontinued and is no longer being actively maintained. New development efforts have moved to [Xilem], which is considered the future of Druid. Developers looking for a modern, high-performance successor should explore Xilem.
  2. Manage application state and themes with `Env`

    master

    The Env represents the application environment, used for managing and accessing resources like color schemes, localized strings, and other application-specific state. It is created at launch and passed down to all widgets.

    Key behaviors:

    • Inheritance: Values in the Env are inherited by all child widgets. If a value is overridden at a specific level in the widget tree, that new value applies to all children of that widget.
    • Immutability of Existence: Values can be overwritten with new values of the same type, but they can never be removed from the Env once set.
    • Type Safety: The Env uses typed keys. You must ensure the type used to set a value matches the type used to retrieve it via a Key to avoid runtime panics.
  3. Understand Druid's core architecture and components

    master

    Druid is a data-oriented framework for building cross-platform desktop applications. It is composed of several specialized libraries:

    • druid: An opinionated set of high-level APIs for building applications.
    • druid-shell: A low-level library providing abstractions for interacting with the OS and window manager.
    • piet: An abstraction for 2D graphics.
    • kurbo: A library for 2D geometry.
  4. Understand the Widget lifecycle and the Widget trait

    master

    The Widget trait defines the components of a Druid UI. A UI is structured as a widget tree starting from a single root widget. The Widget trait is generic over T, which represents the [Data] handled by the widget.

    Druid operates through a specific lifecycle of method calls:

    1. event: Receives an Event (e.g., key press, mouse movement). This method provides mutable access to your application model and is the only place where the model can be changed. Events are delivered recursively down the tree.
    2. update: Called after an event if the data was mutated. It receives both the new and previous data. Widgets use this to update internal state (non-model data) and can request layout or paint calls.
    3. layout: Determines widget positioning. Druid uses a box layout model where widgets are passed constraints (minimum and maximum allowed size) and must return a size within that range.
    4. paint: The imperative 2D graphics phase where the widget draws itself.
    5. lifecycle: Called in response to framework state changes (e.g., a widget gaining focus). It is not called predictably during event handling.
  5. Explore related Druid ecosystem projects

    master

    Druid relies on several foundational libraries for graphics, curves, and text layout. If you need to extend Druid's capabilities or understand its low-level primitives, explore these projects:

    • Piet: An abstraction for 2D graphics.
    • Kurbo: A Rust library for manipulating curves.
    • Skribo: A Rust library for low-level text layout.
  6. Core Druid concepts: Data, Widget, and Lens

    master

    Druid's architecture is built around three primary concepts:

    • Data trait: Used to represent your application's model.
    • Widget trait: Used to represent the user interface elements.
    • Lens trait: Used to associate specific parts of your model with specific parts of your UI.
  7. Understand the druid-shell architecture

    master

    The druid-shell architecture is split into two layers:

    1. Platform-agnostic code and types: These are the primary types exposed directly to users.
    2. Platform-specific implementations: These reside in per-backend directories in src/backend. The implementation for the active backend is re-exported via druid-shell::backend.

    To maintain a consistent API, druid-shell uses wrapper structs that define a common interface. When you call methods on these wrappers, they internally invoke the corresponding methods on the concrete backend-specific type.

  8. Use the `Controller` trait to customize widget behavior

    master

    The Controller trait is used to create widgets that handle events, updates, or lifecycle changes without performing layout or drawing. This is useful for adding logic to existing widgets (e.g., intercepting keypresses in a textbox).

    Key characteristics:

    • It implements event, update, and lifecycle methods (all are optional).
    • It does not implement paint or layout.
    • Each method receives a mutable reference to the child widget, allowing the controller to modify the child or forward events.

    Example: A controller that triggers an action after a delay following a keypress:

    // Conceptual example of a Controller implementation
    impl<W: Widget<T>, T> Controller<T, W> for MyController {
        fn event(&mut self, child: &mut W, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
            // Handle event and potentially modify child or data
            child.event(ctx, event, data, env);
        }
        // ... other optional methods
    }
  9. Install Druid dependencies on Linux and OpenBSD

    master

    Druid requires GTK-3's development kit on Linux and OpenBSD platforms.

    Linux

    • Ubuntu: Install via apt-get.
    • Fedora: Install via dnf.

    OpenBSD

    • Install via pkg_add.
    # Ubuntu
    sudo apt-get install libgtk-3-dev
    
    # Fedora
    sudo dnf install gtk3-devel glib2-devel
    
    # OpenBSD
    pkg_add gtk+3
  10. Compose UIs using modular widgets

    master

    Druid widgets are designed to be modular and composable rather than monolithic. Instead of a single widget handling its own alignment or padding, you should wrap specialized widgets in layout or decorator widgets.

    For example, to add padding to a Label, you do not configure the label itself; instead, you wrap the label in a widget that provides padding.