2015-04-30 01:59:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "AccessibleCaretManager.h"
|
|
|
|
|
|
|
|
#include "AccessibleCaret.h"
|
|
|
|
#include "AccessibleCaretEventHub.h"
|
|
|
|
#include "AccessibleCaretLogger.h"
|
2015-06-05 10:03:47 +03:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2018-04-09 12:21:42 +03:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2015-04-30 01:59:00 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
2018-03-20 07:16:06 +03:00
|
|
|
#include "mozilla/dom/MouseEventBinding.h"
|
2018-02-01 22:26:12 +03:00
|
|
|
#include "mozilla/dom/NodeFilterBinding.h"
|
2015-04-30 01:59:00 +03:00
|
|
|
#include "mozilla/dom/Selection.h"
|
|
|
|
#include "mozilla/dom/TreeWalker.h"
|
2015-11-25 00:16:00 +03:00
|
|
|
#include "mozilla/IMEStateManager.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2019-03-29 18:12:47 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_layout.h"
|
2015-04-30 01:59:00 +03:00
|
|
|
#include "nsCaret.h"
|
2015-09-10 13:24:34 +03:00
|
|
|
#include "nsContainerFrame.h"
|
2015-04-30 01:59:00 +03:00
|
|
|
#include "nsContentUtils.h"
|
2020-01-08 13:22:19 +03:00
|
|
|
#include "nsDebug.h"
|
2015-04-30 01:59:00 +03:00
|
|
|
#include "nsFocusManager.h"
|
2020-07-07 01:38:11 +03:00
|
|
|
#include "nsIFrame.h"
|
2015-04-30 01:59:00 +03:00
|
|
|
#include "nsFrameSelection.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
2015-12-11 02:57:02 +03:00
|
|
|
#include "nsIHapticFeedback.h"
|
2015-04-30 01:59:00 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
#undef AC_LOG
|
|
|
|
#define AC_LOG(message, ...) \
|
|
|
|
AC_LOG_BASE("AccessibleCaretManager (%p): " message, this, ##__VA_ARGS__);
|
|
|
|
|
|
|
|
#undef AC_LOGV
|
|
|
|
#define AC_LOGV(message, ...) \
|
|
|
|
AC_LOGV_BASE("AccessibleCaretManager (%p): " message, this, ##__VA_ARGS__);
|
|
|
|
|
|
|
|
using namespace dom;
|
|
|
|
using Appearance = AccessibleCaret::Appearance;
|
|
|
|
using PositionChangedResult = AccessibleCaret::PositionChangedResult;
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
#define AC_PROCESS_ENUM_TO_STREAM(e) \
|
|
|
|
case (e): \
|
|
|
|
aStream << #e; \
|
|
|
|
break;
|
|
|
|
std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const AccessibleCaretManager::CaretMode& aCaretMode) {
|
|
|
|
using CaretMode = AccessibleCaretManager::CaretMode;
|
|
|
|
switch (aCaretMode) {
|
|
|
|
AC_PROCESS_ENUM_TO_STREAM(CaretMode::None);
|
|
|
|
AC_PROCESS_ENUM_TO_STREAM(CaretMode::Cursor);
|
|
|
|
AC_PROCESS_ENUM_TO_STREAM(CaretMode::Selection);
|
|
|
|
}
|
|
|
|
return aStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(
|
|
|
|
std::ostream& aStream,
|
|
|
|
const AccessibleCaretManager::UpdateCaretsHint& aHint) {
|
|
|
|
using UpdateCaretsHint = AccessibleCaretManager::UpdateCaretsHint;
|
|
|
|
switch (aHint) {
|
|
|
|
AC_PROCESS_ENUM_TO_STREAM(UpdateCaretsHint::Default);
|
|
|
|
AC_PROCESS_ENUM_TO_STREAM(UpdateCaretsHint::RespectOldAppearance);
|
2017-03-14 12:50:03 +03:00
|
|
|
AC_PROCESS_ENUM_TO_STREAM(UpdateCaretsHint::DispatchNoEvent);
|
2015-09-18 20:05:19 +03:00
|
|
|
}
|
|
|
|
return aStream;
|
|
|
|
}
|
|
|
|
#undef AC_PROCESS_ENUM_TO_STREAM
|
|
|
|
|
2019-04-16 10:25:10 +03:00
|
|
|
AccessibleCaretManager::AccessibleCaretManager(PresShell* aPresShell)
|
2015-04-30 01:59:00 +03:00
|
|
|
: mPresShell(aPresShell) {
|
2015-12-01 23:25:06 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFirstCaret = MakeUnique<AccessibleCaret>(mPresShell);
|
|
|
|
mSecondCaret = MakeUnique<AccessibleCaret>(mPresShell);
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2018-01-27 21:29:05 +03:00
|
|
|
AccessibleCaretManager::~AccessibleCaretManager() {
|
|
|
|
MOZ_RELEASE_ASSERT(!mFlushingLayout, "Going away in FlushLayout? Bad!");
|
|
|
|
}
|
|
|
|
|
2016-02-15 10:12:35 +03:00
|
|
|
void AccessibleCaretManager::Terminate() {
|
|
|
|
mFirstCaret = nullptr;
|
|
|
|
mSecondCaret = nullptr;
|
|
|
|
mActiveCaret = nullptr;
|
|
|
|
mPresShell = nullptr;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsresult AccessibleCaretManager::OnSelectionChanged(Document* aDoc,
|
2018-05-08 20:52:35 +03:00
|
|
|
Selection* aSel,
|
|
|
|
int16_t aReason) {
|
2015-12-01 23:25:06 +03:00
|
|
|
Selection* selection = GetSelection();
|
2015-09-22 12:39:36 +03:00
|
|
|
AC_LOG("%s: aSel: %p, GetSelection(): %p, aReason: %d", __FUNCTION__, aSel,
|
2015-12-01 23:25:06 +03:00
|
|
|
selection, aReason);
|
|
|
|
if (aSel != selection) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2015-12-01 23:25:06 +03:00
|
|
|
// eSetSelection events from the Fennec widget IME can be generated
|
2016-01-30 08:08:55 +03:00
|
|
|
// by autoSuggest / autoCorrect composition changes, or by TYPE_REPLACE_TEXT
|
|
|
|
// actions, either positioning cursor for text insert, or selecting
|
|
|
|
// text-to-be-replaced. None should affect AccessibleCaret visibility.
|
2015-12-01 23:25:06 +03:00
|
|
|
if (aReason & nsISelectionListener::IME_REASON) {
|
2015-04-30 01:59:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Bug 1463576 - 1. Add layout.accessiblecaret.script_change_update_mode pref; r=bz
Currently, if the "layout.accessiblecaret.allow_script_change_updates"
pref is false, we always hide accessible carets when the selection
changes due to JS calls (e.g. setSelectionRange(0, 1)). If the pref is
true, instead, we update the accessible carets if they are already
visible. In either case, we never show the carets if they're invisible.
However for testing purposes, it's useful to make it so we do try to
show the carets if they're invisible. This patch replaces that pref with
the new integer pref "layout.accessiblecaret.script_change_update_mode",
which can be 0 or 1, which correspond to the old pref, or the value 2,
which introduces this new behavior of always showing the carets.
MozReview-Commit-ID: Bf1gPpIzcyb
--HG--
extra : rebase_source : 5eaba6c18d800da425d5e21f61af880678d7fe3c
2018-06-01 20:39:20 +03:00
|
|
|
// Move the cursor by JavaScript or unknown internal call.
|
2015-04-30 01:59:00 +03:00
|
|
|
if (aReason == nsISelectionListener::NO_REASON) {
|
2018-09-22 00:59:52 +03:00
|
|
|
auto mode = static_cast<ScriptUpdateMode>(
|
|
|
|
StaticPrefs::layout_accessiblecaret_script_change_update_mode());
|
|
|
|
if (mode == kScriptAlwaysShow || (mode == kScriptUpdateVisible &&
|
Bug 1463576 - 1. Add layout.accessiblecaret.script_change_update_mode pref; r=bz
Currently, if the "layout.accessiblecaret.allow_script_change_updates"
pref is false, we always hide accessible carets when the selection
changes due to JS calls (e.g. setSelectionRange(0, 1)). If the pref is
true, instead, we update the accessible carets if they are already
visible. In either case, we never show the carets if they're invisible.
However for testing purposes, it's useful to make it so we do try to
show the carets if they're invisible. This patch replaces that pref with
the new integer pref "layout.accessiblecaret.script_change_update_mode",
which can be 0 or 1, which correspond to the old pref, or the value 2,
which introduces this new behavior of always showing the carets.
MozReview-Commit-ID: Bf1gPpIzcyb
--HG--
extra : rebase_source : 5eaba6c18d800da425d5e21f61af880678d7fe3c
2018-06-01 20:39:20 +03:00
|
|
|
(mFirstCaret->IsLogicallyVisible() ||
|
|
|
|
mSecondCaret->IsLogicallyVisible()))) {
|
2015-12-01 23:25:06 +03:00
|
|
|
UpdateCarets();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// Default for NO_REASON is to make hidden.
|
2015-04-30 01:59:00 +03:00
|
|
|
HideCarets();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move cursor by keyboard.
|
|
|
|
if (aReason & nsISelectionListener::KEYPRESS_REASON) {
|
|
|
|
HideCarets();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-22 12:39:36 +03:00
|
|
|
// OnBlur() might be called between mouse down and mouse up, so we hide carets
|
|
|
|
// upon mouse down anyway, and update carets upon mouse up.
|
|
|
|
if (aReason & nsISelectionListener::MOUSEDOWN_REASON) {
|
|
|
|
HideCarets();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
// Range will collapse after cutting or copying text.
|
|
|
|
if (aReason & (nsISelectionListener::COLLAPSETOSTART_REASON |
|
|
|
|
nsISelectionListener::COLLAPSETOEND_REASON)) {
|
|
|
|
HideCarets();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-03 15:57:44 +03:00
|
|
|
// For mouse input we don't want to show the carets.
|
2018-09-22 00:59:52 +03:00
|
|
|
if (StaticPrefs::layout_accessiblecaret_hide_carets_for_mouse_input() &&
|
2018-06-26 00:20:54 +03:00
|
|
|
mLastInputSource == MouseEvent_Binding::MOZ_SOURCE_MOUSE) {
|
2016-10-03 15:57:44 +03:00
|
|
|
HideCarets();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-02-17 09:08:48 +03:00
|
|
|
// When we want to hide the carets for mouse input, hide them for select
|
|
|
|
// all action fired by keyboard as well.
|
2018-09-22 00:59:52 +03:00
|
|
|
if (StaticPrefs::layout_accessiblecaret_hide_carets_for_mouse_input() &&
|
2018-06-26 00:20:54 +03:00
|
|
|
mLastInputSource == MouseEvent_Binding::MOZ_SOURCE_KEYBOARD &&
|
2016-10-05 10:46:36 +03:00
|
|
|
(aReason & nsISelectionListener::SELECTALL_REASON)) {
|
|
|
|
HideCarets();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
UpdateCarets();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::HideCarets() {
|
|
|
|
if (mFirstCaret->IsLogicallyVisible() || mSecondCaret->IsLogicallyVisible()) {
|
|
|
|
AC_LOG("%s", __FUNCTION__);
|
|
|
|
mFirstCaret->SetAppearance(Appearance::None);
|
|
|
|
mSecondCaret->SetAppearance(Appearance::None);
|
2019-11-02 06:05:28 +03:00
|
|
|
mIsCaretPositionChanged = false;
|
2015-05-14 03:10:00 +03:00
|
|
|
DispatchCaretStateChangedEvent(CaretChangedReason::Visibilitychange);
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 08:49:40 +03:00
|
|
|
void AccessibleCaretManager::UpdateCarets(const UpdateCaretsHintSet& aHint) {
|
2018-01-27 21:29:05 +03:00
|
|
|
if (!FlushLayout()) {
|
2016-02-15 10:12:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:05:18 +03:00
|
|
|
mLastUpdateCaretMode = GetCaretMode();
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2015-09-18 20:05:18 +03:00
|
|
|
switch (mLastUpdateCaretMode) {
|
2015-04-30 01:59:00 +03:00
|
|
|
case CaretMode::None:
|
|
|
|
HideCarets();
|
|
|
|
break;
|
|
|
|
case CaretMode::Cursor:
|
2015-09-09 11:47:20 +03:00
|
|
|
UpdateCaretsForCursorMode(aHint);
|
2015-04-30 01:59:00 +03:00
|
|
|
break;
|
|
|
|
case CaretMode::Selection:
|
2016-02-11 11:22:57 +03:00
|
|
|
UpdateCaretsForSelectionMode(aHint);
|
2015-04-30 01:59:00 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-11-02 06:05:28 +03:00
|
|
|
|
|
|
|
UpdateShouldDisableApz();
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
bool AccessibleCaretManager::IsCaretDisplayableInCursorMode(
|
|
|
|
nsIFrame** aOutFrame, int32_t* aOutOffset) const {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsCaret> caret = mPresShell->GetCaret();
|
2015-04-30 01:59:00 +03:00
|
|
|
if (!caret || !caret->IsVisible()) {
|
2015-09-18 20:05:19 +03:00
|
|
|
return false;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t offset = 0;
|
2015-09-09 11:47:20 +03:00
|
|
|
nsIFrame* frame =
|
|
|
|
nsCaret::GetFrameAndOffset(GetSelection(), nullptr, 0, &offset);
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2015-09-09 11:47:20 +03:00
|
|
|
if (!frame) {
|
2015-09-18 20:05:19 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetEditingHostForFrame(frame)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOutFrame) {
|
|
|
|
*aOutFrame = frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOutOffset) {
|
|
|
|
*aOutOffset = offset;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AccessibleCaretManager::HasNonEmptyTextContent(nsINode* aNode) const {
|
|
|
|
return nsContentUtils::HasNonEmptyTextContent(
|
|
|
|
aNode, nsContentUtils::eRecurseIntoChildren);
|
|
|
|
}
|
|
|
|
|
2017-11-16 08:49:40 +03:00
|
|
|
void AccessibleCaretManager::UpdateCaretsForCursorMode(
|
|
|
|
const UpdateCaretsHintSet& aHints) {
|
2015-09-18 20:05:19 +03:00
|
|
|
AC_LOG("%s, selection: %p", __FUNCTION__, GetSelection());
|
|
|
|
|
|
|
|
int32_t offset = 0;
|
|
|
|
nsIFrame* frame = nullptr;
|
|
|
|
if (!IsCaretDisplayableInCursorMode(&frame, &offset)) {
|
2015-04-30 01:59:00 +03:00
|
|
|
HideCarets();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-09 11:47:20 +03:00
|
|
|
PositionChangedResult result = mFirstCaret->SetPosition(frame, offset);
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case PositionChangedResult::NotChanged:
|
2020-02-08 01:31:40 +03:00
|
|
|
case PositionChangedResult::Position:
|
|
|
|
case PositionChangedResult::Zoom:
|
2019-01-03 08:25:19 +03:00
|
|
|
if (!aHints.contains(UpdateCaretsHint::RespectOldAppearance)) {
|
2017-03-14 10:25:16 +03:00
|
|
|
if (HasNonEmptyTextContent(GetEditingHostForFrame(frame))) {
|
|
|
|
mFirstCaret->SetAppearance(Appearance::Normal);
|
2018-09-22 00:59:52 +03:00
|
|
|
} else if (
|
|
|
|
StaticPrefs::
|
|
|
|
layout_accessiblecaret_caret_shown_when_long_tapping_on_empty_content()) {
|
2017-03-14 10:25:16 +03:00
|
|
|
if (mFirstCaret->IsLogicallyVisible()) {
|
|
|
|
// Possible cases are: 1) SelectWordOrShortcut() sets the
|
|
|
|
// appearance to Normal. 2) When the caret is out of viewport and
|
|
|
|
// now scrolling into viewport, it has appearance NormalNotShown.
|
2015-09-09 11:47:20 +03:00
|
|
|
mFirstCaret->SetAppearance(Appearance::Normal);
|
|
|
|
} else {
|
2017-03-14 10:25:16 +03:00
|
|
|
// Possible cases are: a) Single tap on current empty content;
|
|
|
|
// OnSelectionChanged() sets the appearance to None due to
|
|
|
|
// MOUSEDOWN_REASON. b) Single tap on other empty content;
|
|
|
|
// OnBlur() sets the appearance to None.
|
|
|
|
//
|
|
|
|
// Do nothing to make the appearance remains None so that it can
|
|
|
|
// be distinguished from case 2). Also do not set the appearance
|
|
|
|
// to NormalNotShown here like the default update behavior.
|
2015-09-09 11:47:20 +03:00
|
|
|
}
|
2017-03-14 10:25:16 +03:00
|
|
|
} else {
|
|
|
|
mFirstCaret->SetAppearance(Appearance::NormalNotShown);
|
|
|
|
}
|
2015-09-09 11:47:20 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PositionChangedResult::Invisible:
|
|
|
|
mFirstCaret->SetAppearance(Appearance::NormalNotShown);
|
|
|
|
break;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
2015-09-09 11:47:20 +03:00
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
mSecondCaret->SetAppearance(Appearance::None);
|
2015-05-14 03:10:00 +03:00
|
|
|
|
2020-02-08 01:31:40 +03:00
|
|
|
mIsCaretPositionChanged = (result == PositionChangedResult::Position);
|
2019-11-02 06:05:28 +03:00
|
|
|
|
2017-03-14 12:50:03 +03:00
|
|
|
if (!aHints.contains(UpdateCaretsHint::DispatchNoEvent) && !mActiveCaret) {
|
2015-05-14 03:10:00 +03:00
|
|
|
DispatchCaretStateChangedEvent(CaretChangedReason::Updateposition);
|
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:49:40 +03:00
|
|
|
void AccessibleCaretManager::UpdateCaretsForSelectionMode(
|
|
|
|
const UpdateCaretsHintSet& aHints) {
|
2015-09-09 11:47:20 +03:00
|
|
|
AC_LOG("%s: selection: %p", __FUNCTION__, GetSelection());
|
2015-04-30 01:59:00 +03:00
|
|
|
|
|
|
|
int32_t startOffset = 0;
|
2016-04-11 12:57:29 +03:00
|
|
|
nsIFrame* startFrame =
|
|
|
|
GetFrameForFirstRangeStartOrLastRangeEnd(eDirNext, &startOffset);
|
2015-04-30 01:59:00 +03:00
|
|
|
|
|
|
|
int32_t endOffset = 0;
|
2016-04-11 12:57:29 +03:00
|
|
|
nsIFrame* endFrame =
|
|
|
|
GetFrameForFirstRangeStartOrLastRangeEnd(eDirPrevious, &endOffset);
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
if (!CompareTreePosition(startFrame, endFrame)) {
|
|
|
|
// XXX: Do we really have to hide carets if this condition isn't satisfied?
|
2015-04-30 01:59:00 +03:00
|
|
|
HideCarets();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-14 10:25:16 +03:00
|
|
|
auto updateSingleCaret = [aHints](AccessibleCaret* aCaret, nsIFrame* aFrame,
|
|
|
|
int32_t aOffset) -> PositionChangedResult {
|
2015-04-30 01:59:00 +03:00
|
|
|
PositionChangedResult result = aCaret->SetPosition(aFrame, aOffset);
|
2015-09-09 11:47:20 +03:00
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
switch (result) {
|
2015-09-09 11:47:20 +03:00
|
|
|
case PositionChangedResult::NotChanged:
|
2020-02-08 01:31:40 +03:00
|
|
|
case PositionChangedResult::Position:
|
|
|
|
case PositionChangedResult::Zoom:
|
2019-01-03 08:25:19 +03:00
|
|
|
if (!aHints.contains(UpdateCaretsHint::RespectOldAppearance)) {
|
2017-03-14 10:25:16 +03:00
|
|
|
aCaret->SetAppearance(Appearance::Normal);
|
2016-02-11 11:22:57 +03:00
|
|
|
}
|
2015-09-09 11:47:20 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PositionChangedResult::Invisible:
|
|
|
|
aCaret->SetAppearance(Appearance::NormalNotShown);
|
|
|
|
break;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
PositionChangedResult firstCaretResult =
|
|
|
|
updateSingleCaret(mFirstCaret.get(), startFrame, startOffset);
|
|
|
|
PositionChangedResult secondCaretResult =
|
|
|
|
updateSingleCaret(mSecondCaret.get(), endFrame, endOffset);
|
|
|
|
|
2019-11-02 06:05:28 +03:00
|
|
|
mIsCaretPositionChanged =
|
2020-02-08 01:31:40 +03:00
|
|
|
firstCaretResult == PositionChangedResult::Position ||
|
|
|
|
secondCaretResult == PositionChangedResult::Position;
|
2019-11-02 06:05:28 +03:00
|
|
|
|
|
|
|
if (mIsCaretPositionChanged) {
|
2015-04-30 01:59:00 +03:00
|
|
|
// Flush layout to make the carets intersection correct.
|
2018-01-27 21:29:05 +03:00
|
|
|
if (!FlushLayout()) {
|
2016-02-15 10:12:35 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2019-01-03 08:25:19 +03:00
|
|
|
if (!aHints.contains(UpdateCaretsHint::RespectOldAppearance)) {
|
|
|
|
// Only check for tilt carets when the caller doesn't ask us to preserve
|
|
|
|
// old appearance. Otherwise we might override the appearance set by the
|
|
|
|
// caller.
|
2018-09-22 00:59:52 +03:00
|
|
|
if (StaticPrefs::layout_accessiblecaret_always_tilt()) {
|
2016-02-16 11:55:28 +03:00
|
|
|
UpdateCaretsForAlwaysTilt(startFrame, endFrame);
|
|
|
|
} else {
|
|
|
|
UpdateCaretsForOverlappingTilt();
|
|
|
|
}
|
2016-02-11 11:22:57 +03:00
|
|
|
}
|
2015-05-14 03:10:00 +03:00
|
|
|
|
2017-03-14 12:50:03 +03:00
|
|
|
if (!aHints.contains(UpdateCaretsHint::DispatchNoEvent) && !mActiveCaret) {
|
2015-05-14 03:10:00 +03:00
|
|
|
DispatchCaretStateChangedEvent(CaretChangedReason::Updateposition);
|
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-02 06:05:28 +03:00
|
|
|
void AccessibleCaretManager::UpdateShouldDisableApz() {
|
|
|
|
if (mActiveCaret) {
|
|
|
|
// No need to disable APZ when dragging the caret.
|
|
|
|
mShouldDisableApz = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsScrollStarted) {
|
|
|
|
// During scrolling, the caret's position is changed only if it is in a
|
|
|
|
// position:fixed or a "stuck" position:sticky frame subtree.
|
|
|
|
mShouldDisableApz = mIsCaretPositionChanged;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For other cases, we can only reliably detect whether the caret is in a
|
|
|
|
// position:fixed frame subtree.
|
|
|
|
switch (mLastUpdateCaretMode) {
|
|
|
|
case CaretMode::None:
|
|
|
|
mShouldDisableApz = false;
|
|
|
|
break;
|
|
|
|
case CaretMode::Cursor:
|
|
|
|
mShouldDisableApz = mFirstCaret->IsVisuallyVisible() &&
|
|
|
|
mFirstCaret->IsInPositionFixedSubtree();
|
|
|
|
break;
|
|
|
|
case CaretMode::Selection:
|
|
|
|
mShouldDisableApz = (mFirstCaret->IsVisuallyVisible() &&
|
|
|
|
mFirstCaret->IsInPositionFixedSubtree()) ||
|
|
|
|
(mSecondCaret->IsVisuallyVisible() &&
|
|
|
|
mSecondCaret->IsInPositionFixedSubtree());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 11:55:28 +03:00
|
|
|
bool AccessibleCaretManager::UpdateCaretsForOverlappingTilt() {
|
2017-02-08 10:51:28 +03:00
|
|
|
if (!mFirstCaret->IsVisuallyVisible() || !mSecondCaret->IsVisuallyVisible()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mFirstCaret->Intersects(*mSecondCaret)) {
|
|
|
|
mFirstCaret->SetAppearance(Appearance::Normal);
|
|
|
|
mSecondCaret->SetAppearance(Appearance::Normal);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFirstCaret->LogicalPosition().x <= mSecondCaret->LogicalPosition().x) {
|
|
|
|
mFirstCaret->SetAppearance(Appearance::Left);
|
|
|
|
mSecondCaret->SetAppearance(Appearance::Right);
|
|
|
|
} else {
|
|
|
|
mFirstCaret->SetAppearance(Appearance::Right);
|
|
|
|
mSecondCaret->SetAppearance(Appearance::Left);
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
2017-02-08 10:51:28 +03:00
|
|
|
|
|
|
|
return true;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2016-02-16 11:55:28 +03:00
|
|
|
void AccessibleCaretManager::UpdateCaretsForAlwaysTilt(nsIFrame* aStartFrame,
|
|
|
|
nsIFrame* aEndFrame) {
|
2017-02-08 10:51:28 +03:00
|
|
|
// When a short LTR word in RTL environment is selected, the two carets
|
|
|
|
// tilted inward might be overlapped. Make them tilt outward.
|
|
|
|
if (UpdateCaretsForOverlappingTilt()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-16 11:55:28 +03:00
|
|
|
if (mFirstCaret->IsVisuallyVisible()) {
|
|
|
|
auto startFrameWritingMode = aStartFrame->GetWritingMode();
|
|
|
|
mFirstCaret->SetAppearance(startFrameWritingMode.IsBidiLTR()
|
|
|
|
? Appearance::Left
|
|
|
|
: Appearance::Right);
|
|
|
|
}
|
|
|
|
if (mSecondCaret->IsVisuallyVisible()) {
|
|
|
|
auto endFrameWritingMode = aEndFrame->GetWritingMode();
|
|
|
|
mSecondCaret->SetAppearance(
|
|
|
|
endFrameWritingMode.IsBidiLTR() ? Appearance::Right : Appearance::Left);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 02:57:02 +03:00
|
|
|
void AccessibleCaretManager::ProvideHapticFeedback() {
|
2018-09-22 00:59:52 +03:00
|
|
|
if (StaticPrefs::layout_accessiblecaret_hapticfeedback()) {
|
2015-12-11 02:57:02 +03:00
|
|
|
nsCOMPtr<nsIHapticFeedback> haptic =
|
|
|
|
do_GetService("@mozilla.org/widget/hapticfeedback;1");
|
|
|
|
haptic->PerformSimpleAction(haptic->LongPress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 16:39:30 +03:00
|
|
|
nsresult AccessibleCaretManager::PressCaret(const nsPoint& aPoint,
|
|
|
|
EventClassID aEventClass) {
|
2015-04-30 01:59:00 +03:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
2016-08-14 16:39:30 +03:00
|
|
|
MOZ_ASSERT(aEventClass == eMouseEventClass || aEventClass == eTouchEventClass,
|
|
|
|
"Unexpected event class!");
|
|
|
|
|
|
|
|
using TouchArea = AccessibleCaret::TouchArea;
|
|
|
|
TouchArea touchArea =
|
|
|
|
aEventClass == eMouseEventClass ? TouchArea::CaretImage : TouchArea::Full;
|
|
|
|
|
|
|
|
if (mFirstCaret->Contains(aPoint, touchArea)) {
|
2015-04-30 01:59:00 +03:00
|
|
|
mActiveCaret = mFirstCaret.get();
|
|
|
|
SetSelectionDirection(eDirPrevious);
|
2016-08-14 16:39:30 +03:00
|
|
|
} else if (mSecondCaret->Contains(aPoint, touchArea)) {
|
2015-04-30 01:59:00 +03:00
|
|
|
mActiveCaret = mSecondCaret.get();
|
|
|
|
SetSelectionDirection(eDirNext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mActiveCaret) {
|
|
|
|
mOffsetYToCaretLogicalPosition =
|
|
|
|
mActiveCaret->LogicalPosition().y - aPoint.y;
|
|
|
|
SetSelectionDragState(true);
|
2015-05-14 03:10:00 +03:00
|
|
|
DispatchCaretStateChangedEvent(CaretChangedReason::Presscaret);
|
2015-04-30 01:59:00 +03:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AccessibleCaretManager::DragCaret(const nsPoint& aPoint) {
|
|
|
|
MOZ_ASSERT(mActiveCaret);
|
|
|
|
MOZ_ASSERT(GetCaretMode() != CaretMode::None);
|
|
|
|
|
2017-10-06 10:45:32 +03:00
|
|
|
if (!mPresShell || !mPresShell->GetRootFrame() || !GetSelection()) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2017-10-06 10:54:23 +03:00
|
|
|
StopSelectionAutoScrollTimer();
|
2017-10-06 12:39:56 +03:00
|
|
|
DragCaretInternal(aPoint);
|
2017-10-06 10:54:23 +03:00
|
|
|
|
|
|
|
// We want to scroll the page even if we failed to drag the caret.
|
|
|
|
StartSelectionAutoScrollTimer(aPoint);
|
2015-04-30 01:59:00 +03:00
|
|
|
UpdateCarets();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AccessibleCaretManager::ReleaseCaret() {
|
|
|
|
MOZ_ASSERT(mActiveCaret);
|
|
|
|
|
|
|
|
mActiveCaret = nullptr;
|
|
|
|
SetSelectionDragState(false);
|
2019-11-02 06:05:28 +03:00
|
|
|
UpdateShouldDisableApz();
|
2015-05-14 03:10:00 +03:00
|
|
|
DispatchCaretStateChangedEvent(CaretChangedReason::Releasecaret);
|
2015-04-30 01:59:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AccessibleCaretManager::TapCaret(const nsPoint& aPoint) {
|
|
|
|
MOZ_ASSERT(GetCaretMode() != CaretMode::None);
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (GetCaretMode() == CaretMode::Cursor) {
|
2015-05-14 03:10:00 +03:00
|
|
|
DispatchCaretStateChangedEvent(CaretChangedReason::Taponcaret);
|
2015-04-30 01:59:00 +03:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:35:07 +03:00
|
|
|
static EnumSet<nsLayoutUtils::FrameForPointOption> GetHitTestOptions() {
|
|
|
|
EnumSet<nsLayoutUtils::FrameForPointOption> options = {
|
|
|
|
nsLayoutUtils::FrameForPointOption::IgnorePaintSuppression,
|
|
|
|
nsLayoutUtils::FrameForPointOption::IgnoreCrossDoc};
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
nsresult AccessibleCaretManager::SelectWordOrShortcut(const nsPoint& aPoint) {
|
2016-09-30 16:36:10 +03:00
|
|
|
// If the long-tap is landing on a pre-existing selection, don't replace
|
|
|
|
// it with a new one. Instead just return and let the context menu pop up
|
|
|
|
// on the pre-existing selection.
|
|
|
|
if (GetCaretMode() == CaretMode::Selection &&
|
|
|
|
GetSelection()->ContainsPoint(aPoint)) {
|
|
|
|
AC_LOG("%s: UpdateCarets() for current selection", __FUNCTION__);
|
2018-01-29 22:35:17 +03:00
|
|
|
UpdateCarets();
|
|
|
|
ProvideHapticFeedback();
|
2016-09-30 16:36:10 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
if (!rootFrame) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2015-08-19 10:54:10 +03:00
|
|
|
// Find the frame under point.
|
2020-05-05 18:24:12 +03:00
|
|
|
AutoWeakFrame ptFrame = nsLayoutUtils::GetFrameForPoint(
|
|
|
|
RelativeTo{rootFrame}, aPoint, GetHitTestOptions());
|
2018-12-21 14:22:07 +03:00
|
|
|
if (!ptFrame.GetFrame()) {
|
2015-04-30 01:59:00 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-08-27 23:55:33 +03:00
|
|
|
nsIFrame* focusableFrame = GetFocusableFrame(ptFrame);
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2015-08-19 10:54:10 +03:00
|
|
|
#ifdef DEBUG_FRAME_DUMP
|
|
|
|
AC_LOG("%s: Found %s under (%d, %d)", __FUNCTION__, ptFrame->ListTag().get(),
|
|
|
|
aPoint.x, aPoint.y);
|
2015-08-27 23:55:33 +03:00
|
|
|
AC_LOG("%s: Found %s focusable", __FUNCTION__,
|
|
|
|
focusableFrame ? focusableFrame->ListTag().get() : "no frame");
|
2015-08-19 10:54:10 +03:00
|
|
|
#endif
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2016-07-10 09:36:02 +03:00
|
|
|
// Get ptInFrame here so that we don't need to check whether rootFrame is
|
|
|
|
// alive later. Note that if ptFrame is being moved by
|
|
|
|
// IMEStateManager::NotifyIME() or ChangeFocusToOrClearOldFocus() below,
|
|
|
|
// something under the original point will be selected, which may not be the
|
|
|
|
// original text the user wants to select.
|
|
|
|
nsPoint ptInFrame = aPoint;
|
2020-05-05 22:26:38 +03:00
|
|
|
nsLayoutUtils::TransformPoint(RelativeTo{rootFrame}, RelativeTo{ptFrame},
|
|
|
|
ptInFrame);
|
2016-07-10 09:36:02 +03:00
|
|
|
|
2015-08-19 10:54:10 +03:00
|
|
|
// Firstly check long press on an empty editable content.
|
2015-09-18 20:05:19 +03:00
|
|
|
Element* newFocusEditingHost = GetEditingHostForFrame(ptFrame);
|
2015-08-27 23:55:33 +03:00
|
|
|
if (focusableFrame && newFocusEditingHost &&
|
2015-09-18 20:05:19 +03:00
|
|
|
!HasNonEmptyTextContent(newFocusEditingHost)) {
|
2015-08-27 23:55:33 +03:00
|
|
|
ChangeFocusToOrClearOldFocus(focusableFrame);
|
2016-02-08 11:08:46 +03:00
|
|
|
|
2018-09-22 00:59:52 +03:00
|
|
|
if (StaticPrefs::
|
|
|
|
layout_accessiblecaret_caret_shown_when_long_tapping_on_empty_content()) {
|
2016-02-08 11:08:46 +03:00
|
|
|
mFirstCaret->SetAppearance(Appearance::Normal);
|
|
|
|
}
|
2015-08-19 10:54:10 +03:00
|
|
|
// We need to update carets to get correct information before dispatching
|
|
|
|
// CaretStateChangedEvent.
|
2018-01-29 22:35:17 +03:00
|
|
|
UpdateCarets();
|
|
|
|
ProvideHapticFeedback();
|
2015-08-19 10:54:10 +03:00
|
|
|
DispatchCaretStateChangedEvent(CaretChangedReason::Longpressonemptycontent);
|
2015-04-30 01:59:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-29 02:31:29 +03:00
|
|
|
bool selectable = ptFrame->IsSelectable(nullptr);
|
2015-08-27 23:55:33 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG_FRAME_DUMP
|
|
|
|
AC_LOG("%s: %s %s selectable.", __FUNCTION__, ptFrame->ListTag().get(),
|
|
|
|
selectable ? "is" : "is NOT");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!selectable) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-11-25 00:16:00 +03:00
|
|
|
// Commit the composition string of the old editable focus element (if there
|
|
|
|
// is any) before changing the focus.
|
|
|
|
IMEStateManager::NotifyIME(widget::REQUEST_TO_COMMIT_COMPOSITION,
|
|
|
|
mPresShell->GetPresContext());
|
2016-07-10 09:36:02 +03:00
|
|
|
if (!ptFrame.IsAlive()) {
|
|
|
|
// Cannot continue because ptFrame died.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-11-25 00:16:00 +03:00
|
|
|
|
2015-08-27 23:55:33 +03:00
|
|
|
// ptFrame is selectable. Now change the focus.
|
|
|
|
ChangeFocusToOrClearOldFocus(focusableFrame);
|
2016-07-10 09:36:02 +03:00
|
|
|
if (!ptFrame.IsAlive()) {
|
|
|
|
// Cannot continue because ptFrame died.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-08-27 23:55:33 +03:00
|
|
|
|
2019-09-26 11:36:46 +03:00
|
|
|
// If long tap point isn't selectable frame for caret and frame selection
|
|
|
|
// can find a better frame for caret, we don't select a word.
|
|
|
|
// See https://webcompat.com/issues/15953
|
|
|
|
nsIFrame::ContentOffsets offsets =
|
|
|
|
ptFrame->GetContentOffsetsFromPoint(ptInFrame, nsIFrame::SKIP_HIDDEN);
|
|
|
|
if (offsets.content) {
|
|
|
|
RefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
|
|
|
|
if (frameSelection) {
|
|
|
|
int32_t offset;
|
2020-02-07 19:13:27 +03:00
|
|
|
nsIFrame* theFrame = nsFrameSelection::GetFrameForNodeOffset(
|
2019-09-26 11:36:46 +03:00
|
|
|
offsets.content, offsets.offset, offsets.associate, &offset);
|
|
|
|
if (theFrame && theFrame != ptFrame) {
|
|
|
|
SetSelectionDragState(true);
|
2020-02-11 18:12:26 +03:00
|
|
|
frameSelection->HandleClick(
|
|
|
|
offsets.content, offsets.StartOffset(), offsets.EndOffset(),
|
|
|
|
nsFrameSelection::FocusMode::kCollapseToNewPoint,
|
|
|
|
offsets.associate);
|
2019-09-26 11:36:46 +03:00
|
|
|
SetSelectionDragState(false);
|
|
|
|
ClearMaintainedSelection();
|
|
|
|
|
|
|
|
if (StaticPrefs::
|
|
|
|
layout_accessiblecaret_caret_shown_when_long_tapping_on_empty_content()) {
|
|
|
|
mFirstCaret->SetAppearance(Appearance::Normal);
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateCarets();
|
|
|
|
ProvideHapticFeedback();
|
|
|
|
DispatchCaretStateChangedEvent(
|
|
|
|
CaretChangedReason::Longpressonemptycontent);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 10:54:10 +03:00
|
|
|
// Then try select a word under point.
|
2015-04-30 01:59:00 +03:00
|
|
|
nsresult rv = SelectWord(ptFrame, ptInFrame);
|
2018-01-29 22:35:17 +03:00
|
|
|
UpdateCarets();
|
|
|
|
ProvideHapticFeedback();
|
2015-12-11 02:57:02 +03:00
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::OnScrollStart() {
|
|
|
|
AC_LOG("%s", __FUNCTION__);
|
|
|
|
|
2019-01-03 13:52:51 +03:00
|
|
|
AutoRestore<bool> saveAllowFlushingLayout(mAllowFlushingLayout);
|
|
|
|
mAllowFlushingLayout = false;
|
|
|
|
|
2019-09-26 23:55:58 +03:00
|
|
|
Maybe<PresShell::AutoAssertNoFlush> assert;
|
|
|
|
if (mPresShell) {
|
|
|
|
assert.emplace(*mPresShell);
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:50:03 +03:00
|
|
|
mIsScrollStarted = true;
|
|
|
|
|
2016-04-21 11:53:40 +03:00
|
|
|
if (mFirstCaret->IsLogicallyVisible() || mSecondCaret->IsLogicallyVisible()) {
|
|
|
|
// Dispatch the event only if one of the carets is logically visible like in
|
|
|
|
// HideCarets().
|
|
|
|
DispatchCaretStateChangedEvent(CaretChangedReason::Scroll);
|
2015-12-01 23:25:06 +03:00
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::OnScrollEnd() {
|
2015-09-18 20:05:18 +03:00
|
|
|
if (mLastUpdateCaretMode != GetCaretMode()) {
|
2015-04-30 01:59:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:52:51 +03:00
|
|
|
AutoRestore<bool> saveAllowFlushingLayout(mAllowFlushingLayout);
|
|
|
|
mAllowFlushingLayout = false;
|
|
|
|
|
2019-09-26 23:55:58 +03:00
|
|
|
Maybe<PresShell::AutoAssertNoFlush> assert;
|
|
|
|
if (mPresShell) {
|
|
|
|
assert.emplace(*mPresShell);
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:50:03 +03:00
|
|
|
mIsScrollStarted = false;
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
if (GetCaretMode() == CaretMode::Cursor) {
|
2015-10-08 10:18:03 +03:00
|
|
|
if (!mFirstCaret->IsLogicallyVisible()) {
|
2017-03-17 03:50:30 +03:00
|
|
|
// If the caret is hidden (Appearance::None) due to blur, no
|
2016-02-11 11:22:57 +03:00
|
|
|
// need to update it.
|
2015-10-08 10:18:03 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
2015-10-08 10:18:03 +03:00
|
|
|
|
2016-10-03 15:57:44 +03:00
|
|
|
// For mouse input we don't want to show the carets.
|
2018-09-22 00:59:52 +03:00
|
|
|
if (StaticPrefs::layout_accessiblecaret_hide_carets_for_mouse_input() &&
|
2018-06-26 00:20:54 +03:00
|
|
|
mLastInputSource == MouseEvent_Binding::MOZ_SOURCE_MOUSE) {
|
2016-10-03 15:57:44 +03:00
|
|
|
AC_LOG("%s: HideCarets()", __FUNCTION__);
|
|
|
|
HideCarets();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-08 10:18:03 +03:00
|
|
|
AC_LOG("%s: UpdateCarets()", __FUNCTION__);
|
|
|
|
UpdateCarets();
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::OnScrollPositionChanged() {
|
2015-09-18 20:05:18 +03:00
|
|
|
if (mLastUpdateCaretMode != GetCaretMode()) {
|
2015-04-30 01:59:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:52:51 +03:00
|
|
|
AutoRestore<bool> saveAllowFlushingLayout(mAllowFlushingLayout);
|
|
|
|
mAllowFlushingLayout = false;
|
|
|
|
|
2019-09-26 23:55:58 +03:00
|
|
|
Maybe<PresShell::AutoAssertNoFlush> assert;
|
|
|
|
if (mPresShell) {
|
|
|
|
assert.emplace(*mPresShell);
|
|
|
|
}
|
|
|
|
|
2015-09-22 12:39:36 +03:00
|
|
|
if (mFirstCaret->IsLogicallyVisible() || mSecondCaret->IsLogicallyVisible()) {
|
2017-03-14 12:50:03 +03:00
|
|
|
if (mIsScrollStarted) {
|
|
|
|
// We don't want extra CaretStateChangedEvents dispatched when user is
|
|
|
|
// scrolling the page.
|
|
|
|
AC_LOG("%s: UpdateCarets(RespectOldAppearance | DispatchNoEvent)",
|
|
|
|
__FUNCTION__);
|
|
|
|
UpdateCarets({UpdateCaretsHint::RespectOldAppearance,
|
|
|
|
UpdateCaretsHint::DispatchNoEvent});
|
|
|
|
} else {
|
|
|
|
AC_LOG("%s: UpdateCarets(RespectOldAppearance)", __FUNCTION__);
|
|
|
|
UpdateCarets(UpdateCaretsHint::RespectOldAppearance);
|
|
|
|
}
|
2015-09-22 12:39:36 +03:00
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::OnReflow() {
|
2015-09-18 20:05:18 +03:00
|
|
|
if (mLastUpdateCaretMode != GetCaretMode()) {
|
2015-04-30 01:59:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:52:51 +03:00
|
|
|
AutoRestore<bool> saveAllowFlushingLayout(mAllowFlushingLayout);
|
|
|
|
mAllowFlushingLayout = false;
|
|
|
|
|
2019-09-26 23:55:58 +03:00
|
|
|
Maybe<PresShell::AutoAssertNoFlush> assert;
|
|
|
|
if (mPresShell) {
|
|
|
|
assert.emplace(*mPresShell);
|
|
|
|
}
|
|
|
|
|
2015-09-22 12:39:36 +03:00
|
|
|
if (mFirstCaret->IsLogicallyVisible() || mSecondCaret->IsLogicallyVisible()) {
|
|
|
|
AC_LOG("%s: UpdateCarets(RespectOldAppearance)", __FUNCTION__);
|
|
|
|
UpdateCarets(UpdateCaretsHint::RespectOldAppearance);
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::OnBlur() {
|
|
|
|
AC_LOG("%s: HideCarets()", __FUNCTION__);
|
|
|
|
HideCarets();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::OnKeyboardEvent() {
|
|
|
|
if (GetCaretMode() == CaretMode::Cursor) {
|
|
|
|
AC_LOG("%s: HideCarets()", __FUNCTION__);
|
|
|
|
HideCarets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 15:16:39 +03:00
|
|
|
void AccessibleCaretManager::OnFrameReconstruction() {
|
|
|
|
mFirstCaret->EnsureApzAware();
|
|
|
|
mSecondCaret->EnsureApzAware();
|
|
|
|
}
|
|
|
|
|
2016-10-03 15:57:44 +03:00
|
|
|
void AccessibleCaretManager::SetLastInputSource(uint16_t aInputSource) {
|
|
|
|
mLastInputSource = aInputSource;
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
Selection* AccessibleCaretManager::GetSelection() const {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2015-04-30 01:59:00 +03:00
|
|
|
if (!fs) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-06-09 12:35:22 +03:00
|
|
|
return fs->GetSelection(SelectionType::eNormal);
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsFrameSelection> AccessibleCaretManager::GetFrameSelection()
|
|
|
|
const {
|
2015-09-18 20:05:19 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
MOZ_ASSERT(fm);
|
|
|
|
|
2018-04-26 22:28:31 +03:00
|
|
|
nsIContent* focusedContent = fm->GetFocusedElement();
|
2017-11-16 08:48:20 +03:00
|
|
|
if (!focusedContent) {
|
2015-04-30 01:59:00 +03:00
|
|
|
// For non-editable content
|
|
|
|
return mPresShell->FrameSelection();
|
|
|
|
}
|
2017-11-16 08:48:20 +03:00
|
|
|
|
|
|
|
nsIFrame* focusFrame = focusedContent->GetPrimaryFrame();
|
|
|
|
if (!focusFrame) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent us from touching the nsFrameSelection associated with other
|
|
|
|
// PresShell.
|
|
|
|
RefPtr<nsFrameSelection> fs = focusFrame->GetFrameSelection();
|
2019-04-03 10:53:16 +03:00
|
|
|
if (!fs || fs->GetPresShell() != mPresShell) {
|
2017-11-16 08:48:20 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.forget();
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2017-03-07 12:09:39 +03:00
|
|
|
nsAutoString AccessibleCaretManager::StringifiedSelection() const {
|
|
|
|
nsAutoString str;
|
2019-01-14 07:58:59 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2017-06-08 07:49:05 +03:00
|
|
|
if (selection) {
|
2019-01-14 07:58:59 +03:00
|
|
|
selection->Stringify(str, mAllowFlushingLayout
|
|
|
|
? Selection::FlushFrames::Yes
|
|
|
|
: Selection::FlushFrames::No);
|
2017-06-08 07:49:05 +03:00
|
|
|
}
|
2017-03-07 12:09:39 +03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
Element* AccessibleCaretManager::GetEditingHostForFrame(
|
|
|
|
nsIFrame* aFrame) const {
|
|
|
|
if (!aFrame) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto content = aFrame->GetContent();
|
|
|
|
if (!content) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return content->GetEditingHost();
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
AccessibleCaretManager::CaretMode AccessibleCaretManager::GetCaretMode() const {
|
|
|
|
Selection* selection = GetSelection();
|
|
|
|
if (!selection) {
|
|
|
|
return CaretMode::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t rangeCount = selection->RangeCount();
|
|
|
|
if (rangeCount <= 0) {
|
|
|
|
return CaretMode::None;
|
|
|
|
}
|
|
|
|
|
2020-01-17 02:06:21 +03:00
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
MOZ_ASSERT(fm);
|
|
|
|
if (fm->GetFocusedWindow() != mPresShell->GetDocument()->GetWindow()) {
|
|
|
|
// Hide carets if the window is not focused.
|
|
|
|
return CaretMode::None;
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
if (selection->IsCollapsed()) {
|
|
|
|
return CaretMode::Cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CaretMode::Selection;
|
|
|
|
}
|
|
|
|
|
2015-08-27 23:55:33 +03:00
|
|
|
nsIFrame* AccessibleCaretManager::GetFocusableFrame(nsIFrame* aFrame) const {
|
2015-08-19 10:54:10 +03:00
|
|
|
// This implementation is similar to EventStateManager::PostHandleEvent().
|
|
|
|
// Look for the nearest enclosing focusable frame.
|
|
|
|
nsIFrame* focusableFrame = aFrame;
|
|
|
|
while (focusableFrame) {
|
|
|
|
if (focusableFrame->IsFocusable(nullptr, true)) {
|
|
|
|
break;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
2015-08-19 10:54:10 +03:00
|
|
|
focusableFrame = focusableFrame->GetParent();
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
2015-08-27 23:55:33 +03:00
|
|
|
return focusableFrame;
|
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2015-08-27 23:55:33 +03:00
|
|
|
void AccessibleCaretManager::ChangeFocusToOrClearOldFocus(
|
|
|
|
nsIFrame* aFrame) const {
|
2015-04-30 01:59:00 +03:00
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
2015-08-19 10:54:10 +03:00
|
|
|
MOZ_ASSERT(fm);
|
|
|
|
|
2015-08-27 23:55:33 +03:00
|
|
|
if (aFrame) {
|
|
|
|
nsIContent* focusableContent = aFrame->GetContent();
|
2015-08-19 10:54:10 +03:00
|
|
|
MOZ_ASSERT(focusableContent, "Focusable frame must have content!");
|
2018-05-30 17:56:24 +03:00
|
|
|
RefPtr<Element> focusableElement = Element::FromNode(focusableContent);
|
2019-06-28 03:39:45 +03:00
|
|
|
fm->SetFocus(focusableElement, nsIFocusManager::FLAG_BYLONGPRESS);
|
2015-04-30 01:59:00 +03:00
|
|
|
} else {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowOuter* win = mPresShell->GetDocument()->GetWindow();
|
2015-08-19 10:54:10 +03:00
|
|
|
if (win) {
|
|
|
|
fm->ClearFocus(win);
|
|
|
|
fm->SetFocusedWindow(win);
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult AccessibleCaretManager::SelectWord(nsIFrame* aFrame,
|
|
|
|
const nsPoint& aPoint) const {
|
|
|
|
SetSelectionDragState(true);
|
2020-07-07 01:29:42 +03:00
|
|
|
nsresult rs = aFrame->SelectByTypeAtPoint(
|
|
|
|
mPresShell->GetPresContext(), aPoint, eSelectWord, eSelectWord, 0);
|
2015-04-30 01:59:00 +03:00
|
|
|
|
|
|
|
SetSelectionDragState(false);
|
|
|
|
ClearMaintainedSelection();
|
|
|
|
|
2016-04-14 09:14:16 +03:00
|
|
|
// Smart-select phone numbers if possible.
|
2018-09-22 00:59:52 +03:00
|
|
|
if (StaticPrefs::layout_accessiblecaret_extend_selection_for_phone_number()) {
|
2016-04-14 09:14:16 +03:00
|
|
|
SelectMoreIfPhoneNumber();
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::SetSelectionDragState(bool aState) const {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2015-04-30 01:59:00 +03:00
|
|
|
if (fs) {
|
|
|
|
fs->SetDragState(aState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:21:26 +03:00
|
|
|
bool AccessibleCaretManager::IsPhoneNumber(nsAString& aCandidate) const {
|
2019-01-02 16:05:23 +03:00
|
|
|
RefPtr<Document> doc = mPresShell->GetDocument();
|
2020-07-01 11:29:29 +03:00
|
|
|
nsAutoString phoneNumberRegex(u"(^\\+)?[0-9 ,\\-.()*#pw]{1,30}$"_ns);
|
2019-09-16 13:11:42 +03:00
|
|
|
return nsContentUtils::IsPatternMatching(aCandidate, phoneNumberRegex, doc)
|
|
|
|
.valueOr(false);
|
2017-03-07 12:21:26 +03:00
|
|
|
}
|
|
|
|
|
2016-04-14 09:14:16 +03:00
|
|
|
void AccessibleCaretManager::SelectMoreIfPhoneNumber() const {
|
2017-03-07 12:21:26 +03:00
|
|
|
nsAutoString selectedText = StringifiedSelection();
|
|
|
|
|
|
|
|
if (IsPhoneNumber(selectedText)) {
|
|
|
|
SetSelectionDirection(eDirNext);
|
2020-07-01 11:29:29 +03:00
|
|
|
ExtendPhoneNumberSelection(u"forward"_ns);
|
2016-04-14 09:14:16 +03:00
|
|
|
|
2017-03-07 12:21:26 +03:00
|
|
|
SetSelectionDirection(eDirPrevious);
|
2020-07-01 11:29:29 +03:00
|
|
|
ExtendPhoneNumberSelection(u"backward"_ns);
|
2016-04-21 15:53:13 +03:00
|
|
|
|
2017-03-07 12:21:26 +03:00
|
|
|
SetSelectionDirection(eDirNext);
|
|
|
|
}
|
2016-04-14 09:14:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::ExtendPhoneNumberSelection(
|
|
|
|
const nsAString& aDirection) const {
|
2016-04-28 08:59:39 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-14 09:14:16 +03:00
|
|
|
// Extend the phone number selection until we find a boundary.
|
2016-04-28 08:59:39 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2016-04-14 09:14:16 +03:00
|
|
|
|
|
|
|
while (selection) {
|
2016-04-27 12:19:51 +03:00
|
|
|
const nsRange* anchorFocusRange = selection->GetAnchorFocusRange();
|
|
|
|
if (!anchorFocusRange) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-21 15:53:13 +03:00
|
|
|
// Backup the anchor focus range since both anchor node and focus node might
|
|
|
|
// be changed after calling Selection::Modify().
|
2016-04-27 12:19:51 +03:00
|
|
|
RefPtr<nsRange> oldAnchorFocusRange = anchorFocusRange->CloneRange();
|
2016-04-21 15:53:13 +03:00
|
|
|
|
2017-02-17 17:38:08 +03:00
|
|
|
// Save current focus node, focus offset and the selected text so that
|
|
|
|
// we can compare them with the modified ones later.
|
|
|
|
nsINode* oldFocusNode = selection->GetFocusNode();
|
|
|
|
uint32_t oldFocusOffset = selection->FocusOffset();
|
2017-03-07 12:09:39 +03:00
|
|
|
nsAutoString oldSelectedText = StringifiedSelection();
|
2017-02-17 17:38:08 +03:00
|
|
|
|
|
|
|
// Extend the selection by one char.
|
2020-07-01 11:29:29 +03:00
|
|
|
selection->Modify(u"extend"_ns, aDirection, u"character"_ns,
|
|
|
|
IgnoreErrors());
|
2016-04-28 08:59:39 +03:00
|
|
|
if (IsTerminated()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-14 09:14:16 +03:00
|
|
|
|
|
|
|
// If the selection didn't change, (can't extend further), we're done.
|
2017-02-17 17:38:08 +03:00
|
|
|
if (selection->GetFocusNode() == oldFocusNode &&
|
|
|
|
selection->FocusOffset() == oldFocusOffset) {
|
2016-04-14 09:14:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the changed selection isn't a valid phone number, we're done.
|
2017-02-17 17:38:08 +03:00
|
|
|
// Also, if the selection was extended to a new block node, the string
|
|
|
|
// returned by stringify() won't have a new line at the beginning or the
|
|
|
|
// end of the string. Therefore, if either focus node or offset is
|
|
|
|
// changed, but selected text is not changed, we're done, too.
|
2017-03-07 12:09:39 +03:00
|
|
|
nsAutoString selectedText = StringifiedSelection();
|
2016-04-14 09:14:16 +03:00
|
|
|
|
2017-03-07 12:21:26 +03:00
|
|
|
if (!IsPhoneNumber(selectedText) || oldSelectedText == selectedText) {
|
2016-04-21 15:53:13 +03:00
|
|
|
// Backout the undesired selection extend, restore the old anchor focus
|
|
|
|
// range before exit.
|
|
|
|
selection->SetAnchorFocusToRange(oldAnchorFocusRange);
|
2016-04-14 09:14:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
void AccessibleCaretManager::SetSelectionDirection(nsDirection aDir) const {
|
|
|
|
Selection* selection = GetSelection();
|
|
|
|
if (selection) {
|
|
|
|
selection->AdjustAnchorFocusForMultiRange(aDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::ClearMaintainedSelection() const {
|
|
|
|
// Selection made by double-clicking for example will maintain the original
|
|
|
|
// word selection. We should clear it so that we can drag caret freely.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2015-04-30 01:59:00 +03:00
|
|
|
if (fs) {
|
|
|
|
fs->MaintainSelection(eSelectNoAmount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 21:29:05 +03:00
|
|
|
bool AccessibleCaretManager::FlushLayout() {
|
2019-01-03 13:52:51 +03:00
|
|
|
if (mPresShell && mAllowFlushingLayout) {
|
2018-01-27 21:29:05 +03:00
|
|
|
AutoRestore<bool> flushing(mFlushingLayout);
|
|
|
|
mFlushingLayout = true;
|
2018-01-20 01:39:10 +03:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
if (Document* doc = mPresShell->GetDocument()) {
|
2018-01-27 21:29:05 +03:00
|
|
|
doc->FlushPendingNotifications(FlushType::Layout);
|
|
|
|
}
|
2015-09-18 20:05:19 +03:00
|
|
|
}
|
2018-01-27 21:29:05 +03:00
|
|
|
|
|
|
|
return !IsTerminated();
|
2015-09-18 20:05:19 +03:00
|
|
|
}
|
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
nsIFrame* AccessibleCaretManager::GetFrameForFirstRangeStartOrLastRangeEnd(
|
2017-05-02 09:24:54 +03:00
|
|
|
nsDirection aDirection, int32_t* aOutOffset, nsIContent** aOutContent,
|
|
|
|
int32_t* aOutContentOffset) const {
|
2015-04-30 01:59:00 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
MOZ_ASSERT(GetCaretMode() == CaretMode::Selection);
|
2016-07-04 12:39:15 +03:00
|
|
|
MOZ_ASSERT(aOutOffset, "aOutOffset shouldn't be nullptr!");
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2020-05-03 02:46:01 +03:00
|
|
|
const nsRange* range = nullptr;
|
2016-04-11 12:57:29 +03:00
|
|
|
RefPtr<nsINode> startNode;
|
|
|
|
RefPtr<nsINode> endNode;
|
|
|
|
int32_t nodeOffset = 0;
|
|
|
|
CaretAssociationHint hint;
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
|
|
|
bool findInFirstRangeStart = aDirection == eDirNext;
|
|
|
|
|
|
|
|
if (findInFirstRangeStart) {
|
|
|
|
range = selection->GetRangeAt(0);
|
2017-07-11 14:53:04 +03:00
|
|
|
startNode = range->GetStartContainer();
|
2017-07-11 15:11:37 +03:00
|
|
|
endNode = range->GetEndContainer();
|
2016-04-11 12:57:29 +03:00
|
|
|
nodeOffset = range->StartOffset();
|
|
|
|
hint = CARET_ASSOCIATE_AFTER;
|
|
|
|
} else {
|
|
|
|
range = selection->GetRangeAt(selection->RangeCount() - 1);
|
2017-07-11 15:11:37 +03:00
|
|
|
startNode = range->GetEndContainer();
|
2017-07-11 14:53:04 +03:00
|
|
|
endNode = range->GetStartContainer();
|
2016-04-11 12:57:29 +03:00
|
|
|
nodeOffset = range->EndOffset();
|
|
|
|
hint = CARET_ASSOCIATE_BEFORE;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> startContent = do_QueryInterface(startNode);
|
2020-02-07 19:13:27 +03:00
|
|
|
nsIFrame* startFrame = nsFrameSelection::GetFrameForNodeOffset(
|
|
|
|
startContent, nodeOffset, hint, aOutOffset);
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2016-07-04 12:39:15 +03:00
|
|
|
if (!startFrame) {
|
|
|
|
ErrorResult err;
|
|
|
|
RefPtr<TreeWalker> walker = mPresShell->GetDocument()->CreateTreeWalker(
|
2018-06-26 00:20:54 +03:00
|
|
|
*startNode, dom::NodeFilter_Binding::SHOW_ALL, nullptr, err);
|
2016-07-04 12:39:15 +03:00
|
|
|
|
|
|
|
if (!walker) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
startFrame = startContent ? startContent->GetPrimaryFrame() : nullptr;
|
|
|
|
while (!startFrame && startNode != endNode) {
|
|
|
|
startNode = findInFirstRangeStart ? walker->NextNode(err)
|
|
|
|
: walker->PreviousNode(err);
|
|
|
|
|
|
|
|
if (!startNode) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
startContent = startNode->AsContent();
|
|
|
|
startFrame = startContent ? startContent->GetPrimaryFrame() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are walking among the nodes in the content tree, so the node offset
|
|
|
|
// relative to startNode should be set to 0.
|
|
|
|
nodeOffset = 0;
|
|
|
|
*aOutOffset = 0;
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
if (startFrame) {
|
2017-05-02 09:24:54 +03:00
|
|
|
if (aOutContent) {
|
|
|
|
startContent.forget(aOutContent);
|
2016-04-11 12:57:29 +03:00
|
|
|
}
|
2017-05-02 09:24:54 +03:00
|
|
|
if (aOutContentOffset) {
|
|
|
|
*aOutContentOffset = nodeOffset;
|
2016-04-11 12:57:29 +03:00
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return startFrame;
|
|
|
|
}
|
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
bool AccessibleCaretManager::RestrictCaretDraggingOffsets(
|
|
|
|
nsIFrame::ContentOffsets& aOffsets) {
|
|
|
|
if (!mPresShell) {
|
2015-04-30 01:59:00 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
MOZ_ASSERT(GetCaretMode() == CaretMode::Selection);
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
nsDirection dir = mActiveCaret == mFirstCaret.get() ? eDirPrevious : eDirNext;
|
|
|
|
int32_t offset = 0;
|
2017-05-02 09:24:54 +03:00
|
|
|
nsCOMPtr<nsIContent> content;
|
2016-04-11 12:57:29 +03:00
|
|
|
int32_t contentOffset = 0;
|
2017-05-02 09:24:54 +03:00
|
|
|
nsIFrame* frame = GetFrameForFirstRangeStartOrLastRangeEnd(
|
|
|
|
dir, &offset, getter_AddRefs(content), &contentOffset);
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
if (!frame) {
|
2015-04-30 01:59:00 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
// Compare the active caret's new position (aOffsets) to the inactive caret's
|
|
|
|
// position.
|
2020-01-08 13:22:19 +03:00
|
|
|
const Maybe<int32_t> cmpToInactiveCaretPos = nsContentUtils::ComparePoints(
|
2016-04-11 12:57:29 +03:00
|
|
|
aOffsets.content, aOffsets.StartOffset(), content, contentOffset);
|
2020-01-08 13:22:19 +03:00
|
|
|
if (NS_WARN_IF(!cmpToInactiveCaretPos)) {
|
|
|
|
// Potentially handle this properly when Selection across Shadow DOM
|
|
|
|
// boundary is implemented
|
|
|
|
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1607497).
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-11 12:57:29 +03:00
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
// Move one character (in the direction of dir) from the inactive caret's
|
|
|
|
// position. This is the limit for the active caret's new position.
|
|
|
|
nsPeekOffsetStruct limit(eSelectCluster, dir, offset, nsPoint(0, 0), true,
|
|
|
|
true, false, false, false);
|
|
|
|
nsresult rv = frame->PeekOffset(&limit);
|
2015-04-30 01:59:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-04-11 12:57:29 +03:00
|
|
|
limit.mResultContent = content;
|
|
|
|
limit.mContentOffset = contentOffset;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
// Compare the active caret's new position (aOffsets) to the limit.
|
2020-01-08 13:22:19 +03:00
|
|
|
const Maybe<int32_t> cmpToLimit =
|
|
|
|
nsContentUtils::ComparePoints(aOffsets.content, aOffsets.StartOffset(),
|
|
|
|
limit.mResultContent, limit.mContentOffset);
|
|
|
|
if (NS_WARN_IF(!cmpToLimit)) {
|
|
|
|
// Potentially handle this properly when Selection across Shadow DOM
|
|
|
|
// boundary is implemented
|
|
|
|
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1607497).
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-11 12:57:29 +03:00
|
|
|
|
|
|
|
auto SetOffsetsToLimit = [&aOffsets, &limit]() {
|
2016-04-11 12:57:29 +03:00
|
|
|
aOffsets.content = limit.mResultContent;
|
|
|
|
aOffsets.offset = limit.mContentOffset;
|
|
|
|
aOffsets.secondaryOffset = limit.mContentOffset;
|
2016-04-11 12:57:29 +03:00
|
|
|
};
|
|
|
|
|
2018-09-22 00:59:52 +03:00
|
|
|
if (!StaticPrefs::
|
|
|
|
layout_accessiblecaret_allow_dragging_across_other_caret()) {
|
2020-01-08 13:22:19 +03:00
|
|
|
if ((mActiveCaret == mFirstCaret.get() && *cmpToLimit == 1) ||
|
|
|
|
(mActiveCaret == mSecondCaret.get() && *cmpToLimit == -1)) {
|
2016-04-11 12:57:29 +03:00
|
|
|
// The active caret's position is past the limit, which we don't allow
|
|
|
|
// here. So set it to the limit, resulting in one character being
|
|
|
|
// selected.
|
|
|
|
SetOffsetsToLimit();
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-08 13:22:19 +03:00
|
|
|
switch (*cmpToInactiveCaretPos) {
|
2016-04-11 12:57:29 +03:00
|
|
|
case 0:
|
|
|
|
// The active caret's position is the same as the position of the
|
|
|
|
// inactive caret. So set it to the limit to prevent the selection from
|
|
|
|
// being collapsed, resulting in one character being selected.
|
|
|
|
SetOffsetsToLimit();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (mActiveCaret == mFirstCaret.get()) {
|
|
|
|
// First caret was moved across the second caret. After making change
|
|
|
|
// to the selection, the user will drag the second caret.
|
|
|
|
mActiveCaret = mSecondCaret.get();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
if (mActiveCaret == mSecondCaret.get()) {
|
|
|
|
// Second caret was moved across the first caret. After making change
|
|
|
|
// to the selection, the user will drag the first caret.
|
|
|
|
mActiveCaret = mFirstCaret.get();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
bool AccessibleCaretManager::CompareTreePosition(nsIFrame* aStartFrame,
|
|
|
|
nsIFrame* aEndFrame) const {
|
|
|
|
return (aStartFrame && aEndFrame &&
|
|
|
|
nsLayoutUtils::CompareTreePosition(aStartFrame, aEndFrame) <= 0);
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
nsresult AccessibleCaretManager::DragCaretInternal(const nsPoint& aPoint) {
|
2017-10-06 10:45:32 +03:00
|
|
|
MOZ_ASSERT(mPresShell);
|
2015-04-30 01:59:00 +03:00
|
|
|
|
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
2017-10-06 10:45:32 +03:00
|
|
|
MOZ_ASSERT(rootFrame, "We need root frame to compute caret dragging!");
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2017-10-06 12:39:56 +03:00
|
|
|
nsPoint point = AdjustDragBoundary(
|
|
|
|
nsPoint(aPoint.x, aPoint.y + mOffsetYToCaretLogicalPosition));
|
2015-04-30 01:59:00 +03:00
|
|
|
|
|
|
|
// Find out which content we point to
|
2019-04-16 23:35:07 +03:00
|
|
|
|
2020-05-05 18:24:12 +03:00
|
|
|
nsIFrame* ptFrame = nsLayoutUtils::GetFrameForPoint(
|
|
|
|
RelativeTo{rootFrame}, point, GetHitTestOptions());
|
2015-04-30 01:59:00 +03:00
|
|
|
if (!ptFrame) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2017-10-06 10:45:32 +03:00
|
|
|
MOZ_ASSERT(fs);
|
2015-04-30 01:59:00 +03:00
|
|
|
|
|
|
|
nsresult result;
|
|
|
|
nsIFrame* newFrame = nullptr;
|
|
|
|
nsPoint newPoint;
|
|
|
|
nsPoint ptInFrame = point;
|
2020-05-05 22:26:38 +03:00
|
|
|
nsLayoutUtils::TransformPoint(RelativeTo{rootFrame}, RelativeTo{ptFrame},
|
|
|
|
ptInFrame);
|
2015-04-30 01:59:00 +03:00
|
|
|
result = fs->ConstrainFrameAndPointToAnchorSubtree(ptFrame, ptInFrame,
|
|
|
|
&newFrame, newPoint);
|
|
|
|
if (NS_FAILED(result) || !newFrame) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-11-29 02:31:29 +03:00
|
|
|
if (!newFrame->IsSelectable(nullptr)) {
|
2015-04-30 01:59:00 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame::ContentOffsets offsets =
|
|
|
|
newFrame->GetContentOffsetsFromPoint(newPoint);
|
2016-04-11 12:57:29 +03:00
|
|
|
if (offsets.IsNull()) {
|
2015-04-30 01:59:00 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetCaretMode() == CaretMode::Selection &&
|
2016-04-11 12:57:29 +03:00
|
|
|
!RestrictCaretDraggingOffsets(offsets)) {
|
2015-04-30 01:59:00 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearMaintainedSelection();
|
|
|
|
|
2020-02-11 18:12:26 +03:00
|
|
|
const nsFrameSelection::FocusMode focusMode =
|
|
|
|
(GetCaretMode() == CaretMode::Selection)
|
|
|
|
? nsFrameSelection::FocusMode::kExtendSelection
|
|
|
|
: nsFrameSelection::FocusMode::kCollapseToNewPoint;
|
2015-04-30 01:59:00 +03:00
|
|
|
fs->HandleClick(offsets.content, offsets.StartOffset(), offsets.EndOffset(),
|
2020-02-11 18:12:26 +03:00
|
|
|
focusMode, offsets.associate);
|
2015-04-30 01:59:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-10 12:38:33 +03:00
|
|
|
nsRect AccessibleCaretManager::GetAllChildFrameRectsUnion(
|
|
|
|
nsIFrame* aFrame) const {
|
|
|
|
nsRect unionRect;
|
2015-10-07 13:09:04 +03:00
|
|
|
|
2016-03-10 12:38:33 +03:00
|
|
|
// Drill through scroll frames, we don't want to include scrollbar child
|
|
|
|
// frames below.
|
|
|
|
for (nsIFrame* frame = aFrame->GetContentInsertionFrame(); frame;
|
|
|
|
frame = frame->GetNextContinuation()) {
|
|
|
|
nsRect frameRect;
|
2015-10-07 13:09:04 +03:00
|
|
|
|
2020-05-19 15:37:37 +03:00
|
|
|
for (const auto& childList : frame->ChildLists()) {
|
2016-03-10 12:38:33 +03:00
|
|
|
// Loop all children to union their scrollable overflow rect.
|
2020-05-18 04:17:15 +03:00
|
|
|
for (nsIFrame* child : childList.mList) {
|
2020-07-20 23:17:36 +03:00
|
|
|
nsRect childRect = child->ScrollableOverflowRectRelativeToSelf();
|
2016-03-10 12:38:33 +03:00
|
|
|
nsLayoutUtils::TransformRect(child, frame, childRect);
|
|
|
|
|
|
|
|
// A TextFrame containing only '\n' has positive height and width 0, or
|
|
|
|
// positive width and height 0 if it's vertical. Need to use UnionEdges
|
|
|
|
// to add its rect. BRFrame rect should be non-empty.
|
|
|
|
if (childRect.IsEmpty()) {
|
|
|
|
frameRect = frameRect.UnionEdges(childRect);
|
|
|
|
} else {
|
|
|
|
frameRect = frameRect.Union(childRect);
|
|
|
|
}
|
2015-10-07 13:09:04 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-10 12:38:33 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(!frameRect.IsEmpty(),
|
|
|
|
"Editable frames should have at least one BRFrame child to make "
|
|
|
|
"frameRect non-empty!");
|
|
|
|
if (frame != aFrame) {
|
|
|
|
nsLayoutUtils::TransformRect(frame, aFrame, frameRect);
|
|
|
|
}
|
|
|
|
unionRect = unionRect.Union(frameRect);
|
2015-10-07 13:09:04 +03:00
|
|
|
}
|
|
|
|
|
2016-03-10 12:38:33 +03:00
|
|
|
return unionRect;
|
2015-10-07 13:09:04 +03:00
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
nsPoint AccessibleCaretManager::AdjustDragBoundary(
|
|
|
|
const nsPoint& aPoint) const {
|
|
|
|
nsPoint adjustedPoint = aPoint;
|
|
|
|
|
2015-10-07 13:09:04 +03:00
|
|
|
int32_t focusOffset = 0;
|
|
|
|
nsIFrame* focusFrame =
|
|
|
|
nsCaret::GetFrameAndOffset(GetSelection(), nullptr, 0, &focusOffset);
|
|
|
|
Element* editingHost = GetEditingHostForFrame(focusFrame);
|
|
|
|
|
|
|
|
if (editingHost) {
|
2016-03-10 12:38:33 +03:00
|
|
|
nsIFrame* editingHostFrame = editingHost->GetPrimaryFrame();
|
|
|
|
if (editingHostFrame) {
|
|
|
|
nsRect boundary = GetAllChildFrameRectsUnion(editingHostFrame);
|
|
|
|
nsLayoutUtils::TransformRect(editingHostFrame, mPresShell->GetRootFrame(),
|
|
|
|
boundary);
|
|
|
|
|
|
|
|
// Shrink the rect to make sure we never hit the boundary.
|
|
|
|
boundary.Deflate(kBoundaryAppUnits);
|
|
|
|
|
|
|
|
adjustedPoint = boundary.ClampPoint(adjustedPoint);
|
|
|
|
}
|
2015-10-07 13:09:04 +03:00
|
|
|
}
|
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
if (GetCaretMode() == CaretMode::Selection &&
|
2018-09-22 00:59:52 +03:00
|
|
|
!StaticPrefs::
|
|
|
|
layout_accessiblecaret_allow_dragging_across_other_caret()) {
|
2015-10-07 13:09:04 +03:00
|
|
|
// Bug 1068474: Adjust the Y-coordinate so that the carets won't be in tilt
|
|
|
|
// mode when a caret is being dragged surpass the other caret.
|
|
|
|
//
|
|
|
|
// For example, when dragging the second caret, the horizontal boundary
|
|
|
|
// (lower bound) of its Y-coordinate is the logical position of the first
|
|
|
|
// caret. Likewise, when dragging the first caret, the horizontal boundary
|
|
|
|
// (upper bound) of its Y-coordinate is the logical position of the second
|
|
|
|
// caret.
|
2015-04-30 01:59:00 +03:00
|
|
|
if (mActiveCaret == mFirstCaret.get()) {
|
|
|
|
nscoord dragDownBoundaryY = mSecondCaret->LogicalPosition().y;
|
2015-06-14 17:40:00 +03:00
|
|
|
if (dragDownBoundaryY > 0 && adjustedPoint.y > dragDownBoundaryY) {
|
2015-04-30 01:59:00 +03:00
|
|
|
adjustedPoint.y = dragDownBoundaryY;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nscoord dragUpBoundaryY = mFirstCaret->LogicalPosition().y;
|
|
|
|
if (adjustedPoint.y < dragUpBoundaryY) {
|
|
|
|
adjustedPoint.y = dragUpBoundaryY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return adjustedPoint;
|
|
|
|
}
|
|
|
|
|
2017-10-06 10:54:23 +03:00
|
|
|
void AccessibleCaretManager::StartSelectionAutoScrollTimer(
|
|
|
|
const nsPoint& aPoint) const {
|
|
|
|
Selection* selection = GetSelection();
|
|
|
|
MOZ_ASSERT(selection);
|
|
|
|
|
2020-06-11 13:56:11 +03:00
|
|
|
nsIFrame* anchorFrame = selection->GetPrimaryFrameForAnchorNode();
|
2017-10-06 10:54:23 +03:00
|
|
|
if (!anchorFrame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScrollableFrame* scrollFrame = nsLayoutUtils::GetNearestScrollableFrame(
|
|
|
|
anchorFrame, nsLayoutUtils::SCROLLABLE_SAME_DOC |
|
|
|
|
nsLayoutUtils::SCROLLABLE_INCLUDE_HIDDEN);
|
|
|
|
if (!scrollFrame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* capturingFrame = scrollFrame->GetScrolledFrame();
|
|
|
|
if (!capturingFrame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
MOZ_ASSERT(rootFrame);
|
|
|
|
nsPoint ptInScrolled = aPoint;
|
2020-05-05 22:26:38 +03:00
|
|
|
nsLayoutUtils::TransformPoint(RelativeTo{rootFrame},
|
|
|
|
RelativeTo{capturingFrame}, ptInScrolled);
|
2017-10-06 10:54:23 +03:00
|
|
|
|
|
|
|
RefPtr<nsFrameSelection> fs = GetFrameSelection();
|
|
|
|
MOZ_ASSERT(fs);
|
|
|
|
fs->StartAutoScrollTimer(capturingFrame, ptInScrolled, kAutoScrollTimerDelay);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessibleCaretManager::StopSelectionAutoScrollTimer() const {
|
|
|
|
RefPtr<nsFrameSelection> fs = GetFrameSelection();
|
|
|
|
MOZ_ASSERT(fs);
|
|
|
|
fs->StopAutoScrollTimer();
|
|
|
|
}
|
|
|
|
|
2018-01-27 21:29:05 +03:00
|
|
|
void AccessibleCaretManager::DispatchCaretStateChangedEvent(
|
|
|
|
CaretChangedReason aReason) {
|
|
|
|
if (!FlushLayout()) {
|
2015-05-20 03:59:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Selection* sel = GetSelection();
|
|
|
|
if (!sel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = mPresShell->GetDocument();
|
2015-05-20 03:59:00 +03:00
|
|
|
MOZ_ASSERT(doc);
|
|
|
|
|
|
|
|
CaretStateChangedEventInit init;
|
|
|
|
init.mBubbles = true;
|
|
|
|
|
|
|
|
const nsRange* range = sel->GetAnchorFocusRange();
|
|
|
|
nsINode* commonAncestorNode = nullptr;
|
|
|
|
if (range) {
|
2020-01-13 13:29:44 +03:00
|
|
|
commonAncestorNode = range->GetClosestCommonInclusiveAncestor();
|
2015-05-20 03:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!commonAncestorNode) {
|
|
|
|
commonAncestorNode = sel->GetFrameSelection()->GetAncestorLimiter();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMRect> domRect = new DOMRect(ToSupports(doc));
|
2015-10-23 20:39:00 +03:00
|
|
|
nsRect rect = nsLayoutUtils::GetSelectionBoundingRect(sel);
|
2015-05-20 03:59:00 +03:00
|
|
|
|
|
|
|
nsIFrame* commonAncestorFrame = nullptr;
|
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
|
|
|
|
if (commonAncestorNode && commonAncestorNode->IsContent()) {
|
|
|
|
commonAncestorFrame = commonAncestorNode->AsContent()->GetPrimaryFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commonAncestorFrame && rootFrame) {
|
|
|
|
nsLayoutUtils::TransformRect(rootFrame, commonAncestorFrame, rect);
|
|
|
|
nsRect clampedRect =
|
|
|
|
nsLayoutUtils::ClampRectToScrollFrames(commonAncestorFrame, rect);
|
|
|
|
nsLayoutUtils::TransformRect(commonAncestorFrame, rootFrame, clampedRect);
|
2018-09-25 20:46:58 +03:00
|
|
|
rect = clampedRect;
|
2015-05-20 03:59:00 +03:00
|
|
|
init.mSelectionVisible = !clampedRect.IsEmpty();
|
|
|
|
} else {
|
|
|
|
init.mSelectionVisible = true;
|
|
|
|
}
|
|
|
|
|
2018-09-25 20:46:58 +03:00
|
|
|
// The rect computed above is relative to rootFrame, which is the (layout)
|
|
|
|
// viewport frame. However, the consumers of this event expect the bounds
|
|
|
|
// of the selection relative to the screen (visual viewport origin), so
|
|
|
|
// translate between the two.
|
|
|
|
rect -= mPresShell->GetVisualViewportOffsetRelativeToLayoutViewport();
|
|
|
|
|
|
|
|
domRect->SetLayoutRect(rect);
|
|
|
|
|
2015-07-30 23:34:00 +03:00
|
|
|
// Send isEditable info w/ event detail. This info can help determine
|
|
|
|
// whether to show cut command on selection dialog or not.
|
|
|
|
init.mSelectionEditable =
|
2015-09-18 20:05:19 +03:00
|
|
|
commonAncestorFrame && GetEditingHostForFrame(commonAncestorFrame);
|
2015-07-30 23:34:00 +03:00
|
|
|
|
2015-05-20 03:59:00 +03:00
|
|
|
init.mBoundingClientRect = domRect;
|
|
|
|
init.mReason = aReason;
|
|
|
|
init.mCollapsed = sel->IsCollapsed();
|
|
|
|
init.mCaretVisible =
|
|
|
|
mFirstCaret->IsLogicallyVisible() || mSecondCaret->IsLogicallyVisible();
|
|
|
|
init.mCaretVisuallyVisible =
|
2015-12-01 23:25:06 +03:00
|
|
|
mFirstCaret->IsVisuallyVisible() || mSecondCaret->IsVisuallyVisible();
|
2019-01-14 07:58:59 +03:00
|
|
|
init.mSelectedTextContent = StringifiedSelection();
|
2015-05-20 03:59:00 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CaretStateChangedEvent> event = CaretStateChangedEvent::Constructor(
|
2020-07-01 11:29:29 +03:00
|
|
|
doc, u"mozcaretstatechanged"_ns, init);
|
2015-05-20 03:59:00 +03:00
|
|
|
|
|
|
|
event->SetTrusted(true);
|
2016-02-12 18:40:07 +03:00
|
|
|
event->WidgetEventPtr()->mFlags.mOnlyChromeDispatch = true;
|
2015-08-19 10:54:10 +03:00
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
AC_LOG("%s: reason %" PRIu32 ", collapsed %d, caretVisible %" PRIu32,
|
|
|
|
__FUNCTION__, static_cast<uint32_t>(init.mReason), init.mCollapsed,
|
|
|
|
static_cast<uint32_t>(init.mCaretVisible));
|
2015-08-19 10:54:10 +03:00
|
|
|
|
2019-09-26 23:55:53 +03:00
|
|
|
(new AsyncEventDispatcher(doc, event))->PostDOMEvent();
|
2015-05-20 03:59:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
} // namespace mozilla
|