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

Side by Side Diff: ui/views/window/dialog_client_view.cc

Issue 1717453003: Introduce BubbleDialogDelegateView, which extends DialogDelegateView (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 10 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "ui/views/window/dialog_client_view.h" 5 #include "ui/views/window/dialog_client_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 #include "ui/events/keycodes/keyboard_codes.h" 10 #include "ui/events/keycodes/keyboard_codes.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 row_bounds->set_width(row_bounds->width() - kRelatedButtonHSpacing); 47 row_bounds->set_width(row_bounds->width() - kRelatedButtonHSpacing);
48 } 48 }
49 49
50 } // namespace 50 } // namespace
51 51
52 /////////////////////////////////////////////////////////////////////////////// 52 ///////////////////////////////////////////////////////////////////////////////
53 // DialogClientView, public: 53 // DialogClientView, public:
54 54
55 DialogClientView::DialogClientView(Widget* owner, View* contents_view) 55 DialogClientView::DialogClientView(Widget* owner, View* contents_view)
56 : ClientView(owner, contents_view), 56 : ClientView(owner, contents_view),
57 button_row_insets_(gfx::Insets(0,
58 kButtonHEdgeMarginNew,
59 kButtonVEdgeMarginNew,
60 kButtonHEdgeMarginNew)),
57 ok_button_(NULL), 61 ok_button_(NULL),
58 cancel_button_(NULL), 62 cancel_button_(NULL),
59 extra_view_(NULL), 63 extra_view_(NULL),
60 footnote_view_(NULL), 64 notified_delegate_(false) {}
61 notified_delegate_(false) {
62 }
63 65
64 DialogClientView::~DialogClientView() { 66 DialogClientView::~DialogClientView() {
65 } 67 }
66 68
67 void DialogClientView::AcceptWindow() { 69 void DialogClientView::AcceptWindow() {
68 // Only notify the delegate once. See |notified_delegate_|'s comment. 70 // Only notify the delegate once. See |notified_delegate_|'s comment.
69 if (!notified_delegate_ && GetDialogDelegate()->Accept(false)) { 71 if (!notified_delegate_ && GetDialogDelegate()->Accept(false)) {
70 notified_delegate_ = true; 72 notified_delegate_ = true;
71 Close(); 73 Close();
72 } 74 }
73 } 75 }
74 76
75 void DialogClientView::CancelWindow() { 77 void DialogClientView::CancelWindow() {
76 // Only notify the delegate once. See |notified_delegate_|'s comment. 78 // Only notify the delegate once. See |notified_delegate_|'s comment.
77 if (!notified_delegate_ && GetDialogDelegate()->Cancel()) { 79 if (!notified_delegate_ && GetDialogDelegate()->Cancel()) {
78 notified_delegate_ = true; 80 notified_delegate_ = true;
79 Close(); 81 Close();
80 } 82 }
81 } 83 }
82 84
83 void DialogClientView::UpdateDialogButtons() { 85 void DialogClientView::UpdateDialogButtons() {
84 const int buttons = GetDialogDelegate()->GetDialogButtons(); 86 const int buttons = GetDialogDelegate()->GetDialogButtons();
85 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); 87 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE);
86 88
87 if (buttons & ui::DIALOG_BUTTON_OK) { 89 if (buttons & ui::DIALOG_BUTTON_OK) {
88 if (!ok_button_) { 90 if (!ok_button_) {
89 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); 91 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK);
90 if (!(buttons & ui::DIALOG_BUTTON_CANCEL))
91 ok_button_->AddAccelerator(escape);
msw 2016/02/22 21:43:56 Is this behavior change necessary? It seems tangen
Evan Stade 2016/02/22 22:58:23 It's not a behavior change. I guess I forgot to ex
msw 2016/02/22 23:24:57 Hmm, okay; it seems like this should yield the sam
92 AddChildView(ok_button_); 92 AddChildView(ok_button_);
93 } 93 }
94 94
95 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); 95 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK);
96 } else if (ok_button_) { 96 } else if (ok_button_) {
97 delete ok_button_; 97 delete ok_button_;
98 ok_button_ = NULL; 98 ok_button_ = NULL;
99 } 99 }
100 100
101 if (buttons & ui::DIALOG_BUTTON_CANCEL) { 101 if (buttons & ui::DIALOG_BUTTON_CANCEL) {
102 if (!cancel_button_) { 102 if (!cancel_button_) {
103 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); 103 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL);
104 cancel_button_->AddAccelerator(escape); 104 cancel_button_->AddAccelerator(escape);
105 AddChildView(cancel_button_); 105 AddChildView(cancel_button_);
106 } 106 }
107 107
108 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); 108 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL);
109 } else if (cancel_button_) { 109 } else if (cancel_button_) {
110 delete cancel_button_; 110 delete cancel_button_;
111 cancel_button_ = NULL; 111 cancel_button_ = NULL;
112 } 112 }
113 113
114 // Use the escape key to close the window if there are no dialog buttons. 114 // Use the escape key to close the window if there's no cancel button.
115 if (!has_dialog_buttons()) 115 if (!cancel_button_)
116 AddAccelerator(escape); 116 AddAccelerator(escape);
117 else 117 else
118 ResetAccelerators(); 118 ResetAccelerators();
119 } 119 }
120 120
121 /////////////////////////////////////////////////////////////////////////////// 121 ///////////////////////////////////////////////////////////////////////////////
122 // DialogClientView, ClientView overrides: 122 // DialogClientView, ClientView overrides:
123 123
124 bool DialogClientView::CanClose() { 124 bool DialogClientView::CanClose() {
125 if (notified_delegate_) 125 if (notified_delegate_)
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 const gfx::Insets insets = GetButtonRowInsets(); 167 const gfx::Insets insets = GetButtonRowInsets();
168 size.Enlarge(insets.width(), insets.height()); 168 size.Enlarge(insets.width(), insets.height());
169 } 169 }
170 170
171 // Increase the size as needed to fit the contents view. 171 // Increase the size as needed to fit the contents view.
172 // NOTE: The contents view is not inset on the top or side client view edges. 172 // NOTE: The contents view is not inset on the top or side client view edges.
173 gfx::Size contents_size = contents_view()->GetPreferredSize(); 173 gfx::Size contents_size = contents_view()->GetPreferredSize();
174 size.Enlarge(0, contents_size.height()); 174 size.Enlarge(0, contents_size.height());
175 size.set_width(std::max(size.width(), contents_size.width())); 175 size.set_width(std::max(size.width(), contents_size.width()));
176 176
177 // Increase the size as needed to fit the footnote view.
178 if (ShouldShow(footnote_view_)) {
179 gfx::Size footnote_size = footnote_view_->GetPreferredSize();
180 if (!footnote_size.IsEmpty())
181 size.set_width(std::max(size.width(), footnote_size.width()));
182
183 int footnote_height = footnote_view_->GetHeightForWidth(size.width());
184 size.Enlarge(0, footnote_height);
185 }
186
187 return size; 177 return size;
188 } 178 }
189 179
190 void DialogClientView::Layout() { 180 void DialogClientView::Layout() {
191 gfx::Rect bounds = GetContentsBounds(); 181 gfx::Rect bounds = GetContentsBounds();
192 182
193 // Layout the footnote view.
194 if (ShouldShow(footnote_view_)) {
195 const int height = footnote_view_->GetHeightForWidth(bounds.width());
196 footnote_view_->SetBounds(bounds.x(), bounds.bottom() - height,
197 bounds.width(), height);
198 if (height != 0)
199 bounds.Inset(0, 0, 0, height);
200 }
201
202 // Layout the row containing the buttons and the extra view. 183 // Layout the row containing the buttons and the extra view.
203 if (has_dialog_buttons() || ShouldShow(extra_view_)) { 184 if (has_dialog_buttons() || ShouldShow(extra_view_)) {
204 bounds.Inset(GetButtonRowInsets()); 185 bounds.Inset(GetButtonRowInsets());
205 const int height = GetButtonsAndExtraViewRowHeight(); 186 const int height = GetButtonsAndExtraViewRowHeight();
206 gfx::Rect row_bounds(bounds.x(), bounds.bottom() - height, 187 gfx::Rect row_bounds(bounds.x(), bounds.bottom() - height,
207 bounds.width(), height); 188 bounds.width(), height);
208 if (kIsOkButtonOnLeftSide) { 189 if (kIsOkButtonOnLeftSide) {
209 LayoutButton(cancel_button_, &row_bounds); 190 LayoutButton(cancel_button_, &row_bounds);
210 LayoutButton(ok_button_, &row_bounds); 191 LayoutButton(ok_button_, &row_bounds);
211 } else { 192 } else {
(...skipping 30 matching lines...) Expand all
242 Close(); 223 Close();
243 return true; 224 return true;
244 } 225 }
245 226
246 void DialogClientView::ViewHierarchyChanged( 227 void DialogClientView::ViewHierarchyChanged(
247 const ViewHierarchyChangedDetails& details) { 228 const ViewHierarchyChangedDetails& details) {
248 ClientView::ViewHierarchyChanged(details); 229 ClientView::ViewHierarchyChanged(details);
249 if (details.is_add && details.child == this) { 230 if (details.is_add && details.child == this) {
250 UpdateDialogButtons(); 231 UpdateDialogButtons();
251 CreateExtraView(); 232 CreateExtraView();
252 CreateFootnoteView();
253 } else if (!details.is_add && details.child != this) { 233 } else if (!details.is_add && details.child != this) {
254 if (details.child == ok_button_) 234 if (details.child == ok_button_)
255 ok_button_ = NULL; 235 ok_button_ = NULL;
256 if (details.child == cancel_button_) 236 if (details.child == cancel_button_)
257 cancel_button_ = NULL; 237 cancel_button_ = NULL;
258 } 238 }
259 } 239 }
260 240
261 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { 241 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
262 // The old dialog style needs an explicit background color, while the new 242 // The old dialog style needs an explicit background color, while the new
(...skipping 23 matching lines...) Expand all
286 } 266 }
287 267
288 //////////////////////////////////////////////////////////////////////////////// 268 ////////////////////////////////////////////////////////////////////////////////
289 // DialogClientView, protected: 269 // DialogClientView, protected:
290 270
291 DialogClientView::DialogClientView(View* contents_view) 271 DialogClientView::DialogClientView(View* contents_view)
292 : ClientView(NULL, contents_view), 272 : ClientView(NULL, contents_view),
293 ok_button_(NULL), 273 ok_button_(NULL),
294 cancel_button_(NULL), 274 cancel_button_(NULL),
295 extra_view_(NULL), 275 extra_view_(NULL),
296 footnote_view_(NULL),
297 notified_delegate_(false) {} 276 notified_delegate_(false) {}
298 277
299 DialogDelegate* DialogClientView::GetDialogDelegate() const { 278 DialogDelegate* DialogClientView::GetDialogDelegate() const {
300 return GetWidget()->widget_delegate()->AsDialogDelegate(); 279 return GetWidget()->widget_delegate()->AsDialogDelegate();
301 } 280 }
302 281
303 void DialogClientView::CreateExtraView() { 282 void DialogClientView::CreateExtraView() {
304 if (extra_view_) 283 if (extra_view_)
305 return; 284 return;
306 285
307 extra_view_ = GetDialogDelegate()->CreateExtraView(); 286 extra_view_ = GetDialogDelegate()->CreateExtraView();
308 if (extra_view_) { 287 if (extra_view_) {
309 extra_view_->SetGroup(kButtonGroup); 288 extra_view_->SetGroup(kButtonGroup);
310 AddChildView(extra_view_); 289 AddChildView(extra_view_);
311 } 290 }
312 } 291 }
313 292
314 void DialogClientView::CreateFootnoteView() {
315 if (footnote_view_)
316 return;
317
318 footnote_view_ = GetDialogDelegate()->CreateFootnoteView();
319 if (footnote_view_)
320 AddChildView(footnote_view_);
321 }
322
323 void DialogClientView::ChildPreferredSizeChanged(View* child) { 293 void DialogClientView::ChildPreferredSizeChanged(View* child) {
324 if (child == footnote_view_ || child == extra_view_) 294 if (child == extra_view_)
325 Layout(); 295 Layout();
326 } 296 }
327 297
328 void DialogClientView::ChildVisibilityChanged(View* child) { 298 void DialogClientView::ChildVisibilityChanged(View* child) {
329 ChildPreferredSizeChanged(child); 299 ChildPreferredSizeChanged(child);
330 } 300 }
331 301
332 //////////////////////////////////////////////////////////////////////////////// 302 ////////////////////////////////////////////////////////////////////////////////
333 // DialogClientView, private: 303 // DialogClientView, private:
334 304
(...skipping 27 matching lines...) Expand all
362 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { 332 int DialogClientView::GetButtonsAndExtraViewRowHeight() const {
363 int extra_view_height = ShouldShow(extra_view_) ? 333 int extra_view_height = ShouldShow(extra_view_) ?
364 extra_view_->GetPreferredSize().height() : 0; 334 extra_view_->GetPreferredSize().height() : 0;
365 int buttons_height = std::max( 335 int buttons_height = std::max(
366 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, 336 ok_button_ ? ok_button_->GetPreferredSize().height() : 0,
367 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); 337 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0);
368 return std::max(extra_view_height, buttons_height); 338 return std::max(extra_view_height, buttons_height);
369 } 339 }
370 340
371 gfx::Insets DialogClientView::GetButtonRowInsets() const { 341 gfx::Insets DialogClientView::GetButtonRowInsets() const {
372 // NOTE: The insets only apply to the buttons, extra view, and footnote view. 342 // NOTE: The insets only apply to the buttons, extra view, and footnote view.
msw 2016/02/22 21:43:56 nit: remove mention of footnote view
Evan Stade 2016/02/22 22:58:23 I just removed the comment because it seems fairly
373 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() : 343 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets()
374 gfx::Insets(0, kButtonHEdgeMarginNew, 344 : button_row_insets_;
375 kButtonVEdgeMarginNew, kButtonHEdgeMarginNew);
376 } 345 }
377 346
378 void DialogClientView::Close() { 347 void DialogClientView::Close() {
379 GetWidget()->Close(); 348 GetWidget()->Close();
380 GetDialogDelegate()->OnClosed(); 349 GetDialogDelegate()->OnClosed();
381 } 350 }
382 351
383 } // namespace views 352 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698