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
219
220
221
222
223
224
225
226
227
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use super::{ControllerContext, InputController};
use crate::event::{InputEvent, PointerEvent};
use crate::scrollbar::{
scrollbar_drag_offset, scrollbar_drag_offset_with_grab, scrollbar_geometry_for_node,
scrollbar_hit_test, scrollbar_point_for_node, ScrollbarDragState, ScrollbarHitKind,
};
use crate::{ActionEnvelope, ActionId, ActionInput};
use fission_ir::op::RichTextAnnotation;
use fission_ir::{semantics::ActionTrigger, NodeId, Op};
use fission_layout::LayoutPoint;
pub struct GestureController;
impl InputController for GestureController {
fn handle_event(&mut self, ctx: &mut ControllerContext, event: &InputEvent) -> bool {
match event {
InputEvent::Pointer(pe) => {
match pe {
PointerEvent::Down { point, button, .. } => {
ctx.gesture.start_point = Some(*point);
ctx.gesture.last_point = Some(*point);
ctx.gesture.is_panning = false;
ctx.gesture.pressed_button = Some(button.clone());
ctx.gesture.scrollbar_drag = None;
if matches!(button, crate::event::PointerButton::Primary) {
if let Some(hit) =
scrollbar_hit_test(ctx.ir, ctx.layout, ctx.scroll, *point)
{
let pointer_to_thumb_start = match hit.kind {
ScrollbarHitKind::Thumb => hit.pointer_to_thumb_start,
ScrollbarHitKind::Rail => hit.geometry.thumb_extent() * 0.5,
};
let new_offset = match hit.kind {
ScrollbarHitKind::Thumb => hit.geometry.offset,
ScrollbarHitKind::Rail => {
scrollbar_drag_offset(hit.geometry, hit.layout_point)
}
};
ctx.scroll.set_offset(hit.geometry.node_id, new_offset);
ctx.gesture.target_node = Some(hit.geometry.node_id);
ctx.gesture.dragging_payload = None;
ctx.gesture.scrollbar_drag = Some(ScrollbarDragState {
node_id: hit.geometry.node_id,
pointer_to_thumb_start,
});
return true;
}
}
if let Some(hit) = crate::hit_test::hit_test_with_scroll(
ctx.ir, ctx.layout, ctx.scroll, *point,
) {
ctx.gesture.target_node = Some(hit);
ctx.gesture.dragging_payload = self.find_drag_payload(ctx, hit);
} else {
ctx.gesture.target_node = None;
ctx.gesture.dragging_payload = None;
}
}
PointerEvent::Move { point, .. } => {
if let Some(drag) = ctx.gesture.scrollbar_drag {
if let Some(geometry) = scrollbar_geometry_for_node(
ctx.ir,
ctx.layout,
ctx.scroll,
drag.node_id,
) {
let new_offset = scrollbar_drag_offset_with_grab(
geometry,
scrollbar_point_for_node(
ctx.ir,
ctx.scroll,
drag.node_id,
*point,
),
drag.pointer_to_thumb_start,
);
ctx.scroll.set_offset(drag.node_id, new_offset);
}
ctx.gesture.last_point = Some(*point);
return true;
}
if let Some(start) = ctx.gesture.start_point {
let dx = point.x - start.x;
let dy = point.y - start.y;
let dist_sq = dx * dx + dy * dy;
let threshold = 5.0 * 5.0;
if !ctx.gesture.is_panning && dist_sq > threshold {
ctx.gesture.is_panning = true;
// Dispatch DragStart now
if let Some(target) = ctx.gesture.target_node {
self.dispatch_trigger(
ctx,
target,
ActionTrigger::DragStart,
*point,
None,
);
}
}
if ctx.gesture.is_panning {
let last = ctx.gesture.last_point.unwrap_or(start);
let delta = LayoutPoint {
x: point.x - last.x,
y: point.y - last.y,
};
ctx.gesture.last_point = Some(*point);
// Try dispatching DragUpdate
let dispatched = if let Some(target) = ctx.gesture.target_node {
self.dispatch_trigger(
ctx,
target,
ActionTrigger::DragUpdate,
*point,
Some(delta),
)
} else {
false
};
if dispatched {
return true;
}
// Fallback to Scroll Panning if DragUpdate not handled
if self.handle_pan_update(ctx, delta) {
return true;
}
}
}
}
PointerEvent::Up { point, .. } => {
let scrollbar_drag = ctx.gesture.scrollbar_drag.take();
let mut handled = false;
let was_secondary = matches!(
ctx.gesture.pressed_button,
Some(crate::event::PointerButton::Secondary)
);
if ctx.gesture.is_panning {
// Internal Drop
if let Some(payload) = ctx.gesture.dragging_payload.take() {
if let Some(up_hit) = crate::hit_test::hit_test_with_scroll(
ctx.ir, ctx.layout, ctx.scroll, *point,
) {
let _ =
self.dispatch_internal_drop(ctx, up_hit, payload, *point);
}
}
if let Some(target) = ctx.gesture.target_node {
self.dispatch_trigger(
ctx,
target,
ActionTrigger::DragEnd,
*point,
None,
);
}
handled = true;
} else if was_secondary {
// Secondary click (right-click)
if let Some(target) = ctx.gesture.target_node {
if let Some(up_hit) = crate::hit_test::hit_test_with_scroll(
ctx.ir, ctx.layout, ctx.scroll, *point,
) {
if up_hit == target
|| self.is_descendant(ctx, up_hit, target)
|| self.is_descendant(ctx, target, up_hit)
{
let rich_text_path = self.path_from_node(ctx, up_hit);
if let Some((annotation_node_id, annotation)) =
crate::input::hover::resolve_rich_text_annotation_at_point(
ctx,
&rich_text_path,
*point,
)
{
handled = self.dispatch_annotation_trigger(
ctx,
annotation_node_id,
&annotation,
ActionTrigger::SecondaryClick,
*point,
);
}
if !handled
&& self.dispatch_trigger(
ctx,
target,
ActionTrigger::SecondaryClick,
*point,
None,
)
{
handled = true;
}
}
}
}
} else {
// Tap (primary click)
if let Some(target) = ctx.gesture.target_node {
if let Some(up_hit) = crate::hit_test::hit_test_with_scroll(
ctx.ir, ctx.layout, ctx.scroll, *point,
) {
if up_hit == target
|| self.is_descendant(ctx, up_hit, target)
|| self.is_descendant(ctx, target, up_hit)
{
let rich_text_path = self.path_from_node(ctx, up_hit);
if let Some((annotation_node_id, annotation)) =
crate::input::hover::resolve_rich_text_annotation_at_point(
ctx,
&rich_text_path,
*point,
)
{
handled = self.dispatch_annotation_trigger(
ctx,
annotation_node_id,
&annotation,
ActionTrigger::Default,
*point,
);
}
if !handled
&& self.dispatch_trigger(
ctx,
target,
ActionTrigger::Default,
*point,
None,
)
{
handled = true;
}
}
}
}
}
ctx.gesture.start_point = None;
ctx.gesture.is_panning = false;
ctx.gesture.dragging_payload = None;
ctx.gesture.pressed_button = None;
if scrollbar_drag.is_some() {
ctx.gesture.target_node = None;
return true;
}
return handled;
}
_ => {}
}
}
_ => {}
}
false
}
}
impl GestureController {
fn path_from_node(&self, ctx: &ControllerContext, node_id: NodeId) -> Vec<NodeId> {
let mut path = Vec::new();
let mut curr = Some(node_id);
while let Some(id) = curr {
path.push(id);
curr = ctx.ir.nodes.get(&id).and_then(|node| node.parent);
}
path
}
fn is_descendant(&self, ctx: &ControllerContext, child: NodeId, ancestor: NodeId) -> bool {
let mut curr = Some(child);
while let Some(id) = curr {
if id == ancestor {
return true;
}
if let Some(node) = ctx.ir.nodes.get(&id) {
curr = node.parent;
} else {
break;
}
}
false
}
fn dispatch_annotation_trigger(
&self,
ctx: &mut ControllerContext,
node_id: NodeId,
annotation: &RichTextAnnotation,
trigger: ActionTrigger,
point: LayoutPoint,
) -> bool {
let Some(action_entry) = annotation
.actions
.iter()
.find(|entry| entry.trigger == trigger)
else {
return false;
};
let Some(payload) = &action_entry.payload_data else {
return false;
};
let input = crate::input::scoped_action_input(
ctx.ir,
node_id,
ActionInput::Pointer {
x: point.x,
y: point.y,
delta_x: 0.0,
delta_y: 0.0,
},
);
ctx.dispatched_actions.push((
node_id,
ActionEnvelope {
id: ActionId::from_u128(action_entry.action_id),
payload: payload.clone(),
},
input,
));
true
}
fn find_drag_payload(&self, ctx: &ControllerContext, start_node: NodeId) -> Option<Vec<u8>> {
let mut current_id = Some(start_node);
while let Some(node_id) = current_id {
if let Some(node) = ctx.ir.nodes.get(&node_id) {
if let Op::Semantics(sem) = &node.op {
if let Some(p) = &sem.drag_payload {
return Some(p.clone());
}
}
current_id = node.parent;
} else {
break;
}
}
None
}
fn dispatch_internal_drop(
&self,
ctx: &mut ControllerContext,
target_node: NodeId,
payload: Vec<u8>,
point: LayoutPoint,
) -> bool {
let mut current_id = Some(target_node);
while let Some(node_id) = current_id {
if let Some(node) = ctx.ir.nodes.get(&node_id) {
if let Op::Semantics(sem) = &node.op {
for entry in &sem.actions.entries {
if entry.trigger == ActionTrigger::Drop {
let envelope = ActionEnvelope {
id: ActionId::from_u128(entry.action_id),
payload: entry.payload_data.clone().unwrap_or_default(),
};
let input = crate::input::scoped_action_input(
ctx.ir,
node_id,
ActionInput::InternalDrop {
payload: payload.clone(),
x: point.x,
y: point.y,
},
);
ctx.dispatched_actions.push((node_id, envelope, input));
return true;
}
}
}
current_id = node.parent;
} else {
break;
}
}
false
}
fn dispatch_trigger(
&self,
ctx: &mut ControllerContext,
start_node: NodeId,
trigger: ActionTrigger,
point: LayoutPoint,
delta: Option<LayoutPoint>,
) -> bool {
let mut current_id = Some(start_node);
while let Some(node_id) = current_id {
if let Some(node) = ctx.ir.nodes.get(&node_id) {
if let Op::Semantics(sem) = &node.op {
for entry in &sem.actions.entries {
if entry.trigger == trigger {
let envelope = ActionEnvelope {
id: ActionId::from_u128(entry.action_id),
payload: entry.payload_data.clone().unwrap_or_default(),
};
let input = crate::input::scoped_action_input(
ctx.ir,
node_id,
ActionInput::Pointer {
x: point.x,
y: point.y,
delta_x: delta.map(|d| d.x).unwrap_or(0.0),
delta_y: delta.map(|d| d.y).unwrap_or(0.0),
},
);
ctx.dispatched_actions.push((node_id, envelope, input));
return true;
}
}
}
current_id = node.parent;
} else {
break;
}
}
false
}
fn handle_pan_update(&self, ctx: &mut ControllerContext, delta: LayoutPoint) -> bool {
if let Some(target) = ctx.gesture.target_node {
let mut current = Some(target);
while let Some(id) = current {
if let Some(node) = ctx.ir.nodes.get(&id) {
if let fission_ir::Op::Semantics(sem) = &node.op {
if sem.draggable {
return false;
}
}
if let fission_ir::Op::Layout(fission_ir::op::LayoutOp::Scroll {
direction,
..
}) = &node.op
{
let current_offset = ctx.scroll.get_offset(id);
let move_val = match direction {
fission_ir::op::FlexDirection::Row => -delta.x,
fission_ir::op::FlexDirection::Column => -delta.y,
};
let mut new_offset = current_offset + move_val;
if let Some(geom) = ctx.layout.get_node_geometry(id) {
let max_offset =
if matches!(direction, fission_ir::op::FlexDirection::Row) {
(geom.content_size.width - geom.rect.width()).max(0.0)
} else {
(geom.content_size.height - geom.rect.height()).max(0.0)
};
new_offset = new_offset.clamp(0.0, max_offset);
}
ctx.scroll.set_offset(id, new_offset);
return true;
}
current = node.parent;
} else {
break;
}
}
}
false
}
}