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

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, 9 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
« no previous file with comments | « ui/views/window/dialog_client_view.h ('k') | ui/views/window/dialog_client_view_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_(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),
61 delegate_allowed_close_(false) {} 64 delegate_allowed_close_(false) {}
62 65
63 DialogClientView::~DialogClientView() { 66 DialogClientView::~DialogClientView() {
64 } 67 }
65 68
66 void DialogClientView::AcceptWindow() { 69 void DialogClientView::AcceptWindow() {
67 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. 70 // Only notify the delegate once. See |delegate_allowed_close_|'s comment.
68 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept(false)) { 71 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept(false)) {
69 delegate_allowed_close_ = true; 72 delegate_allowed_close_ = true;
70 GetWidget()->Close(); 73 GetWidget()->Close();
71 } 74 }
72 } 75 }
73 76
74 void DialogClientView::CancelWindow() { 77 void DialogClientView::CancelWindow() {
75 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. 78 // Only notify the delegate once. See |delegate_allowed_close_|'s comment.
76 if (!delegate_allowed_close_ && GetDialogDelegate()->Cancel()) { 79 if (!delegate_allowed_close_ && GetDialogDelegate()->Cancel()) {
77 delegate_allowed_close_ = true; 80 delegate_allowed_close_ = true;
78 GetWidget()->Close(); 81 GetWidget()->Close();
79 } 82 }
80 } 83 }
81 84
82 void DialogClientView::UpdateDialogButtons() { 85 void DialogClientView::UpdateDialogButtons() {
83 const int buttons = GetDialogDelegate()->GetDialogButtons(); 86 const int buttons = GetDialogDelegate()->GetDialogButtons();
84 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); 87 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE);
85 88
86 if (buttons & ui::DIALOG_BUTTON_OK) { 89 if (buttons & ui::DIALOG_BUTTON_OK) {
87 if (!ok_button_) { 90 if (!ok_button_) {
88 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); 91 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK);
89 if (!(buttons & ui::DIALOG_BUTTON_CANCEL))
90 ok_button_->AddAccelerator(escape);
91 AddChildView(ok_button_); 92 AddChildView(ok_button_);
92 } 93 }
93 94
94 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); 95 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK);
95 } else if (ok_button_) { 96 } else if (ok_button_) {
96 delete ok_button_; 97 delete ok_button_;
97 ok_button_ = NULL; 98 ok_button_ = NULL;
98 } 99 }
99 100
100 if (buttons & ui::DIALOG_BUTTON_CANCEL) { 101 if (buttons & ui::DIALOG_BUTTON_CANCEL) {
101 if (!cancel_button_) { 102 if (!cancel_button_) {
102 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); 103 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL);
103 cancel_button_->AddAccelerator(escape); 104 cancel_button_->AddAccelerator(escape);
104 AddChildView(cancel_button_); 105 AddChildView(cancel_button_);
105 } 106 }
106 107
107 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); 108 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL);
108 } else if (cancel_button_) { 109 } else if (cancel_button_) {
109 delete cancel_button_; 110 delete cancel_button_;
110 cancel_button_ = NULL; 111 cancel_button_ = NULL;
111 } 112 }
112 113
113 // 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 is no cancel button.
114 if (!has_dialog_buttons()) 115 if (!cancel_button_)
115 AddAccelerator(escape); 116 AddAccelerator(escape);
116 else 117 else
117 ResetAccelerators(); 118 ResetAccelerators();
118 } 119 }
119 120
120 /////////////////////////////////////////////////////////////////////////////// 121 ///////////////////////////////////////////////////////////////////////////////
121 // DialogClientView, ClientView overrides: 122 // DialogClientView, ClientView overrides:
122 123
123 bool DialogClientView::CanClose() { 124 bool DialogClientView::CanClose() {
124 // If the dialog is closing but no Accept or Cancel action has been performed 125 // If the dialog is closing but no Accept or Cancel action has been performed
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 const gfx::Insets insets = GetButtonRowInsets(); 161 const gfx::Insets insets = GetButtonRowInsets();
161 size.Enlarge(insets.width(), insets.height()); 162 size.Enlarge(insets.width(), insets.height());
162 } 163 }
163 164
164 // Increase the size as needed to fit the contents view. 165 // Increase the size as needed to fit the contents view.
165 // NOTE: The contents view is not inset on the top or side client view edges. 166 // NOTE: The contents view is not inset on the top or side client view edges.
166 gfx::Size contents_size = contents_view()->GetPreferredSize(); 167 gfx::Size contents_size = contents_view()->GetPreferredSize();
167 size.Enlarge(0, contents_size.height()); 168 size.Enlarge(0, contents_size.height());
168 size.set_width(std::max(size.width(), contents_size.width())); 169 size.set_width(std::max(size.width(), contents_size.width()));
169 170
170 // Increase the size as needed to fit the footnote view.
171 if (ShouldShow(footnote_view_)) {
172 gfx::Size footnote_size = footnote_view_->GetPreferredSize();
173 if (!footnote_size.IsEmpty())
174 size.set_width(std::max(size.width(), footnote_size.width()));
175
176 int footnote_height = footnote_view_->GetHeightForWidth(size.width());
177 size.Enlarge(0, footnote_height);
178 }
179
180 return size; 171 return size;
181 } 172 }
182 173
183 void DialogClientView::Layout() { 174 void DialogClientView::Layout() {
184 gfx::Rect bounds = GetContentsBounds(); 175 gfx::Rect bounds = GetContentsBounds();
185 176
186 // Layout the footnote view.
187 if (ShouldShow(footnote_view_)) {
188 const int height = footnote_view_->GetHeightForWidth(bounds.width());
189 footnote_view_->SetBounds(bounds.x(), bounds.bottom() - height,
190 bounds.width(), height);
191 if (height != 0)
192 bounds.Inset(0, 0, 0, height);
193 }
194
195 // Layout the row containing the buttons and the extra view. 177 // Layout the row containing the buttons and the extra view.
196 if (has_dialog_buttons() || ShouldShow(extra_view_)) { 178 if (has_dialog_buttons() || ShouldShow(extra_view_)) {
197 bounds.Inset(GetButtonRowInsets()); 179 bounds.Inset(GetButtonRowInsets());
198 const int height = GetButtonsAndExtraViewRowHeight(); 180 const int height = GetButtonsAndExtraViewRowHeight();
199 gfx::Rect row_bounds(bounds.x(), bounds.bottom() - height, 181 gfx::Rect row_bounds(bounds.x(), bounds.bottom() - height,
200 bounds.width(), height); 182 bounds.width(), height);
201 if (kIsOkButtonOnLeftSide) { 183 if (kIsOkButtonOnLeftSide) {
202 LayoutButton(cancel_button_, &row_bounds); 184 LayoutButton(cancel_button_, &row_bounds);
203 LayoutButton(ok_button_, &row_bounds); 185 LayoutButton(ok_button_, &row_bounds);
204 } else { 186 } else {
(...skipping 30 matching lines...) Expand all
235 GetWidget()->Close(); 217 GetWidget()->Close();
236 return true; 218 return true;
237 } 219 }
238 220
239 void DialogClientView::ViewHierarchyChanged( 221 void DialogClientView::ViewHierarchyChanged(
240 const ViewHierarchyChangedDetails& details) { 222 const ViewHierarchyChangedDetails& details) {
241 ClientView::ViewHierarchyChanged(details); 223 ClientView::ViewHierarchyChanged(details);
242 if (details.is_add && details.child == this) { 224 if (details.is_add && details.child == this) {
243 UpdateDialogButtons(); 225 UpdateDialogButtons();
244 CreateExtraView(); 226 CreateExtraView();
245 CreateFootnoteView();
246 } else if (!details.is_add && details.child != this) { 227 } else if (!details.is_add && details.child != this) {
247 if (details.child == ok_button_) 228 if (details.child == ok_button_)
248 ok_button_ = nullptr; 229 ok_button_ = nullptr;
249 else if (details.child == cancel_button_) 230 else if (details.child == cancel_button_)
250 cancel_button_ = nullptr; 231 cancel_button_ = nullptr;
251 else if (details.child == extra_view_) 232 else if (details.child == extra_view_)
252 extra_view_ = nullptr; 233 extra_view_ = nullptr;
253 else if (details.child == footnote_view_)
254 footnote_view_ = nullptr;
255 } 234 }
256 235
257 SetupFocusChain(); 236 SetupFocusChain();
258 } 237 }
259 238
260 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { 239 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
261 // The old dialog style needs an explicit background color, while the new 240 // The old dialog style needs an explicit background color, while the new
262 // dialog style simply inherits the bubble's frame view color. 241 // dialog style simply inherits the bubble's frame view color.
263 const DialogDelegate* dialog = GetDialogDelegate(); 242 const DialogDelegate* dialog = GetDialogDelegate();
264 243
(...skipping 20 matching lines...) Expand all
285 } 264 }
286 265
287 //////////////////////////////////////////////////////////////////////////////// 266 ////////////////////////////////////////////////////////////////////////////////
288 // DialogClientView, protected: 267 // DialogClientView, protected:
289 268
290 DialogClientView::DialogClientView(View* contents_view) 269 DialogClientView::DialogClientView(View* contents_view)
291 : ClientView(NULL, contents_view), 270 : ClientView(NULL, contents_view),
292 ok_button_(NULL), 271 ok_button_(NULL),
293 cancel_button_(NULL), 272 cancel_button_(NULL),
294 extra_view_(NULL), 273 extra_view_(NULL),
295 footnote_view_(NULL),
296 delegate_allowed_close_(false) {} 274 delegate_allowed_close_(false) {}
297 275
298 DialogDelegate* DialogClientView::GetDialogDelegate() const { 276 DialogDelegate* DialogClientView::GetDialogDelegate() const {
299 return GetWidget()->widget_delegate()->AsDialogDelegate(); 277 return GetWidget()->widget_delegate()->AsDialogDelegate();
300 } 278 }
301 279
302 void DialogClientView::CreateExtraView() { 280 void DialogClientView::CreateExtraView() {
303 if (extra_view_) 281 if (extra_view_)
304 return; 282 return;
305 283
306 extra_view_ = GetDialogDelegate()->CreateExtraView(); 284 extra_view_ = GetDialogDelegate()->CreateExtraView();
307 if (extra_view_) { 285 if (extra_view_) {
308 extra_view_->SetGroup(kButtonGroup); 286 extra_view_->SetGroup(kButtonGroup);
309 AddChildView(extra_view_); 287 AddChildView(extra_view_);
310 } 288 }
311 } 289 }
312 290
313 void DialogClientView::CreateFootnoteView() {
314 if (footnote_view_)
315 return;
316
317 footnote_view_ = GetDialogDelegate()->CreateFootnoteView();
318 if (footnote_view_)
319 AddChildView(footnote_view_);
320 }
321
322 void DialogClientView::ChildPreferredSizeChanged(View* child) { 291 void DialogClientView::ChildPreferredSizeChanged(View* child) {
323 if (child == footnote_view_ || child == extra_view_) 292 if (child == extra_view_)
324 Layout(); 293 Layout();
325 } 294 }
326 295
327 void DialogClientView::ChildVisibilityChanged(View* child) { 296 void DialogClientView::ChildVisibilityChanged(View* child) {
328 ChildPreferredSizeChanged(child); 297 ChildPreferredSizeChanged(child);
329 } 298 }
330 299
331 //////////////////////////////////////////////////////////////////////////////// 300 ////////////////////////////////////////////////////////////////////////////////
332 // DialogClientView, private: 301 // DialogClientView, private:
333 302
(...skipping 27 matching lines...) Expand all
361 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { 330 int DialogClientView::GetButtonsAndExtraViewRowHeight() const {
362 int extra_view_height = ShouldShow(extra_view_) ? 331 int extra_view_height = ShouldShow(extra_view_) ?
363 extra_view_->GetPreferredSize().height() : 0; 332 extra_view_->GetPreferredSize().height() : 0;
364 int buttons_height = std::max( 333 int buttons_height = std::max(
365 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, 334 ok_button_ ? ok_button_->GetPreferredSize().height() : 0,
366 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); 335 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0);
367 return std::max(extra_view_height, buttons_height); 336 return std::max(extra_view_height, buttons_height);
368 } 337 }
369 338
370 gfx::Insets DialogClientView::GetButtonRowInsets() const { 339 gfx::Insets DialogClientView::GetButtonRowInsets() const {
371 // NOTE: The insets only apply to the buttons, extra view, and footnote view. 340 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets()
372 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() : 341 : button_row_insets_;
373 gfx::Insets(0, kButtonHEdgeMarginNew,
374 kButtonVEdgeMarginNew, kButtonHEdgeMarginNew);
375 } 342 }
376 343
377
378
379 void DialogClientView::SetupFocusChain() { 344 void DialogClientView::SetupFocusChain() {
380 // Create a vector of child views in the order of intended focus. 345 // Create a vector of child views in the order of intended focus.
381 std::vector<View*> child_views; 346 std::vector<View*> child_views;
382 child_views.push_back(contents_view()); 347 child_views.push_back(contents_view());
383 child_views.push_back(extra_view_); 348 child_views.push_back(extra_view_);
384 if (kIsOkButtonOnLeftSide) { 349 if (kIsOkButtonOnLeftSide) {
385 child_views.push_back(ok_button_); 350 child_views.push_back(ok_button_);
386 child_views.push_back(cancel_button_); 351 child_views.push_back(cancel_button_);
387 } else { 352 } else {
388 child_views.push_back(cancel_button_); 353 child_views.push_back(cancel_button_);
389 child_views.push_back(ok_button_); 354 child_views.push_back(ok_button_);
390 } 355 }
391 child_views.push_back(footnote_view_);
392 356
393 // Remove all null views from the vector. 357 // Remove all null views from the vector.
394 child_views.erase( 358 child_views.erase(
395 std::remove(child_views.begin(), child_views.end(), nullptr), 359 std::remove(child_views.begin(), child_views.end(), nullptr),
396 child_views.end()); 360 child_views.end());
397 361
398 // Setup focus. 362 // Setup focus.
399 for (size_t i = 0; i < child_views.size(); i++) { 363 for (size_t i = 0; i < child_views.size(); i++) {
400 child_views[i]->SetNextFocusableView( 364 child_views[i]->SetNextFocusableView(
401 i + 1 != child_views.size() ? child_views[i + 1] : nullptr); 365 i + 1 != child_views.size() ? child_views[i + 1] : nullptr);
402 } 366 }
403 } 367 }
404 368
405 } // namespace views 369 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/window/dialog_client_view.h ('k') | ui/views/window/dialog_client_view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698