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

Side by Side Diff: ui/views/bubble/bubble_delegate.cc

Issue 1922813002: Vanquish views::BubbleDelegate{View,} (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix unit tests Created 4 years, 7 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 (c) 2012 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 #include "ui/views/bubble/bubble_delegate.h"
6
7 #include "build/build_config.h"
8 #include "ui/accessibility/ax_view_state.h"
9 #include "ui/base/default_style.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/color_utils.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/native_theme/native_theme.h"
14 #include "ui/views/bubble/bubble_frame_view.h"
15 #include "ui/views/focus/view_storage.h"
16 #include "ui/views/layout/layout_constants.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/views/widget/widget_observer.h"
19
20 #if defined(OS_WIN)
21 #include "ui/base/win/shell.h"
22 #endif
23
24 namespace views {
25
26 namespace {
27
28 // Create a widget to host the bubble.
29 Widget* CreateBubbleWidget(BubbleDelegateView* bubble) {
30 Widget* bubble_widget = new Widget();
31 Widget::InitParams bubble_params(Widget::InitParams::TYPE_BUBBLE);
32 bubble_params.delegate = bubble;
33 bubble_params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW;
34 bubble_params.accept_events = bubble->accept_events();
35 if (bubble->parent_window())
36 bubble_params.parent = bubble->parent_window();
37 else if (bubble->anchor_widget())
38 bubble_params.parent = bubble->anchor_widget()->GetNativeView();
39 bubble_params.activatable = bubble->CanActivate() ?
40 Widget::InitParams::ACTIVATABLE_YES : Widget::InitParams::ACTIVATABLE_NO;
41 bubble->OnBeforeBubbleWidgetInit(&bubble_params, bubble_widget);
42 bubble_widget->Init(bubble_params);
43 if (bubble_params.parent)
44 bubble_widget->StackAbove(bubble_params.parent);
45 return bubble_widget;
46 }
47
48 } // namespace
49
50 // static
51 const char BubbleDelegateView::kViewClassName[] = "BubbleDelegateView";
52
53 BubbleDelegateView::BubbleDelegateView()
54 : BubbleDelegateView(nullptr, BubbleBorder::TOP_LEFT) {}
55
56 BubbleDelegateView::BubbleDelegateView(View* anchor_view,
57 BubbleBorder::Arrow arrow)
58 : close_on_esc_(true),
59 close_on_deactivate_(true),
60 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
61 anchor_widget_(NULL),
62 arrow_(arrow),
63 shadow_(BubbleBorder::SMALL_SHADOW),
64 color_explicitly_set_(false),
65 margins_(kPanelVertMargin,
66 kPanelHorizMargin,
67 kPanelVertMargin,
68 kPanelHorizMargin),
69 accept_events_(true),
70 border_accepts_events_(true),
71 adjust_if_offscreen_(true),
72 parent_window_(NULL),
73 close_reason_(CloseReason::UNKNOWN) {
74 if (anchor_view)
75 SetAnchorView(anchor_view);
76 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
77 UpdateColorsFromTheme(GetNativeTheme());
78 }
79
80 BubbleDelegateView::~BubbleDelegateView() {
81 if (GetWidget())
82 GetWidget()->RemoveObserver(this);
83 SetLayoutManager(NULL);
84 SetAnchorView(NULL);
85 }
86
87 // static
88 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) {
89 bubble_delegate->Init();
90 // Get the latest anchor widget from the anchor view at bubble creation time.
91 bubble_delegate->SetAnchorView(bubble_delegate->GetAnchorView());
92 Widget* bubble_widget = CreateBubbleWidget(bubble_delegate);
93
94 #if defined(OS_WIN)
95 // If glass is enabled, the bubble is allowed to extend outside the bounds of
96 // the parent frame and let DWM handle compositing. If not, then we don't
97 // want to allow the bubble to extend the frame because it will be clipped.
98 bubble_delegate->set_adjust_if_offscreen(ui::win::IsAeroGlassEnabled());
99 #elif (defined(OS_LINUX) && !defined(OS_CHROMEOS)) || defined(OS_MACOSX)
100 // Linux clips bubble windows that extend outside their parent window bounds.
101 // Mac never adjusts.
102 bubble_delegate->set_adjust_if_offscreen(false);
103 #endif
104
105 bubble_delegate->SizeToContents();
106 bubble_widget->AddObserver(bubble_delegate);
107 return bubble_widget;
108 }
109
110 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() {
111 return this;
112 }
113
114 bool BubbleDelegateView::ShouldShowCloseButton() const {
115 return false;
116 }
117
118 View* BubbleDelegateView::GetContentsView() {
119 return this;
120 }
121
122 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView(
123 Widget* widget) {
124 BubbleFrameView* frame = new BubbleFrameView(
125 gfx::Insets(kPanelVertMargin, kPanelHorizMargin, 0, kPanelHorizMargin),
126 margins());
127 // Note: In CreateBubble, the call to SizeToContents() will cause
128 // the relayout that this call requires.
129 frame->SetTitleFontList(GetTitleFontList());
130 frame->SetFootnoteView(CreateFootnoteView());
131
132 BubbleBorder::Arrow adjusted_arrow = arrow();
133 if (base::i18n::IsRTL())
134 adjusted_arrow = BubbleBorder::horizontal_mirror(adjusted_arrow);
135 frame->SetBubbleBorder(std::unique_ptr<BubbleBorder>(
136 new BubbleBorder(adjusted_arrow, shadow(), color())));
137 return frame;
138 }
139
140 void BubbleDelegateView::GetAccessibleState(ui::AXViewState* state) {
141 state->role = ui::AX_ROLE_DIALOG;
142 }
143
144 const char* BubbleDelegateView::GetClassName() const {
145 return kViewClassName;
146 }
147
148 void BubbleDelegateView::OnWidgetClosing(Widget* widget) {
149 DCHECK(GetBubbleFrameView());
150 if (widget == GetWidget() && close_reason_ == CloseReason::UNKNOWN &&
151 GetBubbleFrameView()->close_button_clicked()) {
152 close_reason_ = CloseReason::CLOSE_BUTTON;
153 }
154 }
155
156 void BubbleDelegateView::OnWidgetDestroying(Widget* widget) {
157 if (anchor_widget() == widget)
158 SetAnchorView(NULL);
159 }
160
161 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget* widget,
162 bool visible) {
163 #if defined(OS_WIN)
164 // On Windows we need to handle this before the bubble is visible or hidden.
165 // Please see the comment on the OnWidgetVisibilityChanging function. On
166 // other platforms it is fine to handle it after the bubble is shown/hidden.
167 HandleVisibilityChanged(widget, visible);
168 #endif
169 }
170
171 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget* widget,
172 bool visible) {
173 #if !defined(OS_WIN)
174 HandleVisibilityChanged(widget, visible);
175 #endif
176 }
177
178 void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget,
179 bool active) {
180 if (close_on_deactivate() && widget == GetWidget() && !active) {
181 if (close_reason_ == CloseReason::UNKNOWN)
182 close_reason_ = CloseReason::DEACTIVATION;
183 GetWidget()->Close();
184 }
185 }
186
187 void BubbleDelegateView::OnWidgetBoundsChanged(Widget* widget,
188 const gfx::Rect& new_bounds) {
189 if (GetBubbleFrameView() && anchor_widget() == widget)
190 SizeToContents();
191 }
192
193 View* BubbleDelegateView::GetAnchorView() const {
194 return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_);
195 }
196
197 gfx::Rect BubbleDelegateView::GetAnchorRect() const {
198 if (!GetAnchorView())
199 return anchor_rect_;
200
201 anchor_rect_ = GetAnchorView()->GetBoundsInScreen();
202 anchor_rect_.Inset(anchor_view_insets_);
203 return anchor_rect_;
204 }
205
206 void BubbleDelegateView::OnBeforeBubbleWidgetInit(Widget::InitParams* params,
207 Widget* widget) const {
208 }
209
210 View* BubbleDelegateView::CreateFootnoteView() {
211 return nullptr;
212 }
213
214 void BubbleDelegateView::UseCompactMargins() {
215 const int kCompactMargin = 6;
216 margins_.Set(kCompactMargin, kCompactMargin, kCompactMargin, kCompactMargin);
217 }
218
219 void BubbleDelegateView::SetAlignment(BubbleBorder::BubbleAlignment alignment) {
220 GetBubbleFrameView()->bubble_border()->set_alignment(alignment);
221 SizeToContents();
222 }
223
224 void BubbleDelegateView::SetArrowPaintType(
225 BubbleBorder::ArrowPaintType paint_type) {
226 GetBubbleFrameView()->bubble_border()->set_paint_arrow(paint_type);
227 SizeToContents();
228 }
229
230 void BubbleDelegateView::OnAnchorBoundsChanged() {
231 SizeToContents();
232 }
233
234 bool BubbleDelegateView::AcceleratorPressed(
235 const ui::Accelerator& accelerator) {
236 if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE)
237 return false;
238 close_reason_ = CloseReason::ESCAPE;
239 GetWidget()->Close();
240 return true;
241 }
242
243 void BubbleDelegateView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
244 UpdateColorsFromTheme(theme);
245 }
246
247 void BubbleDelegateView::Init() {}
248
249 void BubbleDelegateView::SetAnchorView(View* anchor_view) {
250 // When the anchor view gets set the associated anchor widget might
251 // change as well.
252 if (!anchor_view || anchor_widget() != anchor_view->GetWidget()) {
253 if (anchor_widget()) {
254 anchor_widget_->RemoveObserver(this);
255 anchor_widget_ = NULL;
256 }
257 if (anchor_view) {
258 anchor_widget_ = anchor_view->GetWidget();
259 if (anchor_widget_)
260 anchor_widget_->AddObserver(this);
261 }
262 }
263
264 // Remove the old storage item and set the new (if there is one).
265 ViewStorage* view_storage = ViewStorage::GetInstance();
266 if (view_storage->RetrieveView(anchor_view_storage_id_))
267 view_storage->RemoveView(anchor_view_storage_id_);
268 if (anchor_view)
269 view_storage->StoreView(anchor_view_storage_id_, anchor_view);
270
271 // Do not update anchoring for NULL views; this could indicate that our
272 // NativeWindow is being destroyed, so it would be dangerous for us to update
273 // our anchor bounds at that point. (It's safe to skip this, since if we were
274 // to update the bounds when |anchor_view| is NULL, the bubble won't move.)
275 if (anchor_view && GetWidget())
276 OnAnchorBoundsChanged();
277 }
278
279 void BubbleDelegateView::SetAnchorRect(const gfx::Rect& rect) {
280 anchor_rect_ = rect;
281 if (GetWidget())
282 OnAnchorBoundsChanged();
283 }
284
285 void BubbleDelegateView::SizeToContents() {
286 GetWidget()->SetBounds(GetBubbleBounds());
287 }
288
289 BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const {
290 const NonClientView* view =
291 GetWidget() ? GetWidget()->non_client_view() : NULL;
292 return view ? static_cast<BubbleFrameView*>(view->frame_view()) : NULL;
293 }
294
295 gfx::Rect BubbleDelegateView::GetBubbleBounds() {
296 // The argument rect has its origin at the bubble's arrow anchor point;
297 // its size is the preferred size of the bubble's client view (this view).
298 bool anchor_minimized = anchor_widget() && anchor_widget()->IsMinimized();
299 return GetBubbleFrameView()->GetUpdatedWindowBounds(GetAnchorRect(),
300 GetPreferredSize(), adjust_if_offscreen_ && !anchor_minimized);
301 }
302
303 const gfx::FontList& BubbleDelegateView::GetTitleFontList() const {
304 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
305 return rb.GetFontListWithDelta(ui::kTitleFontSizeDelta);
306 }
307
308 void BubbleDelegateView::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
309 if (!color_explicitly_set_)
310 color_ = theme->GetSystemColor(ui::NativeTheme::kColorId_BubbleBackground);
311 set_background(Background::CreateSolidBackground(color()));
312 BubbleFrameView* frame_view = GetBubbleFrameView();
313 if (frame_view)
314 frame_view->bubble_border()->set_background_color(color());
315 }
316
317 void BubbleDelegateView::HandleVisibilityChanged(Widget* widget, bool visible) {
318 if (widget == GetWidget() && anchor_widget() &&
319 anchor_widget()->GetTopLevelWidget()) {
320 anchor_widget()->GetTopLevelWidget()->SetAlwaysRenderAsActive(visible);
321 }
322
323 // Fire AX_EVENT_ALERT for bubbles marked as AX_ROLE_ALERT_DIALOG; this
324 // instructs accessibility tools to read the bubble in its entirety rather
325 // than just its title and initially focused view. See
326 // http://crbug.com/474622 for details.
327 if (widget == GetWidget() && visible) {
328 ui::AXViewState state;
329 GetAccessibleState(&state);
330 if (state.role == ui::AX_ROLE_ALERT_DIALOG)
331 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
332 }
333 }
334
335 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698