Backed out changeset 496886cb30a5 (bug 1867152) for bc failures on browser_user_input...
[gecko.git] / layout / generic / ScrollSnap.h
bloba599c0507b38f0ed60a8e53e176b66079854d15a
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 mozilla_layout_ScrollSnap_h_
8 #define mozilla_layout_ScrollSnap_h_
10 #include <memory>
11 #include "mozilla/ScrollTypes.h"
12 #include "mozilla/ScrollSnapTargetId.h"
13 #include "mozilla/Maybe.h"
15 class nsIContent;
16 class nsIFrame;
17 struct nsPoint;
18 struct nsRect;
19 struct nsSize;
21 namespace mozilla {
23 struct ScrollSnapInfo;
25 struct ScrollSnapUtils {
26 /**
27 * GetSnapPointForDestination determines which point to snap to after
28 * scrolling. |aStartPos| gives the position before scrolling and
29 * |aDestination| gives the position after scrolling, with no snapping.
30 * Behaviour is dependent on the value of |aUnit|.
31 * |aSnapInfo| and |aScrollRange| are characteristics of the scroll frame for
32 * which snapping is being performed.
33 * If a suitable snap point could be found, it is returned. Otherwise, an
34 * empty Maybe is returned.
35 * IMPORTANT NOTE: This function is designed to be called both on and off
36 * the main thread. If modifying its implementation, be sure
37 * not to touch main-thread-only data structures without
38 * appropriate locking.
40 static Maybe<SnapDestination> GetSnapPointForDestination(
41 const ScrollSnapInfo& aSnapInfo, ScrollUnit aUnit,
42 ScrollSnapFlags aSnapFlags, const nsRect& aScrollRange,
43 const nsPoint& aStartPos, const nsPoint& aDestination);
45 /**
46 * Similar to above GetSnapPointForDestination but for re-snapping.
48 * |aCurrentPosition| are the snap point(s) last time when we scrolled.
49 * |aLastSnapTargetIds| are the snap point(s) last time when we scrolled if
50 * exists.
51 * |aFocusedContent| is the focused content in the document if exists.
52 * Other parameters are same as GetSnapPointForDestination.
55 static mozilla::Maybe<SnapDestination> GetSnapPointForResnap(
56 const ScrollSnapInfo& aSnapInfo, const nsRect& aScrollRange,
57 const nsPoint& aCurrentPosition,
58 const UniquePtr<ScrollSnapTargetIds>& aLastSnapTargetIds,
59 const nsIContent* aFocusedContent);
61 static ScrollSnapTargetId GetTargetIdFor(const nsIFrame* aFrame);
63 // Post a pending re-snap request if the given |aFrame| is one of the snap
64 // points on the last scroll operation.
65 static void PostPendingResnapIfNeededFor(nsIFrame* aFrame);
67 // Similar to above PostPendingResnapIfNeededFor but post a pending re-snap
68 // request even if the given |aFrame| is not one of the last snap point.
69 // This is basically used for cases there was no valid snap point on the last
70 // scroll operation but the given |aFrame| might be a valid snap point now,
71 // e.g changing the scroll-snap-align property from `none` to something.
72 static void PostPendingResnapFor(nsIFrame* aFrame);
74 // Returns true if the writing-mode of the snap target element needs to be
75 // respected to resolve scroll-snap-align property.
76 // Note that usually the scroll container's writing-mode is used for resolving
77 // the property but there's a special case defined in the CSS scroll snap
78 // spec.
79 static bool NeedsToRespectTargetWritingMode(const nsSize& aSnapAreaSize,
80 const nsSize& aSnapportSize);
82 // Returns the scroll snap area for the snap target frame |aFrame| inside the
83 // nearest scroll container |aScrolledFrame| and its scrolled rect
84 // |aScrolledRect|.
85 static nsRect GetSnapAreaFor(const nsIFrame* aFrame,
86 const nsIFrame* aScrolledFrame,
87 const nsRect& aScrolledRect);
90 } // namespace mozilla
92 #endif // mozilla_layout_ScrollSnap_h_