Skip to main content

kas_widgets/adapt/
adapt_cx.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! Adapted contexts
7
8use kas::event::TimerHandle;
9use kas::prelude::*;
10use std::time::Duration;
11
12/// An [`EventCx`] with embedded [`Id`] and input data
13///
14/// NOTE: this is a temporary design: it may be expanded or integrated with
15/// `EventCx` in the future.
16#[autoimpl(Deref, DerefMut using self.cx)]
17pub struct AdaptEventCx<'a: 'b, 'b> {
18    cx: &'b mut EventCx<'a>,
19    id: Id,
20}
21
22impl<'a: 'b, 'b> AdaptEventCx<'a, 'b> {
23    /// Construct
24    #[inline]
25    pub fn new(cx: &'b mut EventCx<'a>, id: Id) -> Self {
26        AdaptEventCx { cx, id }
27    }
28
29    /// Get the widget's identifier
30    #[inline]
31    pub fn id(&self) -> Id {
32        self.id.clone()
33    }
34
35    /// Check whether this widget is disabled
36    #[inline]
37    pub fn is_disabled(&self) -> bool {
38        self.cx.is_disabled(&self.id)
39    }
40
41    /// Set/unset disabled status for this widget
42    #[inline]
43    pub fn set_disabled(&mut self, state: bool) {
44        self.cx.set_disabled(self.id.clone(), state);
45    }
46
47    /// Schedule a timed update
48    ///
49    /// Widget updates may be used for delayed action. For animation, prefer to
50    /// use [`Draw::animate`](kas::draw::Draw::animate) or
51    /// [`Self::request_frame_timer`].
52    ///
53    /// This widget will receive an update for timer `handle` at
54    /// approximately `time = now + delay` (or possibly a little later due to
55    /// frame-rate limiters and processing time).
56    ///
57    /// Requesting an update with `delay == 0` is valid except from a timer
58    /// handler where it may cause an infinite loop.
59    ///
60    /// Multiple timer requests with the same `id` and `handle` are merged
61    /// (see [`TimerHandle`] documentation).
62    #[inline]
63    pub fn request_timer(&mut self, handle: TimerHandle, delay: Duration) {
64        self.cx.request_timer(self.id.clone(), handle, delay);
65    }
66
67    /// Schedule a frame timer update
68    ///
69    /// Widget `id` will receive [`Event::Timer`] with this `handle` either
70    /// before or soon after the next frame is drawn.
71    ///
72    /// This may be useful for animations which mutate widget state. Animations
73    /// which don't mutate widget state may use
74    /// [`Draw::animate`](kas::draw::Draw::animate) instead.
75    ///
76    /// It is expected that `handle.earliest() == true` (style guide).
77    #[inline]
78    pub fn request_frame_timer(&mut self, handle: TimerHandle) {
79        self.cx.request_frame_timer(self.id.clone(), handle);
80    }
81}
82
83/// A [`ConfigCx`] with embedded [`Id`]
84///
85/// NOTE: this is a temporary design: it may be expanded or integrated with
86/// `ConfigCx` in the future.
87#[autoimpl(Deref, DerefMut using self.cx)]
88pub struct AdaptConfigCx<'a: 'b, 'b> {
89    cx: &'b mut ConfigCx<'a>,
90    id: Id,
91}
92
93impl<'a: 'b, 'b> AdaptConfigCx<'a, 'b> {
94    /// Construct
95    #[inline]
96    pub fn new(cx: &'b mut ConfigCx<'a>, id: Id) -> Self {
97        AdaptConfigCx { cx, id }
98    }
99
100    /// Get the widget's identifier
101    #[inline]
102    pub fn id(&self) -> Id {
103        self.id.clone()
104    }
105
106    /// Check whether this widget is disabled
107    #[inline]
108    pub fn is_disabled(&self) -> bool {
109        self.cx.is_disabled(&self.id)
110    }
111
112    /// Set/unset disabled status for this widget
113    #[inline]
114    pub fn set_disabled(&mut self, state: bool) {
115        self.cx.set_disabled(self.id.clone(), state);
116    }
117
118    /// Schedule a timed update
119    ///
120    /// Widget updates may be used for delayed action. For animation, prefer to
121    /// use [`Draw::animate`](kas::draw::Draw::animate) or
122    /// [`Self::request_frame_timer`].
123    ///
124    /// This widget will receive an update for timer `handle` at
125    /// approximately `time = now + delay` (or possibly a little later due to
126    /// frame-rate limiters and processing time).
127    ///
128    /// Requesting an update with `delay == 0` is valid except from a timer
129    /// handler where it may cause an infinite loop.
130    ///
131    /// Multiple timer requests with the same `id` and `handle` are merged
132    /// (see [`TimerHandle`] documentation).
133    #[inline]
134    pub fn request_timer(&mut self, handle: TimerHandle, delay: Duration) {
135        self.cx.request_timer(self.id.clone(), handle, delay);
136    }
137
138    /// Schedule a frame timer update
139    ///
140    /// Widget `id` will receive [`Event::Timer`] with this `handle` either
141    /// before or soon after the next frame is drawn.
142    ///
143    /// This may be useful for animations which mutate widget state. Animations
144    /// which don't mutate widget state may use
145    /// [`Draw::animate`](kas::draw::Draw::animate) instead.
146    ///
147    /// It is expected that `handle.earliest() == true` (style guide).
148    #[inline]
149    pub fn request_frame_timer(&mut self, handle: TimerHandle) {
150        self.cx.request_frame_timer(self.id.clone(), handle);
151    }
152}