Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Get the timestamp from the DOM event
  • Loading branch information
bleroux committed Oct 19, 2022
commit a5c151ab0be675a546c254737aeff70cd4fb4070
9 changes: 5 additions & 4 deletions lib/web_ui/lib/src/engine/keyboard_binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,15 @@ class KeyboardBinding {
}

// Synthesize shift key up or down event only when the known pressing state is different.
void synthesizeShiftKeyIfNeeded(ui.KeyEventType type) {
void synthesizeShiftKeyIfNeeded(ui.KeyEventType type, num eventTimestamp) {
// TODO(bleroux): should we take care of shift left AND shift right?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Let's use a "least event dispatched" strategy:

  • If the true state is pressed, and neither key is pressed, press the left key.
  • If the true state is not pressed, and any key is pressed, release these keys.
  • Otherwise, dispatch no events.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

final int physicalShift = kWebToPhysicalKey['ShiftLeft']!;
final bool alreadyPressed = _converter._pressingRecords.containsKey(physicalShift);
final bool synthesizeDown = type == ui.KeyEventType.down && !alreadyPressed;
final bool synthesizeUp = type == ui.KeyEventType.up && alreadyPressed;
if (synthesizeDown || synthesizeUp) {
_converter.performDispatchKeyData(_shiftLeftKeyData(type));
final Duration timestamp = _eventTimeStampToDuration(eventTimestamp);
_converter.performDispatchKeyData(_shiftLeftKeyData(type, timestamp));
// Update pressing state
if (synthesizeDown) {
_converter._pressingRecords[physicalShift] = _kLogicalShiftLeft;
Expand All @@ -170,9 +171,9 @@ class KeyboardBinding {
}
}

ui.KeyData _shiftLeftKeyData(ui.KeyEventType type) {
ui.KeyData _shiftLeftKeyData(ui.KeyEventType type, Duration timestamp) {
return ui.KeyData(
timeStamp: Duration.zero,
timeStamp: timestamp,
type: type,
physical: kWebToPhysicalKey['ShiftLeft']!,
logical: _kLogicalShiftLeft,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This magic value is concerning, but for now we don't have other ways than keeping a static map from physical keys to logical keys.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added _kPhysicalxxx variables (similar to the _kLogicalxxx ones) to centralize those magic values.
Maybe adding a generated enum in key_map.g.dart could help to not dissiminate those magic values and might make it possible to rely on const variables?

Expand Down
2 changes: 2 additions & 0 deletions lib/web_ui/lib/src/engine/pointer_binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,11 @@ class _PointerAdapter extends _BaseAdapter with _WheelEventListenerMixin {
void _checkModifiersState(DomEvent event) {
// TODO(bleroux): add support for 'Meta', 'Ctrl' and 'Alt'
final DomPointerEvent pointerEvent = event as DomPointerEvent;

final bool shiftPressed = pointerEvent.getModifierState('Shift');
KeyboardBinding.instance!.synthesizeShiftKeyIfNeeded(
shiftPressed ? ui.KeyEventType.down : ui.KeyEventType.up,
event.timeStamp!,
);
}

Expand Down