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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
mod touch_state;
use crate::data::input::{
Event, EventFilter, KeyboardShortcut, Modifiers, MouseWheelUnit, NUM_POINTER_BUTTONS,
PointerButton, RawInput, TouchDeviceId, ViewportInfo,
};
use crate::{
SafeAreaInsets,
emath::{NumExt as _, Pos2, Rect, Vec2, vec2},
util::History,
};
use std::{
collections::{BTreeMap, HashSet},
time::Duration,
};
pub use crate::Key;
pub use touch_state::MultiTouchInfo;
use touch_state::TouchState;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum SurrenderFocusOn {
/// Surrender focus if the user _presses_ somewhere outside the focused widget.
Presses,
/// Surrender focus if the user _clicks_ somewhere outside the focused widget.
#[default]
Clicks,
/// Never surrender focus.
Never,
}
impl SurrenderFocusOn {
pub fn ui(&mut self, ui: &mut crate::Ui) {
ui.horizontal(|ui| {
ui.selectable_value(self, Self::Presses, "Presses")
.on_hover_text(
"Surrender focus if the user presses somewhere outside the focused widget.",
);
ui.selectable_value(self, Self::Clicks, "Clicks")
.on_hover_text(
"Surrender focus if the user clicks somewhere outside the focused widget.",
);
ui.selectable_value(self, Self::Never, "Never")
.on_hover_text("Never surrender focus.");
});
}
}
/// Options for input state handling.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct InputOptions {
/// Multiplier for the scroll speed when reported in [`crate::MouseWheelUnit::Line`]s.
pub line_scroll_speed: f32,
/// Controls the speed at which we zoom in when doing ctrl/cmd + scroll.
pub scroll_zoom_speed: f32,
/// After a pointer-down event, if the pointer moves more than this, it won't become a click.
pub max_click_dist: f32,
/// If the pointer is down for longer than this it will no longer register as a click.
///
/// If a touch is held for this many seconds while still, then it will register as a
/// "long-touch" which is equivalent to a secondary click.
///
/// This is to support "press and hold for context menu" on touch screens.
pub max_click_duration: f64,
/// The new pointer press must come within this many seconds from previous pointer release
/// for double click (or when this value is doubled, triple click) to count.
pub max_double_click_delay: f64,
/// When this modifier is down, all scroll events are treated as zoom events.
///
/// The default is CTRL/CMD, and it is STRONGLY recommended to NOT change this.
pub zoom_modifier: Modifiers,
/// When this modifier is down, all scroll events are treated as horizontal scrolls,
/// and when combined with [`Self::zoom_modifier`] it will result in zooming
/// on only the horizontal axis.
///
/// The default is SHIFT, and it is STRONGLY recommended to NOT change this.
pub horizontal_scroll_modifier: Modifiers,
/// When this modifier is down, all scroll events are treated as vertical scrolls,
/// and when combined with [`Self::zoom_modifier`] it will result in zooming
/// on only the vertical axis.
pub vertical_scroll_modifier: Modifiers,
/// When should we surrender focus from the focused widget?
pub surrender_focus_on: SurrenderFocusOn,
}
impl Default for InputOptions {
fn default() -> Self {
// TODO(emilk): figure out why these constants need to be different on web and on native (winit).
let is_web = cfg!(target_arch = "wasm32");
let line_scroll_speed = if is_web {
8.0
} else {
40.0 // Scroll speed decided by consensus: https://github.com/emilk/egui/issues/461
};
Self {
line_scroll_speed,
scroll_zoom_speed: 1.0 / 200.0,
max_click_dist: 6.0,
max_click_duration: 0.8,
max_double_click_delay: 0.3,
zoom_modifier: Modifiers::COMMAND,
horizontal_scroll_modifier: Modifiers::SHIFT,
vertical_scroll_modifier: Modifiers::ALT,
surrender_focus_on: SurrenderFocusOn::default(),
}
}
}
impl InputOptions {
/// Show the options in the ui.
pub fn ui(&mut self, ui: &mut crate::Ui) {
let Self {
line_scroll_speed,
scroll_zoom_speed,
max_click_dist,
max_click_duration,
max_double_click_delay,
zoom_modifier,
horizontal_scroll_modifier,
vertical_scroll_modifier,
surrender_focus_on,
} = self;
crate::Grid::new("InputOptions")
.num_columns(2)
.striped(true)
.show(ui, |ui| {
ui.label("Line scroll speed");
ui.add(crate::DragValue::new(line_scroll_speed).range(0.0..=f32::INFINITY))
.on_hover_text(
"How many lines to scroll with each tick of the mouse wheel",
);
ui.end_row();
ui.label("Scroll zoom speed");
ui.add(
crate::DragValue::new(scroll_zoom_speed)
.range(0.0..=f32::INFINITY)
.speed(0.001),
)
.on_hover_text("How fast to zoom with ctrl/cmd + scroll");
ui.end_row();
ui.label("Max click distance");
ui.add(crate::DragValue::new(max_click_dist).range(0.0..=f32::INFINITY))
.on_hover_text(
"If the pointer moves more than this, it won't become a click",
);
ui.end_row();
ui.label("Max click duration");
ui.add(
crate::DragValue::new(max_click_duration)
.range(0.1..=f64::INFINITY)
.speed(0.1),
)
.on_hover_text(
"If the pointer is down for longer than this it will no longer register as a click",
);
ui.end_row();
ui.label("Max double click delay");
ui.add(
crate::DragValue::new(max_double_click_delay)
.range(0.01..=f64::INFINITY)
.speed(0.1),
)
.on_hover_text("Max time interval for double click to count");
ui.end_row();
ui.label("zoom_modifier");
zoom_modifier.ui(ui);
ui.end_row();
ui.label("horizontal_scroll_modifier");
horizontal_scroll_modifier.ui(ui);
ui.end_row();
ui.label("vertical_scroll_modifier");
vertical_scroll_modifier.ui(ui);
ui.end_row();
ui.label("surrender_focus_on");
surrender_focus_on.ui(ui);
ui.end_row();
});
}
}
/// Input state that egui updates each frame.
///
/// You can access this with [`crate::Context::input`].
///
/// You can check if `egui` is using the inputs using
/// [`crate::Context::wants_pointer_input`] and [`crate::Context::wants_keyboard_input`].
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct InputState {
/// The raw input we got this frame from the backend.
pub raw: RawInput,
/// State of the mouse or simple touch gestures which can be mapped to mouse operations.
pub pointer: PointerState,
/// State of touches, except those covered by `PointerState` (like clicks and drags).
/// (We keep a separate [`TouchState`] for each encountered touch device.)
touch_states: BTreeMap<TouchDeviceId, TouchState>,
// ----------------------------------------------
// Scrolling:
//
/// Time of the last scroll event.
last_scroll_time: f64,
/// Used for smoothing the scroll delta.
unprocessed_scroll_delta: Vec2,
/// Used for smoothing the scroll delta when zooming.
unprocessed_scroll_delta_for_zoom: f32,
/// You probably want to use [`Self::smooth_scroll_delta`] instead.
///
/// The raw input of how many points the user scrolled.
///
/// The delta dictates how the _content_ should move.
///
/// A positive X-value indicates the content is being moved right,
/// as when swiping right on a touch-screen or track-pad with natural scrolling.
///
/// A positive Y-value indicates the content is being moved down,
/// as when swiping down on a touch-screen or track-pad with natural scrolling.
///
/// When using a notched scroll-wheel this will spike very large for one frame,
/// then drop to zero. For a smoother experience, use [`Self::smooth_scroll_delta`].
pub raw_scroll_delta: Vec2,
/// How many points the user scrolled, smoothed over a few frames.
///
/// The delta dictates how the _content_ should move.
///
/// A positive X-value indicates the content is being moved right,
/// as when swiping right on a touch-screen or track-pad with natural scrolling.
///
/// A positive Y-value indicates the content is being moved down,
/// as when swiping down on a touch-screen or track-pad with natural scrolling.
///
/// [`crate::ScrollArea`] will both read and write to this field, so that
/// at the end of the frame this will be zero if a scroll-area consumed the delta.
pub smooth_scroll_delta: Vec2,
/// Zoom scale factor this frame (e.g. from ctrl-scroll or pinch gesture).
///
/// * `zoom = 1`: no change.
/// * `zoom < 1`: pinch together
/// * `zoom > 1`: pinch spread
zoom_factor_delta: f32,
/// Rotation in radians this frame, measuring clockwise (e.g. from a rotation gesture).
rotation_radians: f32,
// ----------------------------------------------
/// Position and size of the egui area.
///
/// This is including the area that may be covered by the `safe_area_insets`.
viewport_rect: Rect,
/// The safe area insets, subtracted from the `viewport_rect` in [`Self::content_rect`].
safe_area_insets: SafeAreaInsets,
/// Also known as device pixel ratio, > 1 for high resolution screens.
pub pixels_per_point: f32,
/// Maximum size of one side of a texture.
///
/// This depends on the backend.
pub max_texture_side: usize,
/// Time in seconds. Relative to whatever. Used for animation.
pub time: f64,
/// Time since last frame, in seconds.
///
/// This can be very unstable in reactive mode (when we don't paint each frame).
/// For animations it is therefore better to use [`Self::stable_dt`].
pub unstable_dt: f32,
/// Estimated time until next frame (provided we repaint right away).
///
/// Used for animations to get instant feedback (avoid frame delay).
/// Should be set to the expected time between frames when painting at vsync speeds.
///
/// On most integrations this has a fixed value of `1.0 / 60.0`, so it is not a very accurate estimate.
pub predicted_dt: f32,
/// Time since last frame (in seconds), but gracefully handles the first frame after sleeping in reactive mode.
///
/// In reactive mode (available in e.g. `eframe`), `egui` only updates when there is new input
/// or something is animating.
/// This can lead to large gaps of time (sleep), leading to large [`Self::unstable_dt`].
///
/// If `egui` requested a repaint the previous frame, then `egui` will use
/// `stable_dt = unstable_dt;`, but if `egui` did not not request a repaint last frame,
/// then `egui` will assume `unstable_dt` is too large, and will use
/// `stable_dt = predicted_dt;`.
///
/// This means that for the first frame after a sleep,
/// `stable_dt` will be a prediction of the delta-time until the next frame,
/// and in all other situations this will be an accurate measurement of time passed
/// since the previous frame.
///
/// Note that a frame can still stall for various reasons, so `stable_dt` can
/// still be unusually large in some situations.
///
/// When animating something, it is recommended that you use something like
/// `stable_dt.min(0.1)` - this will give you smooth animations when the framerate is good
/// (even in reactive mode), but will avoid large jumps when framerate is bad,
/// and will effectively slow down the animation when FPS drops below 10.
pub stable_dt: f32,
/// The native window has the keyboard focus (i.e. is receiving key presses).
///
/// False when the user alt-tab away from the application, for instance.
pub focused: bool,
/// Which modifier keys are down at the start of the frame?
pub modifiers: Modifiers,
// The keys that are currently being held down.
pub keys_down: HashSet<Key>,
/// In-order events received this frame
pub events: Vec<Event>,
/// Input state management configuration.
///
/// This gets copied from `egui::Options` at the start of each frame for convenience.
options: InputOptions,
}
impl Default for InputState {
fn default() -> Self {
Self {
raw: Default::default(),
pointer: Default::default(),
touch_states: Default::default(),
last_scroll_time: f64::NEG_INFINITY,
unprocessed_scroll_delta: Vec2::ZERO,
unprocessed_scroll_delta_for_zoom: 0.0,
raw_scroll_delta: Vec2::ZERO,
smooth_scroll_delta: Vec2::ZERO,
zoom_factor_delta: 1.0,
rotation_radians: 0.0,
viewport_rect: Rect::from_min_size(Default::default(), vec2(10_000.0, 10_000.0)),
safe_area_insets: Default::default(),
pixels_per_point: 1.0,
max_texture_side: 2048,
time: 0.0,
unstable_dt: 1.0 / 60.0,
predicted_dt: 1.0 / 60.0,
stable_dt: 1.0 / 60.0,
focused: false,
modifiers: Default::default(),
keys_down: Default::default(),
events: Default::default(),
options: Default::default(),
}
}
}
impl InputState {
#[must_use]
pub fn begin_pass(
mut self,
mut new: RawInput,
requested_immediate_repaint_prev_frame: bool,
pixels_per_point: f32,
options: InputOptions,
) -> Self {
profiling::function_scope!();
let time = new.time.unwrap_or(self.time + new.predicted_dt as f64);
let unstable_dt = (time - self.time) as f32;
let stable_dt = if requested_immediate_repaint_prev_frame {
// we should have had a repaint straight away,
// so this should be trustable.
unstable_dt
} else {
new.predicted_dt
};
let safe_area_insets = new.safe_area_insets.unwrap_or(self.safe_area_insets);
let viewport_rect = new.screen_rect.unwrap_or(self.viewport_rect);
self.create_touch_states_for_new_devices(&new.events);
for touch_state in self.touch_states.values_mut() {
touch_state.begin_pass(time, &new, self.pointer.interact_pos);
}
let pointer = self.pointer.begin_pass(time, &new, options);
let mut keys_down = self.keys_down;
let mut zoom_factor_delta = 1.0; // TODO(emilk): smoothing for zoom factor
let mut rotation_radians = 0.0;
let mut raw_scroll_delta = Vec2::ZERO;
let mut unprocessed_scroll_delta = self.unprocessed_scroll_delta;
let mut unprocessed_scroll_delta_for_zoom = self.unprocessed_scroll_delta_for_zoom;
let mut smooth_scroll_delta = Vec2::ZERO;
let mut smooth_scroll_delta_for_zoom = 0.0;
for event in &mut new.events {
match event {
Event::Key {
key,
pressed,
repeat,
..
} => {
if *pressed {
let first_press = keys_down.insert(*key);
*repeat = !first_press;
} else {
keys_down.remove(key);
}
}
Event::MouseWheel {
unit,
delta,
modifiers,
} => {
let mut delta = match unit {
MouseWheelUnit::Point => *delta,
MouseWheelUnit::Line => options.line_scroll_speed * *delta,
MouseWheelUnit::Page => viewport_rect.height() * *delta,
};
let is_horizontal = modifiers.matches_any(options.horizontal_scroll_modifier);
let is_vertical = modifiers.matches_any(options.vertical_scroll_modifier);
if is_horizontal && !is_vertical {
// Treat all scrolling as horizontal scrolling.
// Note: one Mac we already get horizontal scroll events when shift is down.
delta = vec2(delta.x + delta.y, 0.0);
}
if !is_horizontal && is_vertical {
// Treat all scrolling as vertical scrolling.
delta = vec2(0.0, delta.x + delta.y);
}
raw_scroll_delta += delta;
// Mouse wheels often go very large steps.
// A single notch on a logitech mouse wheel connected to a Macbook returns 14.0 raw_scroll_delta.
// So we smooth it out over several frames for a nicer user experience when scrolling in egui.
// BUT: if the user is using a nice smooth mac trackpad, we don't add smoothing,
// because it adds latency.
let is_smooth = match unit {
MouseWheelUnit::Point => delta.length() < 8.0, // a bit arbitrary here
MouseWheelUnit::Line | MouseWheelUnit::Page => false,
};
let is_zoom = modifiers.matches_any(options.zoom_modifier);
#[expect(clippy::collapsible_else_if)]
if is_zoom {
if is_smooth {
smooth_scroll_delta_for_zoom += delta.x + delta.y;
} else {
unprocessed_scroll_delta_for_zoom += delta.x + delta.y;
}
} else {
if is_smooth {
smooth_scroll_delta += delta;
} else {
unprocessed_scroll_delta += delta;
}
}
}
Event::Zoom(factor) => {
zoom_factor_delta *= *factor;
}
Event::Rotate(radians) => {
rotation_radians += *radians;
}
Event::WindowFocused(false) => {
// Example: pressing `Cmd+S` brings up a save-dialog (e.g. using rfd),
// but we get no key-up event for the `S` key (in winit).
// This leads to `S` being mistakenly marked as down when we switch back to the app.
// So we take the safe route and just clear all the keys and modifiers when
// the app loses focus.
keys_down.clear();
}
_ => {}
}
}
{
let dt = stable_dt.at_most(0.1);
let t = crate::emath::exponential_smooth_factor(0.90, 0.1, dt); // reach _% in _ seconds. TODO(emilk): parameterize
if unprocessed_scroll_delta != Vec2::ZERO {
for d in 0..2 {
if unprocessed_scroll_delta[d].abs() < 1.0 {
smooth_scroll_delta[d] += unprocessed_scroll_delta[d];
unprocessed_scroll_delta[d] = 0.0;
} else {
let applied = t * unprocessed_scroll_delta[d];
smooth_scroll_delta[d] += applied;
unprocessed_scroll_delta[d] -= applied;
}
}
}
{
// Smooth scroll-to-zoom:
if unprocessed_scroll_delta_for_zoom.abs() < 1.0 {
smooth_scroll_delta_for_zoom += unprocessed_scroll_delta_for_zoom;
unprocessed_scroll_delta_for_zoom = 0.0;
} else {
let applied = t * unprocessed_scroll_delta_for_zoom;
smooth_scroll_delta_for_zoom += applied;
unprocessed_scroll_delta_for_zoom -= applied;
}
zoom_factor_delta *=
(options.scroll_zoom_speed * smooth_scroll_delta_for_zoom).exp();
}
}
let is_scrolling = raw_scroll_delta != Vec2::ZERO || smooth_scroll_delta != Vec2::ZERO;
let last_scroll_time = if is_scrolling {
time
} else {
self.last_scroll_time
};
Self {
pointer,
touch_states: self.touch_states,
last_scroll_time,
unprocessed_scroll_delta,
unprocessed_scroll_delta_for_zoom,
raw_scroll_delta,
smooth_scroll_delta,
zoom_factor_delta,
rotation_radians,
viewport_rect,
safe_area_insets,
pixels_per_point,
max_texture_side: new.max_texture_side.unwrap_or(self.max_texture_side),
time,
unstable_dt,
predicted_dt: new.predicted_dt,
stable_dt,
focused: new.focused,
modifiers: new.modifiers,
keys_down,
events: new.events.clone(), // TODO(emilk): remove clone() and use raw.events
raw: new,
options,
}
}
/// Info about the active viewport
#[inline]
pub fn viewport(&self) -> &ViewportInfo {
self.raw.viewport()
}
/// Returns the region of the screen that is safe for content rendering
///
/// Returns the `viewport_rect` with the `safe_area_insets` removed.
///
/// If you want to render behind e.g. the dynamic island on iOS, use [`Self::viewport_rect`].
///
/// See also [`RawInput::safe_area_insets`].
#[inline(always)]
pub fn content_rect(&self) -> Rect {
self.viewport_rect - self.safe_area_insets
}
/// Returns the full area available to egui, including parts that might be partially covered,
/// for example, by the OS status bar or notches (see [`Self::safe_area_insets`]).
///
/// Usually you want to use [`Self::content_rect`] instead.
///
/// This rectangle includes e.g. the dynamic island on iOS.
/// If you want to only render _below_ the that (not behind), then you should use
/// [`Self::content_rect`] instead.
///
/// See also [`RawInput::safe_area_insets`].
pub fn viewport_rect(&self) -> Rect {
self.viewport_rect
}
/// Position and size of the egui area.
#[deprecated(
note = "screen_rect has been split into viewport_rect() and content_rect(). You likely should use content_rect()"
)]
pub fn screen_rect(&self) -> Rect {
self.content_rect()
}
/// Get the safe area insets.
///
/// This represents the area of the screen covered by status bars, navigation controls, notches,
/// or other items that obscure part of the screen.
///
/// See [`Self::content_rect`] to get the `viewport_rect` with the safe area insets removed.
pub fn safe_area_insets(&self) -> SafeAreaInsets {
self.safe_area_insets
}
/// Uniform zoom scale factor this frame (e.g. from ctrl-scroll or pinch gesture).
/// * `zoom = 1`: no change
/// * `zoom < 1`: pinch together
/// * `zoom > 1`: pinch spread
///
/// If your application supports non-proportional zooming,
/// then you probably want to use [`Self::zoom_delta_2d`] instead.
#[inline(always)]
pub fn zoom_delta(&self) -> f32 {
// If a multi touch gesture is detected, it measures the exact and linear proportions of
// the distances of the finger tips. It is therefore potentially more accurate than
// `zoom_factor_delta` which is based on the `ctrl-scroll` event which, in turn, may be
// synthesized from an original touch gesture.
self.multi_touch()
.map_or(self.zoom_factor_delta, |touch| touch.zoom_delta)
}
/// 2D non-proportional zoom scale factor this frame (e.g. from ctrl-scroll or pinch gesture).
///
/// For multitouch devices the user can do a horizontal or vertical pinch gesture.
/// In these cases a non-proportional zoom factor is a available.
/// In other cases, this reverts to `Vec2::splat(self.zoom_delta())`.
///
/// For horizontal pinches, this will return `[z, 1]`,
/// for vertical pinches this will return `[1, z]`,
/// and otherwise this will return `[z, z]`,
/// where `z` is the zoom factor:
/// * `zoom = 1`: no change
/// * `zoom < 1`: pinch together
/// * `zoom > 1`: pinch spread
#[inline(always)]
pub fn zoom_delta_2d(&self) -> Vec2 {
// If a multi touch gesture is detected, it measures the exact and linear proportions of
// the distances of the finger tips. It is therefore potentially more accurate than
// `zoom_factor_delta` which is based on the `ctrl-scroll` event which, in turn, may be
// synthesized from an original touch gesture.
if let Some(multi_touch) = self.multi_touch() {
multi_touch.zoom_delta_2d
} else {
let mut zoom = Vec2::splat(self.zoom_factor_delta);
let is_horizontal = self
.modifiers
.matches_any(self.options.horizontal_scroll_modifier);
let is_vertical = self
.modifiers
.matches_any(self.options.vertical_scroll_modifier);
if is_horizontal && !is_vertical {
// Horizontal-only zooming.
zoom.y = 1.0;
}
if !is_horizontal && is_vertical {
// Vertical-only zooming.
zoom.x = 1.0;
}
zoom
}
}
/// Rotation in radians this frame, measuring clockwise (e.g. from a rotation gesture).
#[inline(always)]
pub fn rotation_delta(&self) -> f32 {
self.multi_touch()
.map_or(self.rotation_radians, |touch| touch.rotation_delta)
}
/// Panning translation in pixels this frame (e.g. from scrolling or a pan gesture)
///
/// The delta indicates how the **content** should move.
///
/// A positive X-value indicates the content is being moved right, as when swiping right on a touch-screen or track-pad with natural scrolling.
///
/// A positive Y-value indicates the content is being moved down, as when swiping down on a touch-screen or track-pad with natural scrolling.
#[inline(always)]
pub fn translation_delta(&self) -> Vec2 {
self.multi_touch()
.map_or(self.smooth_scroll_delta, |touch| touch.translation_delta)
}
/// How long has it been (in seconds) since the use last scrolled?
#[inline(always)]
pub fn time_since_last_scroll(&self) -> f32 {
(self.time - self.last_scroll_time) as f32
}
/// The [`crate::Context`] will call this at the beginning of each frame to see if we need a repaint.
///
/// Returns how long to wait for a repaint.
///
/// NOTE: It's important to call this immediately after [`Self::begin_pass`] since calls to
/// [`Self::consume_key`] will remove events from the vec, meaning those key presses wouldn't
/// cause a repaint.
pub(crate) fn wants_repaint_after(&self) -> Option<Duration> {
if self.pointer.wants_repaint()
|| self.unprocessed_scroll_delta.abs().max_elem() > 0.2
|| self.unprocessed_scroll_delta_for_zoom.abs() > 0.2
|| !self.events.is_empty()
{
// Immediate repaint
return Some(Duration::ZERO);
}
if self.any_touches() && !self.pointer.is_decidedly_dragging() {
// We need to wake up and check for press-and-hold for the context menu.
if let Some(press_start_time) = self.pointer.press_start_time {
let press_duration = self.time - press_start_time;
if self.options.max_click_duration.is_finite()
&& press_duration < self.options.max_click_duration
{
let secs_until_menu = self.options.max_click_duration - press_duration;
return Some(Duration::from_secs_f64(secs_until_menu));
}
}
}
None
}
/// Count presses of a key. If non-zero, the presses are consumed, so that this will only return non-zero once.
///
/// Includes key-repeat events.
///
/// This uses [`Modifiers::matches_logically`] to match modifiers,
/// meaning extra Shift and Alt modifiers are ignored.
/// Therefore, you should match most specific shortcuts first,
/// i.e. check for `Cmd-Shift-S` ("Save as…") before `Cmd-S` ("Save"),
/// so that a user pressing `Cmd-Shift-S` won't trigger the wrong command!
pub fn count_and_consume_key(&mut self, modifiers: Modifiers, logical_key: Key) -> usize {
let mut count = 0usize;
self.events.retain(|event| {
let is_match = matches!(
event,
Event::Key {
key: ev_key,
modifiers: ev_mods,
pressed: true,
..
} if *ev_key == logical_key && ev_mods.matches_logically(modifiers)
);
count += is_match as usize;
!is_match
});
count
}
/// Check for a key press. If found, `true` is returned and the key pressed is consumed, so that this will only return `true` once.
///
/// Includes key-repeat events.
///
/// This uses [`Modifiers::matches_logically`] to match modifiers,
/// meaning extra Shift and Alt modifiers are ignored.
/// Therefore, you should match most specific shortcuts first,
/// i.e. check for `Cmd-Shift-S` ("Save as…") before `Cmd-S` ("Save"),
/// so that a user pressing `Cmd-Shift-S` won't trigger the wrong command!
pub fn consume_key(&mut self, modifiers: Modifiers, logical_key: Key) -> bool {
self.count_and_consume_key(modifiers, logical_key) > 0
}
/// Check if the given shortcut has been pressed.
///
/// If so, `true` is returned and the key pressed is consumed, so that this will only return `true` once.
///
/// This uses [`Modifiers::matches_logically`] to match modifiers,
/// meaning extra Shift and Alt modifiers are ignored.
/// Therefore, you should match most specific shortcuts first,
/// i.e. check for `Cmd-Shift-S` ("Save as…") before `Cmd-S` ("Save"),
/// so that a user pressing `Cmd-Shift-S` won't trigger the wrong command!
pub fn consume_shortcut(&mut self, shortcut: &KeyboardShortcut) -> bool {
let KeyboardShortcut {
modifiers,
logical_key,
} = *shortcut;
self.consume_key(modifiers, logical_key)
}
/// Was the given key pressed this frame?
///
/// Includes key-repeat events.
pub fn key_pressed(&self, desired_key: Key) -> bool {
self.num_presses(desired_key) > 0
}
/// How many times was the given key pressed this frame?
///
/// Includes key-repeat events.
pub fn num_presses(&self, desired_key: Key) -> usize {
self.events
.iter()
.filter(|event| {
matches!(
event,
Event::Key { key, pressed: true, .. }
if *key == desired_key
)
})
.count()
}
/// Is the given key currently held down?
pub fn key_down(&self, desired_key: Key) -> bool {
self.keys_down.contains(&desired_key)
}
/// Was the given key released this frame?
pub fn key_released(&self, desired_key: Key) -> bool {
self.events.iter().any(|event| {
matches!(
event,
Event::Key {
key,
pressed: false,
..
} if *key == desired_key
)
})
}
/// Also known as device pixel ratio, > 1 for high resolution screens.
#[inline(always)]
pub fn pixels_per_point(&self) -> f32 {
self.pixels_per_point
}
/// Size of a physical pixel in logical gui coordinates (points).
#[inline(always)]
pub fn physical_pixel_size(&self) -> f32 {
1.0 / self.pixels_per_point()
}
/// How imprecise do we expect the mouse/touch input to be?
/// Returns imprecision in points.
#[inline(always)]
pub fn aim_radius(&self) -> f32 {
// TODO(emilk): multiply by ~3 for touch inputs because fingers are fat
self.physical_pixel_size()
}
/// Returns details about the currently ongoing multi-touch gesture, if any. Note that this
/// method returns `None` for single-touch gestures (click, drag, …).
///
/// ```
/// # use egui::emath::Rot2;
/// # egui::__run_test_ui(|ui| {
/// let mut zoom = 1.0; // no zoom
/// let mut rotation = 0.0; // no rotation
/// let multi_touch = ui.input(|i| i.multi_touch());
/// if let Some(multi_touch) = multi_touch {
/// zoom *= multi_touch.zoom_delta;
/// rotation += multi_touch.rotation_delta;
/// }
/// let transform = zoom * Rot2::from_angle(rotation);
/// # });
/// ```
///
/// By far not all touch devices are supported, and the details depend on the `egui`
/// integration backend you are using. `eframe` web supports multi touch for most mobile
/// devices, but not for a `Trackpad` on `MacOS`, for example. The backend has to be able to
/// capture native touch events, but many browsers seem to pass such events only for touch
/// _screens_, but not touch _pads._
///
/// Refer to [`MultiTouchInfo`] for details about the touch information available.
///
/// Consider using `zoom_delta()` instead of `MultiTouchInfo::zoom_delta` as the former
/// delivers a synthetic zoom factor based on ctrl-scroll events, as a fallback.
pub fn multi_touch(&self) -> Option<MultiTouchInfo> {
// In case of multiple touch devices simply pick the touch_state of the first active device
self.touch_states.values().find_map(|t| t.info())
}
/// True if there currently are any fingers touching egui.
pub fn any_touches(&self) -> bool {
self.touch_states.values().any(|t| t.any_touches())
}
/// True if we have ever received a touch event.
pub fn has_touch_screen(&self) -> bool {
!self.touch_states.is_empty()
}
/// Scans `events` for device IDs of touch devices we have not seen before,
/// and creates a new [`TouchState`] for each such device.
fn create_touch_states_for_new_devices(&mut self, events: &[Event]) {
for event in events {
if let Event::Touch { device_id, .. } = event {
self.touch_states
.entry(*device_id)
.or_insert_with(|| TouchState::new(*device_id));
}
}
}
#[cfg(feature = "accesskit")]
pub fn accesskit_action_requests(
&self,
id: crate::Id,
action: accesskit::Action,
) -> impl Iterator<Item = &accesskit::ActionRequest> {
let accesskit_id = id.accesskit_id();
self.events.iter().filter_map(move |event| {
if let Event::AccessKitActionRequest(request) = event
&& request.target == accesskit_id
&& request.action == action
{
return Some(request);
}
None
})
}
#[cfg(feature = "accesskit")]
pub fn consume_accesskit_action_requests(
&mut self,
id: crate::Id,
mut consume: impl FnMut(&accesskit::ActionRequest) -> bool,
) {
let accesskit_id = id.accesskit_id();
self.events.retain(|event| {
if let Event::AccessKitActionRequest(request) = event
&& request.target == accesskit_id
{
return !consume(request);
}
true
});
}
#[cfg(feature = "accesskit")]
pub fn has_accesskit_action_request(&self, id: crate::Id, action: accesskit::Action) -> bool {
self.accesskit_action_requests(id, action).next().is_some()
}
#[cfg(feature = "accesskit")]
pub fn num_accesskit_action_requests(&self, id: crate::Id, action: accesskit::Action) -> usize {
self.accesskit_action_requests(id, action).count()
}
/// Get all events that matches the given filter.
pub fn filtered_events(&self, filter: &EventFilter) -> Vec<Event> {
self.events
.iter()
.filter(|event| filter.matches(event))
.cloned()
.collect()
}
/// A long press is something we detect on touch screens
/// to trigger a secondary click (context menu).
///
/// Returns `true` only on one frame.
pub(crate) fn is_long_touch(&self) -> bool {
self.any_touches() && self.pointer.is_long_press()
}
}
// ----------------------------------------------------------------------------
/// A pointer (mouse or touch) click.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub(crate) struct Click {
pub pos: Pos2,
/// 1 or 2 (double-click) or 3 (triple-click)
pub count: u32,
/// Allows you to check for e.g. shift-click
pub modifiers: Modifiers,
}
impl Click {
pub fn is_double(&self) -> bool {
self.count == 2
}
pub fn is_triple(&self) -> bool {
self.count == 3
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub(crate) enum PointerEvent {
Moved(Pos2),
Pressed {
position: Pos2,
button: PointerButton,
},
Released {
click: Option<Click>,
button: PointerButton,
},
}
impl PointerEvent {
pub fn is_press(&self) -> bool {
matches!(self, Self::Pressed { .. })
}
pub fn is_release(&self) -> bool {
matches!(self, Self::Released { .. })
}
pub fn is_click(&self) -> bool {
matches!(self, Self::Released { click: Some(_), .. })
}
}
/// Mouse or touch state.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct PointerState {
/// Latest known time
time: f64,
// Consider a finger tapping a touch screen.
// What position should we report?
// The location of the touch, or `None`, because the finger is gone?
//
// For some cases we want the first: e.g. to check for interaction.
// For showing tooltips, we want the latter (no tooltips, since there are no fingers).
/// Latest reported pointer position.
/// When tapping a touch screen, this will be `None`.
latest_pos: Option<Pos2>,
/// Latest position of the mouse, but ignoring any [`Event::PointerGone`]
/// if there were interactions this frame.
/// When tapping a touch screen, this will be the location of the touch.
interact_pos: Option<Pos2>,
/// How much the pointer moved compared to last frame, in points.
delta: Vec2,
/// How much the mouse moved since the last frame, in unspecified units.
/// Represents the actual movement of the mouse, without acceleration or clamped by screen edges.
/// May be unavailable on some integrations.
motion: Option<Vec2>,
/// Current velocity of pointer.
velocity: Vec2,
/// Current direction of pointer.
direction: Vec2,
/// Recent movement of the pointer.
/// Used for calculating velocity of pointer.
pos_history: History<Pos2>,
down: [bool; NUM_POINTER_BUTTONS],
/// Where did the current click/drag originate?
/// `None` if no mouse button is down.
press_origin: Option<Pos2>,
/// When did the current click/drag originate?
/// `None` if no mouse button is down.
press_start_time: Option<f64>,
/// Set to `true` if the pointer has moved too much (since being pressed)
/// for it to be registered as a click.
pub(crate) has_moved_too_much_for_a_click: bool,
/// Did [`Self::is_decidedly_dragging`] go from `false` to `true` this frame?
///
/// This could also be the trigger point for a long-touch.
pub(crate) started_decidedly_dragging: bool,
/// When did the pointer get click last?
/// Used to check for double-clicks.
last_click_time: f64,
/// When did the pointer get click two clicks ago?
/// Used to check for triple-clicks.
last_last_click_time: f64,
/// When was the pointer last moved?
/// Used for things like showing hover ui/tooltip with a delay.
last_move_time: f64,
/// All button events that occurred this frame
pub(crate) pointer_events: Vec<PointerEvent>,
/// Input state management configuration.
///
/// This gets copied from `egui::Options` at the start of each frame for convenience.
options: InputOptions,
}
impl Default for PointerState {
fn default() -> Self {
Self {
time: -f64::INFINITY,
latest_pos: None,
interact_pos: None,
delta: Vec2::ZERO,
motion: None,
velocity: Vec2::ZERO,
direction: Vec2::ZERO,
pos_history: History::new(2..1000, 0.1),
down: Default::default(),
press_origin: None,
press_start_time: None,
has_moved_too_much_for_a_click: false,
started_decidedly_dragging: false,
last_click_time: f64::NEG_INFINITY,
last_last_click_time: f64::NEG_INFINITY,
last_move_time: f64::NEG_INFINITY,
pointer_events: vec![],
options: Default::default(),
}
}
}
impl PointerState {
#[must_use]
pub(crate) fn begin_pass(mut self, time: f64, new: &RawInput, options: InputOptions) -> Self {
let was_decidedly_dragging = self.is_decidedly_dragging();
self.time = time;
self.options = options;
self.pointer_events.clear();
let old_pos = self.latest_pos;
self.interact_pos = self.latest_pos;
if self.motion.is_some() {
self.motion = Some(Vec2::ZERO);
}
let mut clear_history_after_velocity_calculation = false;
for event in &new.events {
match event {
Event::PointerMoved(pos) => {
let pos = *pos;
self.latest_pos = Some(pos);
self.interact_pos = Some(pos);
if let Some(press_origin) = self.press_origin {
self.has_moved_too_much_for_a_click |=
press_origin.distance(pos) > self.options.max_click_dist;
}
self.last_move_time = time;
self.pointer_events.push(PointerEvent::Moved(pos));
}
Event::PointerButton {
pos,
button,
pressed,
modifiers,
} => {
let pos = *pos;
let button = *button;
let pressed = *pressed;
let modifiers = *modifiers;
self.latest_pos = Some(pos);
self.interact_pos = Some(pos);
if pressed {
// Start of a drag: we want to track the velocity for during the drag
// and ignore any incoming movement
self.pos_history.clear();
}
if pressed {
self.press_origin = Some(pos);
self.press_start_time = Some(time);
self.has_moved_too_much_for_a_click = false;
self.pointer_events.push(PointerEvent::Pressed {
position: pos,
button,
});
} else {
// Released
let clicked = self.could_any_button_be_click();
let click = if clicked {
let double_click =
(time - self.last_click_time) < self.options.max_double_click_delay;
let triple_click = (time - self.last_last_click_time)
< (self.options.max_double_click_delay * 2.0);
let count = if triple_click {
3
} else if double_click {
2
} else {
1
};
self.last_last_click_time = self.last_click_time;
self.last_click_time = time;
Some(Click {
pos,
count,
modifiers,
})
} else {
None
};
self.pointer_events
.push(PointerEvent::Released { click, button });
self.press_origin = None;
self.press_start_time = None;
}
self.down[button as usize] = pressed; // must be done after the above call to `could_any_button_be_click`
}
Event::PointerGone => {
self.latest_pos = None;
// When dragging a slider and the mouse leaves the viewport, we still want the drag to work,
// so we don't treat this as a `PointerEvent::Released`.
// NOTE: we do NOT clear `self.interact_pos` here. It will be cleared next frame.
// Delay the clearing until after the final velocity calculation, so we can
// get the final velocity when `drag_stopped` is true.
clear_history_after_velocity_calculation = true;
}
Event::MouseMoved(delta) => *self.motion.get_or_insert(Vec2::ZERO) += *delta,
_ => {}
}
}
self.delta = if let (Some(old_pos), Some(new_pos)) = (old_pos, self.latest_pos) {
new_pos - old_pos
} else {
Vec2::ZERO
};
if let Some(pos) = self.latest_pos {
self.pos_history.add(time, pos);
} else {
// we do not clear the `pos_history` here, because it is exactly when a finger has
// released from the touch screen that we may want to assign a velocity to whatever
// the user tried to throw.
}
self.pos_history.flush(time);
self.velocity = if self.pos_history.len() >= 3 && self.pos_history.duration() > 0.01 {
self.pos_history.velocity().unwrap_or_default()
} else {
Vec2::default()
};
if self.velocity != Vec2::ZERO {
self.last_move_time = time;
}
if clear_history_after_velocity_calculation {
self.pos_history.clear();
}
self.direction = self.pos_history.velocity().unwrap_or_default().normalized();
self.started_decidedly_dragging = self.is_decidedly_dragging() && !was_decidedly_dragging;
self
}
fn wants_repaint(&self) -> bool {
!self.pointer_events.is_empty() || self.delta != Vec2::ZERO
}
/// How much the pointer moved compared to last frame, in points.
#[inline(always)]
pub fn delta(&self) -> Vec2 {
self.delta
}
/// How much the mouse moved since the last frame, in unspecified units.
/// Represents the actual movement of the mouse, without acceleration or clamped by screen edges.
/// May be unavailable on some integrations.
#[inline(always)]
pub fn motion(&self) -> Option<Vec2> {
self.motion
}
/// Current velocity of pointer.
///
/// This is smoothed over a few frames,
/// but can be ZERO when frame-rate is bad.
#[inline(always)]
pub fn velocity(&self) -> Vec2 {
self.velocity
}
/// Current direction of the pointer.
///
/// This is less sensitive to bad framerate than [`Self::velocity`].
#[inline(always)]
pub fn direction(&self) -> Vec2 {
self.direction
}
/// Where did the current click/drag originate?
/// `None` if no mouse button is down.
#[inline(always)]
pub fn press_origin(&self) -> Option<Pos2> {
self.press_origin
}
/// How far has the pointer moved since the start of the drag (if any)?
pub fn total_drag_delta(&self) -> Option<Vec2> {
Some(self.latest_pos? - self.press_origin?)
}
/// When did the current click/drag originate?
/// `None` if no mouse button is down.
#[inline(always)]
pub fn press_start_time(&self) -> Option<f64> {
self.press_start_time
}
/// Latest reported pointer position.
/// When tapping a touch screen, this will be `None`.
#[inline(always)]
pub fn latest_pos(&self) -> Option<Pos2> {
self.latest_pos
}
/// If it is a good idea to show a tooltip, where is pointer?
#[inline(always)]
pub fn hover_pos(&self) -> Option<Pos2> {
self.latest_pos
}
/// If you detect a click or drag and wants to know where it happened, use this.
///
/// Latest position of the mouse, but ignoring any [`Event::PointerGone`]
/// if there were interactions this frame.
/// When tapping a touch screen, this will be the location of the touch.
#[inline(always)]
pub fn interact_pos(&self) -> Option<Pos2> {
self.interact_pos
}
/// Do we have a pointer?
///
/// `false` if the mouse is not over the egui area, or if no touches are down on touch screens.
#[inline(always)]
pub fn has_pointer(&self) -> bool {
self.latest_pos.is_some()
}
/// Is the pointer currently still?
/// This is smoothed so a few frames of stillness is required before this returns `true`.
#[inline(always)]
pub fn is_still(&self) -> bool {
self.velocity == Vec2::ZERO
}
/// Is the pointer currently moving?
/// This is smoothed so a few frames of stillness is required before this returns `false`.
#[inline]
pub fn is_moving(&self) -> bool {
self.velocity != Vec2::ZERO
}
/// How long has it been (in seconds) since the pointer was last moved?
#[inline(always)]
pub fn time_since_last_movement(&self) -> f32 {
(self.time - self.last_move_time) as f32
}
/// How long has it been (in seconds) since the pointer was clicked?
#[inline(always)]
pub fn time_since_last_click(&self) -> f32 {
(self.time - self.last_click_time) as f32
}
/// Was any pointer button pressed (`!down -> down`) this frame?
///
/// This can sometimes return `true` even if `any_down() == false`
/// because a press can be shorted than one frame.
pub fn any_pressed(&self) -> bool {
self.pointer_events.iter().any(|event| event.is_press())
}
/// Was any pointer button released (`down -> !down`) this frame?
pub fn any_released(&self) -> bool {
self.pointer_events.iter().any(|event| event.is_release())
}
/// Was the button given pressed this frame?
pub fn button_pressed(&self, button: PointerButton) -> bool {
self.pointer_events
.iter()
.any(|event| matches!(event, &PointerEvent::Pressed{button: b, ..} if button == b))
}
/// Was the button given released this frame?
pub fn button_released(&self, button: PointerButton) -> bool {
self.pointer_events
.iter()
.any(|event| matches!(event, &PointerEvent::Released{button: b, ..} if button == b))
}
/// Was the primary button pressed this frame?
pub fn primary_pressed(&self) -> bool {
self.button_pressed(PointerButton::Primary)
}
/// Was the secondary button pressed this frame?
pub fn secondary_pressed(&self) -> bool {
self.button_pressed(PointerButton::Secondary)
}
/// Was the primary button released this frame?
pub fn primary_released(&self) -> bool {
self.button_released(PointerButton::Primary)
}
/// Was the secondary button released this frame?
pub fn secondary_released(&self) -> bool {
self.button_released(PointerButton::Secondary)
}
/// Is any pointer button currently down?
pub fn any_down(&self) -> bool {
self.down.iter().any(|&down| down)
}
/// Were there any type of click this frame?
pub fn any_click(&self) -> bool {
self.pointer_events.iter().any(|event| event.is_click())
}
/// Was the given pointer button given clicked this frame?
///
/// Returns true on double- and triple- clicks too.
pub fn button_clicked(&self, button: PointerButton) -> bool {
self.pointer_events
.iter()
.any(|event| matches!(event, &PointerEvent::Released { button: b, click: Some(_) } if button == b))
}
/// Was the button given double clicked this frame?
pub fn button_double_clicked(&self, button: PointerButton) -> bool {
self.pointer_events.iter().any(|event| {
matches!(
&event,
PointerEvent::Released {
click: Some(click),
button: b,
} if *b == button && click.is_double()
)
})
}
/// Was the button given triple clicked this frame?
pub fn button_triple_clicked(&self, button: PointerButton) -> bool {
self.pointer_events.iter().any(|event| {
matches!(
&event,
PointerEvent::Released {
click: Some(click),
button: b,
} if *b == button && click.is_triple()
)
})
}
/// Was the primary button clicked this frame?
pub fn primary_clicked(&self) -> bool {
self.button_clicked(PointerButton::Primary)
}
/// Was the secondary button clicked this frame?
pub fn secondary_clicked(&self) -> bool {
self.button_clicked(PointerButton::Secondary)
}
/// Is this button currently down?
#[inline(always)]
pub fn button_down(&self, button: PointerButton) -> bool {
self.down[button as usize]
}
/// If the pointer button is down, will it register as a click when released?
///
/// See also [`Self::is_decidedly_dragging`].
pub fn could_any_button_be_click(&self) -> bool {
if self.any_down() || self.any_released() {
if self.has_moved_too_much_for_a_click {
return false;
}
if let Some(press_start_time) = self.press_start_time
&& self.time - press_start_time > self.options.max_click_duration
{
return false;
}
true
} else {
false
}
}
/// Just because the mouse is down doesn't mean we are dragging.
/// We could be at the start of a click.
/// But if the mouse is down long enough, or has moved far enough,
/// then we consider it a drag.
///
/// This function can return true on the same frame the drag is released,
/// but NOT on the first frame it was started.
///
/// See also [`Self::could_any_button_be_click`].
pub fn is_decidedly_dragging(&self) -> bool {
(self.any_down() || self.any_released())
&& !self.any_pressed()
&& !self.could_any_button_be_click()
&& !self.any_click()
}
/// A long press is something we detect on touch screens
/// to trigger a secondary click (context menu).
///
/// Returns `true` only on one frame.
pub(crate) fn is_long_press(&self) -> bool {
self.started_decidedly_dragging
&& !self.has_moved_too_much_for_a_click
&& self.button_down(PointerButton::Primary)
&& self.press_start_time.is_some_and(|press_start_time| {
self.time - press_start_time > self.options.max_click_duration
})
}
/// Is the primary button currently down?
#[inline(always)]
pub fn primary_down(&self) -> bool {
self.button_down(PointerButton::Primary)
}
/// Is the secondary button currently down?
#[inline(always)]
pub fn secondary_down(&self) -> bool {
self.button_down(PointerButton::Secondary)
}
/// Is the middle button currently down?
#[inline(always)]
pub fn middle_down(&self) -> bool {
self.button_down(PointerButton::Middle)
}
/// Is the mouse moving in the direction of the given rect?
pub fn is_moving_towards_rect(&self, rect: &Rect) -> bool {
if self.is_still() {
return false;
}
if let Some(pos) = self.hover_pos() {
let dir = self.direction();
if dir != Vec2::ZERO {
return rect.intersects_ray(pos, self.direction());
}
}
false
}
}
impl InputState {
pub fn ui(&self, ui: &mut crate::Ui) {
let Self {
raw,
pointer,
touch_states,
last_scroll_time,
unprocessed_scroll_delta,
unprocessed_scroll_delta_for_zoom,
raw_scroll_delta,
smooth_scroll_delta,
rotation_radians,
zoom_factor_delta,
viewport_rect,
safe_area_insets,
pixels_per_point,
max_texture_side,
time,
unstable_dt,
predicted_dt,
stable_dt,
focused,
modifiers,
keys_down,
events,
options: _,
} = self;
ui.style_mut()
.text_styles
.get_mut(&crate::TextStyle::Body)
.unwrap()
.family = crate::FontFamily::Monospace;
ui.collapsing("Raw Input", |ui| raw.ui(ui));
crate::containers::CollapsingHeader::new("🖱 Pointer")
.default_open(false)
.show(ui, |ui| {
pointer.ui(ui);
});
for (device_id, touch_state) in touch_states {
ui.collapsing(format!("Touch State [device {}]", device_id.0), |ui| {
touch_state.ui(ui);
});
}
ui.label(format!(
"Time since last scroll: {:.1} s",
time - last_scroll_time
));
if cfg!(debug_assertions) {
ui.label(format!(
"unprocessed_scroll_delta: {unprocessed_scroll_delta:?} points"
));
ui.label(format!(
"unprocessed_scroll_delta_for_zoom: {unprocessed_scroll_delta_for_zoom:?} points"
));
}
ui.label(format!("raw_scroll_delta: {raw_scroll_delta:?} points"));
ui.label(format!(
"smooth_scroll_delta: {smooth_scroll_delta:?} points"
));
ui.label(format!("zoom_factor_delta: {zoom_factor_delta:4.2}x"));
ui.label(format!("rotation_radians: {rotation_radians:.3} radians"));
ui.label(format!("viewport_rect: {viewport_rect:?} points"));
ui.label(format!("safe_area_insets: {safe_area_insets:?} points"));
ui.label(format!(
"{pixels_per_point} physical pixels for each logical point"
));
ui.label(format!(
"max texture size (on each side): {max_texture_side}"
));
ui.label(format!("time: {time:.3} s"));
ui.label(format!(
"time since previous frame: {:.1} ms",
1e3 * unstable_dt
));
ui.label(format!("predicted_dt: {:.1} ms", 1e3 * predicted_dt));
ui.label(format!("stable_dt: {:.1} ms", 1e3 * stable_dt));
ui.label(format!("focused: {focused}"));
ui.label(format!("modifiers: {modifiers:#?}"));
ui.label(format!("keys_down: {keys_down:?}"));
ui.scope(|ui| {
ui.set_min_height(150.0);
ui.label(format!("events: {events:#?}"))
.on_hover_text("key presses etc");
});
}
}
impl PointerState {
pub fn ui(&self, ui: &mut crate::Ui) {
let Self {
time: _,
latest_pos,
interact_pos,
delta,
motion,
velocity,
direction,
pos_history: _,
down,
press_origin,
press_start_time,
has_moved_too_much_for_a_click,
started_decidedly_dragging,
last_click_time,
last_last_click_time,
pointer_events,
last_move_time,
options: _,
} = self;
ui.label(format!("latest_pos: {latest_pos:?}"));
ui.label(format!("interact_pos: {interact_pos:?}"));
ui.label(format!("delta: {delta:?}"));
ui.label(format!("motion: {motion:?}"));
ui.label(format!(
"velocity: [{:3.0} {:3.0}] points/sec",
velocity.x, velocity.y
));
ui.label(format!("direction: {direction:?}"));
ui.label(format!("down: {down:#?}"));
ui.label(format!("press_origin: {press_origin:?}"));
ui.label(format!("press_start_time: {press_start_time:?} s"));
ui.label(format!(
"has_moved_too_much_for_a_click: {has_moved_too_much_for_a_click}"
));
ui.label(format!(
"started_decidedly_dragging: {started_decidedly_dragging}"
));
ui.label(format!("last_click_time: {last_click_time:#?}"));
ui.label(format!("last_last_click_time: {last_last_click_time:#?}"));
ui.label(format!("last_move_time: {last_move_time:#?}"));
ui.label(format!("pointer_events: {pointer_events:?}"));
}
}