Struct kas_core::TkAction

source ·
pub struct TkAction { /* private fields */ }
Expand description

Action required after processing

This type is returned by many widgets on modification to self and is tracked internally by event::EventMgr to determine which updates are needed to the UI.

Two TkAction values may be combined via bit-or (a | b). Bit-or assignments are supported by both TkAction and event::EventMgr.

Users receiving a value of this type from a widget update method should usually handle with *mgr |= action;. Before the event loop starts (toolkit.run()) or if the widget in question is not part of a UI these values can be ignored.

Implementations§

No flags

This is a zero flag.

The whole window requires redrawing

Note that [event::EventMgr::redraw] can instead be used for more selective redrawing.

Some widgets within a region moved

Used when a pop-up is closed or a region adjusted (e.g. scroll or switch tab) to update which widget is under the mouse cursor / touch events. Identifier is that of the parent widget/window encapsulating the region.

Implies window redraw.

Reset size of all widgets without recalculating requirements

Resize all widgets in the window

Update theme memory

Reconfigure all widgets of the window

Configuring widgets assigns WidgetId identifiers and calls crate::Widget::configure.

The current window should be closed

Close all windows and exit

Returns an empty set of flags.

Examples found in repository?
src/theme/mod.rs (line 44)
43
44
45
    fn set_theme(&mut self, _theme: &str) -> TkAction {
        TkAction::empty()
    }
More examples
Hide additional examples
src/event/components.rs (line 156)
153
154
155
156
157
158
159
160
161
    pub fn set_offset(&mut self, offset: Offset) -> TkAction {
        let offset = offset.min(self.max_offset).max(Offset::ZERO);
        if offset == self.offset {
            TkAction::empty()
        } else {
            self.offset = offset;
            TkAction::REGION_MOVED
        }
    }
src/event/manager/mgr_shell.rs (line 53)
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    pub fn new(config: SharedRc<Config>, scale_factor: f32, dpem: f32) -> Self {
        EventState {
            config: WindowConfig::new(config, scale_factor, dpem),
            disabled: vec![],
            window_has_focus: false,
            modifiers: ModifiersState::empty(),
            char_focus: false,
            sel_focus: None,
            nav_focus: None,
            nav_fallback: None,
            hover: None,
            hover_icon: CursorIcon::Default,
            key_depress: Default::default(),
            last_mouse_coord: Coord::ZERO,
            last_click_button: FAKE_MOUSE_BUTTON,
            last_click_repetitions: 0,
            last_click_timeout: Instant::now(), // unimportant value
            mouse_grab: None,
            touch_grab: Default::default(),
            pan_grab: SmallVec::new(),
            accel_layers: Default::default(),
            popups: Default::default(),
            popup_removed: Default::default(),
            time_updates: vec![],
            pending: Default::default(),
            action: TkAction::empty(),
        }
    }

    /// Update scale factor
    pub fn set_scale_factor(&mut self, scale_factor: f32, dpem: f32) {
        self.config.update(scale_factor, dpem);
    }

    /// Configure event manager for a widget tree.
    ///
    /// This should be called by the toolkit on the widget tree when the window
    /// is created (before or after resizing).
    ///
    /// This method calls [`ConfigMgr::configure`] in order to assign
    /// [`WidgetId`] identifiers and call widgets' [`Widget::configure`]
    /// method. Additionally, it updates the [`EventState`] to account for
    /// renamed and removed widgets.
    pub fn full_configure(&mut self, shell: &mut dyn ShellWindow, widget: &mut dyn Widget) {
        log::debug!(target: "kas_core::event::manager", "full_configure");
        self.action.remove(TkAction::RECONFIGURE);

        // These are recreated during configure:
        self.accel_layers.clear();
        self.nav_fallback = None;

        self.new_accel_layer(WidgetId::ROOT, false);

        shell.size_and_draw_shared(&mut |size, draw_shared| {
            let mut mgr = ConfigMgr::new(size, draw_shared, self);
            mgr.configure(WidgetId::ROOT, widget);
        });

        let hover = widget.find_id(self.last_mouse_coord);
        self.with(shell, |mgr| mgr.set_hover(hover));
    }

    /// Update the widgets under the cursor and touch events
    pub fn region_moved(&mut self, shell: &mut dyn ShellWindow, widget: &mut dyn Widget) {
        log::trace!(target: "kas_core::event::manager", "region_moved");
        // Note: redraw is already implied.

        // Update hovered widget
        let hover = widget.find_id(self.last_mouse_coord);
        self.with(shell, |mgr| mgr.set_hover(hover));

        for grab in self.touch_grab.iter_mut() {
            grab.cur_id = widget.find_id(grab.coord);
        }
    }

    /// Get the next resume time
    pub fn next_resume(&self) -> Option<Instant> {
        self.time_updates.last().map(|time| time.0)
    }

    /// Construct a [`EventMgr`] referring to this state
    ///
    /// Invokes the given closure on this [`EventMgr`].
    #[inline]
    pub fn with<F>(&mut self, shell: &mut dyn ShellWindow, f: F)
    where
        F: FnOnce(&mut EventMgr),
    {
        let mut mgr = EventMgr {
            state: self,
            shell,
            messages: vec![],
            scroll: Scroll::None,
            action: TkAction::empty(),
        };
        f(&mut mgr);
        let action = mgr.action;
        drop(mgr);
        self.send_action(action);
    }

    /// Update, after receiving all events
    #[inline]
    pub fn update(&mut self, shell: &mut dyn ShellWindow, widget: &mut dyn Widget) -> TkAction {
        let old_hover_icon = self.hover_icon;

        let mut mgr = EventMgr {
            state: self,
            shell,
            messages: vec![],
            scroll: Scroll::None,
            action: TkAction::empty(),
        };

        while let Some((parent, wid)) = mgr.popup_removed.pop() {
            mgr.send_event(widget, parent, Event::PopupRemoved(wid));
        }

        if let Some((id, event)) = mgr.mouse_grab().and_then(|g| g.flush_move()) {
            mgr.send_event(widget, id, event);
        }

        for i in 0..mgr.touch_grab.len() {
            if let Some((id, event)) = mgr.touch_grab[i].flush_move() {
                mgr.send_event(widget, id, event);
            }
        }

        for gi in 0..mgr.pan_grab.len() {
            let grab = &mut mgr.pan_grab[gi];
            debug_assert!(grab.mode != GrabMode::Grab);
            assert!(grab.n > 0);

            // Terminology: pi are old coordinates, qi are new coords
            let (p1, q1) = (DVec2::conv(grab.coords[0].0), DVec2::conv(grab.coords[0].1));
            grab.coords[0].0 = grab.coords[0].1;

            let alpha;
            let delta;

            if grab.mode == GrabMode::PanOnly || grab.n == 1 {
                alpha = DVec2(1.0, 0.0);
                delta = q1 - p1;
            } else {
                // We don't use more than two touches: information would be
                // redundant (although it could be averaged).
                let (p2, q2) = (DVec2::conv(grab.coords[1].0), DVec2::conv(grab.coords[1].1));
                grab.coords[1].0 = grab.coords[1].1;
                let (pd, qd) = (p2 - p1, q2 - q1);

                alpha = match grab.mode {
                    GrabMode::PanFull => qd.complex_div(pd),
                    GrabMode::PanScale => DVec2((qd.sum_square() / pd.sum_square()).sqrt(), 0.0),
                    GrabMode::PanRotate => {
                        let a = qd.complex_div(pd);
                        a / a.sum_square().sqrt()
                    }
                    _ => unreachable!(),
                };

                // Average delta from both movements:
                delta = (q1 - alpha.complex_mul(p1) + q2 - alpha.complex_mul(p2)) * 0.5;
            }

            let id = grab.id.clone();
            if alpha != DVec2(1.0, 0.0) || delta != DVec2::ZERO {
                let event = Event::Pan { alpha, delta };
                mgr.send_event(widget, id, event);
            }
        }

        // Warning: infinite loops are possible here if widgets always queue a
        // new pending event when evaluating one of these:
        while let Some(item) = mgr.pending.pop_front() {
            log::trace!(target: "kas_core::event::manager", "update: handling Pending::{item:?}");
            let (id, event) = match item {
                Pending::SetNavFocus(id, key_focus) => (id, Event::NavFocus(key_focus)),
                Pending::MouseHover(id) => (id, Event::MouseHover),
                Pending::LostNavFocus(id) => (id, Event::LostNavFocus),
                Pending::LostMouseHover(id) => {
                    mgr.hover_icon = Default::default();
                    (id, Event::LostMouseHover)
                }
                Pending::LostCharFocus(id) => (id, Event::LostCharFocus),
                Pending::LostSelFocus(id) => (id, Event::LostSelFocus),
            };
            mgr.send_event(widget, id, event);
        }

        let action = mgr.action;
        drop(mgr);

        if self.hover_icon != old_hover_icon && self.mouse_grab.is_none() {
            shell.set_cursor_icon(self.hover_icon);
        }

        let action = action | self.action;
        self.action = TkAction::empty();
        action
    }

Returns the set containing all flags.

Returns the raw value of the flags currently stored.

Convert from underlying bit representation, unless that representation contains bits that do not correspond to a flag.

Convert from underlying bit representation, dropping any bits that do not correspond to flags.

Convert from underlying bit representation, preserving all bits (even those not corresponding to a defined flag).

Safety

The caller of the bitflags! macro can chose to allow or disallow extra bits for their bitflags type.

The caller of from_bits_unchecked() has to ensure that all bits correspond to a defined flag or that extra bits are valid for this bitflags type.

Returns true if no flags are currently stored.

Examples found in repository?
src/event/components.rs (line 259)
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    pub fn scroll_by_event(
        &mut self,
        mgr: &mut EventMgr,
        event: Event,
        id: WidgetId,
        window_rect: Rect,
    ) -> (bool, Response) {
        let mut moved = false;
        match event {
            Event::Command(cmd) => {
                let offset = match cmd {
                    Command::Home => Offset::ZERO,
                    Command::End => self.max_offset,
                    cmd => {
                        let delta = match cmd {
                            Command::Left => LineDelta(-1.0, 0.0),
                            Command::Right => LineDelta(1.0, 0.0),
                            Command::Up => LineDelta(0.0, 1.0),
                            Command::Down => LineDelta(0.0, -1.0),
                            Command::PageUp => PixelDelta(Offset(0, window_rect.size.1 / 2)),
                            Command::PageDown => PixelDelta(Offset(0, -(window_rect.size.1 / 2))),
                            _ => return (false, Response::Unused),
                        };
                        let delta = match delta {
                            LineDelta(x, y) => mgr.config().scroll_distance((x, y)),
                            PixelDelta(d) => d,
                        };
                        self.offset - delta
                    }
                };
                let action = self.set_offset(offset);
                if !action.is_empty() {
                    moved = true;
                    *mgr |= action;
                }
                mgr.set_scroll(Scroll::Rect(window_rect));
            }
            Event::Scroll(delta) => {
                let delta = match delta {
                    LineDelta(x, y) => mgr.config().scroll_distance((x, y)),
                    PixelDelta(d) => d,
                };
                moved = self.scroll_by_delta(mgr, delta);
            }
            Event::PressStart { source, coord, .. }
                if self.max_offset != Offset::ZERO && mgr.config_enable_pan(source) =>
            {
                let icon = Some(CursorIcon::Grabbing);
                mgr.grab_press_unique(id, source, coord, icon);
            }
            Event::PressMove { delta, .. } => {
                self.glide.move_delta(delta);
                moved = self.scroll_by_delta(mgr, delta);
            }
            Event::PressEnd { .. } => {
                if self.glide.opt_start(mgr.config().scroll_flick_timeout()) {
                    mgr.request_update(id, PAYLOAD_GLIDE, Duration::new(0, 0), true);
                }
            }
            Event::TimerUpdate(pl) if pl == PAYLOAD_GLIDE => {
                // Momentum/glide scrolling: update per arbitrary step time until movment stops.
                let decay = mgr.config().scroll_flick_decay();
                if let Some(delta) = self.glide.step(decay) {
                    let action = self.set_offset(self.offset - delta);
                    if !action.is_empty() {
                        *mgr |= action;
                        moved = true;
                    }
                    if delta == Offset::ZERO || !action.is_empty() {
                        // Note: when FPS > pixels/sec, delta may be zero while
                        // still scrolling. Glide returns None when we're done,
                        // but we're also done if unable to scroll further.
                        let dur = Duration::from_millis(GLIDE_POLL_MS);
                        mgr.request_update(id, PAYLOAD_GLIDE, dur, true);
                        mgr.set_scroll(Scroll::Scrolled);
                    }
                }
            }
            _ => return (false, Response::Unused),
        }
        (moved, Response::Used)
    }

Returns true if all flags are currently set.

Returns true if there are flags common to both self and other.

Returns true if all of the flags in other are contained within self.

Inserts the specified flags in-place.

Removes the specified flags in-place.

Examples found in repository?
src/event/manager/mgr_shell.rs (line 73)
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    pub fn full_configure(&mut self, shell: &mut dyn ShellWindow, widget: &mut dyn Widget) {
        log::debug!(target: "kas_core::event::manager", "full_configure");
        self.action.remove(TkAction::RECONFIGURE);

        // These are recreated during configure:
        self.accel_layers.clear();
        self.nav_fallback = None;

        self.new_accel_layer(WidgetId::ROOT, false);

        shell.size_and_draw_shared(&mut |size, draw_shared| {
            let mut mgr = ConfigMgr::new(size, draw_shared, self);
            mgr.configure(WidgetId::ROOT, widget);
        });

        let hover = widget.find_id(self.last_mouse_coord);
        self.with(shell, |mgr| mgr.set_hover(hover));
    }

Toggles the specified flags in-place.

Inserts or removes the specified flags depending on the passed value.

Returns the intersection between the flags in self and other.

Specifically, the returned set contains only the flags which are present in both self and other.

This is equivalent to using the & operator (e.g. ops::BitAnd), as in flags & other.

Returns the union of between the flags in self and other.

Specifically, the returned set contains all flags which are present in either self or other, including any which are present in both (see Self::symmetric_difference if that is undesirable).

This is equivalent to using the | operator (e.g. ops::BitOr), as in flags | other.

Returns the difference between the flags in self and other.

Specifically, the returned set contains all flags present in self, except for the ones present in other.

It is also conceptually equivalent to the “bit-clear” operation: flags & !other (and this syntax is also supported).

This is equivalent to using the - operator (e.g. ops::Sub), as in flags - other.

Returns the symmetric difference between the flags in self and other.

Specifically, the returned set contains the flags present which are present in self or other, but that are not present in both. Equivalently, it contains the flags present in exactly one of the sets self and other.

This is equivalent to using the ^ operator (e.g. ops::BitXor), as in flags ^ other.

Returns the complement of this set of flags.

Specifically, the returned set contains all the flags which are not set in self, but which are allowed for this type.

Alternatively, it can be thought of as the set difference between Self::all() and self (e.g. Self::all() - self)

This is equivalent to using the ! operator (e.g. ops::Not), as in !flags.

Trait Implementations§

Formats the value using the given formatter.

Returns the intersection between the two sets of flags.

The resulting type after applying the & operator.

Disables all flags disabled in the set.

Returns the union of the two sets of flags.

The resulting type after applying the | operator.
Performs the |= operation. Read more
Performs the |= operation. Read more
Performs the |= operation. Read more
Performs the |= operation. Read more

Adds the set of flags.

Returns the left flags, but with all the right flags toggled.

The resulting type after applying the ^ operator.

Toggles the set of flags.

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
Formats the value using the given formatter.

Returns the complement of this set of flags.

The resulting type after applying the ! operator.
Formats the value using the given formatter.
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Returns the set difference of the two sets of flags.

The resulting type after applying the - operator.

Disables all flags enabled in the set.

Formats the value using the given formatter.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Cast from Self to T Read more
Try converting from Self to T Read more
Try approximate conversion from Self to T Read more
Cast approximately from Self to T Read more
Cast to integer, truncating Read more
Cast to the nearest integer Read more
Cast the floor to an integer Read more
Cast the ceiling to an integer Read more
Try converting to integer with truncation Read more
Try converting to the nearest integer Read more
Try converting the floor to an integer Read more
Try convert the ceiling to an integer Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.