Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1307)

Side by Side Diff: chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_mouse_tracker.mm

Issue 2446313005: [Mac] Refactor the fullscreen toolbar mouse tracker (Closed)
Patch Set: Fix for rsesek Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #import "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_mouse_tracker.h"
6
7 #include "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_animation_contro ller.h"
8 #import "chrome/browser/ui/cocoa/fullscreen_toolbar_controller.h"
9 #include "ui/base/cocoa/appkit_utils.h"
10 #import "ui/base/cocoa/tracking_area.h"
11
12 namespace {
13
14 // Additional height threshold added at the toolbar's bottom. This is to mimic
15 // threshold the mouse position needs to be at before the menubar automatically
16 // hides.
17 const CGFloat kTrackingAreaAdditionalThreshold = 20;
18
19 } // namespace
20
21 @interface FullscreenToolbarMouseTracker () {
22 // The frame for the tracking area. The value is the toolbar's frame with
23 // additional height added at the bottom.
24 NSRect trackingAreaFrame_;
25
26 // The tracking area associated with the toolbar. This tracking area is used
27 // to keep the toolbar active if the menubar had animated out but the mouse
28 // is still on the toolbar.
29 base::scoped_nsobject<CrTrackingArea> trackingArea_;
30
31 // The content view for the window.
32 NSView* contentView_; // weak
33
34 // The owner of this class.
35 FullscreenToolbarController* owner_; // weak
36
37 // The object managing the fullscreen toolbar's animations.
38 FullscreenToolbarAnimationController* animationController_; // weak
39 }
40
41 @end
42
43 @implementation FullscreenToolbarMouseTracker
44
45 - (instancetype)
46 initWithFullscreenToolbarController:(FullscreenToolbarController*)owner
47 animationController:
48 (FullscreenToolbarAnimationController*)animationController {
49 if ((self = [super init])) {
50 owner_ = owner;
51 animationController_ = animationController;
52 }
53
54 return self;
55 }
56
57 - (void)dealloc {
58 [self removeTrackingArea];
59 [super dealloc];
60 }
61
62 - (void)updateTrackingArea {
63 // Remove the tracking area if the toolbar isn't fully shown.
64 if (!ui::IsCGFloatEqual([owner_ toolbarFraction], 1.0)) {
65 [self removeTrackingArea];
66 return;
67 }
68
69 if (trackingArea_) {
70 // If |trackingArea_|'s rect matches |trackingAreaFrame_|, quit early.
71 if (NSEqualRects(trackingAreaFrame_, [trackingArea_ rect]))
72 return;
73
74 [self removeTrackingArea];
75 }
76
77 BrowserWindowController* bwc = [owner_ browserWindowController];
78 contentView_ = [[bwc window] contentView];
79
80 trackingArea_.reset([[CrTrackingArea alloc]
81 initWithRect:trackingAreaFrame_
82 options:NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow
83 owner:self
84 userInfo:nil]);
85
86 [contentView_ addTrackingArea:trackingArea_];
87 }
88
89 - (void)updateToolbarFrame:(NSRect)frame {
90 NSRect contentBounds = [contentView_ bounds];
91 trackingAreaFrame_ = frame;
92 trackingAreaFrame_.origin.y -= kTrackingAreaAdditionalThreshold;
93 trackingAreaFrame_.size.height =
94 NSMaxY(contentBounds) - trackingAreaFrame_.origin.y;
95
96 [self updateTrackingArea];
97 }
98
99 - (void)removeTrackingArea {
100 if (!trackingArea_)
101 return;
102
103 DCHECK(contentView_);
104 [contentView_ removeTrackingArea:trackingArea_];
105 trackingArea_.reset();
106 contentView_ = nil;
107 }
108
109 - (BOOL)mouseInsideTrackingArea {
110 if (!trackingArea_)
111 return NO;
112
113 NSPoint mouseLoc = [[contentView_ window] mouseLocationOutsideOfEventStream];
114 NSPoint mousePos = [contentView_ convertPoint:mouseLoc fromView:nil];
115 return NSMouseInRect(mousePos, trackingAreaFrame_, [contentView_ isFlipped]);
116 }
117
118 - (void)mouseEntered:(NSEvent*)event {
119 // Empty implementation. Required for CrTrackingArea.
120 }
121
122 - (void)mouseExited:(NSEvent*)event {
123 DCHECK_EQ([event trackingArea], trackingArea_.get());
124
125 animationController_->AnimateToolbarOutIfPossible();
126
127 [owner_ updateToolbar];
128 [self removeTrackingArea];
129 }
130
131 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698