2015-05-04 22:25: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 AccessibleCaret_h__
|
|
|
|
#define AccessibleCaret_h__
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/dom/AnonymousContent.h"
|
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
2020-07-15 16:44:39 +03:00
|
|
|
#include "nsIFrame.h" // for WeakFrame only
|
2015-05-04 22:25:00 +03:00
|
|
|
#include "nsISupportsBase.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2016-08-18 10:09:19 +03:00
|
|
|
#include "nsLiteralString.h"
|
2015-05-04 22:25:00 +03:00
|
|
|
#include "nsRect.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-05-04 22:25:00 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
class nsIFrame;
|
|
|
|
struct nsPoint;
|
|
|
|
|
|
|
|
namespace mozilla {
|
2019-04-16 10:25:10 +03:00
|
|
|
class PresShell;
|
2018-04-20 07:49:29 +03:00
|
|
|
namespace dom {
|
|
|
|
class Event;
|
|
|
|
} // namespace dom
|
|
|
|
|
2015-05-04 22:25:00 +03:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Upon the creation of AccessibleCaret, it will insert DOM Element as an
|
|
|
|
// anonymous content containing the caret image. The caret appearance and
|
|
|
|
// position can be controlled by SetAppearance() and SetPosition().
|
|
|
|
//
|
|
|
|
// All the rect or point are relative to root frame except being specified
|
|
|
|
// explicitly.
|
|
|
|
//
|
|
|
|
// None of the methods in AccessibleCaret will flush layout or style. To ensure
|
|
|
|
// that SetPosition() works correctly, the caller must make sure the layout is
|
|
|
|
// up to date.
|
|
|
|
//
|
2015-11-18 06:44:00 +03:00
|
|
|
// Please see the wiki page for more information.
|
2016-08-10 10:46:27 +03:00
|
|
|
// https://wiki.mozilla.org/AccessibleCaret
|
|
|
|
//
|
2015-09-18 20:05:18 +03:00
|
|
|
class AccessibleCaret {
|
2015-05-04 22:25:00 +03:00
|
|
|
public:
|
2019-04-16 10:25:10 +03:00
|
|
|
explicit AccessibleCaret(PresShell* aPresShell);
|
2015-09-18 20:05:18 +03:00
|
|
|
virtual ~AccessibleCaret();
|
2015-05-04 22:25:00 +03:00
|
|
|
|
|
|
|
// This enumeration representing the visibility and visual style of an
|
|
|
|
// AccessibleCaret.
|
|
|
|
//
|
|
|
|
// Use SetAppearance() to change the appearance, and use GetAppearance() to
|
|
|
|
// get the current appearance.
|
|
|
|
enum class Appearance : uint8_t {
|
|
|
|
// Do not display the caret at all.
|
|
|
|
None,
|
|
|
|
|
|
|
|
// Display the caret in default style.
|
|
|
|
Normal,
|
|
|
|
|
|
|
|
// The caret should be displayed logically but it is kept invisible to the
|
|
|
|
// user. This enum is the only difference between "logically visible" and
|
|
|
|
// "visually visible". It can be used for reasons such as:
|
|
|
|
// 1. Out of scroll port.
|
|
|
|
// 2. For UX requirement such as hide a caret in an empty text area.
|
|
|
|
NormalNotShown,
|
|
|
|
|
|
|
|
// Display the caret which is tilted to the left.
|
|
|
|
Left,
|
|
|
|
|
|
|
|
// Display the caret which is tilted to the right.
|
|
|
|
Right
|
|
|
|
};
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
friend std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const Appearance& aAppearance);
|
|
|
|
|
2015-05-04 22:25:00 +03:00
|
|
|
Appearance GetAppearance() const { return mAppearance; }
|
|
|
|
|
2015-09-18 20:05:18 +03:00
|
|
|
virtual void SetAppearance(Appearance aAppearance);
|
2015-05-04 22:25:00 +03:00
|
|
|
|
|
|
|
// Return true if current appearance is either Normal, NormalNotShown, Left,
|
|
|
|
// or Right.
|
|
|
|
bool IsLogicallyVisible() const { return mAppearance != Appearance::None; }
|
|
|
|
|
|
|
|
// Return true if current appearance is either Normal, Left, or Right.
|
|
|
|
bool IsVisuallyVisible() const {
|
|
|
|
return (mAppearance != Appearance::None) &&
|
|
|
|
(mAppearance != Appearance::NormalNotShown);
|
|
|
|
}
|
|
|
|
|
2020-02-08 01:31:40 +03:00
|
|
|
// This enum represents the result returned by SetPosition().
|
2015-05-04 22:25:00 +03:00
|
|
|
enum class PositionChangedResult : uint8_t {
|
2020-02-08 01:31:40 +03:00
|
|
|
// Both position and the zoom level are not changed.
|
2015-05-04 22:25:00 +03:00
|
|
|
NotChanged,
|
|
|
|
|
2020-02-08 01:31:40 +03:00
|
|
|
// The position is changed. (The zoom level may or may not be changed.)
|
|
|
|
Position,
|
2015-05-04 22:25:00 +03:00
|
|
|
|
2020-02-08 01:31:40 +03:00
|
|
|
// Only the zoom level is changed. The position is *not* changed.
|
|
|
|
Zoom,
|
|
|
|
|
|
|
|
// The position is out of scroll port.
|
2015-05-04 22:25:00 +03:00
|
|
|
Invisible
|
|
|
|
};
|
2015-09-18 20:05:19 +03:00
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const PositionChangedResult& aResult);
|
|
|
|
|
2015-09-18 20:05:18 +03:00
|
|
|
virtual PositionChangedResult SetPosition(nsIFrame* aFrame, int32_t aOffset);
|
2015-05-04 22:25:00 +03:00
|
|
|
|
|
|
|
// Does two AccessibleCarets overlap?
|
|
|
|
bool Intersects(const AccessibleCaret& aCaret) const;
|
|
|
|
|
|
|
|
// Is the point within the caret's rect? The point should be relative to root
|
|
|
|
// frame.
|
2016-08-14 16:39:30 +03:00
|
|
|
enum class TouchArea {
|
|
|
|
Full, // Contains both text overlay and caret image.
|
|
|
|
CaretImage
|
|
|
|
};
|
|
|
|
bool Contains(const nsPoint& aPoint, TouchArea aTouchArea) const;
|
2015-05-04 22:25:00 +03:00
|
|
|
|
|
|
|
// The geometry center of the imaginary caret (nsCaret) to which this
|
|
|
|
// AccessibleCaret is attached. It is needed when dragging the caret.
|
|
|
|
nsPoint LogicalPosition() const { return mImaginaryCaretRect.Center(); }
|
|
|
|
|
2018-08-31 16:51:51 +03:00
|
|
|
// Element for 'Intersects' test. This is the container of the caret image
|
|
|
|
// and text-overlay elements. See CreateCaretElement() for the content
|
|
|
|
// structure.
|
2018-08-13 12:56:48 +03:00
|
|
|
dom::Element& CaretElement() const {
|
|
|
|
return mCaretElementHolder->ContentNode();
|
2015-05-04 22:25:00 +03:00
|
|
|
}
|
|
|
|
|
2016-07-11 15:16:39 +03:00
|
|
|
// Ensures that the caret element is made "APZ aware" so that the APZ code
|
|
|
|
// doesn't scroll the page when the user is trying to drag the caret.
|
|
|
|
void EnsureApzAware();
|
|
|
|
|
2019-11-02 06:05:28 +03:00
|
|
|
bool IsInPositionFixedSubtree() const;
|
|
|
|
|
2015-09-18 20:05:18 +03:00
|
|
|
protected:
|
2015-05-04 22:25:00 +03:00
|
|
|
// Argument aRect should be relative to CustomContentContainerFrame().
|
2016-02-15 13:29:32 +03:00
|
|
|
void SetCaretElementStyle(const nsRect& aRect, float aZoomLevel);
|
2016-08-14 15:22:04 +03:00
|
|
|
void SetTextOverlayElementStyle(const nsRect& aRect, float aZoomLevel);
|
|
|
|
void SetCaretImageElementStyle(const nsRect& aRect, float aZoomLevel);
|
2015-05-15 19:05:00 +03:00
|
|
|
|
|
|
|
// Get current zoom level.
|
|
|
|
float GetZoomLevel();
|
2015-05-04 22:25:00 +03:00
|
|
|
|
2016-08-14 15:22:04 +03:00
|
|
|
// Element which contains the text overly for the 'Contains' test.
|
|
|
|
dom::Element* TextOverlayElement() const {
|
|
|
|
return mCaretElementHolder->GetElementById(sTextOverlayElementId);
|
|
|
|
}
|
|
|
|
|
2015-05-04 22:25:00 +03:00
|
|
|
// Element which contains the caret image for 'Contains' test.
|
|
|
|
dom::Element* CaretImageElement() const {
|
2016-08-18 10:09:19 +03:00
|
|
|
return mCaretElementHolder->GetElementById(sCaretImageElementId);
|
2015-05-04 22:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* RootFrame() const { return mPresShell->GetRootFrame(); }
|
|
|
|
|
|
|
|
nsIFrame* CustomContentContainerFrame() const;
|
|
|
|
|
2016-08-21 16:41:32 +03:00
|
|
|
// Transform Appearance to CSS id used in ua.css.
|
|
|
|
static nsAutoString AppearanceString(Appearance aAppearance);
|
2015-05-04 22:25:00 +03:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
already_AddRefed<dom::Element> CreateCaretElement(dom::Document*) const;
|
2015-05-04 22:25:00 +03:00
|
|
|
|
|
|
|
// Inject caret element into custom content container.
|
2019-01-02 16:05:23 +03:00
|
|
|
void InjectCaretElement(dom::Document*);
|
2015-05-04 22:25:00 +03:00
|
|
|
|
|
|
|
// Remove caret element from custom content container.
|
2019-01-02 16:05:23 +03:00
|
|
|
void RemoveCaretElement(dom::Document*);
|
2015-05-04 22:25:00 +03:00
|
|
|
|
2019-11-02 01:19:18 +03:00
|
|
|
// Clear the cached rects and zoom level.
|
|
|
|
void ClearCachedData();
|
|
|
|
|
2015-05-27 11:37:28 +03:00
|
|
|
// The top-center of the imaginary caret to which this AccessibleCaret is
|
2015-05-04 22:25:00 +03:00
|
|
|
// attached.
|
|
|
|
static nsPoint CaretElementPosition(const nsRect& aRect) {
|
2015-05-27 11:37:28 +03:00
|
|
|
return aRect.TopLeft() + nsPoint(aRect.width / 2, 0);
|
2015-05-04 22:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class DummyTouchListener final : public nsIDOMEventListener {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2018-04-20 07:49:29 +03:00
|
|
|
NS_IMETHOD HandleEvent(mozilla::dom::Event* aEvent) override {
|
2015-05-04 22:25:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-03-17 12:38:32 +03:00
|
|
|
virtual ~DummyTouchListener() = default;
|
2015-05-04 22:25:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Member variables
|
|
|
|
Appearance mAppearance = Appearance::None;
|
|
|
|
|
2016-02-15 10:12:35 +03:00
|
|
|
// AccessibleCaretManager owns us by a UniquePtr. When it's terminated by
|
2015-05-04 22:25:00 +03:00
|
|
|
// AccessibleCaretEventHub::Terminate() which is called in
|
2016-02-15 10:12:35 +03:00
|
|
|
// PresShell::Destroy(), it frees us automatically. No need to worry if we
|
2015-05-04 22:25:00 +03:00
|
|
|
// outlive mPresShell.
|
2019-04-16 10:25:10 +03:00
|
|
|
PresShell* const MOZ_NON_OWNING_REF mPresShell = nullptr;
|
2015-05-04 22:25:00 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<dom::AnonymousContent> mCaretElementHolder;
|
2015-05-04 22:25:00 +03:00
|
|
|
|
|
|
|
// mImaginaryCaretRect is relative to root frame.
|
|
|
|
nsRect mImaginaryCaretRect;
|
|
|
|
|
2019-11-02 01:19:18 +03:00
|
|
|
// Cached mImaginaryCaretRect relative to the custom content container. This
|
|
|
|
// is used in SetPosition() to check whether the caret position has changed.
|
|
|
|
nsRect mImaginaryCaretRectInContainerFrame;
|
|
|
|
|
2019-11-02 06:05:28 +03:00
|
|
|
// The reference frame we used to calculate mImaginaryCaretRect and
|
|
|
|
// mImaginaryCaretRectInContainerFrame.
|
|
|
|
WeakFrame mImaginaryCaretReferenceFrame;
|
|
|
|
|
2016-02-15 13:29:32 +03:00
|
|
|
// Cache current zoom level to determine whether position is changed.
|
|
|
|
float mZoomLevel = 0.0f;
|
|
|
|
|
2015-05-04 22:25:00 +03:00
|
|
|
// A no-op touch-start listener which prevents APZ from panning when dragging
|
|
|
|
// the caret.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DummyTouchListener> mDummyTouchListener{new DummyTouchListener()};
|
2015-05-04 22:25:00 +03:00
|
|
|
|
2015-05-15 19:05:00 +03:00
|
|
|
// Static class variables
|
2016-08-14 15:22:04 +03:00
|
|
|
static const nsLiteralString sTextOverlayElementId;
|
2016-08-18 10:09:19 +03:00
|
|
|
static const nsLiteralString sCaretImageElementId;
|
2015-05-15 19:05:00 +03:00
|
|
|
|
2015-05-04 22:25:00 +03:00
|
|
|
}; // class AccessibleCaret
|
|
|
|
|
2015-09-18 20:05:19 +03:00
|
|
|
std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const AccessibleCaret::Appearance& aAppearance);
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const AccessibleCaret::PositionChangedResult& aResult);
|
|
|
|
|
2015-05-04 22:25:00 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // AccessibleCaret_h__
|