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

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

Issue 2355413007: [Mac] Refactor the Fullscreen Toolbar (Closed)
Patch Set: Nits and grits Created 4 years, 2 months 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_manage r.h"
8 #import "chrome/browser/ui/cocoa/fullscreen/fullscreen_toolbar_layout_manager.h"
9 #import "ui/base/cocoa/tracking_area.h"
10
11 namespace {
12
13 // Additional height threshold added at the toolbar's bottom. This is to mimic
14 // threshold the mouse position needs to be at before the menubar automatically
15 // hides.
16 const CGFloat kTrackingAreaAdditionalThreshold = 20;
17
18 } // namespace
19
20 @interface FullscreenToolbarMouseTracker () {
21 // The frame for the tracking area. The value is the toolbar overlay's frame
22 // with additional height added at the bottom.
23 NSRect trackingAreaFrame_;
24
25 // The tracking area associated with the toolbar overlay bar. This tracking
26 // area is used to keep the toolbar active if the menubar had animated out
27 // but the mouse is still on the toolbar.
28 base::scoped_nsobject<CrTrackingArea> trackingArea_;
29
30 // The content view for the window.
31 NSView* contentView_; // weak
32
33 // The owner of this class.
34 FullscreenToolbarLayoutManager* owner_; // weak
35
36 // The object managing the fullscreen toolbar's animations.
37 FullscreenToolbarAnimationManager* animationManager_; // weak
38 }
39
40 @end
41
42 @implementation FullscreenToolbarMouseTracker
43
44 - (id)initWithFullscreenToolbarLayoutManager:
45 (FullscreenToolbarLayoutManager*)owner
46 toolbarAnimationManager:
47 (FullscreenToolbarAnimationManager*)animationManager {
48 if ((self = [super init])) {
49 owner_ = owner;
50 contentView_ = [[owner_->browser_window_controller() window] contentView];
51 animationManager_ = animationManager;
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 FullscreenToolbarLayout layout = owner_->ComputeToolbarLayout();
65 if (!base::mac::IsCGFloatEqual(layout.toolbarFraction, 1.0)) {
66 [self removeTrackingArea];
67 return;
68 }
69
70 if (trackingArea_) {
71 // If the tracking rectangle is already |trackingAreaBounds_|, quit early.
72 NSRect oldRect = [trackingArea_ rect];
73 if (NSEqualRects(trackingAreaFrame_, oldRect))
74 return;
75
76 [self removeTrackingArea];
77 }
78
79 // Create and add a new tracking area for |frame|.
80 trackingArea_.reset([[CrTrackingArea alloc]
81 initWithRect:trackingAreaFrame_
82 options:NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow
83 owner:self
84 userInfo:nil]);
85 DCHECK(contentView_);
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 }
107
108 - (BOOL)mouseInsideTrackingArea {
109 if (!trackingArea_)
110 return NO;
111
112 NSPoint mouseLoc = [[contentView_ window] mouseLocationOutsideOfEventStream];
113 NSPoint mousePos = [contentView_ convertPoint:mouseLoc fromView:nil];
114 return NSMouseInRect(mousePos, trackingAreaFrame_, [contentView_ isFlipped]);
115 }
116
117 - (void)mouseEntered:(NSEvent*)event {
118 // Empty implementation. Required for CrTrackingArea.
119 }
120
121 - (void)mouseExited:(NSEvent*)event {
122 DCHECK_EQ([event trackingArea], trackingArea_.get());
123
124 animationManager_->AnimateToolbarOutIfPossible();
125
126 owner_->Layout();
127 [self removeTrackingArea];
128 }
129
130 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698