Bug 1837119 - Support double click event on GeckoView. r=geckoview-reviewers,owlish

When using mouse on GeckoView, dblclick event isn't fired. Android doesn't
have a state of double click for mouse, so we have to emulate it like
Windows widget.

The default of timeout values are from android.view.GestureDetector's double
tap.

Differential Revision: https://phabricator.services.mozilla.com/D209654
This commit is contained in:
Makoto Kato 2024-05-15 01:43:46 +00:00
parent 109f993774
commit 1603a600a3
4 changed files with 223 additions and 1 deletions

View File

@ -16683,6 +16683,26 @@
value: false
mirror: once
#ifdef ANDROID
# A threshold value for double click by mouse.
- name: widget.double-click.threshold
type: RelaxedAtomicInt32
value: 4
mirror: always
# A timeout value for double click by mouse.
- name: widget.double-click.timeout
type: RelaxedAtomicInt32
value: 500
mirror: always
# A min time value for double click by mouse.
- name: widget.double-click.min
type: RelaxedAtomicInt32
value: 40
mirror: always
#endif
#---------------------------------------------------------------------------
# Prefs starting with "zoom."
#---------------------------------------------------------------------------

View File

@ -184,6 +184,13 @@ bool DispatchToUiThread(const char* aName, Lambda&& aLambda) {
namespace mozilla {
namespace widget {
// For double click detection
static int64_t sLastMouseDownTime = 0;
static int32_t sLastMouseButtons = 0;
static int32_t sLastClickCount = 0;
static float sLastMouseDownX = 0;
static float sLastMouseDownY = 0;
using WindowPtr = jni::NativeWeakPtr<GeckoViewSupport>;
/**
@ -543,6 +550,28 @@ class NPZCSupport final
ConvertScrollDirections(aHandledResult.mOverscrollDirections));
}
static bool IsIntoDoubleClickThreshold(float aX, float aY) {
int32_t deltaX = abs((int32_t)floorf(sLastMouseDownX - aX));
int32_t deltaY = abs((int32_t)floorf(sLastMouseDownY - aY));
int32_t threshold = StaticPrefs::widget_double_click_threshold();
return (deltaX * deltaX + deltaY * deltaY < threshold * threshold);
}
static bool IsDoubleClick(int64_t aTime, float aX, float aY, int buttons) {
if (sLastMouseButtons != buttons) {
return false;
}
int64_t deltaTime = aTime - sLastMouseDownTime;
if (deltaTime < (int64_t)StaticPrefs::widget_double_click_min() ||
deltaTime > (int64_t)StaticPrefs::widget_double_click_timeout()) {
return false;
}
return IsIntoDoubleClickThreshold(aX, aY);
}
public:
int32_t HandleMouseEvent(int32_t aAction, int64_t aTime, int32_t aMetaState,
float aX, float aY, int buttons) {
@ -568,6 +597,16 @@ class NPZCSupport final
mouseType = MouseInput::MOUSE_DOWN;
buttonType = GetButtonType(buttons ^ mPreviousButtons);
mPreviousButtons = buttons;
if (IsDoubleClick(aTime, aX, aY, buttons)) {
sLastClickCount++;
} else {
sLastClickCount = 1;
}
sLastMouseDownTime = aTime;
sLastMouseDownX = aX;
sLastMouseDownY = aY;
sLastMouseButtons = buttons;
break;
case java::sdk::MotionEvent::ACTION_UP:
mouseType = MouseInput::MOUSE_UP;
@ -576,6 +615,10 @@ class NPZCSupport final
break;
case java::sdk::MotionEvent::ACTION_MOVE:
mouseType = MouseInput::MOUSE_MOVE;
if (!IsIntoDoubleClickThreshold(aX, aY)) {
sLastClickCount = 0;
}
break;
case java::sdk::MotionEvent::ACTION_HOVER_MOVE:
mouseType = MouseInput::MOUSE_MOVE;
@ -606,8 +649,10 @@ class NPZCSupport final
return INPUT_RESULT_IGNORED;
}
PostInputEvent([input = std::move(input), result](nsWindow* window) {
PostInputEvent([input = std::move(input), result,
clickCount = sLastClickCount](nsWindow* window) {
WidgetMouseEvent mouseEvent = input.ToWidgetEvent(window);
mouseEvent.mClickCount = clickCount;
window->ProcessUntransformedAPZEvent(&mouseEvent, result);
if (MouseInput::SECONDARY_BUTTON == input.mButtonType) {
if ((StaticPrefs::ui_context_menus_after_mouseup() &&

View File

@ -42,6 +42,11 @@ run-if = [
]
skip-if = ["headless"] # headless widget doesn't dispatch contextmenu event by mouse event.
["test_mouse_double_click_on_android.html"]
run-if = [
"os == 'android'",
]
["test_keypress_event_with_alt_on_mac.html"]
run-if = ["os == 'mac'"]

View File

@ -0,0 +1,152 @@
<!DOCTYPE html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1837119
-->
<html>
<head>
<meta charset="utf-8" />
<title>Test to fire dblclick event by Android's widget level</title>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
<style>
#target {
width: 100px;
height: 100px;
background-color: blue;
user-select: none;
}
</style>
<script>
"use strict";
// If this test runs repeatedly, we have to reset internal double click state
// to detect double click correctly.
function resetMouseState() {
const target = document.getElementById("target");
const promise =
new Promise(resolve => target.addEventListener("click", resolve, { once: true }));
synthesizeNativeMouseEvent({
type: "mousedown",
target,
offsetX: 30,
offsetY: 10,
});
synthesizeNativeMouseEvent({
type: "mouseup",
target,
offsetX: 30,
offsetY: 10,
});
return promise;
}
add_task(async function test_fire_dblclick() {
// Don't use min time for double click to reduce execution time.
// Also, we cannot make a min time on parent process since
// synthesizeNativeMouseEvent is called on child process.
await SpecialPowers.pushPrefEnv({
set: [["widget.double-click.min", -1]],
});
await SimpleTest.promiseFocus();
await resetMouseState();
const target = document.getElementById("target");
let click = 0;
target.addEventListener("click", () => {
info("click event is received");
click++;
});
const promise = new Promise(resolve => {
target.addEventListener("dblclick", e => {
resolve();
e.preventDefault();
}, { once: true });
});
for (let i = 0; i < 2; i++) {
synthesizeNativeMouseEvent({
type: "mousedown",
target,
offsetX: 10,
offsetY: 10,
button: 0,
});
synthesizeNativeMouseEvent({
type: "mouseup",
target,
offsetX: 10,
offsetY: 10,
button: 0,
});
}
await promise;
is(click, 2, "Click event is twice");
await SpecialPowers.popPrefEnv();
});
add_task(async function test_fire_no_dblclick() {
await SimpleTest.promiseFocus();
await resetMouseState();
let click = 0;
let dblclick = 0;
const target = document.getElementById("target");
const promise = new Promise(resolve => {
target.addEventListener("click", () => {
info("click event is received");
click++;
if (click == 2) {
resolve();
}
});
})
target.addEventListener("dblclick", () => {
info("dblclick event is received");
dblclick++
});
// no delay means that dblclick isn't fired.
for (let i = 0; i < 2; i++) {
synthesizeNativeMouseEvent({
type: "mousedown",
target,
offsetX: 10,
offsetY: 10,
button: 0,
});
synthesizeNativeMouseEvent({
type: "mouseup",
target,
offsetX: 10,
offsetY: 10,
button: 0,
});
}
await promise;
// This test is that dblclick isn't fired when each click is no delayed.
// So we need to spin event loop to check whether dblclick is fired.
await new Promise(resolve => window.requestAnimationFrame(resolve));
is(click, 2, "Click event is twice");
is(dblclick, 0, "No double click event is fired");
});
</script>
</head>
<body>
<div id="target"></div>
</body>
</html>