Commit a7aa5167 authored by andresantoso's avatar andresantoso Committed by Commit bot

MacViews: Support continuous scrolling and horizontal scrolling

Add support for continuous devices (Magic Mouse and trackpad) using NSEvent
APIs introduced in 10.7.
Tweak ScrollView/ScrollBar to allow horizontal scrolling from MouseWheel
events.

BUG=363529

Review URL: https://codereview.chromium.org/456913002

Cr-Commit-Position: refs/heads/master@{#293038}
parent 683b1856
...@@ -70,6 +70,7 @@ typedef NSUInteger NSWindowButton; ...@@ -70,6 +70,7 @@ typedef NSUInteger NSWindowButton;
- (NSEventPhase)momentumPhase; - (NSEventPhase)momentumPhase;
- (NSEventPhase)phase; - (NSEventPhase)phase;
- (BOOL)hasPreciseScrollingDeltas;
- (CGFloat)scrollingDeltaX; - (CGFloat)scrollingDeltaX;
- (CGFloat)scrollingDeltaY; - (CGFloat)scrollingDeltaY;
- (void)trackSwipeEventWithOptions:(NSEventSwipeTrackingOptions)options - (void)trackSwipeEventWithOptions:(NSEventSwipeTrackingOptions)options
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include <Cocoa/Cocoa.h> #include <Cocoa/Cocoa.h>
#import "base/mac/mac_util.h"
#import "base/mac/sdk_forward_declarations.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -130,6 +132,15 @@ int GetChangedMouseButtonFlagsFromNative( ...@@ -130,6 +132,15 @@ int GetChangedMouseButtonFlagsFromNative(
} }
gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) { gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) {
if ([event respondsToSelector:@selector(hasPreciseScrollingDeltas)] &&
[event hasPreciseScrollingDeltas]) {
// Handle continuous scrolling devices such as a Magic Mouse or a trackpad.
// -scrollingDelta{X|Y} have float return types but they return values that
// are already rounded to integers.
// The values are the same as the values returned from calling
// CGEventGetIntegerValueField(kCGScrollWheelEventPointDeltaAxis{1|2}).
return gfx::Vector2d([event scrollingDeltaX], [event scrollingDeltaY]);
} else {
// Empirically, a value of 0.1 is typical for one mousewheel click. Positive // Empirically, a value of 0.1 is typical for one mousewheel click. Positive
// values when scrolling up or to the left. Scrolling quickly results in a // values when scrolling up or to the left. Scrolling quickly results in a
// higher delta per click, up to about 15.0. (Quartz documentation suggests // higher delta per click, up to about 15.0. (Quartz documentation suggests
...@@ -138,6 +149,7 @@ gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) { ...@@ -138,6 +149,7 @@ gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) {
const CGFloat kWheelDeltaMultiplier = 1000; const CGFloat kWheelDeltaMultiplier = 1000;
return gfx::Vector2d(kWheelDeltaMultiplier * [event deltaX], return gfx::Vector2d(kWheelDeltaMultiplier * [event deltaX],
kWheelDeltaMultiplier * [event deltaY]); kWheelDeltaMultiplier * [event deltaY]);
}
} }
base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) { base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) {
......
...@@ -149,7 +149,11 @@ ...@@ -149,7 +149,11 @@
} }
- (void)scrollWheel:(NSEvent*)theEvent { - (void)scrollWheel:(NSEvent*)theEvent {
[self handleMouseEvent:theEvent]; if (!hostedView_)
return;
ui::MouseWheelEvent event(theEvent);
hostedView_->GetWidget()->OnMouseEvent(&event);
} }
- (void)deleteBackward:(id)sender { - (void)deleteBackward:(id)sender {
......
...@@ -354,12 +354,12 @@ bool ScrollView::OnKeyPressed(const ui::KeyEvent& event) { ...@@ -354,12 +354,12 @@ bool ScrollView::OnKeyPressed(const ui::KeyEvent& event) {
bool ScrollView::OnMouseWheel(const ui::MouseWheelEvent& e) { bool ScrollView::OnMouseWheel(const ui::MouseWheelEvent& e) {
bool processed = false; bool processed = false;
// Give vertical scrollbar priority
if (vert_sb_->visible()) if (vert_sb_->visible())
processed = vert_sb_->OnMouseWheel(e); processed = vert_sb_->OnMouseWheel(e);
if (!processed && horiz_sb_->visible()) if (horiz_sb_->visible())
processed = horiz_sb_->OnMouseWheel(e); processed = horiz_sb_->OnMouseWheel(e) || processed;
return processed; return processed;
} }
......
...@@ -194,7 +194,7 @@ bool BaseScrollBar::OnKeyPressed(const ui::KeyEvent& event) { ...@@ -194,7 +194,7 @@ bool BaseScrollBar::OnKeyPressed(const ui::KeyEvent& event) {
} }
bool BaseScrollBar::OnMouseWheel(const ui::MouseWheelEvent& event) { bool BaseScrollBar::OnMouseWheel(const ui::MouseWheelEvent& event) {
ScrollByContentsOffset(event.y_offset()); OnScroll(event.x_offset(), event.y_offset());
return true; return true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment