Bug 1688832: part 8) Add `AccessibleCaretManager::Carets::GetSecond`. r=TYLin
[gecko.git] / layout / base / AccessibleCaretManager.h
blob4874dc5ffb5157f549b156688b8621671a60d754
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef AccessibleCaretManager_h
8 #define AccessibleCaretManager_h
10 #include "AccessibleCaret.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/dom/CaretStateChangedEvent.h"
14 #include "mozilla/dom/MouseEventBinding.h"
15 #include "mozilla/EnumSet.h"
16 #include "mozilla/EventForwards.h"
17 #include "mozilla/RefPtr.h"
18 #include "mozilla/UniquePtr.h"
19 #include "nsCOMPtr.h"
20 #include "nsCoord.h"
21 #include "nsIFrame.h"
22 #include "nsISelectionListener.h"
24 class nsFrameSelection;
25 class nsIContent;
27 struct nsPoint;
29 namespace mozilla {
30 class PresShell;
31 namespace dom {
32 class Element;
33 class Selection;
34 } // namespace dom
36 // -----------------------------------------------------------------------------
37 // AccessibleCaretManager does not deal with events or callbacks directly. It
38 // relies on AccessibleCaretEventHub to call its public methods to do the work.
39 // All codes needed to interact with PresShell, Selection, and AccessibleCaret
40 // should be written in AccessibleCaretManager.
42 // None the public methods in AccessibleCaretManager will flush layout or style
43 // prior to performing its task. The caller must ensure the layout is up to
44 // date.
45 // TODO: it's unclear, whether that's true. `OnSelectionChanged` calls
46 // `UpdateCarets`, which may flush layout.
48 // Please see the wiki page for more information.
49 // https://wiki.mozilla.org/AccessibleCaret
51 class AccessibleCaretManager {
52 public:
53 // @param aPresShell may be nullptr for testing.
54 explicit AccessibleCaretManager(PresShell* aPresShell);
55 virtual ~AccessibleCaretManager() = default;
57 // Called by AccessibleCaretEventHub to inform us that PresShell is destroyed.
58 void Terminate();
60 // The aPoint in the following public methods should be relative to root
61 // frame.
63 // Press caret on the given point. Return NS_OK if the point is actually on
64 // one of the carets.
65 MOZ_CAN_RUN_SCRIPT
66 virtual nsresult PressCaret(const nsPoint& aPoint, EventClassID aEventClass);
68 // Drag caret to the given point. It's required to call PressCaret()
69 // beforehand.
70 MOZ_CAN_RUN_SCRIPT
71 virtual nsresult DragCaret(const nsPoint& aPoint);
73 // Release caret from he previous press action. It's required to call
74 // PressCaret() beforehand.
75 MOZ_CAN_RUN_SCRIPT
76 virtual nsresult ReleaseCaret();
78 // A quick single tap on caret on given point without dragging.
79 MOZ_CAN_RUN_SCRIPT
80 virtual nsresult TapCaret(const nsPoint& aPoint);
82 // Select a word or bring up paste shortcut (if Gaia is listening) under the
83 // given point.
84 MOZ_CAN_RUN_SCRIPT
85 virtual nsresult SelectWordOrShortcut(const nsPoint& aPoint);
87 // Handle scroll-start event.
88 MOZ_CAN_RUN_SCRIPT
89 virtual void OnScrollStart();
91 // Handle scroll-end event.
92 MOZ_CAN_RUN_SCRIPT
93 virtual void OnScrollEnd();
95 // Handle ScrollPositionChanged from nsIScrollObserver. This might be called
96 // at anytime, not necessary between OnScrollStart and OnScrollEnd.
97 MOZ_CAN_RUN_SCRIPT
98 virtual void OnScrollPositionChanged();
100 // Handle reflow event from nsIReflowObserver.
101 MOZ_CAN_RUN_SCRIPT
102 virtual void OnReflow();
104 // Handle blur event from nsFocusManager.
105 MOZ_CAN_RUN_SCRIPT
106 virtual void OnBlur();
108 // Handle NotifySelectionChanged event from nsISelectionListener.
109 // @param aReason potentially multiple of the reasons defined in
110 // nsISelectionListener.idl.
111 MOZ_CAN_RUN_SCRIPT
112 virtual nsresult OnSelectionChanged(dom::Document* aDoc, dom::Selection* aSel,
113 int16_t aReason);
114 // Handle key event.
115 MOZ_CAN_RUN_SCRIPT
116 virtual void OnKeyboardEvent();
118 // The canvas frame holding the accessible caret anonymous content elements
119 // was reconstructed, resulting in the content elements getting cloned.
120 virtual void OnFrameReconstruction();
122 // Update the manager with the last input source that was observed. This
123 // is used in part to determine if the carets should be shown or hidden.
124 void SetLastInputSource(uint16_t aInputSource);
126 // Returns True indicating that we should disable APZ to avoid jumpy carets.
127 bool ShouldDisableApz() const;
129 protected:
130 // This enum representing the number of AccessibleCarets on the screen.
131 enum class CaretMode : uint8_t {
132 // No caret on the screen.
133 None,
135 // One caret, i.e. the selection is collapsed.
136 Cursor,
138 // Two carets, i.e. the selection is not collapsed.
139 Selection
142 friend std::ostream& operator<<(std::ostream& aStream,
143 const CaretMode& aCaretMode);
145 enum class UpdateCaretsHint : uint8_t {
146 // Update everything including appearance and position.
147 Default,
149 // Update everything while respecting the old appearance. For example, if
150 // the caret in cursor mode is hidden due to blur, do not change its
151 // appearance to Normal.
152 RespectOldAppearance,
154 // No CaretStateChangedEvent will be dispatched in the end of
155 // UpdateCarets().
156 DispatchNoEvent,
159 using UpdateCaretsHintSet = mozilla::EnumSet<UpdateCaretsHint>;
161 friend std::ostream& operator<<(std::ostream& aStream,
162 const UpdateCaretsHint& aResult);
164 enum class Terminated : bool { No, Yes };
166 // This method could kill the shell, so callers to methods that call
167 // MaybeFlushLayout should ensure the event hub that owns us is still alive.
169 // See the mRefCnt assertions in AccessibleCaretEventHub.
171 [[nodiscard]] MOZ_CAN_RUN_SCRIPT virtual Terminated MaybeFlushLayout();
173 // Update carets based on current selection status. This function will flush
174 // layout, so caller must ensure the PresShell is still valid after calling
175 // this method.
176 MOZ_CAN_RUN_SCRIPT
177 void UpdateCarets(
178 const UpdateCaretsHintSet& aHints = UpdateCaretsHint::Default);
180 // Force hiding all carets regardless of the current selection status, and
181 // dispatch CaretStateChangedEvent if one of the carets is logically-visible.
182 MOZ_CAN_RUN_SCRIPT
183 void HideCaretsAndDispatchCaretStateChangedEvent();
185 MOZ_CAN_RUN_SCRIPT
186 void UpdateCaretsForCursorMode(const UpdateCaretsHintSet& aHints);
188 MOZ_CAN_RUN_SCRIPT
189 void UpdateCaretsForSelectionMode(const UpdateCaretsHintSet& aHints);
191 // A helper function to update mShouldDisableApz.
192 void UpdateShouldDisableApz();
194 // Provide haptic / touch feedback, primarily for select on longpress.
195 void ProvideHapticFeedback();
197 // Get the nearest enclosing focusable frame of aFrame.
198 // @return focusable frame if there is any; nullptr otherwise.
199 nsIFrame* GetFocusableFrame(nsIFrame* aFrame) const;
201 // Change focus to aFrame if it isn't nullptr. Otherwise, clear the old focus
202 // then re-focus the window.
203 MOZ_CAN_RUN_SCRIPT_BOUNDARY void ChangeFocusToOrClearOldFocus(
204 nsIFrame* aFrame) const;
206 MOZ_CAN_RUN_SCRIPT
207 nsresult SelectWord(nsIFrame* aFrame, const nsPoint& aPoint) const;
208 MOZ_CAN_RUN_SCRIPT void SetSelectionDragState(bool aState) const;
210 // Return true if the candidate string is a phone number.
211 bool IsPhoneNumber(nsAString& aCandidate) const;
213 // Extend the current selection forwards and backwards if it's already a
214 // phone number.
215 MOZ_CAN_RUN_SCRIPT
216 void SelectMoreIfPhoneNumber() const;
218 // Extend the current phone number selection in the requested direction.
219 MOZ_CAN_RUN_SCRIPT
220 void ExtendPhoneNumberSelection(const nsAString& aDirection) const;
222 void SetSelectionDirection(nsDirection aDir) const;
224 // If aDirection is eDirNext, get the frame for the range start in the first
225 // range from the current selection, and return the offset into that frame as
226 // well as the range start content and the content offset. Otherwise, get the
227 // frame and the offset for the range end in the last range instead.
228 nsIFrame* GetFrameForFirstRangeStartOrLastRangeEnd(
229 nsDirection aDirection, int32_t* aOutOffset,
230 nsIContent** aOutContent = nullptr,
231 int32_t* aOutContentOffset = nullptr) const;
233 MOZ_CAN_RUN_SCRIPT nsresult DragCaretInternal(const nsPoint& aPoint);
234 nsPoint AdjustDragBoundary(const nsPoint& aPoint) const;
236 // Start the selection scroll timer if the caret is being dragged out of
237 // the scroll port.
238 MOZ_CAN_RUN_SCRIPT
239 void StartSelectionAutoScrollTimer(const nsPoint& aPoint) const;
240 void StopSelectionAutoScrollTimer() const;
242 void ClearMaintainedSelection() const;
244 static dom::Element* GetEditingHostForFrame(const nsIFrame* aFrame);
246 dom::Selection* GetSelection() const;
247 static dom::Selection* GetSelection(PresShell& aPresShell);
249 already_AddRefed<nsFrameSelection> GetFrameSelection() const;
250 static already_AddRefed<nsFrameSelection> GetFrameSelection(
251 PresShell& aPresShell);
253 class LayoutFlusher final {
254 public:
255 ~LayoutFlusher();
257 MOZ_CAN_RUN_SCRIPT void MaybeFlush(const PresShell& aPresShell);
259 // Set to false to disallow flushing layout in some callbacks such as
260 // OnReflow(), OnScrollStart(), OnScrollStart(), or
261 // OnScrollPositionChanged().
262 bool mAllowFlushing = true;
264 private:
265 // Whether we're flushing layout, used for sanity-checking.
266 bool mFlushing = false;
269 LayoutFlusher mLayoutFlusher;
271 MOZ_CAN_RUN_SCRIPT nsAutoString StringifiedSelection() const;
273 class SelectionStringifyer final {
274 public:
275 explicit SelectionStringifyer(LayoutFlusher& aLayoutFlusher)
276 : mLayoutFlusher{aLayoutFlusher} {}
278 [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsAutoString
279 Stringify(dom::Selection& aSelection) const;
281 private:
282 LayoutFlusher mLayoutFlusher;
285 SelectionStringifyer mSelectionStringifyer;
287 // Get the union of all the child frame scrollable overflow rects for aFrame,
288 // which is used as a helper function to restrict the area where the caret can
289 // be dragged. Returns the rect relative to aFrame.
290 nsRect GetAllChildFrameRectsUnion(nsIFrame* aFrame) const;
292 // Restrict the active caret's dragging position based on
293 // sCaretsAllowDraggingAcrossOtherCaret. If the active caret is the first
294 // caret, the `limit` will be the previous character of the second caret.
295 // Otherwise, the `limit` will be the next character of the first caret.
297 // @param aOffsets is the new position of the active caret, and it will be set
298 // to the `limit` when 1) sCaretsAllowDraggingAcrossOtherCaret is false and
299 // it's being dragged past the limit. 2) sCaretsAllowDraggingAcrossOtherCaret
300 // is true and the active caret's position is the same as the inactive's
301 // position.
302 // @return true if the aOffsets is suitable for changing the selection.
303 bool RestrictCaretDraggingOffsets(nsIFrame::ContentOffsets& aOffsets);
305 // ---------------------------------------------------------------------------
306 // The following functions are made virtual for stubbing or mocking in gtest.
308 // @return Yes if Terminate() had been called.
309 virtual Terminated IsTerminated() const {
310 return mPresShell ? Terminated::No : Terminated::Yes;
313 // Get caret mode based on current selection.
314 virtual CaretMode GetCaretMode() const;
316 // @return true if aStartFrame comes before aEndFrame.
317 virtual bool CompareTreePosition(nsIFrame* aStartFrame,
318 nsIFrame* aEndFrame) const;
320 // Check if the two carets is overlapping to become tilt.
321 // @return true if the two carets become tilt; false, otherwise.
322 virtual bool UpdateCaretsForOverlappingTilt();
324 // Make the two carets always tilt.
325 virtual void UpdateCaretsForAlwaysTilt(const nsIFrame* aStartFrame,
326 const nsIFrame* aEndFrame);
328 // Check whether AccessibleCaret is displayable in cursor mode or not.
329 // @param aOutFrame returns frame of the cursor if it's displayable.
330 // @param aOutOffset returns frame offset as well.
331 virtual bool IsCaretDisplayableInCursorMode(
332 nsIFrame** aOutFrame = nullptr, int32_t* aOutOffset = nullptr) const;
334 virtual bool HasNonEmptyTextContent(nsINode* aNode) const;
336 // This function will flush layout, so caller must ensure the PresShell is
337 // still valid after calling this method.
338 MOZ_CAN_RUN_SCRIPT
339 virtual void DispatchCaretStateChangedEvent(dom::CaretChangedReason aReason);
341 // ---------------------------------------------------------------------------
342 // Member variables
344 nscoord mOffsetYToCaretLogicalPosition = NS_UNCONSTRAINEDSIZE;
346 // AccessibleCaretEventHub owns us by a UniquePtr. When it's destroyed, we'll
347 // also be destroyed. No need to worry if we outlive mPresShell.
349 // mPresShell will be set to nullptr in Terminate(). Therefore mPresShell is
350 // nullptr either we are in gtest or PresShell::IsDestroying() is true.
351 PresShell* MOZ_NON_OWNING_REF mPresShell = nullptr;
353 class Carets {
354 public:
355 // See `AccessibleCaret::IsLogicallyVisible`.
356 bool AreLogicallyVisible() const {
357 return mFirst->IsLogicallyVisible() || mSecond->IsLogicallyVisible();
360 // See `AccessibleCaret::IsVisuallyVisible`.
361 bool AreVisuallyVisible() const {
362 return mFirst->IsVisuallyVisible() || mSecond->IsVisuallyVisible();
365 AccessibleCaret* GetFirst() const { return mFirst.get(); }
367 AccessibleCaret* GetSecond() const { return mSecond.get(); }
369 // First caret is attached to nsCaret in cursor mode, and is attached to
370 // selection highlight as the left caret in selection mode.
371 UniquePtr<AccessibleCaret> mFirst;
373 // Second caret is used solely in selection mode, and is attached to
374 // selection highlight as the right caret.
375 UniquePtr<AccessibleCaret> mSecond;
378 Carets mCarets;
380 // The caret being pressed or dragged.
381 AccessibleCaret* mActiveCaret = nullptr;
383 // The caret mode since last update carets.
384 CaretMode mLastUpdateCaretMode = CaretMode::None;
386 // The last input source that the event hub saw. We use this to decide whether
387 // or not show the carets when the selection is updated, as we want to hide
388 // the carets for mouse-triggered selection changes but show them for other
389 // input types such as touch.
390 uint16_t mLastInputSource = dom::MouseEvent_Binding::MOZ_SOURCE_UNKNOWN;
392 // Set to true in OnScrollStart() and set to false in OnScrollEnd().
393 bool mIsScrollStarted = false;
395 // Set to True if one of the caret's position is changed in last update.
396 bool mIsCaretPositionChanged = false;
398 class DesiredAsyncPanZoomState final {
399 public:
400 void Update(const AccessibleCaretManager& aAccessibleCaretManager);
402 enum class Value : bool { Disabled, Enabled };
404 Value Get() const { return mValue; }
406 private:
407 Value mValue = Value::Enabled;
410 DesiredAsyncPanZoomState mDesiredAsyncPanZoomState;
412 static const int32_t kAutoScrollTimerDelay = 30;
414 // Clicking on the boundary of input or textarea will move the caret to the
415 // front or end of the content. To avoid this, we need to deflate the content
416 // boundary by 61 app units, which is 1 pixel + 1 app unit as defined in
417 // AppUnit.h.
418 static const int32_t kBoundaryAppUnits = 61;
420 enum ScriptUpdateMode : int32_t {
421 // By default, always hide carets for selection changes due to JS calls.
422 kScriptAlwaysHide,
423 // Update any visible carets for selection changes due to JS calls,
424 // but don't show carets if carets are hidden.
425 kScriptUpdateVisible,
426 // Always show carets for selection changes due to JS calls.
427 kScriptAlwaysShow
431 std::ostream& operator<<(std::ostream& aStream,
432 const AccessibleCaretManager::CaretMode& aCaretMode);
434 std::ostream& operator<<(
435 std::ostream& aStream,
436 const AccessibleCaretManager::UpdateCaretsHint& aResult);
438 } // namespace mozilla
440 #endif // AccessibleCaretManager_h