Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 BrowserWindowController* bwc = [owner_ browserWindowController]; | |
| 52 contentView_ = [[bwc window] contentView]; | |
|
erikchen
2016/10/26 17:26:10
When we add the tracking area, we should dynamical
spqchan
2016/10/26 17:42:50
Done.
| |
| 53 animationController_ = animationController; | |
| 54 } | |
| 55 | |
| 56 return self; | |
| 57 } | |
| 58 | |
| 59 - (void)dealloc { | |
| 60 [self removeTrackingArea]; | |
| 61 [super dealloc]; | |
| 62 } | |
| 63 | |
| 64 - (void)updateTrackingArea { | |
| 65 // Remove the tracking area if the toolbar isn't fully shown. | |
| 66 if (!ui::IsCGFloatEqual([owner_ toolbarFraction], 1.0)) { | |
| 67 [self removeTrackingArea]; | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 if (trackingArea_) { | |
| 72 // If |trackingArea_|'s rect matches |trackingAreaFrame_|, quit early. | |
| 73 NSRect oldRect = [trackingArea_ rect]; | |
| 74 if (NSEqualRects(trackingAreaFrame_, [trackingArea_ rect])) | |
| 75 return; | |
| 76 | |
| 77 [self removeTrackingArea]; | |
| 78 } | |
| 79 | |
| 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 animationController_->AnimateToolbarOutIfPossible(); | |
| 125 | |
| 126 [owner_ updateToolbar]; | |
| 127 [self removeTrackingArea]; | |
| 128 } | |
| 129 | |
| 130 @end | |
| OLD | NEW |