ltk/input/dispatch/
outcomes.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

use crate::app::App;
use crate::event_loop::{ AppData, SurfaceFocus };

use crate::input::gesture::{ MoveOutcome, ReleaseEvent };

impl<A: App> AppData<A>
{
	/// Apply the outcome of a motion event. Non-blocking side-effects
	/// only: push messages, call app callbacks, set redraw flags.
	pub( crate ) fn apply_move_outcome
	(
		&mut self,
		focus:   SurfaceFocus,
		outcome: MoveOutcome<A::Message>,
	)
	{
		match outcome
		{
			MoveOutcome::Idle => {}
			MoveOutcome::Drag { pos } =>
			{
				let ( ox, oy ) = self.surface_offset_for( focus );
				self.app.on_drag_move( pos.x + ox, pos.y + oy );
				self.dirty_caches();
				self.surface_mut( focus ).request_redraw();
				self.main.request_redraw();
				for ss in self.overlays.values_mut()
				{
					ss.request_redraw();
				}
			}
			MoveOutcome::Slider { msg } =>
			{
				if let Some( m ) = msg
				{
					self.pending_msgs.push( m );
				}
				self.surface_mut( focus ).request_redraw();
			}
			MoveOutcome::Scroll =>
			{
				self.surface_mut( focus ).request_redraw();
			}
			MoveOutcome::Swipe { up, down, horizontal } =>
			{
				if let Some( v ) = up         { self.app.on_swipe_progress( v ); }
				if let Some( v ) = down       { self.app.on_swipe_down_progress( v ); }
				if let Some( v ) = horizontal { self.app.on_swipe_horizontal_progress( v ); }
				// Swipe-progress callbacks mutate app state outside `update`,
				// so the cached trees are stale and the affected surfaces must
				// redraw. Only the HORIZONTAL pager moves the main surface,
				// though — vertical swipes drive overlay panels and leave the
				// main static. Re-rastering the (full-screen) main on every
				// motion event of a vertical swipe is pure waste and, on a slow
				// GPU, stalls the event loop so the gesture itself feels laggy
				// to start. So refresh the overlays always, but the main only
				// for a horizontal swipe.
				if horizontal.is_some()
				{
					self.view_dirty = true;
					if let SurfaceFocus::Main = focus
					{
						self.main.request_redraw();
					}
				}
				self.overlays_dirty = true;
				for ss in self.overlays.values_mut()
				{
					ss.request_redraw();
				}
			}
		}
	}

	/// Apply the ordered list of events from a release. A single
	/// release can emit more than one event (e.g. horizontal
	/// fall-through followed by a vertical commit or fall-through), so
	/// we walk the list and run each event's side-effects in order.
	pub( crate ) fn apply_release_events
	(
		&mut self,
		focus:  SurfaceFocus,
		events: Vec<ReleaseEvent<A::Message>>,
	)
	{
		for event in events
		{
			self.apply_release_event( focus, event );
		}
	}

	fn apply_release_event
	(
		&mut self,
		focus: SurfaceFocus,
		event: ReleaseEvent<A::Message>,
	)
	{
		match event
		{
			ReleaseEvent::Drop { pos } =>
			{
				let ( ox, oy ) = self.surface_offset_for( focus );
				if let Some( msg ) = self.app.on_drop( pos.x + ox, pos.y + oy )
				{
					self.pending_msgs.push( msg );
				}
				self.clear_long_press_drag();
				self.dirty_caches();
				self.main.request_redraw();
				self.main.frame_pending = false;
				for ss in self.overlays.values_mut()
				{
					ss.request_redraw();
				}
			}
			ReleaseEvent::SwipeLeft =>
			{
				if let Some( msg ) = self.app.on_swipe_left()
				{
					self.pending_msgs.push( msg );
				}
				// When the app handles the release internally (settle
				// animation, state-only mutation) it returns no
				// Message, so the update-driven redraw never fires.
				// Kick the main surface here so `is_animating()` has a
				// frame to latch onto.
				self.dirty_caches();
				self.main.request_redraw();
				self.main.frame_pending = false;
			}
			ReleaseEvent::SwipeRight =>
			{
				if let Some( msg ) = self.app.on_swipe_right()
				{
					self.pending_msgs.push( msg );
				}
				self.dirty_caches();
				self.main.request_redraw();
				self.main.frame_pending = false;
			}
			ReleaseEvent::SwipeUp =>
			{
				if let Some( msg ) = self.app.on_swipe_up()
				{
					self.pending_msgs.push( msg );
				}
				self.dirty_caches();
				self.main.request_redraw();
				self.main.frame_pending = false;
				for ss in self.overlays.values_mut()
				{
					ss.request_redraw();
					ss.frame_pending = false;
				}
			}
			ReleaseEvent::SwipeDown =>
			{
				if let Some( msg ) = self.app.on_swipe_down()
				{
					self.pending_msgs.push( msg );
				}
				self.dirty_caches();
				self.main.request_redraw();
				self.main.frame_pending = false;
				for ss in self.overlays.values_mut()
				{
					ss.request_redraw();
					ss.frame_pending = false;
				}
			}
			ReleaseEvent::HorizontalFellThrough =>
			{
				// Below threshold: pulse horizontal 0 so a
				// vertical-dominant gesture that drifted laterally
				// does not leave the app stuck on a stale horizontal
				// progress.
				self.app.on_swipe_horizontal_progress( 0.0 );
				self.dirty_caches();
				self.main.request_redraw();
				self.main.frame_pending = false;
			}
			ReleaseEvent::VerticalFellThrough =>
			{
				self.app.on_swipe_progress( 0.0 );
				self.app.on_swipe_down_progress( 0.0 );
				self.dirty_caches();
			}
			ReleaseEvent::PushMsg( msg ) =>
			{
				self.pending_msgs.push( msg );
			}
			ReleaseEvent::EmptyRelease =>
			{
				match focus
				{
					SurfaceFocus::Main =>
					{
						if let Some( msg ) = self.app.on_tap()
						{
							self.pending_msgs.push( msg );
						}
					}
					SurfaceFocus::Overlay( id ) =>
					{
						if let Some( msg ) = self.overlay_dismiss_msg( id )
						{
							self.pending_msgs.push( msg );
						}
					}
				}
			}
		}
	}
}