Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/cocoa/bubble_anchor_helper_views.h" | 5 #include "chrome/browser/ui/cocoa/bubble_anchor_helper_views.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #import "base/mac/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
| 10 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | |
| 11 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h" | |
| 12 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" | |
| 13 #import "chrome/browser/ui/cocoa/location_bar/manage_passwords_decoration.h" | |
| 14 #include "ui/gfx/mac/coordinate_conversion.h" | |
|
tapted
2017/05/18 03:40:38
remove?
spqchan
2017/05/19 01:47:28
Done.
| |
| 10 #include "ui/views/bubble/bubble_dialog_delegate.h" | 15 #include "ui/views/bubble/bubble_dialog_delegate.h" |
| 11 #include "ui/views/widget/widget_observer.h" | 16 #include "ui/views/widget/widget_observer.h" |
| 12 | 17 |
| 13 namespace { | 18 namespace { |
| 14 | 19 |
| 15 // Self-deleting object that hosts Objective-C observers watching for parent | 20 // Self-deleting object that hosts Objective-C observers watching for parent |
| 16 // window resizes to reposition a bubble Widget. Deletes itself when the bubble | 21 // window resizes to reposition a bubble Widget. Deletes itself when the bubble |
| 17 // Widget closes. | 22 // Widget closes. |
| 18 class BubbleAnchorHelper : public views::WidgetObserver { | 23 class BubbleAnchorHelper : public views::WidgetObserver { |
| 19 public: | 24 public: |
| 20 explicit BubbleAnchorHelper(views::BubbleDialogDelegateView* bubble); | 25 BubbleAnchorHelper(views::BubbleDialogDelegateView* bubble, |
| 26 LocationBarDecoration* decoration); | |
| 21 | 27 |
| 22 private: | 28 private: |
| 23 // Observe |name| on the bubble parent window with a block to call ReAnchor(). | 29 // Observe |name| on the bubble parent window with a block to call ReAnchor(). |
| 24 void Observe(NSString* name); | 30 void Observe(NSString* name); |
| 25 | 31 |
| 26 // Whether offset from the left of the parent window is fixed. | 32 // Whether offset from the left of the parent window is fixed. |
| 27 bool IsMinXFixed() const { | 33 bool IsMinXFixed() const { |
| 28 return views::BubbleBorder::is_arrow_on_left(bubble_->arrow()); | 34 return views::BubbleBorder::is_arrow_on_left(bubble_->arrow()); |
| 29 } | 35 } |
| 30 | 36 |
| 31 // Re-positions |bubble_| so that the offset to the parent window at | 37 // Re-positions |bubble_| so that the offset to the parent window at |
| 32 // construction time is preserved. | 38 // construction time is preserved. |
| 33 void ReAnchor(); | 39 void ReAnchor(); |
| 34 | 40 |
| 35 // WidgetObserver: | 41 // WidgetObserver: |
| 36 void OnWidgetDestroying(views::Widget* widget) override; | 42 void OnWidgetDestroying(views::Widget* widget) override; |
| 37 | 43 |
| 38 base::scoped_nsobject<NSMutableArray> observer_tokens_; | 44 base::scoped_nsobject<NSMutableArray> observer_tokens_; |
| 39 views::BubbleDialogDelegateView* bubble_; | 45 views::BubbleDialogDelegateView* bubble_; |
| 40 CGFloat horizontal_offset_; // Offset from the left or right. | 46 CGFloat horizontal_offset_; // Offset from the left or right. |
| 41 CGFloat vertical_offset_; // Offset from the top. | 47 CGFloat vertical_offset_; // Offset from the top. |
| 42 | 48 |
| 49 // The omnibox decoration that |bubble_| is anchored to. | |
| 50 // Weak. The lifetime is tied to the controller of the bubble's parent | |
| 51 // window, which owns a LocationBarViewMac that directly owns |decoration_|. | |
| 52 LocationBarDecoration* decoration_; | |
| 53 | |
| 43 DISALLOW_COPY_AND_ASSIGN(BubbleAnchorHelper); | 54 DISALLOW_COPY_AND_ASSIGN(BubbleAnchorHelper); |
| 44 }; | 55 }; |
| 45 | 56 |
| 46 } // namespace | 57 } // namespace |
| 47 | 58 |
| 48 void KeepBubbleAnchored(views::BubbleDialogDelegateView* bubble) { | 59 LocationBarDecoration* GetManagePasswordDecoration( |
| 49 new BubbleAnchorHelper(bubble); | 60 views::BubbleDialogDelegateView* bubble) { |
| 61 BrowserWindowController* window_controller = [BrowserWindowController | |
| 62 browserWindowControllerForWindow:[bubble->parent_window() window]]; | |
| 63 LocationBarViewMac* location_bar = [window_controller locationBarBridge]; | |
| 64 return location_bar ? location_bar->manage_passwords_decoration() : nullptr; | |
| 50 } | 65 } |
| 51 | 66 |
| 52 BubbleAnchorHelper::BubbleAnchorHelper(views::BubbleDialogDelegateView* bubble) | 67 void KeepBubbleAnchored(views::BubbleDialogDelegateView* bubble, |
| 53 : observer_tokens_([[NSMutableArray alloc] init]), bubble_(bubble) { | 68 LocationBarDecoration* decoration) { |
| 69 new BubbleAnchorHelper(bubble, decoration); | |
| 70 } | |
| 71 | |
| 72 BubbleAnchorHelper::BubbleAnchorHelper(views::BubbleDialogDelegateView* bubble, | |
| 73 LocationBarDecoration* decoration) | |
| 74 : observer_tokens_([[NSMutableArray alloc] init]), | |
| 75 bubble_(bubble), | |
| 76 decoration_(decoration) { | |
| 54 DCHECK(bubble->GetWidget()); | 77 DCHECK(bubble->GetWidget()); |
| 55 DCHECK(bubble->parent_window()); | 78 DCHECK(bubble->parent_window()); |
| 56 bubble->GetWidget()->AddObserver(this); | 79 bubble->GetWidget()->AddObserver(this); |
| 57 | 80 |
| 81 if (decoration_) | |
| 82 decoration_->SetActive(true); | |
| 83 | |
| 58 NSRect parent_frame = [[bubble->parent_window() window] frame]; | 84 NSRect parent_frame = [[bubble->parent_window() window] frame]; |
| 59 NSRect bubble_frame = [bubble->GetWidget()->GetNativeWindow() frame]; | 85 NSRect bubble_frame = [bubble->GetWidget()->GetNativeWindow() frame]; |
| 60 | 86 |
| 61 // Note: when anchored on the right, this doesn't support changes to the | 87 // Note: when anchored on the right, this doesn't support changes to the |
| 62 // bubble size, just the parent size. | 88 // bubble size, just the parent size. |
| 63 horizontal_offset_ = | 89 horizontal_offset_ = |
| 64 (IsMinXFixed() ? NSMinX(parent_frame) : NSMaxX(parent_frame)) - | 90 (IsMinXFixed() ? NSMinX(parent_frame) : NSMaxX(parent_frame)) - |
| 65 NSMinX(bubble_frame); | 91 NSMinX(bubble_frame); |
| 66 vertical_offset_ = NSMaxY(parent_frame) - NSMinY(bubble_frame); | 92 vertical_offset_ = NSMaxY(parent_frame) - NSMinY(bubble_frame); |
| 67 | 93 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 95 bubble_frame.origin.x = NSMinX(parent_frame) - horizontal_offset_; | 121 bubble_frame.origin.x = NSMinX(parent_frame) - horizontal_offset_; |
| 96 else | 122 else |
| 97 bubble_frame.origin.x = NSMaxX(parent_frame) - horizontal_offset_; | 123 bubble_frame.origin.x = NSMaxX(parent_frame) - horizontal_offset_; |
| 98 bubble_frame.origin.y = NSMaxY(parent_frame) - vertical_offset_; | 124 bubble_frame.origin.y = NSMaxY(parent_frame) - vertical_offset_; |
| 99 [bubble_->GetWidget()->GetNativeWindow() setFrame:bubble_frame | 125 [bubble_->GetWidget()->GetNativeWindow() setFrame:bubble_frame |
| 100 display:YES | 126 display:YES |
| 101 animate:NO]; | 127 animate:NO]; |
| 102 } | 128 } |
| 103 | 129 |
| 104 void BubbleAnchorHelper::OnWidgetDestroying(views::Widget* widget) { | 130 void BubbleAnchorHelper::OnWidgetDestroying(views::Widget* widget) { |
| 131 // NativeWidgetMac guarantees that a child's OnWidgetDestroying() is invoked | |
| 132 // before the parent NSWindow has closed. | |
| 133 if (decoration_) | |
| 134 decoration_->SetActive(false); | |
| 105 widget->RemoveObserver(this); | 135 widget->RemoveObserver(this); |
| 106 for (id token in observer_tokens_.get()) | 136 for (id token in observer_tokens_.get()) |
| 107 [[NSNotificationCenter defaultCenter] removeObserver:token]; | 137 [[NSNotificationCenter defaultCenter] removeObserver:token]; |
| 108 delete this; | 138 delete this; |
| 109 } | 139 } |
| OLD | NEW |