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