From a0b552baf2e52444c81f7f086017f0a3b62879cf Mon Sep 17 00:00:00 2001 From: Olivier FAURE Date: Fri, 10 Jan 2025 18:22:41 +0100 Subject: [PATCH 1/4] Rewrite tutorial --- masonry/src/doc/02_implementing_widget.md | 232 ++++++++++++------ .../doc/03_implementing_container_widget.md | 20 +- 2 files changed, 167 insertions(+), 85 deletions(-) diff --git a/masonry/src/doc/02_implementing_widget.md b/masonry/src/doc/02_implementing_widget.md index c77a60518..1ca92e7f1 100644 --- a/masonry/src/doc/02_implementing_widget.md +++ b/masonry/src/doc/02_implementing_widget.md @@ -21,9 +21,7 @@ This tutorial explains how to create a simple leaf widget. ## The Widget trait -Widgets are types which implement the [`Widget`] trait. - -This trait includes a set of methods that must be implemented to hook into Masonry's internals: +Widgets are types which implement the [`Widget`] trait: ```rust,ignore trait Widget { @@ -44,9 +42,6 @@ trait Widget { } ``` -These methods are called by the framework at various points, with a `FoobarCtx` parameter giving information about the current widget (for example its size, position, or whether it's currently hovered). -The information accessible from the context argument depends on the method. - In the course of a frame, Masonry will run a series of passes over the widget tree, which will call these methods at different points: - `on_pointer_event`, `on_text_event` and `on_access_event` are called once after a user-initiated event (like a mouse click or keyboard input). @@ -55,61 +50,11 @@ In the course of a frame, Masonry will run a series of passes over the widget tr - `layout` is called during Masonry's layout pass. It takes size constraints and returns the widget's desired size. - `paint`, `accessibility_role` and `accessibility` are called roughly every frame for every widget, to allow them to draw to the screen and describe their structure to assistive technologies. -Most passes will skip most widgets by default. -For instance, the paint pass will only call a widget's `paint` method once, and then cache the resulting scene. -If your widget's appearance is changed by another method, you need to call `ctx.request_render()` to tell the framework to re-run the paint and accessibility passes. - -Most context types include these methods for requesting future passes: - -- `request_render()` -- `request_paint_only()` -- `request_accessibility_update()` -- `request_layout()` -- `request_anim_frame()` - - -## Widget mutation - -In Masonry, widgets generally can't be mutated directly. -That is to say, even if you own a window, and even if that window holds a widget tree with a `Label` instance, you can't get a `&mut Label` directly from that window. - -Instead, there are two ways to mutate `Label`: - -- Inside a Widget method. Most methods (`on_pointer_event`, `update`, `layout`, etc) take a `&mut self` argument. -- Through a [`WidgetMut`] wrapper. So, to change your label's text, you will call `WidgetMut::