2014-06-03 07:08:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
|
|
|
/* 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 mozilla_TouchCaret_h__
|
|
|
|
#define mozilla_TouchCaret_h__
|
|
|
|
|
|
|
|
#include "nsISelectionListener.h"
|
|
|
|
#include "nsIScrollObserver.h"
|
|
|
|
#include "nsIWeakReferenceUtils.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "mozilla/EventForwards.h"
|
|
|
|
#include "mozilla/TouchEvents.h"
|
|
|
|
#include "Units.h"
|
|
|
|
|
2014-10-14 01:07:00 +00:00
|
|
|
class nsCanvasFrame;
|
2014-08-06 05:19:25 +00:00
|
|
|
class nsIFrame;
|
|
|
|
class nsIPresShell;
|
|
|
|
|
2014-06-03 07:08:45 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
2014-07-26 22:13:00 +00:00
|
|
|
* The TouchCaret places a touch caret according to caret position when the
|
2014-06-03 07:08:45 +00:00
|
|
|
* caret is shown.
|
|
|
|
* TouchCaret is also responsible for touch caret visibility. Touch caret
|
|
|
|
* won't be shown when timer expires or while key event causes selection change.
|
|
|
|
*/
|
2015-05-15 02:06:13 +00:00
|
|
|
class TouchCaret final : public nsISelectionListener,
|
|
|
|
public nsIScrollObserver,
|
|
|
|
public nsSupportsWeakReference
|
2014-06-03 07:08:45 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit TouchCaret(nsIPresShell* aPresShell);
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISELECTIONLISTENER
|
|
|
|
|
2015-05-15 02:06:13 +00:00
|
|
|
void Init();
|
|
|
|
void Terminate();
|
|
|
|
|
|
|
|
// nsIScrollObserver
|
|
|
|
virtual void ScrollPositionChanged() override;
|
|
|
|
|
|
|
|
// AsyncPanZoom started/stopped callbacks from nsIScrollObserver
|
|
|
|
virtual void AsyncPanZoomStarted() override;
|
|
|
|
virtual void AsyncPanZoomStopped() override;
|
2014-06-03 07:08:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle mouse and touch event only.
|
|
|
|
* Depends on visibility and position of touch caret, HandleEvent may consume
|
|
|
|
* that input event and return nsEventStatus_eConsumeNoDefault to the caller.
|
|
|
|
* In that case, caller should stop bubble up that input event.
|
|
|
|
*/
|
|
|
|
nsEventStatus HandleEvent(WidgetEvent* aEvent);
|
|
|
|
|
2014-07-26 22:15:00 +00:00
|
|
|
void SyncVisibilityWithCaret();
|
2014-06-03 07:08:45 +00:00
|
|
|
|
2014-07-26 22:15:00 +00:00
|
|
|
void UpdatePositionIfNeeded();
|
2014-06-03 07:08:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GetVisibility will get the visibility of the touch caret.
|
|
|
|
*/
|
|
|
|
bool GetVisibility() const
|
|
|
|
{
|
|
|
|
return mVisible;
|
|
|
|
}
|
|
|
|
|
2015-05-15 02:06:13 +00:00
|
|
|
/**
|
|
|
|
* Open or close the Android TextSelection ActionBar based on visibility.
|
|
|
|
*/
|
|
|
|
static void UpdateAndroidActionBarVisibility(bool aVisibility, uint32_t& aViewID);
|
|
|
|
|
2014-06-03 07:08:45 +00:00
|
|
|
private:
|
|
|
|
// Hide default constructor.
|
2015-01-06 23:35:02 +00:00
|
|
|
TouchCaret() = delete;
|
2014-06-03 07:08:45 +00:00
|
|
|
|
2014-07-26 22:13:00 +00:00
|
|
|
~TouchCaret();
|
|
|
|
|
2014-07-26 22:15:00 +00:00
|
|
|
bool IsDisplayable();
|
|
|
|
|
|
|
|
void UpdatePosition();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SetVisibility will set the visibility of the touch caret.
|
|
|
|
* SetVisibility performs an attribute-changed notification which could, in
|
|
|
|
* theory, destroy frames.
|
|
|
|
*/
|
|
|
|
void SetVisibility(bool aVisible);
|
|
|
|
|
2014-10-07 01:02:00 +00:00
|
|
|
/**
|
|
|
|
* Helper function to get caret's focus frame and caret's bounding rect.
|
|
|
|
*/
|
|
|
|
nsIFrame* GetCaretFocusFrame(nsRect* aOutRect = nullptr);
|
|
|
|
|
2014-06-03 07:08:45 +00:00
|
|
|
/**
|
|
|
|
* Find the nsCanvasFrame which holds the touch caret.
|
|
|
|
*/
|
2014-10-14 01:07:00 +00:00
|
|
|
nsCanvasFrame* GetCanvasFrame();
|
2014-06-03 07:08:45 +00:00
|
|
|
|
2015-02-05 00:36:00 +00:00
|
|
|
/**
|
|
|
|
* Find the root frame to update the touch caret's position.
|
|
|
|
*/
|
|
|
|
nsIFrame* GetRootFrame();
|
|
|
|
|
2014-06-03 07:08:45 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the bounding rectangle of the touch caret.
|
|
|
|
*
|
|
|
|
* @returns A nsRect representing the bounding rectangle of this touch caret.
|
|
|
|
* The returned offset is relative to the canvas frame.
|
|
|
|
*/
|
|
|
|
nsRect GetTouchFrameRect();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the bounding rectangle where the caret can be positioned.
|
|
|
|
* If we're positioning a caret in an input field, make sure the touch caret
|
|
|
|
* stays within the bounds of the field.
|
|
|
|
*
|
|
|
|
* @returns A nsRect representing the bounding rectangle of this valid area.
|
|
|
|
* The returned offset is relative to the canvas frame.
|
|
|
|
*/
|
|
|
|
nsRect GetContentBoundary();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the center y position of the caret.
|
|
|
|
* The returned point is relative to the canvas frame.
|
|
|
|
*/
|
|
|
|
nscoord GetCaretYCenterPosition();
|
|
|
|
|
2014-10-15 01:05:00 +00:00
|
|
|
/**
|
2015-04-15 23:15:00 +00:00
|
|
|
* Retrieve the rect of the touch caret.
|
|
|
|
* The returned rect is relative to the canvas frame.
|
2014-10-15 01:05:00 +00:00
|
|
|
*/
|
2015-04-15 23:15:00 +00:00
|
|
|
nsRect GetTouchCaretRect();
|
2014-10-15 01:05:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clamp the position of the touch caret to the scroll frame boundary.
|
2015-04-15 23:15:00 +00:00
|
|
|
* The returned rect is relative to the canvas frame.
|
2014-10-15 01:05:00 +00:00
|
|
|
*/
|
2015-04-15 23:15:00 +00:00
|
|
|
nsRect ClampRectToScrollFrame(const nsRect& aRect);
|
2014-10-15 01:05:00 +00:00
|
|
|
|
2014-06-03 07:08:45 +00:00
|
|
|
/**
|
|
|
|
* Set the position of the touch caret.
|
|
|
|
* Touch caret is an absolute positioned div.
|
|
|
|
*/
|
2015-04-15 23:15:00 +00:00
|
|
|
void SetTouchFramePos(const nsRect& aRect);
|
2014-06-03 07:08:45 +00:00
|
|
|
|
|
|
|
void LaunchExpirationTimer();
|
|
|
|
void CancelExpirationTimer();
|
|
|
|
static void DisableTouchCaretCallback(nsITimer* aTimer, void* aPresShell);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the caret to movePoint which is relative to the canvas frame.
|
|
|
|
* Caret will be scrolled into view.
|
|
|
|
*
|
|
|
|
* @param movePoint tap location relative to the canvas frame.
|
|
|
|
*/
|
|
|
|
void MoveCaret(const nsPoint& movePoint);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if aPoint is inside the touch caret frame.
|
|
|
|
*
|
|
|
|
* @param aPoint tap location relative to the canvas frame.
|
|
|
|
*/
|
|
|
|
bool IsOnTouchCaret(const nsPoint& aPoint);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* These Handle* functions comprise input alphabet of the TouchCaret
|
|
|
|
* finite-state machine triggering state transitions.
|
|
|
|
*/
|
|
|
|
nsEventStatus HandleMouseMoveEvent(WidgetMouseEvent* aEvent);
|
|
|
|
nsEventStatus HandleMouseUpEvent(WidgetMouseEvent* aEvent);
|
|
|
|
nsEventStatus HandleMouseDownEvent(WidgetMouseEvent* aEvent);
|
|
|
|
nsEventStatus HandleTouchMoveEvent(WidgetTouchEvent* aEvent);
|
|
|
|
nsEventStatus HandleTouchUpEvent(WidgetTouchEvent* aEvent);
|
|
|
|
nsEventStatus HandleTouchDownEvent(WidgetTouchEvent* aEvent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the coordinates of a given touch event, relative to canvas frame.
|
|
|
|
* @param aEvent the event
|
|
|
|
* @param aIdentifier the mIdentifier of the touch which is to be converted.
|
|
|
|
* @return the point, or (NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE) if
|
|
|
|
* for some reason the coordinates for the touch are not known (e.g.,
|
|
|
|
* the mIdentifier touch is not found).
|
|
|
|
*/
|
|
|
|
nsPoint GetEventPosition(WidgetTouchEvent* aEvent, int32_t aIdentifier);
|
|
|
|
|
2014-08-04 04:24:00 +00:00
|
|
|
/**
|
|
|
|
* Set mouse down state in nsFrameSelection, we'll set state to true when
|
|
|
|
* user start dragging caret and set state to false when user release the
|
|
|
|
* caret. The reason for setting this state is it will fire drag reason
|
|
|
|
* when moving caret and fire mouseup reason when releasing caret. So that
|
|
|
|
* the display behavior of copy/paste menu becomes more reasonable.
|
|
|
|
*/
|
|
|
|
void SetSelectionDragState(bool aState);
|
|
|
|
|
2014-06-03 07:08:45 +00:00
|
|
|
/**
|
|
|
|
* Get the coordinates of a given mouse event, relative to canvas frame.
|
|
|
|
* @param aEvent the event
|
|
|
|
* @return the point, or (NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE) if
|
|
|
|
* for some reason the coordinates for the mouse are not known.
|
|
|
|
*/
|
|
|
|
nsPoint GetEventPosition(WidgetMouseEvent* aEvent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* States of TouchCaret finite-state machine.
|
|
|
|
*/
|
|
|
|
enum TouchCaretState {
|
|
|
|
// In this state, either there is no touch/mouse event going on, or the
|
|
|
|
// first stroke does not hit the touch caret.
|
|
|
|
// Will enter TOUCHCARET_TOUCHDRAG_ACTIVE state if the first touch stroke
|
|
|
|
// hits the touch caret. Will enter TOUCHCARET_MOUSEDRAG_ACTIVE state if
|
|
|
|
// mouse (left button) down hits the touch caret.
|
|
|
|
// Allowed next state: TOUCHCARET_MOUSEDRAG_ACTIVE,
|
|
|
|
// TOUCHCARET_TOUCHDRAG_ACTIVE.
|
|
|
|
TOUCHCARET_NONE,
|
|
|
|
// The first (left button) mouse down hits on the touch caret and is
|
|
|
|
// alive. Will enter TOUCHCARET_NONE state if the left button is release.
|
|
|
|
// Allowed next states: TOUCHCARET_NONE.
|
|
|
|
TOUCHCARET_MOUSEDRAG_ACTIVE,
|
|
|
|
// The first touch start event hits on touch caret and is alive.
|
|
|
|
// Will enter TOUCHCARET_NONE state if the finger on touch caret is
|
|
|
|
// removed and there are no more fingers on the screen; will enter
|
|
|
|
// TOUCHCARET_TOUCHDRAG_INACTIVE state if the finger on touch caret is
|
|
|
|
// removed but still has fingers touching on the screen.
|
|
|
|
// Allowed next states: TOUCHCARET_NONE, TOUCHCARET_TOUCHDRAG_INACTIVE.
|
|
|
|
TOUCHCARET_TOUCHDRAG_ACTIVE,
|
|
|
|
// The first touch stroke, which hit on touch caret, is dead, but still has
|
|
|
|
// fingers touching on the screen.
|
|
|
|
// Will enter TOUCHCARET_NONE state if all the fingers are removed from the
|
|
|
|
// screen.
|
|
|
|
// Allowed next state: TOUCHCARET_NONE.
|
|
|
|
TOUCHCARET_TOUCHDRAG_INACTIVE,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do actual state transition and reset substates.
|
|
|
|
*/
|
|
|
|
void SetState(TouchCaretState aState);
|
|
|
|
|
2014-10-12 21:44:00 +00:00
|
|
|
/**
|
|
|
|
* Dispatch touch caret tap event to chrome.
|
|
|
|
*/
|
|
|
|
void DispatchTapEvent();
|
|
|
|
|
2014-06-03 07:08:45 +00:00
|
|
|
/**
|
|
|
|
* Current state we're dealing with.
|
|
|
|
*/
|
|
|
|
TouchCaretState mState;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array containing all active touch IDs. When a touch happens, it gets added
|
|
|
|
* to this array, even if we choose not to handle it. When it ends, we remove
|
|
|
|
* it. We need to maintain this array in order to detect the end of the
|
|
|
|
* "multitouch" states because touch start events contain all current touches,
|
|
|
|
* but touch end events contain only those touches that have gone.
|
|
|
|
*/
|
|
|
|
nsTArray<int32_t> mTouchesId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The mIdentifier of the touch which is on the touch caret.
|
|
|
|
*/
|
|
|
|
int32_t mActiveTouchId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The offset between the tap location and the center of caret along y axis.
|
|
|
|
*/
|
|
|
|
nscoord mCaretCenterToDownPointOffsetY;
|
|
|
|
|
2014-09-15 00:58:00 +00:00
|
|
|
/**
|
|
|
|
* Get from pref "touchcaret.inflatesize.threshold". This will inflate the
|
|
|
|
* size of the touch caret frame when checking if user clicks on the caret
|
|
|
|
* or not. In app units.
|
|
|
|
*/
|
|
|
|
static int32_t TouchCaretInflateSize() { return sTouchCaretInflateSize; }
|
2014-06-03 07:08:45 +00:00
|
|
|
|
|
|
|
static int32_t TouchCaretExpirationTime()
|
|
|
|
{
|
|
|
|
return sTouchCaretExpirationTime;
|
|
|
|
}
|
|
|
|
|
2015-05-15 02:06:13 +00:00
|
|
|
void LaunchScrollEndDetector();
|
|
|
|
void CancelScrollEndDetector();
|
|
|
|
static void FireScrollEnd(nsITimer* aTimer, void* aSelectionCarets);
|
|
|
|
|
|
|
|
// This timer is used for detecting scroll end. We don't have
|
|
|
|
// scroll end event now, so we will fire this event with a
|
|
|
|
// const time when we scroll. So when timer triggers, we treat it
|
|
|
|
// as scroll end event.
|
|
|
|
nsCOMPtr<nsITimer> mScrollEndDetectorTimer;
|
|
|
|
|
2014-06-03 07:08:45 +00:00
|
|
|
nsWeakPtr mPresShell;
|
2015-05-15 02:06:13 +00:00
|
|
|
WeakPtr<nsDocShell> mDocShell;
|
|
|
|
|
|
|
|
// True if AsyncPanZoom is started
|
|
|
|
bool mInAsyncPanZoomGesture;
|
2014-06-03 07:08:45 +00:00
|
|
|
|
|
|
|
// Touch caret visibility
|
|
|
|
bool mVisible;
|
2014-10-12 21:44:00 +00:00
|
|
|
// Use for detecting single tap on touch caret.
|
|
|
|
bool mIsValidTap;
|
2014-06-03 07:08:45 +00:00
|
|
|
// Touch caret timer
|
|
|
|
nsCOMPtr<nsITimer> mTouchCaretExpirationTimer;
|
|
|
|
|
|
|
|
// Preference
|
2014-09-15 00:58:00 +00:00
|
|
|
static int32_t sTouchCaretInflateSize;
|
2014-06-03 07:08:45 +00:00
|
|
|
static int32_t sTouchCaretExpirationTime;
|
2015-05-15 02:06:13 +00:00
|
|
|
static bool sCaretManagesAndroidActionbar;
|
|
|
|
static bool sTouchcaretExtendedvisibility;
|
2014-06-04 20:57:00 +00:00
|
|
|
|
|
|
|
// The auto scroll timer's interval in miliseconds.
|
|
|
|
friend class SelectionCarets;
|
|
|
|
static const int32_t sAutoScrollTimerDelay = 30;
|
2015-05-15 02:06:13 +00:00
|
|
|
// Time for trigger scroll end event, in miliseconds.
|
|
|
|
static const int32_t sScrollEndTimerDelay = 300;
|
2015-05-15 02:06:13 +00:00
|
|
|
|
|
|
|
// Unique ID of current Mobile ActionBar view.
|
|
|
|
static uint32_t sActionBarViewCount;
|
|
|
|
uint32_t mActionBarViewID;
|
2014-06-03 07:08:45 +00:00
|
|
|
};
|
|
|
|
} //namespace mozilla
|
|
|
|
#endif //mozilla_TouchCaret_h__
|