2014-06-05 00:57:00 +04: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 SelectionCarets_h__
|
|
|
|
#define SelectionCarets_h__
|
|
|
|
|
2015-03-18 10:26:00 +03:00
|
|
|
#include "nsDirection.h"
|
2014-11-05 23:18:00 +03:00
|
|
|
#include "nsIReflowObserver.h"
|
2014-06-05 00:57:00 +04:00
|
|
|
#include "nsIScrollObserver.h"
|
|
|
|
#include "nsISelectionListener.h"
|
|
|
|
#include "nsWeakPtr.h"
|
|
|
|
#include "nsWeakReference.h"
|
|
|
|
#include "Units.h"
|
2014-12-08 03:55:00 +03:00
|
|
|
#include "mozilla/dom/SelectionStateChangedEvent.h"
|
2014-06-05 00:57:00 +04:00
|
|
|
#include "mozilla/EventForwards.h"
|
2014-11-13 16:32:00 +03:00
|
|
|
#include "mozilla/WeakPtr.h"
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
class nsCanvasFrame;
|
2014-11-13 16:32:00 +03:00
|
|
|
class nsDocShell;
|
2014-10-17 01:17:00 +04:00
|
|
|
class nsFrameSelection;
|
|
|
|
class nsIContent;
|
2014-06-05 00:57:00 +04:00
|
|
|
class nsIDocument;
|
|
|
|
class nsIFrame;
|
|
|
|
class nsIPresShell;
|
|
|
|
class nsITimer;
|
|
|
|
class nsIWidget;
|
|
|
|
class nsPresContext;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-10-17 01:17:00 +04:00
|
|
|
namespace dom {
|
|
|
|
class Selection;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
/**
|
|
|
|
* The SelectionCarets draw a pair of carets when the selection is not
|
|
|
|
* collapsed, one at each end of the selection.
|
|
|
|
* SelectionCarets also handle visibility, dragging caret and selecting word
|
|
|
|
* when long tap event fired.
|
|
|
|
*
|
|
|
|
* The DOM structure is 2 div elements for showing start and end caret.
|
2014-11-13 07:03:00 +03:00
|
|
|
* Each div element has a child div element. That is, each caret consist of
|
|
|
|
* outer div and inner div. Outer div takes responsibility for detecting two
|
|
|
|
* carets are overlapping. Inner div is for actual appearance.
|
2014-06-05 00:57:00 +04:00
|
|
|
*
|
|
|
|
* Here is an explanation of the html class names:
|
|
|
|
* .moz-selectioncaret-left: Indicates start DIV.
|
|
|
|
* .moz-selectioncaret-right: Indicates end DIV.
|
|
|
|
* .hidden: This class name is set by SetVisibility,
|
|
|
|
* SetStartFrameVisibility and SetEndFrameVisibility. Element
|
|
|
|
* with this class name become hidden.
|
|
|
|
* .tilt: This class name is set by SetTilted. According to the
|
2014-11-13 07:03:00 +03:00
|
|
|
* UX spec, when selection carets are overlapping, the image of
|
2014-06-05 00:57:00 +04:00
|
|
|
* caret becomes tilt.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class SelectionCarets final : public nsIReflowObserver,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsISelectionListener,
|
|
|
|
public nsIScrollObserver,
|
|
|
|
public nsSupportsWeakReference
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Indicate which part of caret we are dragging at.
|
|
|
|
*/
|
|
|
|
enum DragMode {
|
|
|
|
NONE,
|
|
|
|
START_FRAME,
|
|
|
|
END_FRAME
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit SelectionCarets(nsIPresShell *aPresShell);
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
2014-11-05 23:18:00 +03:00
|
|
|
NS_DECL_NSIREFLOWOBSERVER
|
2014-06-05 00:57:00 +04:00
|
|
|
NS_DECL_NSISELECTIONLISTENER
|
|
|
|
|
2014-11-27 20:56:00 +03:00
|
|
|
// Notify selection carets about the blur event to hidden itself
|
2015-01-05 03:15:00 +03:00
|
|
|
void NotifyBlur(bool aIsLeavingDocument);
|
2014-11-27 20:56:00 +03:00
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
// nsIScrollObserver
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void ScrollPositionChanged() override;
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2014-09-23 14:37:00 +04:00
|
|
|
// AsyncPanZoom started/stopped callbacks from nsIScrollObserver
|
2015-03-25 14:52:37 +03:00
|
|
|
virtual void AsyncPanZoomStarted() override;
|
|
|
|
virtual void AsyncPanZoomStopped() override;
|
2014-09-23 14:37:00 +04:00
|
|
|
|
2014-11-05 23:18:00 +03:00
|
|
|
void Init();
|
|
|
|
void Terminate();
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
nsEventStatus HandleEvent(WidgetEvent* aEvent);
|
|
|
|
|
|
|
|
bool GetVisibility() const
|
|
|
|
{
|
|
|
|
return mVisible;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get from pref "selectioncaret.inflatesize.threshold". This will inflate size of
|
|
|
|
* caret frame when we checking if user click on caret or not. In app units.
|
|
|
|
*/
|
|
|
|
static int32_t SelectionCaretsInflateSize()
|
|
|
|
{
|
|
|
|
return sSelectionCaretsInflateSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-24 02:40:01 +04:00
|
|
|
virtual ~SelectionCarets();
|
|
|
|
|
2015-01-07 02:35:02 +03:00
|
|
|
SelectionCarets() = delete;
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2014-11-27 20:56:00 +03:00
|
|
|
/**
|
|
|
|
* Set visibility for selection caret.
|
|
|
|
*/
|
|
|
|
void SetVisibility(bool aVisible);
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
/**
|
|
|
|
* Update selection caret position base on current selection range.
|
|
|
|
*/
|
|
|
|
void UpdateSelectionCarets();
|
|
|
|
|
|
|
|
/**
|
2014-12-24 21:14:00 +03:00
|
|
|
* Select a word base on current position, which activates only if element is
|
|
|
|
* selectable. Triggered by long tap event.
|
2014-06-05 00:57:00 +04:00
|
|
|
*/
|
|
|
|
nsresult SelectWord();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move selection base on current touch/mouse point
|
|
|
|
*/
|
|
|
|
nsEventStatus DragSelection(const nsPoint &movePoint);
|
|
|
|
|
|
|
|
/**
|
2014-10-29 01:25:00 +03:00
|
|
|
* Get the vertical center position of selection caret relative to root
|
2014-06-05 00:57:00 +04:00
|
|
|
* frame.
|
|
|
|
*/
|
|
|
|
nscoord GetCaretYCenterPosition();
|
|
|
|
|
|
|
|
/**
|
2014-08-04 08:26:00 +04:00
|
|
|
* Simulate drag state when we change the selection range.
|
2014-06-05 00:57:00 +04:00
|
|
|
* Hence, the selection change event will fire normally.
|
|
|
|
*/
|
2014-08-04 08:26:00 +04:00
|
|
|
void SetSelectionDragState(bool aState);
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2015-03-18 10:26:00 +03:00
|
|
|
void SetSelectionDirection(nsDirection aDir);
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
/**
|
2015-04-17 14:14:13 +03:00
|
|
|
* Move start frame of selection caret to given position.
|
2014-06-05 00:57:00 +04:00
|
|
|
* In app units.
|
|
|
|
*/
|
2015-04-17 14:14:13 +03:00
|
|
|
void SetStartFramePos(const nsPoint& aPosition);
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
/**
|
2015-04-17 14:14:13 +03:00
|
|
|
* Move end frame of selection caret to given position.
|
2014-06-05 00:57:00 +04:00
|
|
|
* In app units.
|
|
|
|
*/
|
2015-04-17 14:14:13 +03:00
|
|
|
void SetEndFramePos(const nsPoint& aPosition);
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2014-09-15 04:57:00 +04:00
|
|
|
/**
|
|
|
|
* Check if aPosition is on the start or end frame of the
|
2014-11-13 07:03:00 +03:00
|
|
|
* selection caret's inner div element.
|
2014-09-15 04:57:00 +04:00
|
|
|
*
|
2014-10-29 01:25:00 +03:00
|
|
|
* @param aPosition should be relative to document's root frame
|
2014-09-15 04:57:00 +04:00
|
|
|
* in app units
|
|
|
|
*/
|
2014-11-13 07:03:00 +03:00
|
|
|
bool IsOnStartFrameInner(const nsPoint& aPosition);
|
|
|
|
bool IsOnEndFrameInner(const nsPoint& aPosition);
|
2014-09-15 04:57:00 +04:00
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
/**
|
2014-11-13 07:03:00 +03:00
|
|
|
* Get rect of selection caret's outer div element relative
|
2014-10-29 01:25:00 +03:00
|
|
|
* to document's root frame, in app units.
|
2014-06-05 00:57:00 +04:00
|
|
|
*/
|
|
|
|
nsRect GetStartFrameRect();
|
2014-11-13 07:03:00 +03:00
|
|
|
nsRect GetEndFrameRect();
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
/**
|
2014-11-13 07:03:00 +03:00
|
|
|
* Get rect of selection caret's inner div element relative
|
2014-10-29 01:25:00 +03:00
|
|
|
* to document's root frame, in app units.
|
2014-06-05 00:57:00 +04:00
|
|
|
*/
|
2014-11-13 07:03:00 +03:00
|
|
|
nsRect GetStartFrameRectInner();
|
|
|
|
nsRect GetEndFrameRectInner();
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set visibility for start part of selection caret, this function
|
|
|
|
* only affects css property of start frame. So it doesn't change
|
|
|
|
* mVisible member. When caret overflows element's box we'll hide
|
|
|
|
* it by calling this function.
|
|
|
|
*/
|
|
|
|
void SetStartFrameVisibility(bool aVisible);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as above function but for end frame of selection caret.
|
|
|
|
*/
|
|
|
|
void SetEndFrameVisibility(bool aVisible);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set tilt class name to start and end frame of selection caret.
|
|
|
|
*/
|
|
|
|
void SetTilted(bool aIsTilt);
|
|
|
|
|
2014-12-08 03:55:00 +03:00
|
|
|
// Utility functions
|
2014-10-17 01:17:00 +04:00
|
|
|
dom::Selection* GetSelection();
|
|
|
|
already_AddRefed<nsFrameSelection> GetFrameSelection();
|
|
|
|
nsIContent* GetFocusedContent();
|
2014-12-10 03:50:00 +03:00
|
|
|
void DispatchSelectionStateChangedEvent(dom::Selection* aSelection,
|
|
|
|
dom::SelectionState aState);
|
2014-12-08 03:55:00 +03:00
|
|
|
void DispatchSelectionStateChangedEvent(dom::Selection* aSelection,
|
|
|
|
const dom::Sequence<dom::SelectionState>& aStates);
|
2014-12-19 09:04:45 +03:00
|
|
|
void DispatchCustomEvent(const nsAString& aEvent);
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Detecting long tap using timer
|
|
|
|
*/
|
|
|
|
void LaunchLongTapDetector();
|
|
|
|
void CancelLongTapDetector();
|
|
|
|
static void FireLongTap(nsITimer* aTimer, void* aSelectionCarets);
|
|
|
|
|
|
|
|
void LaunchScrollEndDetector();
|
2015-02-05 23:13:00 +03:00
|
|
|
void CancelScrollEndDetector();
|
2014-06-05 00:57:00 +04:00
|
|
|
static void FireScrollEnd(nsITimer* aTimer, void* aSelectionCarets);
|
|
|
|
|
|
|
|
nsIPresShell* mPresShell;
|
2014-11-13 16:32:00 +03:00
|
|
|
WeakPtr<nsDocShell> mDocShell;
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
// This timer is used for detecting long tap fire. If content process
|
|
|
|
// has APZC, we'll use APZC for long tap detecting. Otherwise, we use this
|
|
|
|
// timer to detect long tap.
|
|
|
|
nsCOMPtr<nsITimer> mLongTapDetectorTimer;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// When touch or mouse down, we save the position for detecting
|
|
|
|
// drag distance
|
|
|
|
nsPoint mDownPoint;
|
|
|
|
|
|
|
|
// For filter multitouch event
|
|
|
|
int32_t mActiveTouchId;
|
|
|
|
|
|
|
|
nscoord mCaretCenterToDownPointOffsetY;
|
2014-12-19 08:25:17 +03:00
|
|
|
|
|
|
|
// The horizontal boundary is defined by the first selected frame which
|
|
|
|
// determines the start-caret position. When users drag the end-caret up,
|
|
|
|
// the touch input(pos.y) will be changed to not cross this boundary.
|
|
|
|
// Otherwise, the selection range changes to one character only
|
|
|
|
// which causes the bad user experience.
|
|
|
|
nscoord mDragUpYBoundary;
|
|
|
|
// The horizontal boundary is defined by the last selected frame which
|
|
|
|
// determines the end-caret position. When users drag the start-caret down,
|
|
|
|
// the touch input(pos.y) will be changed to not cross this boundary.
|
|
|
|
// Otherwise, the selection range changes to one character only
|
|
|
|
// which causes the bad user experience.
|
|
|
|
nscoord mDragDownYBoundary;
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
DragMode mDragMode;
|
2014-09-23 14:37:00 +04:00
|
|
|
|
2015-03-11 22:29:06 +03:00
|
|
|
// True if async-pan-zoom should be used for selection carets.
|
|
|
|
bool mUseAsyncPanZoom;
|
2015-01-15 08:42:00 +03:00
|
|
|
// True if AsyncPanZoom is started
|
|
|
|
bool mInAsyncPanZoomGesture;
|
2014-11-10 02:29:00 +03:00
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
bool mEndCaretVisible;
|
2014-09-23 14:37:00 +04:00
|
|
|
bool mStartCaretVisible;
|
2014-12-10 03:50:00 +03:00
|
|
|
bool mSelectionVisibleInScrollFrames;
|
2014-09-23 14:37:00 +04:00
|
|
|
bool mVisible;
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
// Preference
|
|
|
|
static int32_t sSelectionCaretsInflateSize;
|
2015-04-14 05:33:02 +03:00
|
|
|
static bool sSelectionCaretDetectsLongTap;
|
2014-06-05 00:57:00 +04:00
|
|
|
};
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif //SelectionCarets_h__
|