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 "TouchActionHelper.h"
8 
9 #include "mozilla/layers/IAPZCTreeManager.h"
10 #include "nsContainerFrame.h"
11 #include "nsIScrollableFrame.h"
12 #include "nsLayoutUtils.h"
13 
14 namespace mozilla {
15 namespace layers {
16 
UpdateAllowedBehavior(uint32_t aTouchActionValue,bool aConsiderPanning,TouchBehaviorFlags & aOutBehavior)17 void TouchActionHelper::UpdateAllowedBehavior(
18     uint32_t aTouchActionValue, bool aConsiderPanning,
19     TouchBehaviorFlags& aOutBehavior) {
20   if (aTouchActionValue != NS_STYLE_TOUCH_ACTION_AUTO) {
21     // Double-tap-zooming need property value AUTO
22     aOutBehavior &= ~AllowedTouchBehavior::DOUBLE_TAP_ZOOM;
23     if (aTouchActionValue != NS_STYLE_TOUCH_ACTION_MANIPULATION) {
24       // Pinch-zooming need value AUTO or MANIPULATION
25       aOutBehavior &= ~AllowedTouchBehavior::PINCH_ZOOM;
26     }
27   }
28 
29   if (aConsiderPanning) {
30     if (aTouchActionValue == NS_STYLE_TOUCH_ACTION_NONE) {
31       aOutBehavior &= ~AllowedTouchBehavior::VERTICAL_PAN;
32       aOutBehavior &= ~AllowedTouchBehavior::HORIZONTAL_PAN;
33     }
34 
35     // Values pan-x and pan-y set at the same time to the same element do not
36     // affect panning constraints. Therefore we need to check whether pan-x is
37     // set without pan-y and the same for pan-y.
38     if ((aTouchActionValue & NS_STYLE_TOUCH_ACTION_PAN_X) &&
39         !(aTouchActionValue & NS_STYLE_TOUCH_ACTION_PAN_Y)) {
40       aOutBehavior &= ~AllowedTouchBehavior::VERTICAL_PAN;
41     } else if ((aTouchActionValue & NS_STYLE_TOUCH_ACTION_PAN_Y) &&
42                !(aTouchActionValue & NS_STYLE_TOUCH_ACTION_PAN_X)) {
43       aOutBehavior &= ~AllowedTouchBehavior::HORIZONTAL_PAN;
44     }
45   }
46 }
47 
GetAllowedTouchBehavior(nsIWidget * aWidget,nsIFrame * aRootFrame,const LayoutDeviceIntPoint & aPoint)48 TouchBehaviorFlags TouchActionHelper::GetAllowedTouchBehavior(
49     nsIWidget* aWidget, nsIFrame* aRootFrame,
50     const LayoutDeviceIntPoint& aPoint) {
51   TouchBehaviorFlags behavior = AllowedTouchBehavior::VERTICAL_PAN |
52                                 AllowedTouchBehavior::HORIZONTAL_PAN |
53                                 AllowedTouchBehavior::PINCH_ZOOM |
54                                 AllowedTouchBehavior::DOUBLE_TAP_ZOOM;
55 
56   nsPoint relativePoint =
57       nsLayoutUtils::GetEventCoordinatesRelativeTo(aWidget, aPoint, aRootFrame);
58 
59   nsIFrame* target = nsLayoutUtils::GetFrameForPoint(
60       aRootFrame, relativePoint, nsLayoutUtils::IGNORE_ROOT_SCROLL_FRAME);
61   if (!target) {
62     return behavior;
63   }
64   nsIScrollableFrame* nearestScrollableParent =
65       nsLayoutUtils::GetNearestScrollableFrame(target, 0);
66   nsIFrame* nearestScrollableFrame = do_QueryFrame(nearestScrollableParent);
67 
68   // We're walking up the DOM tree until we meet the element with touch behavior
69   // and accumulating touch-action restrictions of all elements in this chain.
70   // The exact quote from the spec, that clarifies more:
71   // To determine the effect of a touch, find the nearest ancestor (starting
72   // from the element itself) that has a default touch behavior. Then examine
73   // the touch-action property of each element between the hit tested element
74   // and the element with the default touch behavior (including both the hit
75   // tested element and the element with the default touch behavior). If the
76   // touch-action property of any of those elements disallows the default touch
77   // behavior, do nothing. Otherwise allow the element to start considering the
78   // touch for the purposes of executing a default touch behavior.
79 
80   // Currently we support only two touch behaviors: panning and zooming.
81   // For panning we walk up until we meet the first scrollable element (the
82   // element that supports panning) or root element. For zooming we walk up
83   // until the root element since Firefox currently supports only zooming of the
84   // root frame but not the subframes.
85 
86   bool considerPanning = true;
87 
88   for (nsIFrame* frame = target; frame && frame->GetContent() && behavior;
89        frame = frame->GetParent()) {
90     UpdateAllowedBehavior(nsLayoutUtils::GetTouchActionFromFrame(frame),
91                           considerPanning, behavior);
92 
93     if (frame == nearestScrollableFrame) {
94       // We met the scrollable element, after it we shouldn't consider
95       // touch-action values for the purpose of panning but only for zooming.
96       considerPanning = false;
97     }
98   }
99 
100   return behavior;
101 }
102 
103 }  // namespace layers
104 }  // namespace mozilla
105