File: web_mouse_wheel_event.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 5,111 bytes parent folder | download | duplicates (6)
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/public/common/input/web_mouse_wheel_event.h"

#include "build/build_config.h"

namespace blink {

namespace {

float GetUnacceleratedDelta(float accelerated_delta, float acceleration_ratio) {
  return accelerated_delta * acceleration_ratio;
}

float GetAccelerationRatio(float accelerated_delta, float unaccelerated_delta) {
  if (unaccelerated_delta == 0.f || accelerated_delta == 0.f)
    return 1.f;
  return unaccelerated_delta / accelerated_delta;
}

}  // namespace

std::unique_ptr<WebInputEvent> WebMouseWheelEvent::Clone() const {
  return std::make_unique<WebMouseWheelEvent>(*this);
}

bool WebMouseWheelEvent::CanCoalesce(const WebInputEvent& event) const {
  if (event.GetType() != WebInputEvent::Type::kMouseWheel)
    return false;
  const WebMouseWheelEvent& mouse_wheel_event =
      static_cast<const WebMouseWheelEvent&>(event);

  return GetModifiers() == mouse_wheel_event.GetModifiers() &&
         delta_units == mouse_wheel_event.delta_units &&
         HaveConsistentPhase(mouse_wheel_event);
}

bool WebMouseWheelEvent::HaveConsistentPhase(
    const WebMouseWheelEvent& event) const {
  if (has_synthetic_phase != event.has_synthetic_phase)
    return false;

  if (phase == event.phase && momentum_phase == event.momentum_phase) {
    return true;
  }

  if (has_synthetic_phase) {
    // It is alright to coalesce a wheel event with synthetic phaseChanged to
    // its previous one with synthetic phaseBegan.
    return (phase == WebMouseWheelEvent::kPhaseBegan &&
            event.phase == WebMouseWheelEvent::kPhaseChanged);
  }
  return false;
}

void WebMouseWheelEvent::Coalesce(const WebInputEvent& event) {
  DCHECK(CanCoalesce(event));
  const WebMouseWheelEvent& mouse_wheel_event =
      static_cast<const WebMouseWheelEvent&>(event);
  float unaccelerated_x =
      GetUnacceleratedDelta(delta_x, acceleration_ratio_x) +
      GetUnacceleratedDelta(mouse_wheel_event.delta_x,
                            mouse_wheel_event.acceleration_ratio_x);
  float unaccelerated_y =
      GetUnacceleratedDelta(delta_y, acceleration_ratio_y) +
      GetUnacceleratedDelta(mouse_wheel_event.delta_y,
                            mouse_wheel_event.acceleration_ratio_y);
  float old_deltaX = delta_x;
  float old_deltaY = delta_y;
  float old_wheelTicksX = wheel_ticks_x;
  float old_wheelTicksY = wheel_ticks_y;
  float old_movementX = movement_x;
  float old_movementY = movement_y;
  WebMouseWheelEvent::Phase old_phase = phase;
  WebInputEvent::DispatchType old_dispatch_type = dispatch_type;
  *this = mouse_wheel_event;
  delta_x += old_deltaX;
  delta_y += old_deltaY;
  wheel_ticks_x += old_wheelTicksX;
  wheel_ticks_y += old_wheelTicksY;
  movement_x += old_movementX;
  movement_y += old_movementY;
  acceleration_ratio_x = GetAccelerationRatio(delta_x, unaccelerated_x);
  acceleration_ratio_y = GetAccelerationRatio(delta_y, unaccelerated_y);
  dispatch_type =
      MergeDispatchTypes(old_dispatch_type, mouse_wheel_event.dispatch_type);
  if (mouse_wheel_event.has_synthetic_phase &&
      mouse_wheel_event.phase != old_phase) {
    // Coalesce  a wheel event with synthetic phase changed to a wheel event
    // with synthetic phase began.
    DCHECK_EQ(WebMouseWheelEvent::kPhaseChanged, mouse_wheel_event.phase);
    DCHECK_EQ(WebMouseWheelEvent::kPhaseBegan, old_phase);
    phase = WebMouseWheelEvent::kPhaseBegan;
  }
}

float WebMouseWheelEvent::DeltaXInRootFrame() const {
  return delta_x / frame_scale_;
}

float WebMouseWheelEvent::DeltaYInRootFrame() const {
  return delta_y / frame_scale_;
}

WebMouseWheelEvent WebMouseWheelEvent::FlattenTransform() const {
  WebMouseWheelEvent result = *this;
  result.delta_x /= result.frame_scale_;
  result.delta_y /= result.frame_scale_;
  result.FlattenTransformSelf();
  return result;
}

// static
WebMouseWheelEvent::EventAction
WebMouseWheelEvent::GetPlatformSpecificDefaultEventAction(
    const WebMouseWheelEvent& event) {
#if defined(USE_AURA) || BUILDFLAG(IS_ANDROID)
  // Scroll events generated from the mouse wheel when the control key is held
  // don't trigger scrolling. Instead, they may cause zooming.
  if (event.delta_units != ui::ScrollGranularity::kScrollByPrecisePixel &&
      (event.GetModifiers() & WebInputEvent::kControlKey)) {
    return blink::WebMouseWheelEvent::EventAction::kPageZoom;
  }

  if (event.delta_x == 0 && (event.GetModifiers() & WebInputEvent::kShiftKey))
    return blink::WebMouseWheelEvent::EventAction::kScrollHorizontal;
#endif
  if (event.rails_mode == WebInputEvent::kRailsModeHorizontal ||
      (event.delta_x != 0 && event.delta_y == 0)) {
    return blink::WebMouseWheelEvent::EventAction::kScrollHorizontal;
  }

  if (event.rails_mode == WebInputEvent::kRailsModeVertical ||
      (event.delta_x == 0 && event.delta_y != 0)) {
    return blink::WebMouseWheelEvent::EventAction::kScrollVertical;
  }

  return blink::WebMouseWheelEvent::EventAction::kScroll;
}

}  // namespace blink