scroll_view_with_no_scrollbars.mm revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/app_list/cocoa/scroll_view_with_no_scrollbars.h"
6
7#include "base/mac/mac_util.h"
8#include "base/memory/scoped_nsobject.h"
9
10#if !defined(MAC_OS_X_VERSION_10_7) || \
11    MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
12
13enum {
14   NSEventPhaseNone       = 0,
15   NSEventPhaseBegan      = 0x1 << 0,
16   NSEventPhaseStationary = 0x1 << 1,
17   NSEventPhaseChanged    = 0x1 << 2,
18   NSEventPhaseEnded      = 0x1 << 3,
19   NSEventPhaseCancelled  = 0x1 << 4,
20};
21typedef NSUInteger NSEventPhase;
22
23@interface NSEvent (LionAPI)
24
25- (NSEventPhase)momentumPhase;
26- (NSEventPhase)phase;
27
28@end
29
30#endif  // 10.7
31
32@interface InvisibleScroller : NSScroller;
33@end
34
35@implementation InvisibleScroller
36
37// Makes it non-interactive (and invisible) on Lion with both 10.6 and 10.7
38// SDKs. TODO(tapted): Find a way to make it non-interactive on Snow Leopard.
39// TODO(tapted): Find a way to make it take up no space on Lion with a 10.6 SDK.
40- (NSRect)rectForPart:(NSScrollerPart)aPart {
41  return NSZeroRect;
42}
43
44@end
45
46@implementation ScrollViewWithNoScrollbars
47
48@synthesize delegate = delegate_;
49
50- (id)initWithFrame:(NSRect)frame {
51  if ((self = [super initWithFrame:frame])) {
52    [self setHasHorizontalScroller:YES];
53    NSRect horizontalScrollerRect = [self bounds];
54    horizontalScrollerRect.size.height = 0;
55    scoped_nsobject<InvisibleScroller> horizontalScroller(
56        [[InvisibleScroller alloc] initWithFrame:horizontalScrollerRect]);
57    [self setHorizontalScroller:horizontalScroller];
58  }
59  return self;
60}
61
62- (void)endGestureWithEvent:(NSEvent*)event {
63  [super endGestureWithEvent:event];
64  if (!base::mac::IsOSLionOrLater())
65    [delegate_ userScrolling:NO];
66}
67
68- (void)scrollWheel:(NSEvent*)event {
69  [super scrollWheel:event];
70  if (![event respondsToSelector:@selector(momentumPhase)])
71    return;
72
73  BOOL scrollComplete = [event momentumPhase] == NSEventPhaseEnded ||
74      ([event momentumPhase] == NSEventPhaseNone &&
75          [event phase] == NSEventPhaseEnded);
76
77  [delegate_ userScrolling:!scrollComplete];
78}
79
80@end
81