# ARCHITECTURE **Note - The crate was migrated from the `PoignardAzur/masonry` repository and ported to work with winit. A lot of stuff was changed during that port and all the documentation wasn't updated. some of this might be outdated.** Masonry is a framework that aims to provide the foundation for Rust GUI libraries. Developers trying to write immediate-mode GUIs, Elm-architecture GUIs, functional reactive GUIs, etc, can import Masonry and get a platform to create windows (using Glazier as a backend) each with a tree of widgets. Each widget has to implement the Widget trait that Masonry provides. This crate 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. This means the code looks very similar to Druid's code and has mostly the same dependencies and primitives. ## High-level goals Masonry has some opinionated design goals: - **Be dumb.** As a general rule, Masonry doesn't do "algorithms". It has no reconciliation logic, no behind-the-scenes dataflow, no clever optimizations, etc. It tries to be efficient, but that efficiency comes from well-designed interfaces and well-placed abstraction boundaries. High-level logic should be implemented in downstream crates. - **No mutability tricks.** Masonry tries to use as little unsafe code and cells/mutexes as possible. It's designed to work within Rust's ownership system, not to bypass it. - **Facilitate testing.** Masonry implements a `TestHarness` type that helps users write unit tests, including tests with simulated user interactions and screenshot tests. In general, every feature should be designed with easy-to-write high-reliability tests in mind. - **Facilitate debugging.** GUI app bugs are often easy to fix, but extremely painful to track down. GUI framework bugs are worse. Masonry should facilitate reproducing bugs and pinpointing which bit of code they come from. - **Provide reflection.** Masonry should help developers surface some of the inherent structure in GUI programs. It should provide tools out-of-the-box to get information about the widget tree, performance indicators, etc. It should also provide accessibility data out-of-the-box. ## Code layout ### `src/testing/` Contains the TestHarness type, various helper widgets for writing tests, and the snapshot testing code. ### `src/text2/` Contains text-handling code, for both displaying and editing text. Has been overhauled during the port to winit, but still in rough shape. Here be dragons. ### `src/text/` Dead code, should probably be cleaned up. ### `src/widget/` Contains widget-related items, including the Widget trait, and the WidgetRef, WidgetMut, and WidgetPod types. Also includes a list of basic widgets, each defined in a single file. ### `src/render_root.rs` The composition root of the framework. See **General architecture** section. ### `src/debug_logger.rs`, `src/debug_values.rs` WIP logger to get record of widget passes. See . ## Module organization principles (Some of these principles aren't actually applied in the codebase yet. See .) ### Module structure Virtually every module should be private. The only public modules should be inline module blocks that gather public-facing re-exports. Most items should be exported from the root module, with no other public-facing export. This makes documentation more readable; readers don't need to click on multiple modules to find the item they're looking for. There should be only three public modules: - `widgets` - `commands` - `test_widgets` Module files should not be `foobar/mod.rs`. Instead, they should be `_foobar.rs` (thus in the parent folder); the full name is for readability, the leading underscore is so these names appear first in file hierarchies. ### Imports We should avoid `use super::xxx` imports, except for specific cases like unit tests. Most imports in the code base should use the canonical top-level import. ### No prelude Masonry should have no prelude. Examples and documentation should deliberately have a list of imports that cover everything users will need, so users can copy-paste these lists. ## General architecture ### Platform handlers The composition roots of Masonry are: - **RenderRoot** - The **AppDriver** trait. - The **run_with** function in `event_loop_runner.rs`. TODO - Explain in more detail. The high-level control flow of a Masonry app's render loop is usually: - The platform library (windows, macos, x11, etc) runs some callbacks written in Winit in response to user interactions, timers, or other events. - The callbacks call MasonryWinHandler methods. - Each method calls a single AppRoot method. - That method calls some AppRootInner method. - AppRootInner does a bunch of bookkeeping and calls WindowRoot methods. - WindowRoot does a bunch of bookkeeping and calls the root WidgetPod's on_event/lifecycle/layout/paint methods. ### Widget hierarchy and passes The **Widget** trait is defined in `src/widget/widget.rs`. Most of the widget-related bookkeeping is done by the **WidgetPod** type defined in `src/widget/widget_pod.rs`. A WidgetPod is the main way to store a child for container widgets. In general, the widget hierarchy looks like a tree of container widgets, with each container owning a WidgetPod or a Vec of WidgetPod or something similar. When a pass (on_event/lifecycle/layout/paint) is run on a window, the matching method is called on the root WidgetPod, which recurses to its widget, which calls the same method on each of its children. Currently, container Widgets are encouraged to call pass methods on each of their children, even when the pass only concerns a single child (eg a click event where only one child is under the mouse); the filtering, if any, is done in WidgetPod. The current passes are: - **on_xxx_event:** Handles UX-related events, eg user interactions, timers, and IME updates. Widgets can declare these events as "handled" which has a bunch of semantic implications. Right now this pass is split between "cursor events" (eg mouse stuff) and "text events" (eg IME, keyboard stuff). - **on_status_change:** TODO. - **lifecycle:** Handles internal events, eg when the widget is marked as "disabled". - **layout:** Given size constraints, return the widget's size. Container widgets first call their children's layout method, then set the position and layout date of each child. - **paint** Paint the widget and its children. The general pass order is "For each user event, call on_event once, then lifecycle a variable number of times, then schedule a paint. When the platform starts the paint, run layout, then paint". ### WidgetMut In Masonry, widgets can't be mutated directly. All mutations go through a `WidgetMut` wrapper. So, to change a label's text, you might call `WidgetMut