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"
|
2015-04-30 01:59:00 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#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"
|
2015-09-10 13:24:34 +03:00
|
|
|
#include "mozilla/Preferences.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"
|
|
|
|
#include "nsFocusManager.h"
|
|
|
|
#include "nsFrame.h"
|
|
|
|
#include "nsFrameSelection.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
2015-12-11 02:57:02 +03:00
|
|
|
#include "nsIHapticFeedback.h"
|
2016-03-11 02:06:13 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
#include "nsWindow.h"
|
|
|
|
#endif
|
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
|
|
|
|
|
2017-03-07 11:19:31 +03:00
|
|
|
/* static */ bool
|
2016-01-21 06:11:38 +03:00
|
|
|
AccessibleCaretManager::sSelectionBarEnabled = false;
|
2017-03-07 11:19:31 +03:00
|
|
|
/* static */ bool
|
2016-02-08 11:08:46 +03:00
|
|
|
AccessibleCaretManager::sCaretShownWhenLongTappingOnEmptyContent = false;
|
2017-03-07 11:19:31 +03:00
|
|
|
/* static */ bool
|
2016-02-16 11:55:28 +03:00
|
|
|
AccessibleCaretManager::sCaretsAlwaysTilt = false;
|
2017-03-07 11:19:31 +03:00
|
|
|
/* static */ bool
|
2016-04-21 11:53:40 +03:00
|
|
|
AccessibleCaretManager::sCaretsAlwaysShowWhenScrolling = true;
|
2017-03-07 11:19:31 +03:00
|
|
|
/* static */ bool
|
2016-01-26 17:27:36 +03:00
|
|
|
AccessibleCaretManager::sCaretsScriptUpdates = false;
|
2017-03-07 11:19:31 +03:00
|
|
|
/* static */ bool
|
2016-04-11 12:57:29 +03:00
|
|
|
AccessibleCaretManager::sCaretsAllowDraggingAcrossOtherCaret = true;
|
2017-03-07 11:19:31 +03:00
|
|
|
/* static */ bool
|
2015-12-11 02:57:02 +03:00
|
|
|
AccessibleCaretManager::sHapticFeedback = false;
|
2017-03-07 11:19:31 +03:00
|
|
|
/* static */ bool
|
2016-04-14 09:14:16 +03:00
|
|
|
AccessibleCaretManager::sExtendSelectionForPhoneNumber = false;
|
2017-03-07 11:19:31 +03:00
|
|
|
/* static */ bool
|
2016-10-03 15:57:44 +03:00
|
|
|
AccessibleCaretManager::sHideCaretsForMouseInput = true;
|
2015-12-01 23:25:06 +03:00
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
AccessibleCaretManager::AccessibleCaretManager(nsIPresShell* aPresShell)
|
|
|
|
: 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
|
|
|
|
2015-12-01 23:25:06 +03:00
|
|
|
static bool addedPrefs = false;
|
|
|
|
if (!addedPrefs) {
|
2016-01-21 06:11:38 +03:00
|
|
|
Preferences::AddBoolVarCache(&sSelectionBarEnabled,
|
|
|
|
"layout.accessiblecaret.bar.enabled");
|
2016-02-08 11:08:46 +03:00
|
|
|
Preferences::AddBoolVarCache(&sCaretShownWhenLongTappingOnEmptyContent,
|
|
|
|
"layout.accessiblecaret.caret_shown_when_long_tapping_on_empty_content");
|
2016-02-16 11:55:28 +03:00
|
|
|
Preferences::AddBoolVarCache(&sCaretsAlwaysTilt,
|
|
|
|
"layout.accessiblecaret.always_tilt");
|
2016-04-21 11:53:40 +03:00
|
|
|
Preferences::AddBoolVarCache(&sCaretsAlwaysShowWhenScrolling,
|
|
|
|
"layout.accessiblecaret.always_show_when_scrolling", true);
|
2016-01-26 17:27:36 +03:00
|
|
|
Preferences::AddBoolVarCache(&sCaretsScriptUpdates,
|
|
|
|
"layout.accessiblecaret.allow_script_change_updates");
|
2016-04-11 12:57:29 +03:00
|
|
|
Preferences::AddBoolVarCache(&sCaretsAllowDraggingAcrossOtherCaret,
|
|
|
|
"layout.accessiblecaret.allow_dragging_across_other_caret", true);
|
2015-12-11 02:57:02 +03:00
|
|
|
Preferences::AddBoolVarCache(&sHapticFeedback,
|
|
|
|
"layout.accessiblecaret.hapticfeedback");
|
2016-04-14 09:14:16 +03:00
|
|
|
Preferences::AddBoolVarCache(&sExtendSelectionForPhoneNumber,
|
|
|
|
"layout.accessiblecaret.extend_selection_for_phone_number");
|
2016-10-03 15:57:44 +03:00
|
|
|
Preferences::AddBoolVarCache(&sHideCaretsForMouseInput,
|
|
|
|
"layout.accessiblecaret.hide_carets_for_mouse_input");
|
2015-12-01 23:25:06 +03:00
|
|
|
addedPrefs = true;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessibleCaretManager::~AccessibleCaretManager()
|
2016-02-15 10:12:35 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AccessibleCaretManager::Terminate()
|
2015-04-30 01:59:00 +03:00
|
|
|
{
|
2016-02-15 10:12:35 +03:00
|
|
|
mFirstCaret = nullptr;
|
|
|
|
mSecondCaret = nullptr;
|
|
|
|
mActiveCaret = nullptr;
|
|
|
|
mPresShell = nullptr;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AccessibleCaretManager::OnSelectionChanged(nsIDOMDocument* aDoc,
|
|
|
|
nsISelection* 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__,
|
2015-12-01 23:25:06 +03:00
|
|
|
aSel, 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;
|
|
|
|
}
|
|
|
|
|
2015-12-01 23:25:06 +03:00
|
|
|
// Move the cursor by Javascript / or unknown internal.
|
2015-04-30 01:59:00 +03:00
|
|
|
if (aReason == nsISelectionListener::NO_REASON) {
|
2016-01-26 17:27:36 +03:00
|
|
|
// Update visible carets, if javascript changes are allowed.
|
|
|
|
if (sCaretsScriptUpdates &&
|
2015-12-01 23:25:06 +03:00
|
|
|
(mFirstCaret->IsLogicallyVisible() || mSecondCaret->IsLogicallyVisible())) {
|
|
|
|
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.
|
|
|
|
if (sHideCaretsForMouseInput &&
|
|
|
|
mLastInputSource == nsIDOMMouseEvent::MOZ_SOURCE_MOUSE) {
|
|
|
|
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.
|
2016-10-05 10:46:36 +03:00
|
|
|
if (sHideCaretsForMouseInput &&
|
2017-02-17 09:08:48 +03:00
|
|
|
mLastInputSource == nsIDOMMouseEvent::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);
|
2015-05-14 03:10:00 +03:00
|
|
|
DispatchCaretStateChangedEvent(CaretChangedReason::Visibilitychange);
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-14 10:25:16 +03:00
|
|
|
AccessibleCaretManager::UpdateCarets(UpdateCaretsHintSet aHint)
|
2015-04-30 01:59:00 +03:00
|
|
|
{
|
2016-02-15 10:12:35 +03:00
|
|
|
FlushLayout();
|
|
|
|
if (IsTerminated()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
bool
|
|
|
|
AccessibleCaretManager::IsCaretDisplayableInCursorMode(nsIFrame** aOutFrame,
|
|
|
|
int32_t* aOutOffset) const
|
2015-04-30 01:59:00 +03:00
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-14 10:25:16 +03:00
|
|
|
AccessibleCaretManager::UpdateCaretsForCursorMode(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:
|
|
|
|
case PositionChangedResult::Changed:
|
2017-03-14 10:25:16 +03:00
|
|
|
if (aHints == UpdateCaretsHint::Default) {
|
|
|
|
if (HasNonEmptyTextContent(GetEditingHostForFrame(frame))) {
|
|
|
|
mFirstCaret->SetAppearance(Appearance::Normal);
|
|
|
|
} else if (sCaretShownWhenLongTappingOnEmptyContent) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
} else if (aHints.contains(UpdateCaretsHint::RespectOldAppearance)) {
|
|
|
|
// Do nothing to preserve the appearance of the caret set by the
|
|
|
|
// caller.
|
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
|
|
|
|
|
|
|
mFirstCaret->SetSelectionBarEnabled(false);
|
2015-04-30 01:59:00 +03:00
|
|
|
mSecondCaret->SetAppearance(Appearance::None);
|
2015-05-14 03:10:00 +03:00
|
|
|
|
2017-03-14 12:50:03 +03:00
|
|
|
if (!aHints.contains(UpdateCaretsHint::DispatchNoEvent) &&
|
2015-09-09 11:47:20 +03:00
|
|
|
!mActiveCaret) {
|
2015-05-14 03:10:00 +03:00
|
|
|
DispatchCaretStateChangedEvent(CaretChangedReason::Updateposition);
|
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-14 10:25:16 +03:00
|
|
|
AccessibleCaretManager::UpdateCaretsForSelectionMode(UpdateCaretsHintSet aHints)
|
2015-04-30 01:59:00 +03:00
|
|
|
{
|
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);
|
2016-01-21 06:11:38 +03:00
|
|
|
aCaret->SetSelectionBarEnabled(sSelectionBarEnabled);
|
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:
|
|
|
|
case PositionChangedResult::Changed:
|
2017-03-14 10:25:16 +03:00
|
|
|
if (aHints == UpdateCaretsHint::Default) {
|
|
|
|
aCaret->SetAppearance(Appearance::Normal);
|
|
|
|
} else if (aHints.contains(UpdateCaretsHint::RespectOldAppearance)) {
|
|
|
|
// Do nothing to preserve the appearance of the caret set by the
|
|
|
|
// caller.
|
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);
|
|
|
|
|
|
|
|
if (firstCaretResult == PositionChangedResult::Changed ||
|
|
|
|
secondCaretResult == PositionChangedResult::Changed) {
|
|
|
|
// Flush layout to make the carets intersection correct.
|
2015-09-18 20:05:19 +03:00
|
|
|
FlushLayout();
|
2016-02-15 10:12:35 +03:00
|
|
|
if (IsTerminated()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2017-03-14 10:25:16 +03:00
|
|
|
if (aHints == UpdateCaretsHint::Default) {
|
2016-02-11 11:22:57 +03:00
|
|
|
// Only check for tilt carets with default update hint. Otherwise we might
|
|
|
|
// override the appearance set by the caller.
|
2016-02-16 11:55:28 +03:00
|
|
|
if (sCaretsAlwaysTilt) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-08 10:51:28 +03:00
|
|
|
bool
|
2016-02-16 11:55:28 +03:00
|
|
|
AccessibleCaretManager::UpdateCaretsForOverlappingTilt()
|
2015-04-30 01:59:00 +03:00
|
|
|
{
|
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()
|
|
|
|
{
|
|
|
|
if (sHapticFeedback) {
|
|
|
|
nsCOMPtr<nsIHapticFeedback> haptic =
|
|
|
|
do_GetService("@mozilla.org/widget/hapticfeedback;1");
|
|
|
|
haptic->PerformSimpleAction(haptic->LongPress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
nsresult
|
2016-08-14 16:39:30 +03:00
|
|
|
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);
|
|
|
|
|
|
|
|
nsPoint point(aPoint.x, aPoint.y + mOffsetYToCaretLogicalPosition);
|
|
|
|
DragCaretInternal(point);
|
|
|
|
UpdateCarets();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AccessibleCaretManager::ReleaseCaret()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mActiveCaret);
|
|
|
|
|
|
|
|
mActiveCaret = nullptr;
|
|
|
|
SetSelectionDragState(false);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AccessibleCaretManager::SelectWordOrShortcut(const nsPoint& aPoint)
|
|
|
|
{
|
2016-01-25 10:50:28 +03:00
|
|
|
auto UpdateCaretsWithHapticFeedback = [this] {
|
|
|
|
UpdateCarets();
|
|
|
|
ProvideHapticFeedback();
|
|
|
|
};
|
|
|
|
|
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__);
|
|
|
|
UpdateCaretsWithHapticFeedback();
|
|
|
|
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.
|
2017-03-01 20:03:14 +03:00
|
|
|
AutoWeakFrame ptFrame = nsLayoutUtils::GetFrameForPoint(rootFrame, aPoint,
|
2015-04-30 01:59:00 +03:00
|
|
|
nsLayoutUtils::IGNORE_PAINT_SUPPRESSION | nsLayoutUtils::IGNORE_CROSS_DOC);
|
2016-07-10 09:36:02 +03:00
|
|
|
if (!ptFrame.IsAlive()) {
|
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;
|
|
|
|
nsLayoutUtils::TransformPoint(rootFrame, ptFrame, ptInFrame);
|
|
|
|
|
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
|
|
|
|
|
|
|
if (sCaretShownWhenLongTappingOnEmptyContent) {
|
|
|
|
mFirstCaret->SetAppearance(Appearance::Normal);
|
|
|
|
}
|
2015-08-19 10:54:10 +03:00
|
|
|
// We need to update carets to get correct information before dispatching
|
|
|
|
// CaretStateChangedEvent.
|
2016-01-25 10:50:28 +03:00
|
|
|
UpdateCaretsWithHapticFeedback();
|
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
|
|
|
|
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);
|
2016-01-25 10:50:28 +03:00
|
|
|
UpdateCaretsWithHapticFeedback();
|
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__);
|
|
|
|
|
2017-03-14 12:50:03 +03:00
|
|
|
mIsScrollStarted = true;
|
|
|
|
|
2016-04-21 11:53:40 +03:00
|
|
|
if (!sCaretsAlwaysShowWhenScrolling) {
|
|
|
|
// Backup the appearance so that we can restore them after the scrolling
|
|
|
|
// ends.
|
|
|
|
mFirstCaretAppearanceOnScrollStart = mFirstCaret->GetAppearance();
|
|
|
|
mSecondCaretAppearanceOnScrollStart = mSecondCaret->GetAppearance();
|
2015-12-01 23:25:06 +03:00
|
|
|
HideCarets();
|
2016-04-21 11:53:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:50:03 +03:00
|
|
|
mIsScrollStarted = false;
|
|
|
|
|
2016-04-21 11:53:40 +03:00
|
|
|
if (!sCaretsAlwaysShowWhenScrolling) {
|
|
|
|
// Restore the appearance which is saved before the scrolling is started.
|
|
|
|
mFirstCaret->SetAppearance(mFirstCaretAppearanceOnScrollStart);
|
|
|
|
mSecondCaret->SetAppearance(mSecondCaretAppearanceOnScrollStart);
|
|
|
|
}
|
2016-02-11 11:22:57 +03:00
|
|
|
|
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.
|
|
|
|
if (sHideCaretsForMouseInput &&
|
|
|
|
mLastInputSource == nsIDOMMouseEvent::MOZ_SOURCE_MOUSE) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
nsIContent* focusedContent = fm->GetFocusedContent();
|
2015-04-30 01:59:00 +03:00
|
|
|
if (focusedContent) {
|
|
|
|
nsIFrame* focusFrame = focusedContent->GetPrimaryFrame();
|
|
|
|
if (!focusFrame) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent us from touching the nsFrameSelection associated with other
|
|
|
|
// PresShell.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFrameSelection> fs = focusFrame->GetFrameSelection();
|
2015-04-30 01:59:00 +03:00
|
|
|
if (!fs || fs->GetShell() != mPresShell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.forget();
|
|
|
|
} else {
|
|
|
|
// For non-editable content
|
|
|
|
return mPresShell->FrameSelection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:09:39 +03:00
|
|
|
nsAutoString
|
|
|
|
AccessibleCaretManager::StringifiedSelection() const
|
|
|
|
{
|
|
|
|
nsAutoString str;
|
|
|
|
GetSelection()->Stringify(str);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selection->IsCollapsed()) {
|
|
|
|
return CaretMode::Cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CaretMode::Selection;
|
|
|
|
}
|
|
|
|
|
2015-08-19 10:54:10 +03:00
|
|
|
nsIFrame*
|
2015-08-27 23:55:33 +03:00
|
|
|
AccessibleCaretManager::GetFocusableFrame(nsIFrame* aFrame) const
|
2015-04-30 01:59:00 +03:00
|
|
|
{
|
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!");
|
|
|
|
nsCOMPtr<nsIDOMElement> focusableElement = do_QueryInterface(focusableContent);
|
|
|
|
fm->SetFocus(focusableElement, nsIFocusManager::FLAG_BYMOUSE);
|
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);
|
|
|
|
nsFrame* frame = static_cast<nsFrame*>(aFrame);
|
|
|
|
nsresult rs = frame->SelectByTypeAtPoint(mPresShell->GetPresContext(), aPoint,
|
|
|
|
eSelectWord, eSelectWord, 0);
|
|
|
|
|
|
|
|
SetSelectionDragState(false);
|
|
|
|
ClearMaintainedSelection();
|
|
|
|
|
2016-04-14 09:14:16 +03:00
|
|
|
// Smart-select phone numbers if possible.
|
|
|
|
if (sExtendSelectionForPhoneNumber) {
|
|
|
|
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);
|
|
|
|
}
|
2016-03-11 02:06:13 +03:00
|
|
|
|
|
|
|
// Pin Fennecs DynamicToolbarAnimator in place before/after dragging,
|
|
|
|
// to avoid co-incident screen scrolling.
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
nsIDocument* doc = mPresShell->GetDocument();
|
|
|
|
MOZ_ASSERT(doc);
|
|
|
|
nsIWidget* widget = nsContentUtils::WidgetForDocument(doc);
|
|
|
|
static_cast<nsWindow*>(widget)->SetSelectionDragState(aState);
|
|
|
|
#endif
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
2017-03-07 12:21:26 +03:00
|
|
|
bool
|
|
|
|
AccessibleCaretManager::IsPhoneNumber(nsAString& aCandidate) const
|
|
|
|
{
|
|
|
|
RefPtr<nsIDocument> doc = mPresShell->GetDocument();
|
|
|
|
nsAutoString phoneNumberRegex(
|
|
|
|
NS_LITERAL_STRING("(^\\+)?[0-9 ,\\-.()*#pw]{1,30}$"));
|
|
|
|
return nsContentUtils::IsPatternMatching(aCandidate, phoneNumberRegex, doc);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
ExtendPhoneNumberSelection(NS_LITERAL_STRING("forward"));
|
2016-04-14 09:14:16 +03:00
|
|
|
|
2017-03-07 12:21:26 +03:00
|
|
|
SetSelectionDirection(eDirPrevious);
|
|
|
|
ExtendPhoneNumberSelection(NS_LITERAL_STRING("backward"));
|
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.
|
2016-04-14 09:14:16 +03:00
|
|
|
selection->Modify(NS_LITERAL_STRING("extend"),
|
|
|
|
aDirection,
|
|
|
|
NS_LITERAL_STRING("character"));
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
void
|
|
|
|
AccessibleCaretManager::FlushLayout() const
|
|
|
|
{
|
|
|
|
if (mPresShell) {
|
2017-01-05 10:31:56 +03:00
|
|
|
mPresShell->FlushPendingNotifications(FlushType::Layout);
|
2015-09-18 20:05:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
nsIFrame*
|
2016-04-11 12:57:29 +03:00
|
|
|
AccessibleCaretManager::GetFrameForFirstRangeStartOrLastRangeEnd(
|
|
|
|
nsDirection aDirection, int32_t* aOutOffset, nsINode** aOutNode,
|
|
|
|
int32_t* aOutNodeOffset) 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
|
|
|
|
2016-04-11 12:57:29 +03:00
|
|
|
nsRange* range = nullptr;
|
|
|
|
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);
|
|
|
|
startNode = range->GetStartParent();
|
|
|
|
endNode = range->GetEndParent();
|
|
|
|
nodeOffset = range->StartOffset();
|
|
|
|
hint = CARET_ASSOCIATE_AFTER;
|
|
|
|
} else {
|
|
|
|
range = selection->GetRangeAt(selection->RangeCount() - 1);
|
|
|
|
startNode = range->GetEndParent();
|
|
|
|
endNode = range->GetStartParent();
|
|
|
|
nodeOffset = range->EndOffset();
|
|
|
|
hint = CARET_ASSOCIATE_BEFORE;
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> startContent = do_QueryInterface(startNode);
|
2016-04-11 12:57:29 +03:00
|
|
|
RefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2015-04-30 01:59:00 +03:00
|
|
|
nsIFrame* startFrame =
|
2016-04-11 12:57:29 +03:00
|
|
|
fs->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(
|
|
|
|
*startNode, nsIDOMNodeFilter::SHOW_ALL, nullptr, err);
|
|
|
|
|
|
|
|
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) {
|
2016-04-11 12:57:29 +03:00
|
|
|
if (aOutNode) {
|
|
|
|
*aOutNode = startNode.get();
|
|
|
|
}
|
|
|
|
if (aOutNodeOffset) {
|
|
|
|
*aOutNodeOffset = nodeOffset;
|
|
|
|
}
|
2015-04-30 01:59:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return startFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-04-11 12:57:29 +03:00
|
|
|
AccessibleCaretManager::RestrictCaretDraggingOffsets(
|
|
|
|
nsIFrame::ContentOffsets& aOffsets)
|
2015-04-30 01:59:00 +03:00
|
|
|
{
|
2016-04-11 12:57:29 +03:00
|
|
|
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;
|
2015-04-30 01:59:00 +03:00
|
|
|
nsINode* node = nullptr;
|
2016-04-11 12:57:29 +03:00
|
|
|
int32_t contentOffset = 0;
|
|
|
|
nsIFrame* frame =
|
|
|
|
GetFrameForFirstRangeStartOrLastRangeEnd(dir, &offset, &node, &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
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
|
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 inactive caret's
|
|
|
|
// position.
|
|
|
|
int32_t cmpToInactiveCaretPos =
|
|
|
|
nsContentUtils::ComparePoints(aOffsets.content, aOffsets.StartOffset(),
|
|
|
|
content, contentOffset);
|
|
|
|
|
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.
|
|
|
|
int32_t cmpToLimit =
|
|
|
|
nsContentUtils::ComparePoints(aOffsets.content, aOffsets.StartOffset(),
|
|
|
|
limit.mResultContent, limit.mContentOffset);
|
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
|
|
|
};
|
|
|
|
|
|
|
|
if (!sCaretsAllowDraggingAcrossOtherCaret) {
|
|
|
|
if ((mActiveCaret == mFirstCaret.get() && cmpToLimit == 1) ||
|
|
|
|
(mActiveCaret == mSecondCaret.get() && cmpToLimit == -1)) {
|
|
|
|
// 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 {
|
|
|
|
switch (cmpToInactiveCaretPos) {
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!mPresShell) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
if (!rootFrame) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPoint point = AdjustDragBoundary(aPoint);
|
|
|
|
|
|
|
|
// Find out which content we point to
|
|
|
|
nsIFrame* ptFrame = nsLayoutUtils::GetFrameForPoint(
|
|
|
|
rootFrame, point,
|
|
|
|
nsLayoutUtils::IGNORE_PAINT_SUPPRESSION | nsLayoutUtils::IGNORE_CROSS_DOC);
|
|
|
|
if (!ptFrame) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2015-04-30 01:59:00 +03:00
|
|
|
if (!fs) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult result;
|
|
|
|
nsIFrame* newFrame = nullptr;
|
|
|
|
nsPoint newPoint;
|
|
|
|
nsPoint ptInFrame = point;
|
|
|
|
nsLayoutUtils::TransformPoint(rootFrame, ptFrame, ptInFrame);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Selection* selection = GetSelection();
|
|
|
|
if (!selection) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
nsIFrame* anchorFrame = nullptr;
|
|
|
|
selection->GetPrimaryFrameForAnchorNode(&anchorFrame);
|
|
|
|
|
|
|
|
nsIFrame* scrollable =
|
|
|
|
nsLayoutUtils::GetClosestFrameOfType(anchorFrame, nsGkAtoms::scrollFrame);
|
2017-03-01 20:03:14 +03:00
|
|
|
AutoWeakFrame weakScrollable = scrollable;
|
2015-04-30 01:59:00 +03:00
|
|
|
fs->HandleClick(offsets.content, offsets.StartOffset(), offsets.EndOffset(),
|
|
|
|
GetCaretMode() == CaretMode::Selection, false,
|
|
|
|
offsets.associate);
|
|
|
|
if (!weakScrollable.IsAlive()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scroll scrolled frame.
|
|
|
|
nsIScrollableFrame* saf = do_QueryFrame(scrollable);
|
|
|
|
nsIFrame* capturingFrame = saf->GetScrolledFrame();
|
|
|
|
nsPoint ptInScrolled = point;
|
|
|
|
nsLayoutUtils::TransformPoint(rootFrame, capturingFrame, ptInScrolled);
|
|
|
|
fs->StartAutoScrollTimer(capturingFrame, ptInScrolled, kAutoScrollTimerDelay);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-07 13:09:04 +03:00
|
|
|
nsRect
|
2016-03-10 12:38:33 +03:00
|
|
|
AccessibleCaretManager::GetAllChildFrameRectsUnion(nsIFrame* aFrame) const
|
2015-10-07 13:09:04 +03:00
|
|
|
{
|
2016-03-10 12:38:33 +03:00
|
|
|
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
|
|
|
|
2016-03-10 12:38:33 +03:00
|
|
|
for (nsIFrame::ChildListIterator lists(frame); !lists.IsDone(); lists.Next()) {
|
|
|
|
// Loop all children to union their scrollable overflow rect.
|
2015-10-07 13:09:04 +03:00
|
|
|
for (nsIFrame* child : lists.CurrentList()) {
|
2016-03-10 12:38:33 +03:00
|
|
|
nsRect childRect = child->GetScrollableOverflowRectRelativeToSelf();
|
|
|
|
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 &&
|
|
|
|
!sCaretsAllowDraggingAcrossOtherCaret) {
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-20 03:59:00 +03:00
|
|
|
void
|
|
|
|
AccessibleCaretManager::DispatchCaretStateChangedEvent(CaretChangedReason aReason) const
|
|
|
|
{
|
2016-02-15 10:12:35 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-18 20:05:19 +03:00
|
|
|
|
|
|
|
FlushLayout();
|
2016-02-15 10:12:35 +03:00
|
|
|
if (IsTerminated()) {
|
2015-05-20 03:59:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Selection* sel = GetSelection();
|
|
|
|
if (!sel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* doc = mPresShell->GetDocument();
|
|
|
|
MOZ_ASSERT(doc);
|
|
|
|
|
|
|
|
CaretStateChangedEventInit init;
|
|
|
|
init.mBubbles = true;
|
|
|
|
|
|
|
|
const nsRange* range = sel->GetAnchorFocusRange();
|
|
|
|
nsINode* commonAncestorNode = nullptr;
|
|
|
|
if (range) {
|
|
|
|
commonAncestorNode = range->GetCommonAncestor();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
domRect->SetLayoutRect(clampedRect);
|
|
|
|
init.mSelectionVisible = !clampedRect.IsEmpty();
|
|
|
|
} else {
|
|
|
|
domRect->SetLayoutRect(rect);
|
|
|
|
init.mSelectionVisible = true;
|
|
|
|
}
|
|
|
|
|
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 = commonAncestorFrame &&
|
2015-09-18 20:05:19 +03:00
|
|
|
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();
|
2015-12-01 23:25:06 +03:00
|
|
|
init.mCaretVisuallyVisible = mFirstCaret->IsVisuallyVisible() ||
|
|
|
|
mSecondCaret->IsVisuallyVisible();
|
2015-09-16 12:58:14 +03:00
|
|
|
sel->Stringify(init.mSelectedTextContent);
|
2015-05-20 03:59:00 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CaretStateChangedEvent> event =
|
2015-05-20 03:59:00 +03:00
|
|
|
CaretStateChangedEvent::Constructor(doc, NS_LITERAL_STRING("mozcaretstatechanged"), init);
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-06-05 10:03:47 +03:00
|
|
|
(new AsyncEventDispatcher(doc, event))->RunDOMEventWhenSafe();
|
2015-05-20 03:59:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
} // namespace mozilla
|