Skip to main content

widgetkit_runtime/
context.rs

1use crate::{internal::RuntimeServices, scheduler::Scheduler, tasks::Tasks, widget::Widget};
2use std::{marker::PhantomData, ptr::NonNull};
3use widgetkit_core::{InstanceId, Size, WidgetId};
4
5pub struct MountCtx<W>
6where
7    W: Widget,
8{
9    widget_id: WidgetId,
10    instance_id: InstanceId,
11    _marker: PhantomData<fn() -> W>,
12}
13
14impl<W> MountCtx<W>
15where
16    W: Widget,
17{
18    pub(crate) fn new(widget_id: WidgetId, instance_id: InstanceId) -> Self {
19        Self {
20            widget_id,
21            instance_id,
22            _marker: PhantomData,
23        }
24    }
25
26    pub fn widget_id(&self) -> WidgetId {
27        self.widget_id
28    }
29
30    pub fn instance_id(&self) -> InstanceId {
31        self.instance_id
32    }
33}
34
35pub struct StartCtx<W>
36where
37    W: Widget,
38{
39    widget_id: WidgetId,
40    instance_id: InstanceId,
41    services: NonNull<RuntimeServices<W::Message>>,
42    _marker: PhantomData<fn() -> W>,
43}
44
45impl<W> StartCtx<W>
46where
47    W: Widget,
48{
49    pub(crate) fn new(widget_id: WidgetId, instance_id: InstanceId, services: NonNull<RuntimeServices<W::Message>>) -> Self {
50        Self {
51            widget_id,
52            instance_id,
53            services,
54            _marker: PhantomData,
55        }
56    }
57
58    pub fn widget_id(&self) -> WidgetId {
59        self.widget_id
60    }
61
62    pub fn instance_id(&self) -> InstanceId {
63        self.instance_id
64    }
65
66    pub fn post(&mut self, message: W::Message) {
67        let services = self.services_mut();
68        let _ = services.dispatcher.post_message(message);
69    }
70
71    pub fn request_render(&mut self) {
72        let services = self.services_mut();
73        services.render_requested = true;
74        services.dispatcher.wake.wake();
75    }
76
77    /// Returns the scheduler owned by the current widget instance.
78    /// All timers created here are cleared when the instance stops or is disposed.
79    pub fn scheduler(&mut self) -> Scheduler<'_, W::Message> {
80        let services = self.services_mut();
81        Scheduler::new(&mut services.scheduler, services.dispatcher.clone())
82    }
83
84    /// Returns the task backend owned by the current widget instance.
85    /// All tasks created here are canceled when the instance stops or is disposed.
86    pub fn tasks(&mut self) -> Tasks<'_, W::Message> {
87        let services = self.services_mut();
88        Tasks::new(services.tasks.as_mut())
89    }
90
91    fn services_mut(&mut self) -> &mut RuntimeServices<W::Message> {
92        unsafe { self.services.as_mut() }
93    }
94}
95
96pub struct UpdateCtx<W>
97where
98    W: Widget,
99{
100    widget_id: WidgetId,
101    instance_id: InstanceId,
102    services: NonNull<RuntimeServices<W::Message>>,
103    _marker: PhantomData<fn() -> W>,
104}
105
106impl<W> UpdateCtx<W>
107where
108    W: Widget,
109{
110    pub(crate) fn new(widget_id: WidgetId, instance_id: InstanceId, services: NonNull<RuntimeServices<W::Message>>) -> Self {
111        Self {
112            widget_id,
113            instance_id,
114            services,
115            _marker: PhantomData,
116        }
117    }
118
119    pub fn widget_id(&self) -> WidgetId {
120        self.widget_id
121    }
122
123    pub fn instance_id(&self) -> InstanceId {
124        self.instance_id
125    }
126
127    pub fn post(&mut self, message: W::Message) {
128        let services = self.services_mut();
129        let _ = services.dispatcher.post_message(message);
130    }
131
132    pub fn request_render(&mut self) {
133        let services = self.services_mut();
134        services.render_requested = true;
135        services.dispatcher.wake.wake();
136    }
137
138    /// Returns the scheduler owned by the current widget instance.
139    /// All timers created here are cleared when the instance stops or is disposed.
140    pub fn scheduler(&mut self) -> Scheduler<'_, W::Message> {
141        let services = self.services_mut();
142        Scheduler::new(&mut services.scheduler, services.dispatcher.clone())
143    }
144
145    /// Returns the task backend owned by the current widget instance.
146    /// All tasks created here are canceled when the instance stops or is disposed.
147    pub fn tasks(&mut self) -> Tasks<'_, W::Message> {
148        let services = self.services_mut();
149        Tasks::new(services.tasks.as_mut())
150    }
151
152    fn services_mut(&mut self) -> &mut RuntimeServices<W::Message> {
153        unsafe { self.services.as_mut() }
154    }
155}
156
157pub struct RenderCtx<W>
158where
159    W: Widget,
160{
161    widget_id: WidgetId,
162    instance_id: InstanceId,
163    surface_size: Size,
164    _marker: PhantomData<fn() -> W>,
165}
166
167impl<W> RenderCtx<W>
168where
169    W: Widget,
170{
171    pub(crate) fn new(widget_id: WidgetId, instance_id: InstanceId, surface_size: Size) -> Self {
172        Self {
173            widget_id,
174            instance_id,
175            surface_size,
176            _marker: PhantomData,
177        }
178    }
179
180    pub fn widget_id(&self) -> WidgetId {
181        self.widget_id
182    }
183
184    pub fn instance_id(&self) -> InstanceId {
185        self.instance_id
186    }
187
188    pub fn surface_size(&self) -> Size {
189        self.surface_size
190    }
191}
192
193pub struct StopCtx<W>
194where
195    W: Widget,
196{
197    widget_id: WidgetId,
198    instance_id: InstanceId,
199    services: NonNull<RuntimeServices<W::Message>>,
200    _marker: PhantomData<fn() -> W>,
201}
202
203impl<W> StopCtx<W>
204where
205    W: Widget,
206{
207    pub(crate) fn new(widget_id: WidgetId, instance_id: InstanceId, services: NonNull<RuntimeServices<W::Message>>) -> Self {
208        Self {
209            widget_id,
210            instance_id,
211            services,
212            _marker: PhantomData,
213        }
214    }
215
216    pub fn widget_id(&self) -> WidgetId {
217        self.widget_id
218    }
219
220    pub fn instance_id(&self) -> InstanceId {
221        self.instance_id
222    }
223
224    /// Returns the scheduler owned by the current widget instance.
225    /// All timers created here are cleared when the instance stops or is disposed.
226    pub fn scheduler(&mut self) -> Scheduler<'_, W::Message> {
227        let services = self.services_mut();
228        Scheduler::new(&mut services.scheduler, services.dispatcher.clone())
229    }
230
231    /// Returns the task backend owned by the current widget instance.
232    /// All tasks created here are canceled when the instance stops or is disposed.
233    pub fn tasks(&mut self) -> Tasks<'_, W::Message> {
234        let services = self.services_mut();
235        Tasks::new(services.tasks.as_mut())
236    }
237
238    fn services_mut(&mut self) -> &mut RuntimeServices<W::Message> {
239        unsafe { self.services.as_mut() }
240    }
241}
242
243pub struct DisposeCtx<W>
244where
245    W: Widget,
246{
247    widget_id: WidgetId,
248    instance_id: InstanceId,
249    _marker: PhantomData<fn() -> W>,
250}
251
252impl<W> DisposeCtx<W>
253where
254    W: Widget,
255{
256    pub(crate) fn new(widget_id: WidgetId, instance_id: InstanceId) -> Self {
257        Self {
258            widget_id,
259            instance_id,
260            _marker: PhantomData,
261        }
262    }
263
264    pub fn widget_id(&self) -> WidgetId {
265        self.widget_id
266    }
267
268    pub fn instance_id(&self) -> InstanceId {
269        self.instance_id
270    }
271}