Crate masonry

source ·
Expand description

A framework that aims to provide the foundation for Rust GUI libraries.

Masonry gives you a platform to create windows (using Glazier as a backend) each with a tree of widgets. It also gives you tools to inspect that widget tree at runtime, write unit tests on it, and generally have an easier time debugging and maintaining your app.

The framework is not opinionated about what your user-facing abstraction will be: you can implement immediate-mode GUI, the Elm architecture, functional reactive GUI, etc, on top of Masonry.

This project was originally was originally a fork of Druid that emerged from discussions I had with Raph Levien and Colin Rofls about what it would look like to turn Druid into a foundational library.

Example

The todo-list example looks like this:

use masonry::widget::{prelude::*, TextBox};
use masonry::widget::{Button, Flex, Label, Portal, WidgetMut};
use masonry::Action;
use masonry::{AppDelegate, AppLauncher, DelegateCtx, WindowDescription, WindowId};

const VERTICAL_WIDGET_SPACING: f64 = 20.0;

struct Delegate {
    next_task: String,
}

impl AppDelegate for Delegate {
    fn on_action(
        &mut self,
        ctx: &mut DelegateCtx,
        _window_id: WindowId,
        _widget_id: WidgetId,
        action: Action,
        _env: &Env,
    ) {
        match action {
            Action::ButtonPressed => {
                let mut root: WidgetMut<Portal<Flex>> = ctx.get_root();
                let mut flex = root.child_mut();
                flex.add_child(Label::new(self.next_task.clone()));
            }
            Action::TextChanged(new_text) => {
                self.next_task = new_text.clone();
            }
            _ => {}
        }
    }
}

fn main() {
    // The main button with some space below, all inside a scrollable area.
    let root_widget = Portal::new(
        Flex::column()
            .with_child(
                Flex::row()
                    .with_child(TextBox::new(""))
                    .with_child(Button::new("Add task")),
            )
            .with_spacer(VERTICAL_WIDGET_SPACING),
    );

    let main_window = WindowDescription::new(root_widget)
        .title("To-do list")
        .window_size((400.0, 400.0));

    AppLauncher::with_window(main_window)
        .with_delegate(Delegate {
            next_task: String::new(),
        })
        .log_to_console()
        .launch()
        .expect("Failed to launch application");
}

Re-exports

pub use druid_shell as shell;
pub use command::Command;
pub use command::Notification;
pub use command::Selector;
pub use command::SingleUse;
pub use command::Target;
pub use env::Env;
pub use env::Key;
pub use env::KeyOrValue;
pub use env::Value;
pub use env::ValueType;
pub use env::ValueTypeError;

Modules

Custom commands.
An environment which is passed downward into the widget tree.
Simple handle for submitting external events.
2D geometry, with a focus on curves.
A piet backend appropriate for the current platform.
Helper tools for writing unit tests.
Editing and displaying text.
Theme keys and initial values.
Common widgets.

Macros

Assert a snapshot of a rendered frame of your app.
Declare a mutable reference type for your widget.

Structs

A 2D affine transform.
Handles initial setup of an application, and starts the runloop.
State shared by all windows in the UI.
Constraints for layout.
A context provided to AppDelegate methods.
A context provided to event handling methods of widgets.
An in-memory pixel buffer.
Insets from the edges of a rectangle.
A context provided to layout handling methods of widgets.
A context provided to the lifecycle method on widgets.
A description of a linear gradient in the unit rect, which can be resolved to a fixed gradient.
The top-level handler for a window’s events.
The state of the mouse for a click, mouse-up, move, or wheel event.
A context passed to paint methods of widgets.
A 2D point.
A description of a radial gradient in the unit rect, which can be resolved to a fixed gradient.
A rectangle.
A 2D size.
A representation of a point relative to a unit rectangle.
A 2D vector.
A context provided to implementors of StoreInWidgetMut.
A container for one widget in the hierarchy.
Generic state for all widgets in the hierarchy.
Window configuration that can be applied to a WindowBuilder, or to an existing WindowHandle.
A description of a window to be instantiated.
A unique identifier for a window.
Per-window state not owned by user code.

Enums

Events from UI elements.
Something that can be used as the background for a widget.
A datatype representing color.
An event, propagated downwards during event flow.
An enum for specifying whether an event was handled.
Internal events used by Masonry inside WidgetPod.
Internal lifecycle events used by Masonry inside WidgetPod.
Application life cycle events.
Shell errors.
Event indicating status changes within the widget hierarchy.
Defines how a windows size should be determined

Traits

A type that provides hooks for handling top-level events.
Trait extending Any, implemented for all types that implement Any.
A trait used to represent cheap-to-compare value types.
The main trait for rendering graphics.

Type Definitions

A reference counted string slice.