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    /// Check whether this widget is disabled
30    #[inline]
31    pub fn is_disabled(&self) -> bool {
32        self.cx.is_disabled(&self.id)
33    }
34
35    /// Set/unset disabled status for this widget
36    #[inline]
37    pub fn set_disabled(&mut self, state: bool) {
38        self.cx.set_disabled(self.id.clone(), state);
39    }
40
41    /// Schedule a timed update
42    ///
43    /// Widget updates may be used for delayed action. For animation, prefer to
44    /// use [`Draw::animate`](kas::draw::Draw::animate) or
45    /// [`Self::request_frame_timer`].
46    ///
47    /// This widget will receive an update for timer `handle` at
48    /// approximately `time = now + delay` (or possibly a little later due to
49    /// frame-rate limiters and processing time).
50    ///
51    /// Requesting an update with `delay == 0` is valid except from a timer
52    /// handler where it may cause an infinite loop.
53    ///
54    /// Multiple timer requests with the same `id` and `handle` are merged
55    /// (see [`TimerHandle`] documentation).
56    #[inline]
57    pub fn request_timer(&mut self, handle: TimerHandle, delay: Duration) {
58        self.cx.request_timer(self.id.clone(), handle, delay);
59    }
60
61    /// Schedule a frame timer update
62    ///
63    /// Widget `id` will receive [`Event::Timer`] with this `handle` either
64    /// before or soon after the next frame is drawn.
65    ///
66    /// This may be useful for animations which mutate widget state. Animations
67    /// which don't mutate widget state may use
68    /// [`Draw::animate`](kas::draw::Draw::animate) instead.
69    ///
70    /// It is expected that `handle.earliest() == true` (style guide).
71    #[inline]
72    pub fn request_frame_timer(&mut self, handle: TimerHandle) {
73        self.cx.request_frame_timer(self.id.clone(), handle);
74    }
75}
76
77/// A [`ConfigCx`] with embedded [`Id`]
78///
79/// NOTE: this is a temporary design: it may be expanded or integrated with
80/// `ConfigCx` in the future.
81#[autoimpl(Deref, DerefMut using self.cx)]
82pub struct AdaptConfigCx<'a: 'b, 'b> {
83    cx: &'b mut ConfigCx<'a>,
84    id: Id,
85}
86
87impl<'a: 'b, 'b> AdaptConfigCx<'a, 'b> {
88    /// Construct
89    #[inline]
90    pub fn new(cx: &'b mut ConfigCx<'a>, id: Id) -> Self {
91        AdaptConfigCx { cx, id }
92    }
93
94    /// Check whether this widget is disabled
95    #[inline]
96    pub fn is_disabled(&self) -> bool {
97        self.cx.is_disabled(&self.id)
98    }
99
100    /// Set/unset disabled status for this widget
101    #[inline]
102    pub fn set_disabled(&mut self, state: bool) {
103        self.cx.set_disabled(self.id.clone(), state);
104    }
105
106    /// Schedule a timed update
107    ///
108    /// Widget updates may be used for delayed action. For animation, prefer to
109    /// use [`Draw::animate`](kas::draw::Draw::animate) or
110    /// [`Self::request_frame_timer`].
111    ///
112    /// This widget will receive an update for timer `handle` at
113    /// approximately `time = now + delay` (or possibly a little later due to
114    /// frame-rate limiters and processing time).
115    ///
116    /// Requesting an update with `delay == 0` is valid except from a timer
117    /// handler where it may cause an infinite loop.
118    ///
119    /// Multiple timer requests with the same `id` and `handle` are merged
120    /// (see [`TimerHandle`] documentation).
121    #[inline]
122    pub fn request_timer(&mut self, handle: TimerHandle, delay: Duration) {
123        self.cx.request_timer(self.id.clone(), handle, delay);
124    }
125
126    /// Schedule a frame timer update
127    ///
128    /// Widget `id` will receive [`Event::Timer`] with this `handle` either
129    /// before or soon after the next frame is drawn.
130    ///
131    /// This may be useful for animations which mutate widget state. Animations
132    /// which don't mutate widget state may use
133    /// [`Draw::animate`](kas::draw::Draw::animate) instead.
134    ///
135    /// It is expected that `handle.earliest() == true` (style guide).
136    #[inline]
137    pub fn request_frame_timer(&mut self, handle: TimerHandle) {
138        self.cx.request_frame_timer(self.id.clone(), handle);
139    }
140}