From d03873d9d7eeb335116340b54235a975c3adb21e Mon Sep 17 00:00:00 2001 From: Olivier FAURE Date: Sun, 19 Jan 2025 12:56:03 +0000 Subject: [PATCH] Rewrite tutorial (#813) Rewrite "Creating a new Widget" tutorial to interleave description of the Widget trait with example. Go into more details. Rewrite tutorials to use current WidgetMut syntax (the one with free functions instead of methods). --- masonry/src/doc/02_implementing_widget.md | 245 ++++++++++++------ .../doc/03_implementing_container_widget.md | 20 +- 2 files changed, 177 insertions(+), 88 deletions(-) diff --git a/masonry/src/doc/02_implementing_widget.md b/masonry/src/doc/02_implementing_widget.md index c77a6051..3519f8e7 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: +## Our example widget: `ColorRectangle` -- `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::