Bug 1717887 Part 2: Make RenderThread backed by nsIThread, with a hang monitor. r...
[gecko.git] / layout / base / MobileViewportManager.h
blob4d49f55406747722b1e0224a529ca170b0a2709c
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 MobileViewportManager_h_
8 #define MobileViewportManager_h_
10 #include "mozilla/Maybe.h"
11 #include "mozilla/MVMContext.h"
12 #include "mozilla/PresShellForwards.h"
13 #include "nsCOMPtr.h"
14 #include "nsIDOMEventListener.h"
15 #include "nsIObserver.h"
16 #include "Units.h"
18 class nsViewportInfo;
20 namespace mozilla {
21 class MVMContext;
22 namespace dom {
23 class Document;
24 class EventTarget;
25 } // namespace dom
26 } // namespace mozilla
28 class MobileViewportManager final : public nsIDOMEventListener,
29 public nsIObserver {
30 public:
31 NS_DECL_ISUPPORTS
32 NS_DECL_NSIDOMEVENTLISTENER
33 NS_DECL_NSIOBSERVER
35 /* The MobileViewportManager might be required to handle meta-viewport tags
36 * and changes, or it might not (e.g. if we are in a desktop-zooming setup).
37 * This enum indicates which mode the manager is in. It might make sense to
38 * split these two "modes" into two separate classes but for now they have a
39 * bunch of shared code and it's uncertain if that shared set will expand or
40 * contract. */
41 enum class ManagerType { VisualAndMetaViewport, VisualViewportOnly };
43 explicit MobileViewportManager(mozilla::MVMContext* aContext,
44 ManagerType aType);
45 void Destroy();
47 ManagerType GetManagerType() { return mManagerType; }
49 /* Provide a resolution to use during the first paint instead of the default
50 * resolution computed from the viewport info metadata. This is in the same
51 * "units" as the argument to nsDOMWindowUtils::SetResolutionAndScaleTo.
52 * Also takes the previous display dimensions as they were at the time the
53 * resolution was stored in order to correctly adjust the resolution if the
54 * device was rotated in the meantime. */
55 void SetRestoreResolution(float aResolution,
56 mozilla::LayoutDeviceIntSize aDisplaySize);
58 /* Compute the "intrinsic resolution", which is the smallest resolution at
59 * which the layout viewport fills the visual viewport. (In typical
60 * scenarios, where the aspect ratios of the two viewports match, it's the
61 * resolution at which they are the same size.)
63 * The returned resolution is suitable for passing to
64 * PresShell::SetResolutionAndScaleTo(). It's not in typed units for
65 * reasons explained at the declaration of FrameMetrics::mPresShellResolution.
67 float ComputeIntrinsicResolution() const;
69 /* The only direct calls to this should be in test code.
70 * Normally, it gets called by HandleEvent().
72 void HandleDOMMetaAdded();
74 private:
75 void SetRestoreResolution(float aResolution);
77 public:
78 /* Notify the MobileViewportManager that a reflow is about to happen. This
79 * triggers the MVM to update its internal notion of display size and CSS
80 * viewport, so that code that queries those during the reflow gets an
81 * up-to-date value.
83 void UpdateSizesBeforeReflow();
85 /* Notify the MobileViewportManager that a reflow was requested in the
86 * presShell.*/
87 void RequestReflow(bool aForceAdjustResolution);
89 /* Notify the MobileViewportManager that the resolution on the presShell was
90 * updated, and the visual viewport size needs to be updated. */
91 void ResolutionUpdated(mozilla::ResolutionChangeOrigin aOrigin);
93 /* Called to compute the initial viewport on page load or before-first-paint,
94 * whichever happens first. Also called directly if we are created after the
95 * presShell is initialized. */
96 void SetInitialViewport();
98 const mozilla::LayoutDeviceIntSize& DisplaySize() const {
99 return mDisplaySize;
103 * Shrink the content to fit it to the display width if no initial-scale is
104 * specified and if the content is still wider than the display width.
106 void ShrinkToDisplaySizeIfNeeded();
109 * Similar to UpdateVisualViewportSize but this should be called only when we
110 * need to update visual viewport size in response to dynamic toolbar
111 * transitions.
112 * This function doesn't cause any reflows, just fires a visual viewport
113 * resize event.
115 void UpdateVisualViewportSizeByDynamicToolbar(
116 mozilla::ScreenIntCoord aToolbarHeight);
118 nsSize GetVisualViewportSizeUpdatedByDynamicToolbar() const {
119 return mVisualViewportSizeUpdatedByDynamicToolbar;
123 * This refreshes the visual viewport size based on the most recently
124 * available information. It is intended to be called in particular after
125 * the root scrollframe does a reflow, which may make scrollbars appear or
126 * disappear if the content changed size.
128 void UpdateVisualViewportSizeForPotentialScrollbarChange();
131 * Returns the composition size in CSS units when zoomed to the intrinsic
132 * scale.
134 mozilla::CSSSize GetIntrinsicCompositionSize() const;
136 mozilla::ParentLayerSize GetCompositionSizeWithoutDynamicToolbar() const;
138 private:
139 ~MobileViewportManager();
141 /* Main helper method to update the CSS viewport and any other properties that
142 * need updating. */
143 void RefreshViewportSize(bool aForceAdjustResolution);
145 /* Secondary main helper method to update just the visual viewport size. */
146 void RefreshVisualViewportSize();
148 /* Helper to clamp the given zoom by the min/max in the viewport info. */
149 mozilla::CSSToScreenScale ClampZoom(
150 const mozilla::CSSToScreenScale& aZoom,
151 const nsViewportInfo& aViewportInfo) const;
153 /* Helper to update the given zoom according to changed display and viewport
154 * widths. */
155 mozilla::CSSToScreenScale ScaleZoomWithDisplayWidth(
156 const mozilla::CSSToScreenScale& aZoom,
157 const float& aDisplayWidthChangeRatio,
158 const mozilla::CSSSize& aNewViewport,
159 const mozilla::CSSSize& aOldViewport);
161 mozilla::CSSToScreenScale ResolutionToZoom(
162 const mozilla::LayoutDeviceToLayerScale& aResolution) const;
163 mozilla::LayoutDeviceToLayerScale ZoomToResolution(
164 const mozilla::CSSToScreenScale& aZoom) const;
166 /* Updates the presShell resolution and the visual viewport size for various
167 * types of changes. */
168 void UpdateResolutionForFirstPaint(const mozilla::CSSSize& aViewportSize);
169 void UpdateResolutionForViewportSizeChange(
170 const mozilla::CSSSize& aViewportSize,
171 const mozilla::Maybe<float>& aDisplayWidthChangeRatio);
172 void UpdateResolutionForContentSizeChange(
173 const mozilla::CSSSize& aContentSize);
175 void ApplyNewZoom(const mozilla::ScreenIntSize& aDisplaySize,
176 const mozilla::CSSToScreenScale& aNewZoom);
178 void UpdateVisualViewportSize(const mozilla::ScreenIntSize& aDisplaySize,
179 const mozilla::CSSToScreenScale& aZoom);
181 /* Updates the displayport margins for the presShell's root scrollable frame
183 void UpdateDisplayPortMargins();
185 /* Helper function for ComputeIntrinsicResolution(). */
186 mozilla::CSSToScreenScale ComputeIntrinsicScale(
187 const nsViewportInfo& aViewportInfo,
188 const mozilla::ScreenIntSize& aDisplaySize,
189 const mozilla::CSSSize& aViewportOrContentSize) const;
192 * Returns the screen size subtracted the scrollbar sizes from |aDisplaySize|.
194 mozilla::ScreenIntSize GetCompositionSize(
195 const mozilla::ScreenIntSize& aDisplaySize) const;
197 mozilla::CSSToScreenScale GetZoom() const;
199 RefPtr<mozilla::MVMContext> mContext;
200 ManagerType mManagerType;
201 bool mIsFirstPaint;
202 bool mPainted;
203 mozilla::LayoutDeviceIntSize mDisplaySize;
204 mozilla::CSSSize mMobileViewportSize;
205 mozilla::Maybe<float> mRestoreResolution;
206 mozilla::Maybe<mozilla::ScreenIntSize> mRestoreDisplaySize;
208 * The visual viewport size updated by the dynamic toolbar transitions. This
209 * is typically used for the VisualViewport width/height APIs.
210 * NOTE: If you want to use this value, you should make sure to flush
211 * position:fixed elements layout and update
212 * FrameMetrics.mFixedLayerMargins to conform with this value.
214 nsSize mVisualViewportSizeUpdatedByDynamicToolbar;
217 #endif