Skip to content

Commit c83a822

Browse files
authored
Merge pull request #633 from kas-gui/push-mlyuuutonntu
Remove bool payloads from actions; add `ActionRedraw`, `ActionClose`
2 parents 1644fda + da4072a commit c83a822

16 files changed

Lines changed: 175 additions & 209 deletions

File tree

crates/kas-core/src/action.rs

Lines changed: 21 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,34 @@
77
88
#[allow(unused)]
99
use crate::event::{ConfigCx, EventCx, EventState};
10-
use std::ops::{BitOr, BitOrAssign, Deref};
1110

1211
/// Action: widget has moved/opened/closed
1312
///
14-
/// When the state is `true`, this indicates that the following should happen:
13+
/// This action indicates that the following should happen:
1514
///
1615
/// - Re-probe which widget is under the mouse / any touch instance / any
1716
/// other location picker since widgets may have moved
1817
/// - Redraw the window
1918
#[must_use]
20-
#[derive(Copy, Clone, Debug, Default)]
21-
pub struct ActionMoved(pub bool);
22-
23-
impl BitOr for ActionMoved {
24-
type Output = Self;
25-
#[inline]
26-
fn bitor(self, rhs: Self) -> Self {
27-
ActionMoved(self.0 | rhs.0)
28-
}
29-
}
30-
31-
impl BitOrAssign for ActionMoved {
32-
#[inline]
33-
fn bitor_assign(&mut self, rhs: Self) {
34-
self.0 |= rhs.0;
35-
}
36-
}
37-
38-
impl Deref for ActionMoved {
39-
type Target = bool;
40-
#[inline]
41-
fn deref(&self) -> &bool {
42-
&self.0
43-
}
44-
}
19+
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
20+
pub struct ActionMoved;
4521

4622
/// Action: widget must be resized
23+
///
24+
/// This type implies that either a local or full-window resize is required.
4725
#[must_use]
48-
#[derive(Copy, Clone, Debug, Default)]
49-
pub struct ActionResize(pub bool);
50-
51-
impl ActionResize {
52-
#[inline]
53-
pub(crate) fn clear(&mut self) {
54-
self.0 = false;
55-
}
56-
}
26+
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
27+
pub struct ActionResize;
5728

58-
impl BitOr for ActionResize {
59-
type Output = Self;
60-
#[inline]
61-
fn bitor(self, rhs: Self) -> Self {
62-
ActionResize(self.0 | rhs.0)
63-
}
64-
}
65-
66-
impl BitOrAssign for ActionResize {
67-
#[inline]
68-
fn bitor_assign(&mut self, rhs: Self) {
69-
self.0 |= rhs.0;
70-
}
71-
}
29+
/// Action: content must be redrawn
30+
#[must_use]
31+
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
32+
pub struct ActionRedraw;
7233

73-
impl Deref for ActionResize {
74-
type Target = bool;
75-
#[inline]
76-
fn deref(&self) -> &bool {
77-
&self.0
78-
}
79-
}
34+
/// Action: close window
35+
#[must_use]
36+
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
37+
pub(crate) struct ActionClose;
8038

8139
bitflags! {
8240
/// Action: configuration data updates must be applied
@@ -92,31 +50,10 @@ bitflags! {
9250
}
9351
}
9452

95-
bitflags! {
96-
/// Action required after processing
97-
///
98-
/// Some methods operate directly on a context ([`ConfigCx`] or [`EventCx`])
99-
/// while others don't reqiure a context but do require that some *action*
100-
/// is performed afterwards. This enum is used to convey that action.
101-
///
102-
/// A `WindowAction` produced at run-time should be passed to a context, usually
103-
/// via [`EventState::action`] (to associate the `WindowAction` with a widget)
104-
/// or [`EventState::window_action`] (if no particular widget is relevant).
105-
///
106-
/// A `WindowAction` produced before starting the GUI may be discarded, for
107-
/// example: `let _ = runner.config_mut().font.set_size(24.0);`.
108-
///
109-
/// Two `WindowAction` values may be combined via bit-or (`a | b`).
110-
#[must_use]
111-
#[derive(Copy, Clone, Debug, Default)]
112-
pub struct WindowAction: u32 {
113-
/// The whole window requires redrawing
114-
///
115-
/// See also [`EventState::redraw`].
116-
const REDRAW = 1 << 0;
117-
/// The current window should be closed
118-
///
119-
/// See also [`EventState::exit`] which closes the UI (all windows).
120-
const CLOSE = 1 << 30;
121-
}
53+
/// Set of actions which may affect a window
54+
#[derive(Clone, Debug, Default, PartialEq, Eq)]
55+
pub(crate) struct WindowActions {
56+
pub resize: Option<ActionResize>,
57+
pub redraw: Option<ActionRedraw>,
58+
pub close: Option<ActionClose>,
12259
}

crates/kas-core/src/core/events.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,15 @@ pub trait Events: Widget + Sized {
315315
/// locally and should implement this method to do so
316316
/// (thus avoiding the need for a full-window resize).
317317
///
318-
/// Return `ActionResize(true)` if further resizing is needed, or
319-
/// `ActionResize(false)` if resizing is complete.
318+
/// Return `Some(ActionResize)` if further resizing is needed, or `None` if
319+
/// resizing is complete.
320320
///
321-
/// The default implementation simply returns `ActionResize(true)`.
321+
/// The default implementation simply returns `Some(ActionResize)`.
322322
#[inline]
323-
fn handle_resize(&mut self, cx: &mut ConfigCx, data: &Self::Data) -> ActionResize {
323+
#[must_use]
324+
fn handle_resize(&mut self, cx: &mut ConfigCx, data: &Self::Data) -> Option<ActionResize> {
324325
let _ = (cx, data);
325-
ActionResize(true)
326+
Some(ActionResize)
326327
}
327328

328329
/// Handler for scrolling

crates/kas-core/src/core/impls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn _update<W: Events>(widget: &mut W, cx: &mut ConfigCx, data: &W::Data) {
4141
cx.update(node);
4242
}
4343

44-
if *cx.resize && widget.status().is_sized() {
44+
if cx.resize.is_some() && widget.status().is_sized() {
4545
cx.resize = widget.handle_resize(cx, data);
4646
}
4747
}
@@ -96,7 +96,7 @@ pub fn _send<W: Events>(
9696
);
9797
}
9898

99-
if *cx.resize {
99+
if cx.resize.is_some() {
100100
debug_assert!(widget.status().is_sized());
101101
cx.resize = widget.handle_resize(cx, data);
102102
}
@@ -144,7 +144,7 @@ pub fn _replay<W: Events>(widget: &mut W, cx: &mut EventCx, data: &<W as Widget>
144144
);
145145
}
146146

147-
if *cx.resize && widget.status().is_sized() {
147+
if cx.resize.is_some() && widget.status().is_sized() {
148148
cx.resize = widget.handle_resize(cx, data);
149149
}
150150

crates/kas-core/src/event/components.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,11 @@ impl ScrollComponent {
197197
/// - `content_size`: size of scroll region on the inside (usually larger)
198198
///
199199
/// Returns an [`ActionMoved`] indicating whether the scroll offset changed.
200-
pub fn set_sizes(&mut self, window_size: Size, content_size: Size) -> ActionMoved {
200+
#[must_use]
201+
pub fn set_sizes(&mut self, window_size: Size, content_size: Size) -> Option<ActionMoved> {
201202
let max_offset = (Offset::conv(content_size) - Offset::conv(window_size)).max(Offset::ZERO);
202203
if max_offset == self.max_offset {
203-
return ActionMoved(false);
204+
return None;
204205
}
205206
self.max_offset = max_offset;
206207
self.set_offset(self.offset)
@@ -212,14 +213,15 @@ impl ScrollComponent {
212213
///
213214
/// Also cancels any kinetic scrolling, but only if `offset` is not equal
214215
/// to the current offset.
215-
pub fn set_offset(&mut self, offset: Offset) -> ActionMoved {
216+
#[must_use]
217+
pub fn set_offset(&mut self, offset: Offset) -> Option<ActionMoved> {
216218
let offset = offset.clamp(Offset::ZERO, self.max_offset);
217219
if offset == self.offset {
218-
ActionMoved(false)
220+
None
219221
} else {
220222
self.kinetic.stop();
221223
self.offset = offset;
222-
ActionMoved(true)
224+
Some(ActionMoved)
223225
}
224226
}
225227

@@ -231,7 +233,13 @@ impl ScrollComponent {
231233
/// - `window_rect`: the rect of the scroll window
232234
///
233235
/// Sets [`Scroll::Rect`] to ensure correct scrolling of parents.
234-
pub fn focus_rect(&mut self, cx: &mut EventCx, rect: Rect, window_rect: Rect) -> ActionMoved {
236+
#[must_use]
237+
pub fn focus_rect(
238+
&mut self,
239+
cx: &mut EventCx,
240+
rect: Rect,
241+
window_rect: Rect,
242+
) -> Option<ActionMoved> {
235243
let action = self.self_focus_rect(rect, window_rect);
236244
cx.set_scroll(Scroll::Rect(rect - self.offset));
237245
action
@@ -243,7 +251,8 @@ impl ScrollComponent {
243251
/// [`EventCx::set_scroll`], thus will not affect ancestors.
244252
#[cfg_attr(not(feature = "internal_doc"), doc(hidden))]
245253
#[cfg_attr(docsrs, doc(cfg(internal_doc)))]
246-
pub fn self_focus_rect(&mut self, rect: Rect, window_rect: Rect) -> ActionMoved {
254+
#[must_use]
255+
pub fn self_focus_rect(&mut self, rect: Rect, window_rect: Rect) -> Option<ActionMoved> {
247256
self.kinetic.stop();
248257
let max_vis = rect.pos - window_rect.pos;
249258
let extra_size = Offset::conv(rect.size) - Offset::conv(window_rect.size);

crates/kas-core/src/event/config_cx.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use crate::event::EventState;
99
use crate::text::format::FormattableText;
1010
use crate::theme::{SizeCx, Text, ThemeSize};
11-
use crate::{ActionResize, Id, Node};
11+
use crate::{ActionRedraw, ActionResize, Id, Node};
1212
use std::any::TypeId;
1313
use std::fmt::Debug;
1414
use std::ops::{Deref, DerefMut};
@@ -23,8 +23,8 @@ use std::ops::{Deref, DerefMut};
2323
pub struct ConfigCx<'a> {
2424
pub(super) theme: &'a dyn ThemeSize,
2525
pub(crate) state: &'a mut EventState,
26-
pub(crate) resize: ActionResize,
27-
pub(crate) redraw: bool,
26+
pub(crate) resize: Option<ActionResize>,
27+
pub(crate) redraw: Option<ActionRedraw>,
2828
}
2929

3030
impl<'a> ConfigCx<'a> {
@@ -35,8 +35,8 @@ impl<'a> ConfigCx<'a> {
3535
ConfigCx {
3636
theme: sh,
3737
state: ev,
38-
resize: ActionResize(false),
39-
redraw: false,
38+
resize: None,
39+
redraw: None,
4040
}
4141
}
4242

@@ -64,7 +64,7 @@ impl<'a> ConfigCx<'a> {
6464
// (Except redraw: this doesn't matter.)
6565
let start_resize = std::mem::take(&mut self.resize);
6666
widget._configure(self, id);
67-
self.resize |= start_resize;
67+
self.resize = self.resize.or(start_resize);
6868
}
6969

7070
/// Update a widget
@@ -77,7 +77,7 @@ impl<'a> ConfigCx<'a> {
7777
// (Except redraw: this doesn't matter.)
7878
let start_resize = std::mem::take(&mut self.resize);
7979
widget._update(self);
80-
self.resize |= start_resize;
80+
self.resize = self.resize.or(start_resize);
8181
}
8282

8383
/// Configure a text object
@@ -126,7 +126,7 @@ impl<'a> ConfigCx<'a> {
126126
/// during the traversal unwind if possible.
127127
#[inline]
128128
pub fn redraw(&mut self) {
129-
self.redraw = true;
129+
self.redraw = Some(ActionRedraw);
130130
}
131131

132132
/// Require that the current widget (and its descendants) be resized
@@ -136,7 +136,7 @@ impl<'a> ConfigCx<'a> {
136136
/// during the traversal unwind if possible.
137137
#[inline]
138138
pub fn resize(&mut self) {
139-
self.resize = ActionResize(true);
139+
self.resize = Some(ActionResize);
140140
}
141141
}
142142

0 commit comments

Comments
 (0)