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

(TODO: FIX THIS 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,
    ) {
        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 widget::BackgroundBrush;
pub use widget::WidgetPod;
pub use widget::WidgetState;
pub use text_helpers::ArcStr;
pub use kurbo;
pub use parley;
pub use vello;

Modules§

app_driver
debug_logger
debug_values
event_loop_runner
paint_scene_helpers
promise
render_root
testing
Helper tools for writing unit tests.
text2
Support for text display and rendering
text_helpers
Helper functions for working with text in Masonry.
theme
Theme keys and initial values.
widget
Common widgets.

Macros§

assert_render_snapshot
Assert a snapshot of a rendered frame of your app.

Structs§

AccessCtx
AccessEvent
Affine
A 2D affine transform.
BoxConstraints
Constraints for layout.
Color
32-bit RGBA color.
EventCtx
A context provided to event handling methods of widgets.
Gradient
Definition of a gradient that transitions between two or more colors.
Insets
Insets from the edges of a rectangle.
LayoutCtx
A context provided to layout handling methods of widgets.
LifeCycleCtx
A context provided to the lifecycle method on widgets.
PaintCtx
A context passed to paint methods of widgets.
Point
A 2D point.
Rect
A rectangle.
Size
A 2D size.
Vec2
A 2D vector.
WidgetCtx
A context provided inside of WidgetMut.
WidgetId
A unique identifier for a single Widget.

Enums§

Action
Events from UI elements.
Handled
An enum for specifying whether an event was handled.
InternalLifeCycle
Internal lifecycle events used by Masonry inside WidgetPod.
LifeCycle
Application life cycle events.
PointerEvent
StatusChange
Event indicating status changes within the widget hierarchy.
TextAlignment
Alignment of a layout.
TextEvent
WindowTheme

Traits§

AsAny
Trait extending Any, implemented for all types that implement Any.
Widget
The trait implemented by all widgets.