michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ContentHelper.h" michael@0: #include "nsQueryFrame.h" michael@0: #include "nsIContent.h" michael@0: #include "nsIScrollableFrame.h" michael@0: #include "nsLayoutUtils.h" michael@0: #include "nsStyleConsts.h" michael@0: #include "nsView.h" michael@0: michael@0: namespace mozilla { michael@0: namespace widget { michael@0: michael@0: uint32_t michael@0: ContentHelper::GetTouchActionFromFrame(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame || !aFrame->GetContent() || !aFrame->GetContent()->GetPrimaryFrame()) { michael@0: // If frame is invalid or null then return default value. michael@0: return NS_STYLE_TOUCH_ACTION_AUTO; michael@0: } michael@0: michael@0: if (!aFrame->IsFrameOfType(nsIFrame::eSVG) && !aFrame->IsFrameOfType(nsIFrame::eBlockFrame)) { michael@0: // Since touch-action property can be applied to only svg and block-level michael@0: // elements we ignore frames of other types. michael@0: return NS_STYLE_TOUCH_ACTION_AUTO; michael@0: } michael@0: michael@0: return (aFrame->GetContent()->GetPrimaryFrame()->StyleDisplay()->mTouchAction); michael@0: } michael@0: michael@0: void michael@0: ContentHelper::UpdateAllowedBehavior(uint32_t aTouchActionValue, bool aConsiderPanning, TouchBehaviorFlags& aOutBehavior) michael@0: { michael@0: if (aTouchActionValue != NS_STYLE_TOUCH_ACTION_AUTO) { michael@0: // Double-tap-zooming need property value AUTO michael@0: aOutBehavior &= ~AllowedTouchBehavior::DOUBLE_TAP_ZOOM; michael@0: if (aTouchActionValue != NS_STYLE_TOUCH_ACTION_MANIPULATION) { michael@0: // Pinch-zooming need value AUTO or MANIPULATION michael@0: aOutBehavior &= ~AllowedTouchBehavior::PINCH_ZOOM; michael@0: } michael@0: } michael@0: michael@0: if (aConsiderPanning) { michael@0: if (aTouchActionValue == NS_STYLE_TOUCH_ACTION_NONE) { michael@0: aOutBehavior &= ~AllowedTouchBehavior::VERTICAL_PAN; michael@0: aOutBehavior &= ~AllowedTouchBehavior::HORIZONTAL_PAN; michael@0: } michael@0: michael@0: // Values pan-x and pan-y set at the same time to the same element do not affect panning constraints. michael@0: // Therefore we need to check whether pan-x is set without pan-y and the same for pan-y. michael@0: if ((aTouchActionValue & NS_STYLE_TOUCH_ACTION_PAN_X) && !(aTouchActionValue & NS_STYLE_TOUCH_ACTION_PAN_Y)) { michael@0: aOutBehavior &= ~AllowedTouchBehavior::VERTICAL_PAN; michael@0: } else if ((aTouchActionValue & NS_STYLE_TOUCH_ACTION_PAN_Y) && !(aTouchActionValue & NS_STYLE_TOUCH_ACTION_PAN_X)) { michael@0: aOutBehavior &= ~AllowedTouchBehavior::HORIZONTAL_PAN; michael@0: } michael@0: } michael@0: } michael@0: michael@0: ContentHelper::TouchBehaviorFlags michael@0: ContentHelper::GetAllowedTouchBehavior(nsIWidget* aWidget, const nsIntPoint& aPoint) michael@0: { michael@0: nsView *view = nsView::GetViewFor(aWidget); michael@0: nsIFrame *viewFrame = view->GetFrame(); michael@0: michael@0: nsPoint relativePoint = nsLayoutUtils::GetEventCoordinatesRelativeTo(aWidget, aPoint, viewFrame); michael@0: michael@0: nsIFrame *target = nsLayoutUtils::GetFrameForPoint(viewFrame, relativePoint, nsLayoutUtils::IGNORE_ROOT_SCROLL_FRAME); michael@0: nsIScrollableFrame *nearestScrollableParent = nsLayoutUtils::GetNearestScrollableFrame(target, 0); michael@0: nsIFrame* nearestScrollableFrame = do_QueryFrame(nearestScrollableParent); michael@0: michael@0: // We're walking up the DOM tree until we meet the element with touch behavior and accumulating michael@0: // touch-action restrictions of all elements in this chain. michael@0: // The exact quote from the spec, that clarifies more: michael@0: // To determine the effect of a touch, find the nearest ancestor (starting from the element itself) michael@0: // that has a default touch behavior. Then examine the touch-action property of each element between michael@0: // the hit tested element and the element with the default touch behavior (including both the hit michael@0: // tested element and the element with the default touch behavior). If the touch-action property of michael@0: // any of those elements disallows the default touch behavior, do nothing. Otherwise allow the element michael@0: // to start considering the touch for the purposes of executing a default touch behavior. michael@0: michael@0: // Currently we support only two touch behaviors: panning and zooming. michael@0: // For panning we walk up until we meet the first scrollable element (the element that supports panning) michael@0: // or root element. michael@0: // For zooming we walk up until the root element since Firefox currently supports only zooming of the michael@0: // root frame but not the subframes. michael@0: michael@0: bool considerPanning = true; michael@0: TouchBehaviorFlags behavior = AllowedTouchBehavior::VERTICAL_PAN | AllowedTouchBehavior::HORIZONTAL_PAN | michael@0: AllowedTouchBehavior::PINCH_ZOOM | AllowedTouchBehavior::DOUBLE_TAP_ZOOM; michael@0: michael@0: for (nsIFrame *frame = target; frame && frame->GetContent() && behavior; frame = frame->GetParent()) { michael@0: UpdateAllowedBehavior(GetTouchActionFromFrame(frame), considerPanning, behavior); michael@0: michael@0: if (frame == nearestScrollableFrame) { michael@0: // We met the scrollable element, after it we shouldn't consider touch-action michael@0: // values for the purpose of panning but only for zooming. michael@0: considerPanning = false; michael@0: } michael@0: } michael@0: michael@0: return behavior; michael@0: } michael@0: michael@0: } michael@0: }