Skip to main content

waft_rendering/
relayout_when_system_fonts_change.rs

1//! Flutter counterpart: `rendering/object.dart` (`RelayoutWhenSystemFontsChangeMixin`).
2
3use std::time::Duration;
4
5use waft_foundation::{App, Handle, Listener};
6use waft_painting::PaintingBinding;
7use waft_scheduler::{FrameCallback, SchedulerBinding};
8
9use crate::object::{RenderHandle, RenderObject};
10use waft_foundation::ListenableObject;
11
12/// Flutter's `RelayoutWhenSystemFontsChangeMixin` fields.
13#[derive(Debug, Default)]
14pub struct RelayoutWhenSystemFontsChangeData {
15    has_pending_system_fonts_did_change_callback: bool,
16}
17
18impl RelayoutWhenSystemFontsChangeData {
19    /// No pending system-fonts callback.
20    pub const fn new() -> RelayoutWhenSystemFontsChangeData {
21        RelayoutWhenSystemFontsChangeData {
22            has_pending_system_fonts_did_change_callback: false,
23        }
24    }
25}
26
27/// Mixin for a [`RenderObject`] that relayouts when system fonts change.
28///
29/// System fonts can change when the OS installs or removes a font. Use this mixin if the
30/// render object uses `TextPainter` or `Paragraph` to correctly update the text when it
31/// happens.
32pub trait RelayoutWhenSystemFontsChangeMixin: RenderObject {
33    /// Mixin field access.
34    fn relayout_when_system_fonts_change_data(
35        self: RenderHandle<Self>,
36        app: &App,
37    ) -> &RelayoutWhenSystemFontsChangeData;
38
39    /// See [`relayout_when_system_fonts_change_data`](Self::relayout_when_system_fonts_change_data).
40    fn relayout_when_system_fonts_change_data_mut(
41        self: RenderHandle<Self>,
42        app: &mut App,
43    ) -> &mut RelayoutWhenSystemFontsChangeData;
44
45    /// A callback that is called when system fonts have changed.
46    ///
47    /// The framework defers the invocation of the callback to the
48    /// `SchedulerPhase::TransientCallbacks` phase to ensure that the render object's text
49    /// layout is still valid when user interactions are in progress (which usually take
50    /// place during the idle phase).
51    ///
52    /// By default, `markNeedsLayout` is called on the render object implementing this mixin.
53    ///
54    /// Subclass should override this method to clear any extra cache that depend on
55    /// font-related metrics.
56    fn system_fonts_did_change(self: RenderHandle<Self>, app: &mut App) {
57        self.as_render_object(app).mark_needs_layout(app);
58    }
59
60    /// Flutter's `attach` override body after `super.attach`.
61    fn attach_system_fonts(self: RenderHandle<Self>, app: &mut App) {
62        debug_assert!(
63            !self
64                .relayout_when_system_fonts_change_data(app)
65                .has_pending_system_fonts_did_change_callback
66        );
67        PaintingBinding::instance(app)
68            .system_fonts(app)
69            .add_listener(app, self.system_fonts_listener());
70    }
71
72    /// Flutter's `detach` override body before `super.detach`.
73    fn detach_system_fonts(self: RenderHandle<Self>, app: &mut App) {
74        debug_assert!(
75            !self
76                .relayout_when_system_fonts_change_data(app)
77                .has_pending_system_fonts_did_change_callback
78        );
79        PaintingBinding::instance(app)
80            .system_fonts(app)
81            .remove_listener(app, &self.system_fonts_listener());
82    }
83
84    fn system_fonts_listener(self: RenderHandle<Self>) -> Listener {
85        Listener::handle_method(self.handle(), schedule_system_fonts_update::<Self>)
86    }
87
88    fn schedule_system_fonts_update(self: RenderHandle<Self>, app: &mut App) {
89        if self
90            .relayout_when_system_fonts_change_data(app)
91            .has_pending_system_fonts_did_change_callback
92        {
93            return;
94        }
95        self.relayout_when_system_fonts_change_data_mut(app)
96            .has_pending_system_fonts_did_change_callback = true;
97        SchedulerBinding::schedule_frame_callback(
98            app,
99            FrameCallback::handle_method(self.handle(), run_system_fonts_update::<Self>),
100            None,
101            false,
102            true,
103        );
104    }
105}
106
107fn schedule_system_fonts_update<T: RelayoutWhenSystemFontsChangeMixin>(
108    this: Handle<T>,
109    app: &mut App,
110) {
111    RenderHandle::from_handle(this).schedule_system_fonts_update(app);
112}
113
114fn run_system_fonts_update<T: RelayoutWhenSystemFontsChangeMixin>(
115    this: Handle<T>,
116    app: &mut App,
117    _time_stamp: Duration,
118) {
119    let this = RenderHandle::from_handle(this);
120    if app.is_disposed(this.handle()) {
121        return;
122    }
123    debug_assert!(
124        this.relayout_when_system_fonts_change_data(app)
125            .has_pending_system_fonts_did_change_callback
126    );
127    this.relayout_when_system_fonts_change_data_mut(app)
128        .has_pending_system_fonts_did_change_callback = false;
129    if this.as_render_object(app).attached(app) {
130        RelayoutWhenSystemFontsChangeMixin::system_fonts_did_change(this, app);
131    }
132}