tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

APZPublicUtils.cpp (4105B)


      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/. */
      6 
      7 #include "mozilla/layers/APZPublicUtils.h"
      8 
      9 #include "AsyncPanZoomController.h"
     10 #include "nsLayoutUtils.h"
     11 #include "mozilla/StaticPrefs_general.h"
     12 
     13 namespace mozilla {
     14 namespace layers {
     15 
     16 namespace apz {
     17 
     18 /*static*/ void InitializeGlobalState() {
     19  MOZ_ASSERT(NS_IsMainThread());
     20  AsyncPanZoomController::InitializeGlobalState();
     21 }
     22 
     23 /*static*/ const ScreenMargin CalculatePendingDisplayPort(
     24    const FrameMetrics& aFrameMetrics, const ParentLayerPoint& aVelocity) {
     25  return AsyncPanZoomController::CalculatePendingDisplayPort(
     26      aFrameMetrics, aVelocity, AsyncPanZoomController::ZoomInProgress::No);
     27 }
     28 
     29 /*static*/ gfx::Size GetDisplayportAlignmentMultiplier(
     30    const ScreenSize& aBaseSize) {
     31  return AsyncPanZoomController::GetDisplayportAlignmentMultiplier(aBaseSize);
     32 }
     33 
     34 ScrollAnimationBezierPhysicsSettings ComputeBezierAnimationSettingsForOrigin(
     35    ScrollOrigin aOrigin) {
     36  int32_t minMS = 0;
     37  int32_t maxMS = 0;
     38  bool isOriginSmoothnessEnabled = false;
     39  bool isGeneralSmoothnessEnabled = nsLayoutUtils::IsSmoothScrollingEnabled();
     40 
     41 #define READ_DURATIONS(prefbase)                                              \
     42  isOriginSmoothnessEnabled = isGeneralSmoothnessEnabled &&                   \
     43                              StaticPrefs::general_smoothScroll_##prefbase(); \
     44  if (isOriginSmoothnessEnabled) {                                            \
     45    minMS = StaticPrefs::general_smoothScroll_##prefbase##_durationMinMS();   \
     46    maxMS = StaticPrefs::general_smoothScroll_##prefbase##_durationMaxMS();   \
     47  }
     48 
     49  switch (aOrigin) {
     50    case ScrollOrigin::Pixels:
     51      READ_DURATIONS(pixels)
     52      break;
     53    case ScrollOrigin::Lines:
     54      READ_DURATIONS(lines)
     55      break;
     56    case ScrollOrigin::Pages:
     57      READ_DURATIONS(pages)
     58      break;
     59    case ScrollOrigin::MouseWheel:
     60      READ_DURATIONS(mouseWheel)
     61      break;
     62    case ScrollOrigin::Scrollbars:
     63      READ_DURATIONS(scrollbars)
     64      break;
     65    default:
     66      READ_DURATIONS(other)
     67      break;
     68  }
     69 
     70 #undef READ_DURATIONS
     71 
     72  if (isOriginSmoothnessEnabled) {
     73    static const int32_t kSmoothScrollMaxAllowedAnimationDurationMS = 10000;
     74    maxMS = std::clamp(maxMS, 0, kSmoothScrollMaxAllowedAnimationDurationMS);
     75    minMS = std::clamp(minMS, 0, maxMS);
     76  }
     77 
     78  // Keep the animation duration longer than the average event intervals
     79  // (to "connect" consecutive scroll animations before the scroll comes to a
     80  // stop).
     81  double intervalRatio =
     82      ((double)StaticPrefs::general_smoothScroll_durationToIntervalRatio()) /
     83      100.0;
     84 
     85  // Duration should be at least as long as the intervals -> ratio is at least 1
     86  intervalRatio = std::max(1.0, intervalRatio);
     87 
     88  return ScrollAnimationBezierPhysicsSettings{minMS, maxMS, intervalRatio};
     89 }
     90 
     91 ScrollMode GetScrollModeForOrigin(ScrollOrigin origin) {
     92  bool isSmoothScrollingEnabled = nsLayoutUtils::IsSmoothScrollingEnabled();
     93  if (!isSmoothScrollingEnabled) return ScrollMode::Instant;
     94  switch (origin) {
     95    case ScrollOrigin::Lines:
     96      return StaticPrefs::general_smoothScroll_lines() ? ScrollMode::Smooth
     97                                                       : ScrollMode::Instant;
     98    case ScrollOrigin::Pages:
     99      return StaticPrefs::general_smoothScroll_pages() ? ScrollMode::Smooth
    100                                                       : ScrollMode::Instant;
    101    case ScrollOrigin::Other:
    102      return StaticPrefs::general_smoothScroll_other() ? ScrollMode::Smooth
    103                                                       : ScrollMode::Instant;
    104    default:
    105      MOZ_ASSERT(false, "Unknown keyboard scroll origin");
    106      return isSmoothScrollingEnabled ? ScrollMode::Smooth
    107                                      : ScrollMode::Instant;
    108  }
    109 }
    110 
    111 }  // namespace apz
    112 }  // namespace layers
    113 }  // namespace mozilla