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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
use super::{ControllerContext, InputController};
use crate::event::{ExternalDragEvent, 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, DragSessionPayload, DragSessionState};
use fission_ir::op::RichTextAnnotation;
use fission_ir::{semantics::ActionTrigger, Op, WidgetId};
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;
if let Some(payload) = ctx.gesture.dragging_payload.clone() {
let target = ctx.gesture.target_node;
let source_identifier =
target.and_then(|id| self.semantic_identifier(ctx, id));
ctx.gesture.drag_session = Some(DragSessionState {
source_node: target,
source_identifier,
payload: DragSessionPayload::Internal(payload),
point: *point,
target_node: None,
target_identifier: None,
});
self.update_drag_target(ctx, *point);
}
// 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 {
if let Some(session) = ctx.gesture.drag_session.as_mut() {
session.point = *point;
}
self.update_drag_target(ctx, *point);
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, modifiers, ..
} => {
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, *modifiers,
);
}
}
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)
{
if let Some(menu_owner) =
self.find_context_menu_owner(ctx, up_hit)
{
ctx.context_menu.open(menu_owner, *point);
handled = true;
}
let rich_text_path = self.path_for_node(ctx, up_hit);
if !handled {
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_for_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;
}
}
}
}
}
if !was_secondary {
ctx.context_menu.close();
}
ctx.gesture.start_point = None;
ctx.gesture.is_panning = false;
ctx.gesture.dragging_payload = None;
self.clear_drag_target(ctx, *point);
ctx.gesture.drag_session = None;
ctx.gesture.pressed_button = None;
if scrollbar_drag.is_some() {
ctx.gesture.target_node = None;
return true;
}
return handled;
}
_ => {}
}
}
InputEvent::ExternalDrag(event) => match event {
ExternalDragEvent::Hover { point, paths, .. } => {
ctx.gesture.drag_session = Some(DragSessionState {
source_node: None,
source_identifier: None,
payload: DragSessionPayload::ExternalFiles(paths.clone()),
point: *point,
target_node: ctx
.gesture
.drag_session
.as_ref()
.and_then(|s| s.target_node),
target_identifier: ctx
.gesture
.drag_session
.as_ref()
.and_then(|s| s.target_identifier.clone()),
});
self.update_drag_target(ctx, *point);
return true;
}
ExternalDragEvent::Cancel => {
let point = ctx
.gesture
.drag_session
.as_ref()
.map(|session| session.point)
.unwrap_or(LayoutPoint::ZERO);
self.clear_drag_target(ctx, point);
ctx.gesture.drag_session = None;
return true;
}
ExternalDragEvent::Drop {
point,
paths,
modifiers,
} => {
ctx.gesture.drag_session = Some(DragSessionState {
source_node: None,
source_identifier: None,
payload: DragSessionPayload::ExternalFiles(paths.clone()),
point: *point,
target_node: ctx
.gesture
.drag_session
.as_ref()
.and_then(|s| s.target_node),
target_identifier: ctx
.gesture
.drag_session
.as_ref()
.and_then(|s| s.target_identifier.clone()),
});
self.update_drag_target(ctx, *point);
if let Some(target) = ctx
.gesture
.drag_session
.as_ref()
.and_then(|s| s.target_node)
{
let _ = self.dispatch_external_drop(
ctx,
target,
paths.clone(),
*point,
*modifiers,
);
}
self.clear_drag_target(ctx, *point);
ctx.gesture.drag_session = None;
return true;
}
},
_ => {}
}
false
}
}
impl GestureController {
fn path_for_node(&self, ctx: &ControllerContext, node_id: WidgetId) -> Vec<WidgetId> {
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: WidgetId, ancestor: WidgetId) -> 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 find_context_menu_owner(
&self,
ctx: &ControllerContext,
start_node: WidgetId,
) -> Option<WidgetId> {
let mut current_id = Some(start_node);
while let Some(node_id) = current_id {
let Some(node) = ctx.ir.nodes.get(&node_id) else {
break;
};
if let Op::Semantics(semantics) = &node.op {
if semantics.context_menu && !semantics.disabled {
return Some(node_id);
}
}
current_id = node.parent;
}
None
}
fn dispatch_annotation_trigger(
&self,
ctx: &mut ControllerContext,
node_id: WidgetId,
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: WidgetId) -> 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 semantic_identifier(&self, ctx: &ControllerContext, start_node: WidgetId) -> Option<String> {
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(identifier) = &sem.identifier {
return Some(identifier.clone());
}
}
current_id = node.parent;
} else {
break;
}
}
None
}
fn find_drop_target(&self, ctx: &ControllerContext, start_node: WidgetId) -> Option<WidgetId> {
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 sem
.actions
.entries
.iter()
.any(|entry| entry.trigger == ActionTrigger::Drop)
{
return Some(node_id);
}
}
current_id = node.parent;
} else {
break;
}
}
None
}
fn update_drag_target(&self, ctx: &mut ControllerContext, point: LayoutPoint) {
let next_target =
crate::hit_test::hit_test_with_scroll(ctx.ir, ctx.layout, ctx.scroll, point)
.and_then(|hit| self.find_drop_target(ctx, hit));
let previous_target = ctx
.gesture
.drag_session
.as_ref()
.and_then(|s| s.target_node);
if previous_target == next_target {
return;
}
if let Some(previous) = previous_target {
self.dispatch_trigger(ctx, previous, ActionTrigger::DragLeave, point, None);
}
if let Some(next) = next_target {
self.dispatch_trigger(ctx, next, ActionTrigger::DragEnter, point, None);
}
let next_identifier = next_target.and_then(|id| self.semantic_identifier(ctx, id));
if let Some(session) = ctx.gesture.drag_session.as_mut() {
session.target_node = next_target;
session.target_identifier = next_identifier;
}
}
fn clear_drag_target(&self, ctx: &mut ControllerContext, point: LayoutPoint) {
if let Some(previous) = ctx
.gesture
.drag_session
.as_ref()
.and_then(|s| s.target_node)
{
self.dispatch_trigger(ctx, previous, ActionTrigger::DragLeave, point, None);
}
if let Some(session) = ctx.gesture.drag_session.as_mut() {
session.target_node = None;
session.target_identifier = None;
}
}
fn dispatch_internal_drop(
&self,
ctx: &mut ControllerContext,
target_node: WidgetId,
payload: Vec<u8>,
point: LayoutPoint,
modifiers: u8,
) -> 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,
modifiers,
},
);
ctx.dispatched_actions.push((node_id, envelope, input));
return true;
}
}
}
current_id = node.parent;
} else {
break;
}
}
false
}
fn dispatch_external_drop(
&self,
ctx: &mut ControllerContext,
target_node: WidgetId,
paths: Vec<String>,
point: LayoutPoint,
modifiers: u8,
) -> 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::Drop {
paths: paths.clone(),
x: point.x,
y: point.y,
modifiers,
},
);
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: WidgetId,
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
}
}