# WM Gesture Handler
[WmGestureHandler](https://source.chromium.org/chromium/chromium/src/+/main:ash/wm/gestures/wm_gesture_handler.h;l=23;drc=a98e4438c6229a95ecd8c5f0acebc6c95f9028b4) is responsible for handling the scroll event with 3 or 4 fingers.
Let's see the even handling of gesture on WindowManager.
## ScrollEvent
[ScrollEvent](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/event.h;l=998;drc=946e68c02af4db2e67476042dece1c08ab83ea11) is an event to represent the scroll gesture.
It has `finger_count_` as int. In scroll event, you may touch the touch pad with 2-4 fingers, so `finger_count_` represents its number.
`x_offset_` and `y_offset_` as float is the accelerated offsets. The distance you move your finger within the event.
Also, it inherits Event so it also has [`type_`](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/event.h;l=333;drc=946e68c02af4db2e67476042dece1c08ab83ea11).
As for the scroll event, here are the types:
```cpp=
ET_SCROLL,
ET_SCROLL_FLING_START,
ET_SCROLL_FLING_CANCEL,
```
`ET_SCROLL_FLING_CANCEL` is the start of the scroll.
`ET_SCROLL` is for during the scroll.
`ET_SCROLL_FLING_START` is the end of the scroll.
## How to process scroll
[ProcessScrollEvent](https://source.chromium.org/chromium/chromium/src/+/main:ash/wm/gestures/wm_gesture_handler.cc;l=155;drc=946e68c02af4db2e67476042dece1c08ab83ea11) receives ScrollEvent and handles it.
If the screen is pinned, touchpad is disallowed, so we immediately stop handling.
On starting scroll, it resets `scroll_data_` and start scrolling.
During scrolling, it's passed to [ProcessEventImpl](https://source.chromium.org/chromium/chromium/src/+/main:ash/wm/gestures/wm_gesture_handler.cc;l=183;drc=946e68c02af4db2e67476042dece1c08ab83ea11) and accumulates the scroll distance for the consecutive scroll event.
On ending scroll, call [EndScroll](https://source.chromium.org/chromium/chromium/src/+/main:ash/wm/gestures/wm_gesture_handler.cc;l=245;drc=946e68c02af4db2e67476042dece1c08ab83ea11).
On ending scroll, we check whether the scroll is vertical or horizontal.
We can compare the x and y scroll distance.
```cpp=
if (std::fabs(scroll_x) < std::fabs(scroll_y)) {
return Handle3FingerVerticalScroll(scroll_y);
}
```
### Data
[ScrollData](https://source.chromium.org/chromium/chromium/src/+/main:ash/wm/gestures/wm_gesture_handler.h;l=52;drc=a98e4438c6229a95ecd8c5f0acebc6c95f9028b4) is a struct that contains the relevant data for scroll.
It has `scroll_x`, `scroll_y` represents the total distance moved since the scroll began.
Note that this is not the same as `x_offset_`, `y_offset_`.
It accumulates the offset so it represents the "total distance".
```cpp=
scroll_data_->scroll_x += delta_x;
scroll_data_->scroll_y += delta_y;
```
## Event Handler
WmGestureHandler is owned by [SystemGestureEventFilter](https://source.chromium.org/chromium/chromium/src/+/main:ash/wm/system_gesture_event_filter.h;l=16;drc=3a215d1e60a3b32928a50d00ea07ae52ea491a16).
This is a event filter which handles system level gesture events and inherits [EventHandler](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/event_handler.h;l=29;drc=946e68c02af4db2e67476042dece1c08ab83ea11).
EventHandler dispatches events to appropriate targets.
To handle the events by this handler, you must add the handler via [AddPreTargetHandler](https://source.chromium.org/chromium/chromium/src/+/main:ash/shell.cc;l=1528-1529;drc=946e68c02af4db2e67476042dece1c08ab83ea11).
[`pre_target_list_`](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/event_target.h;l=138;drc=946e68c02af4db2e67476042dece1c08ab83ea11) is the list of registered handlers.
We might be able to add the priority in pre target list as the list item is [PrioritizedHandler](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/event_target.h;l=116;drc=946e68c02af4db2e67476042dece1c08ab83ea11).
[Priority](https://source.chromium.org/chromium/chromium/src/+/main:ui/events/event_target.h;l=70-85;drc=946e68c02af4db2e67476042dece1c08ab83ea11) can take `kAccessibility`, `kSystem` and `kDefault`.