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/. */
|
|
|
|
|
|
|
|
#ifndef AccessibleCaretManager_h
|
|
|
|
#define AccessibleCaretManager_h
|
|
|
|
|
2015-10-08 10:18:03 +03:00
|
|
|
#include "AccessibleCaret.h"
|
2015-04-30 01:59:00 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCoord.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsISelectionListener.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-04-30 01:59:00 +03:00
|
|
|
#include "nsWeakReference.h"
|
2015-05-20 03:59:00 +03:00
|
|
|
#include "mozilla/dom/CaretStateChangedEvent.h"
|
2015-04-30 01:59:00 +03:00
|
|
|
#include "mozilla/EventForwards.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "mozilla/WeakPtr.h"
|
|
|
|
|
|
|
|
class nsFrameSelection;
|
|
|
|
class nsIContent;
|
|
|
|
class nsIPresShell;
|
|
|
|
struct nsPoint;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace dom {
|
2015-09-18 20:05:19 +03:00
|
|
|
class Element;
|
2015-04-30 01:59:00 +03:00
|
|
|
class Selection;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
2015-04-30 01:59:00 +03:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// AccessibleCaretManager does not deal with events or callbacks directly. It
|
|
|
|
// relies on AccessibleCaretEventHub to call its public methods to do the work.
|
|
|
|
// All codes needed to interact with PresShell, Selection, and AccessibleCaret
|
|
|
|
// should be written in AccessibleCaretManager.
|
|
|
|
//
|
|
|
|
// None the public methods in AccessibleCaretManager will flush layout or style
|
|
|
|
// prior to performing its task. The caller must ensure the layout is up to
|
|
|
|
// date.
|
|
|
|
//
|
2015-11-18 06:44:00 +03:00
|
|
|
// Please see the wiki page for more information.
|
|
|
|
// https://wiki.mozilla.org/Copy_n_Paste
|
|
|
|
//
|
2015-04-30 01:59:00 +03:00
|
|
|
class AccessibleCaretManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AccessibleCaretManager(nsIPresShell* aPresShell);
|
|
|
|
virtual ~AccessibleCaretManager();
|
|
|
|
|
|
|
|
// The aPoint in the following public methods should be relative to root
|
|
|
|
// frame.
|
|
|
|
|
|
|
|
// Press caret on the given point. Return NS_OK if the point is actually on
|
|
|
|
// one of the carets.
|
|
|
|
virtual nsresult PressCaret(const nsPoint& aPoint);
|
|
|
|
|
|
|
|
// Drag caret to the given point. It's required to call PressCaret()
|
|
|
|
// beforehand.
|
|
|
|
virtual nsresult DragCaret(const nsPoint& aPoint);
|
|
|
|
|
|
|
|
// Release caret from he previous press action. It's required to call
|
|
|
|
// PressCaret() beforehand.
|
|
|
|
virtual nsresult ReleaseCaret();
|
|
|
|
|
|
|
|
// A quick single tap on caret on given point without dragging.
|
|
|
|
virtual nsresult TapCaret(const nsPoint& aPoint);
|
|
|
|
|
|
|
|
// Select a word or bring up paste shortcut (if Gaia is listening) under the
|
|
|
|
// given point.
|
|
|
|
virtual nsresult SelectWordOrShortcut(const nsPoint& aPoint);
|
|
|
|
|
|
|
|
// Handle scroll-start event.
|
|
|
|
virtual void OnScrollStart();
|
|
|
|
|
|
|
|
// Handle scroll-end event.
|
|
|
|
virtual void OnScrollEnd();
|
|
|
|
|
2015-09-09 11:47:20 +03:00
|
|
|
// Handle ScrollPositionChanged from nsIScrollObserver. This might be called
|
|
|
|
// at anytime, not necessary between OnScrollStart and OnScrollEnd.
|
2015-04-30 01:59:00 +03:00
|
|
|
virtual void OnScrollPositionChanged();
|
|
|
|
|
|
|
|
// Handle reflow event from nsIReflowObserver.
|
|
|
|
virtual void OnReflow();
|
|
|
|
|
|
|
|
// Handle blur event from nsFocusManager.
|
|
|
|
virtual void OnBlur();
|
|
|
|
|
|
|
|
// Handle NotifySelectionChanged event from nsISelectionListener.
|
|
|
|
virtual nsresult OnSelectionChanged(nsIDOMDocument* aDoc,
|
|
|
|
nsISelection* aSel,
|
|
|
|
int16_t aReason);
|
|
|
|
// Handle key event.
|
|
|
|
virtual void OnKeyboardEvent();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// This enum representing the number of AccessibleCarets on the screen.
|
|
|
|
enum class CaretMode : uint8_t {
|
|
|
|
// No caret on the screen.
|
|
|
|
None,
|
|
|
|
|
|
|
|
// One caret, i.e. the selection is collapsed.
|
|
|
|
Cursor,
|
|
|
|
|
|
|
|
// Two carets, i.e. the selection is not collapsed.
|
|
|
|
Selection
|
|
|
|
};
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
friend std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const CaretMode& aCaretMode);
|
|
|
|
|
2015-09-09 11:47:20 +03:00
|
|
|
enum class UpdateCaretsHint : uint8_t {
|
|
|
|
// Update everything including appearance and position.
|
|
|
|
Default,
|
|
|
|
|
|
|
|
// Update everything while respecting the old appearance. For example, if
|
|
|
|
// the caret in cursor mode is hidden due to timeout, do not change its
|
|
|
|
// appearance to Normal.
|
|
|
|
RespectOldAppearance
|
|
|
|
};
|
2015-09-18 20:05:19 +03:00
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const UpdateCaretsHint& aResult);
|
|
|
|
|
2015-09-18 20:05:18 +03:00
|
|
|
// Update carets based on current selection status.
|
2015-09-09 11:47:20 +03:00
|
|
|
void UpdateCarets(UpdateCaretsHint aHint = UpdateCaretsHint::Default);
|
2015-09-18 20:05:18 +03:00
|
|
|
|
|
|
|
// Force hiding all carets regardless of the current selection status.
|
2015-04-30 01:59:00 +03:00
|
|
|
void HideCarets();
|
|
|
|
|
2015-12-01 23:25:06 +03:00
|
|
|
// Force carets to be "present" logically, but not visible. Allows ActionBar
|
|
|
|
// to stay open when carets visibility is supressed during scroll.
|
|
|
|
void DoNotShowCarets();
|
|
|
|
|
2015-09-09 11:47:20 +03:00
|
|
|
void UpdateCaretsForCursorMode(UpdateCaretsHint aHint);
|
2015-12-16 08:24:06 +03:00
|
|
|
void UpdateCaretsForSelectionMode();
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2015-12-11 02:57:02 +03:00
|
|
|
// Provide haptic / touch feedback, primarily for select on longpress.
|
|
|
|
void ProvideHapticFeedback();
|
|
|
|
|
2015-08-27 23:55:33 +03:00
|
|
|
// Get the nearest enclosing focusable frame of aFrame.
|
2015-08-19 10:54:10 +03:00
|
|
|
// @return focusable frame if there is any; nullptr otherwise.
|
2015-08-27 23:55:33 +03:00
|
|
|
nsIFrame* GetFocusableFrame(nsIFrame* aFrame) const;
|
|
|
|
|
|
|
|
// Change focus to aFrame if it isn't nullptr. Otherwise, clear the old focus
|
|
|
|
// then re-focus the window.
|
|
|
|
void ChangeFocusToOrClearOldFocus(nsIFrame* aFrame) const;
|
2015-08-19 10:54:10 +03:00
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
nsresult SelectWord(nsIFrame* aFrame, const nsPoint& aPoint) const;
|
|
|
|
void SetSelectionDragState(bool aState) const;
|
|
|
|
void SetSelectionDirection(nsDirection aDir) const;
|
|
|
|
|
|
|
|
// If aBackward is false, find the first node from the first range in current
|
|
|
|
// selection, and return the frame and the offset into that frame. If aBackward
|
|
|
|
// is true, find the last node from the last range instead.
|
|
|
|
nsIFrame* FindFirstNodeWithFrame(bool aBackward, int32_t* aOutOffset) const;
|
|
|
|
|
|
|
|
nsresult DragCaretInternal(const nsPoint& aPoint);
|
|
|
|
nsPoint AdjustDragBoundary(const nsPoint& aPoint) const;
|
|
|
|
void ClearMaintainedSelection() const;
|
2015-09-18 20:05:19 +03:00
|
|
|
void FlushLayout() const;
|
|
|
|
dom::Element* GetEditingHostForFrame(nsIFrame* aFrame) const;
|
2015-04-30 01:59:00 +03:00
|
|
|
dom::Selection* GetSelection() const;
|
|
|
|
already_AddRefed<nsFrameSelection> GetFrameSelection() const;
|
2015-05-20 03:59:00 +03:00
|
|
|
|
2015-10-07 13:09:04 +03:00
|
|
|
// Get the bounding rectangle for aFrame where the caret under cursor mode can
|
|
|
|
// be positioned. The rectangle is relative to the root frame.
|
|
|
|
nsRect GetContentBoundaryForFrame(nsIFrame* aFrame) const;
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
// If we're dragging the first caret, we do not want to drag it over the
|
|
|
|
// previous character of the second caret. Same as the second caret. So we
|
|
|
|
// check if content offset exceeds the previous/next character of second/first
|
|
|
|
// caret base the active caret.
|
|
|
|
bool CompareRangeWithContentOffset(nsIFrame::ContentOffsets& aOffsets);
|
|
|
|
|
|
|
|
// Timeout in milliseconds to hide the AccessibleCaret under cursor mode while
|
|
|
|
// no one touches it.
|
|
|
|
uint32_t CaretTimeoutMs() const;
|
|
|
|
void LaunchCaretTimeoutTimer();
|
|
|
|
void CancelCaretTimeoutTimer();
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// The following functions are made virtual for stubbing or mocking in gtest.
|
|
|
|
//
|
|
|
|
// Get caret mode based on current selection.
|
|
|
|
virtual CaretMode GetCaretMode() const;
|
|
|
|
|
|
|
|
// @return true if aStartFrame comes before aEndFrame.
|
|
|
|
virtual bool CompareTreePosition(nsIFrame* aStartFrame,
|
|
|
|
nsIFrame* aEndFrame) const;
|
|
|
|
|
|
|
|
// Check if the two carets is overlapping to become tilt.
|
|
|
|
virtual void UpdateCaretsForTilt();
|
|
|
|
|
|
|
|
// Check whether AccessibleCaret is displayable in cursor mode or not.
|
|
|
|
// @param aOutFrame returns frame of the cursor if it's displayable.
|
|
|
|
// @param aOutOffset returns frame offset as well.
|
|
|
|
virtual bool IsCaretDisplayableInCursorMode(nsIFrame** aOutFrame = nullptr,
|
|
|
|
int32_t* aOutOffset = nullptr) const;
|
|
|
|
|
|
|
|
virtual bool HasNonEmptyTextContent(nsINode* aNode) const;
|
|
|
|
|
|
|
|
// This function will call FlushPendingNotifications. So caller must ensure
|
|
|
|
// everything exists after calling this method.
|
|
|
|
virtual void DispatchCaretStateChangedEvent(dom::CaretChangedReason aReason) const;
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2015-04-30 01:59:00 +03:00
|
|
|
// Member variables
|
2015-09-18 20:05:19 +03:00
|
|
|
//
|
2015-04-30 01:59:00 +03:00
|
|
|
nscoord mOffsetYToCaretLogicalPosition = NS_UNCONSTRAINEDSIZE;
|
|
|
|
|
|
|
|
// AccessibleCaretEventHub owns us. When it's Terminate() called by
|
|
|
|
// PresShell::Destroy(), we will be destroyed. No need to worry we outlive
|
|
|
|
// mPresShell.
|
|
|
|
nsIPresShell* MOZ_NON_OWNING_REF const mPresShell = nullptr;
|
|
|
|
|
|
|
|
// First caret is attached to nsCaret in cursor mode, and is attached to
|
|
|
|
// selection highlight as the left caret in selection mode.
|
|
|
|
UniquePtr<AccessibleCaret> mFirstCaret;
|
|
|
|
|
|
|
|
// Second caret is used solely in selection mode, and is attached to selection
|
|
|
|
// highlight as the right caret.
|
|
|
|
UniquePtr<AccessibleCaret> mSecondCaret;
|
|
|
|
|
|
|
|
// The caret being pressed or dragged.
|
|
|
|
AccessibleCaret* mActiveCaret = nullptr;
|
|
|
|
|
2015-09-18 20:05:18 +03:00
|
|
|
// The timer for hiding the caret in cursor mode after timeout behind the
|
|
|
|
// preference "layout.accessiblecaret.timeout_ms".
|
2015-04-30 01:59:00 +03:00
|
|
|
nsCOMPtr<nsITimer> mCaretTimeoutTimer;
|
2015-09-18 20:05:18 +03:00
|
|
|
|
|
|
|
// The caret mode since last update carets.
|
|
|
|
CaretMode mLastUpdateCaretMode = CaretMode::None;
|
2015-04-30 01:59:00 +03:00
|
|
|
|
2015-10-08 10:18:03 +03:00
|
|
|
// Store the appearance of the first caret when calling OnScrollStart so that
|
|
|
|
// it can be restored in OnScrollEnd.
|
|
|
|
AccessibleCaret::Appearance mFirstCaretAppearanceOnScrollStart =
|
|
|
|
AccessibleCaret::Appearance::None;
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
static const int32_t kAutoScrollTimerDelay = 30;
|
2015-10-07 13:09:04 +03:00
|
|
|
|
|
|
|
// Clicking on the boundary of input or textarea will move the caret to the
|
|
|
|
// front or end of the content. To avoid this, we need to deflate the content
|
|
|
|
// boundary by 61 app units, which is 1 pixel + 1 app unit as defined in
|
|
|
|
// AppUnit.h.
|
|
|
|
static const int32_t kBoundaryAppUnits = 61;
|
2015-12-01 23:25:06 +03:00
|
|
|
|
2016-01-21 06:11:38 +03:00
|
|
|
// Preference to show selection bars at the two ends in selection mode. The
|
|
|
|
// selection bar is always disabled in cursor mode.
|
|
|
|
static bool sSelectionBarEnabled;
|
|
|
|
|
2015-12-01 23:25:06 +03:00
|
|
|
// AccessibleCaret visibility preference. Used to avoid hiding caret during
|
|
|
|
// (NO_REASON) selection change notifications generated by keyboard IME, and to
|
2015-12-20 20:01:00 +03:00
|
|
|
// maintain a visible ActionBar while carets NotShown during scroll and while
|
|
|
|
// cursor is on an empty input.
|
2015-12-01 23:25:06 +03:00
|
|
|
static bool sCaretsExtendedVisibility;
|
2015-12-11 02:57:02 +03:00
|
|
|
|
|
|
|
// AccessibleCaret pref for haptic feedback behaviour on longPress.
|
|
|
|
static bool sHapticFeedback;
|
2015-04-30 01:59:00 +03:00
|
|
|
};
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const AccessibleCaretManager::CaretMode& aCaretMode);
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const AccessibleCaretManager::UpdateCaretsHint& aResult);
|
|
|
|
|
2015-04-30 01:59:00 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // AccessibleCaretManager_h
|