#include "ui/events/blink/blink_event_util.h"
#include <stddef.h>
#include <algorithm>
#include <bitset>
#include <limits>
#include <memory>
#include "base/numerics/angle_conversions.h"
#include "base/time/time.h"
#include "base/trace_event/typed_macros.h"
#include "base/types/cxx23_to_underlying.h"
#include "build/build_config.h"
#include "third_party/blink/public/common/input/web_input_event.h"
#include "third_party/blink/public/common/input/web_mouse_wheel_event.h"
#include "third_party/blink/public/common/input/web_pointer_event.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/event_constants.h"
#include "ui/events/gesture_detection/gesture_event_data.h"
#include "ui/events/gesture_event_details.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/types/event_type.h"
#include "ui/events/velocity_tracker/motion_event.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/gfx/geometry/vector2d_f.h"
#if BUILDFLAG(IS_ANDROID)
#include "ui/events/android/gesture_event_android.h"
#include "ui/events/android/gesture_event_type.h"
#endif
WebGestureDevice;
WebGestureEvent;
WebInputEvent;
WebMouseEvent;
WebMouseWheelEvent;
WebPointerEvent;
WebPointerProperties;
WebTouchEvent;
WebTouchPoint;
namespace ui {
namespace {
WebInputEvent::Type ToWebTouchEventType(MotionEvent::Action action) { … }
WebTouchPoint::State ToWebTouchPointState(const MotionEvent& event,
size_t pointer_index) { … }
WebPointerProperties::PointerType ToWebPointerType(
MotionEvent::ToolType tool_type) { … }
WebPointerProperties::PointerType ToWebPointerType(
EventPointerType event_pointer_type) { … }
WebPointerProperties::Button ToWebPointerButton(int android_button_state) { … }
WebTouchPoint CreateWebTouchPoint(const MotionEvent& event,
size_t pointer_index) { … }
}
blink::WebTouchEvent CreateWebTouchEventFromMotionEvent(
const MotionEvent& event,
bool moved_beyond_slop_region,
bool hovering) { … }
int EventFlagsToWebEventModifiers(int flags) { … }
WebGestureEvent CreateWebGestureEvent(const GestureEventDetails& details,
base::TimeTicks timestamp,
const gfx::PointF& location,
const gfx::PointF& raw_location,
int flags,
uint32_t unique_touch_event_id) { … }
float IfNanUseMaxFloat(float value) { … }
WebGestureEvent CreateWebGestureEventFromGestureEventData(
const GestureEventData& data) { … }
std::unique_ptr<blink::WebInputEvent> ScaleWebInputEvent(
const blink::WebInputEvent& event,
float scale,
std::optional<int64_t> trace_id) { … }
std::unique_ptr<blink::WebInputEvent> TranslateAndScaleWebInputEvent(
const blink::WebInputEvent& event,
const gfx::Vector2dF& delta,
float scale,
std::optional<int64_t> trace_id) { … }
WebInputEvent::Type ToWebMouseEventType(MotionEvent::Action action) { … }
void SetWebPointerPropertiesFromMotionEventData(
WebPointerProperties& webPointerProperties,
int pointer_id,
float pressure,
float orientation_rad,
float tilt_x,
float tilt_y,
float twist,
float tangential_pressure,
int android_buttons_changed,
MotionEvent::ToolType tool_type) { … }
int WebEventModifiersToEventFlags(int modifiers) { … }
blink::WebInputEvent::Modifiers DomCodeToWebInputEventModifiers(DomCode code) { … }
bool IsContinuousGestureEvent(WebInputEvent::Type type) { … }
EventPointerType WebPointerTypeToEventPointerType(
WebPointerProperties::PointerType type) { … }
blink::WebGestureEvent ScrollBeginFromScrollUpdate(
const blink::WebGestureEvent& gesture_update) { … }
#if BUILDFLAG(IS_ANDROID)
std::unique_ptr<WebGestureEvent> CreateWebGestureEventFromGestureEventAndroid(
const GestureEventAndroid& event) {
WebInputEvent::Type event_type = WebInputEvent::Type::kUndefined;
switch (event.type()) {
case GESTURE_EVENT_TYPE_PINCH_BEGIN:
event_type = WebInputEvent::Type::kGesturePinchBegin;
break;
case GESTURE_EVENT_TYPE_PINCH_BY:
event_type = WebInputEvent::Type::kGesturePinchUpdate;
break;
case GESTURE_EVENT_TYPE_PINCH_END:
event_type = WebInputEvent::Type::kGesturePinchEnd;
break;
case GESTURE_EVENT_TYPE_SCROLL_START:
event_type = WebInputEvent::Type::kGestureScrollBegin;
break;
case GESTURE_EVENT_TYPE_SCROLL_BY:
event_type = WebInputEvent::Type::kGestureScrollUpdate;
break;
case GESTURE_EVENT_TYPE_SCROLL_END:
event_type = WebInputEvent::Type::kGestureScrollEnd;
break;
case GESTURE_EVENT_TYPE_FLING_START:
event_type = WebInputEvent::Type::kGestureFlingStart;
break;
case GESTURE_EVENT_TYPE_FLING_CANCEL:
event_type = WebInputEvent::Type::kGestureFlingCancel;
break;
case GESTURE_EVENT_TYPE_DOUBLE_TAP:
event_type = WebInputEvent::Type::kGestureDoubleTap;
break;
default:
NOTREACHED_IN_MIGRATION() << "Unknown gesture event type";
return std::make_unique<WebGestureEvent>();
}
auto web_event = std::make_unique<WebGestureEvent>(
event_type, WebInputEvent::kNoModifiers,
base::TimeTicks() + base::Milliseconds(event.time()));
web_event->SetPositionInWidget(event.location());
web_event->SetPositionInScreen(event.screen_location());
web_event->SetSourceDevice(WebGestureDevice::kTouchscreen);
if (event.synthetic_scroll())
web_event->SetSourceDevice(WebGestureDevice::kSyntheticAutoscroll);
if (event_type == WebInputEvent::Type::kGesturePinchUpdate) {
web_event->data.pinch_update.scale = event.scale();
} else if (event_type == WebInputEvent::Type::kGestureScrollBegin) {
web_event->data.scroll_begin.delta_x_hint = event.delta_x();
web_event->data.scroll_begin.delta_y_hint = event.delta_y();
web_event->data.scroll_begin.target_viewport = event.target_viewport();
} else if (event_type == WebInputEvent::Type::kGestureScrollUpdate) {
web_event->data.scroll_update.delta_x = event.delta_x();
web_event->data.scroll_update.delta_y = event.delta_y();
} else if (event_type == WebInputEvent::Type::kGestureFlingStart) {
web_event->data.fling_start.velocity_x = event.velocity_x();
web_event->data.fling_start.velocity_y = event.velocity_y();
web_event->data.fling_start.target_viewport = event.target_viewport();
} else if (event_type == WebInputEvent::Type::kGestureFlingCancel) {
web_event->data.fling_cancel.prevent_boosting = event.prevent_boosting();
if (event.synthetic_scroll())
web_event->data.fling_cancel.target_viewport = true;
} else if (event_type == WebInputEvent::Type::kGestureDoubleTap) {
web_event->data.tap.tap_count = 1;
}
return web_event;
}
#endif
}