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

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

Issue 1645013004: Views - dialog button defaultness shouldn't follow focus. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix win compile 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
« 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 ok_button_(NULL), 57 ok_button_(NULL),
58 cancel_button_(NULL), 58 cancel_button_(NULL),
59 default_button_(NULL),
60 focus_manager_(NULL),
61 extra_view_(NULL), 59 extra_view_(NULL),
62 footnote_view_(NULL), 60 footnote_view_(NULL),
63 notified_delegate_(false) { 61 notified_delegate_(false) {
64 } 62 }
65 63
66 DialogClientView::~DialogClientView() { 64 DialogClientView::~DialogClientView() {
67 } 65 }
68 66
69 void DialogClientView::AcceptWindow() { 67 void DialogClientView::AcceptWindow() {
70 // Only notify the delegate once. See |notified_delegate_|'s comment. 68 // Only notify the delegate once. See |notified_delegate_|'s comment.
71 if (!notified_delegate_ && GetDialogDelegate()->Accept(false)) { 69 if (!notified_delegate_ && GetDialogDelegate()->Accept(false)) {
72 notified_delegate_ = true; 70 notified_delegate_ = true;
73 Close(); 71 Close();
74 } 72 }
75 } 73 }
76 74
77 void DialogClientView::CancelWindow() { 75 void DialogClientView::CancelWindow() {
78 // Only notify the delegate once. See |notified_delegate_|'s comment. 76 // Only notify the delegate once. See |notified_delegate_|'s comment.
79 if (!notified_delegate_ && GetDialogDelegate()->Cancel()) { 77 if (!notified_delegate_ && GetDialogDelegate()->Cancel()) {
80 notified_delegate_ = true; 78 notified_delegate_ = true;
81 Close(); 79 Close();
82 } 80 }
83 } 81 }
84 82
85 void DialogClientView::UpdateDialogButtons() { 83 void DialogClientView::UpdateDialogButtons() {
86 const int buttons = GetDialogDelegate()->GetDialogButtons(); 84 const int buttons = GetDialogDelegate()->GetDialogButtons();
87 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); 85 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE);
88 if (default_button_)
89 default_button_->SetIsDefault(false);
90 default_button_ = NULL;
91 86
92 if (buttons & ui::DIALOG_BUTTON_OK) { 87 if (buttons & ui::DIALOG_BUTTON_OK) {
93 if (!ok_button_) { 88 if (!ok_button_) {
94 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); 89 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK);
95 if (!(buttons & ui::DIALOG_BUTTON_CANCEL)) 90 if (!(buttons & ui::DIALOG_BUTTON_CANCEL))
96 ok_button_->AddAccelerator(escape); 91 ok_button_->AddAccelerator(escape);
97 AddChildView(ok_button_); 92 AddChildView(ok_button_);
98 } 93 }
99 94
100 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); 95 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 } 136 }
142 137
143 DialogClientView* DialogClientView::AsDialogClientView() { 138 DialogClientView* DialogClientView::AsDialogClientView() {
144 return this; 139 return this;
145 } 140 }
146 141
147 const DialogClientView* DialogClientView::AsDialogClientView() const { 142 const DialogClientView* DialogClientView::AsDialogClientView() const {
148 return this; 143 return this;
149 } 144 }
150 145
151 void DialogClientView::OnWillChangeFocus(View* focused_before,
152 View* focused_now) {
153 // Make the newly focused button default or restore the dialog's default.
154 const int default_button = GetDialogDelegate()->GetDefaultDialogButton();
155 LabelButton* new_default_button = NULL;
156 if (focused_now &&
157 !strcmp(focused_now->GetClassName(), LabelButton::kViewClassName)) {
158 new_default_button = static_cast<LabelButton*>(focused_now);
159 } else if (default_button == ui::DIALOG_BUTTON_OK && ok_button_) {
160 new_default_button = ok_button_;
161 } else if (default_button == ui::DIALOG_BUTTON_CANCEL && cancel_button_) {
162 new_default_button = cancel_button_;
163 }
164
165 if (default_button_ && default_button_ != new_default_button)
166 default_button_->SetIsDefault(false);
167 default_button_ = new_default_button;
168 if (default_button_ && !default_button_->is_default())
169 default_button_->SetIsDefault(true);
170 }
171
172 void DialogClientView::OnDidChangeFocus(View* focused_before,
173 View* focused_now) {
174 }
175
176 //////////////////////////////////////////////////////////////////////////////// 146 ////////////////////////////////////////////////////////////////////////////////
177 // DialogClientView, View overrides: 147 // DialogClientView, View overrides:
178 148
179 gfx::Size DialogClientView::GetPreferredSize() const { 149 gfx::Size DialogClientView::GetPreferredSize() const {
180 // Initialize the size to fit the buttons and extra view row. 150 // Initialize the size to fit the buttons and extra view row.
181 int extra_view_padding = 0; 151 int extra_view_padding = 0;
182 if (!GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding)) 152 if (!GetDialogDelegate()->GetExtraViewPadding(&extra_view_padding))
183 extra_view_padding = kRelatedButtonHSpacing; 153 extra_view_padding = kRelatedButtonHSpacing;
184 gfx::Size size( 154 gfx::Size size(
185 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) + 155 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) +
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) { 240 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) {
271 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE); 241 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE);
272 Close(); 242 Close();
273 return true; 243 return true;
274 } 244 }
275 245
276 void DialogClientView::ViewHierarchyChanged( 246 void DialogClientView::ViewHierarchyChanged(
277 const ViewHierarchyChangedDetails& details) { 247 const ViewHierarchyChangedDetails& details) {
278 ClientView::ViewHierarchyChanged(details); 248 ClientView::ViewHierarchyChanged(details);
279 if (details.is_add && details.child == this) { 249 if (details.is_add && details.child == this) {
280 focus_manager_ = GetFocusManager();
281 if (focus_manager_)
282 GetFocusManager()->AddFocusChangeListener(this);
283
284 UpdateDialogButtons(); 250 UpdateDialogButtons();
285 CreateExtraView(); 251 CreateExtraView();
286 CreateFootnoteView(); 252 CreateFootnoteView();
287 } else if (!details.is_add && details.child == this) { 253 } else if (!details.is_add && details.child != this) {
288 if (focus_manager_)
289 focus_manager_->RemoveFocusChangeListener(this);
290 focus_manager_ = NULL;
291 } else if (!details.is_add) {
292 if (details.child == default_button_)
293 default_button_ = NULL;
294 if (details.child == ok_button_) 254 if (details.child == ok_button_)
295 ok_button_ = NULL; 255 ok_button_ = NULL;
296 if (details.child == cancel_button_) 256 if (details.child == cancel_button_)
297 cancel_button_ = NULL; 257 cancel_button_ = NULL;
298 } 258 }
299 } 259 }
300 260
301 void DialogClientView::NativeViewHierarchyChanged() {
302 FocusManager* focus_manager = GetFocusManager();
303 if (focus_manager_ != focus_manager) {
304 if (focus_manager_)
305 focus_manager_->RemoveFocusChangeListener(this);
306 focus_manager_ = focus_manager;
307 if (focus_manager_)
308 focus_manager_->AddFocusChangeListener(this);
309 }
310 }
311
312 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { 261 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
313 // The old dialog style needs an explicit background color, while the new 262 // The old dialog style needs an explicit background color, while the new
314 // dialog style simply inherits the bubble's frame view color. 263 // dialog style simply inherits the bubble's frame view color.
315 const DialogDelegate* dialog = GetDialogDelegate(); 264 const DialogDelegate* dialog = GetDialogDelegate();
316 265
317 if (dialog && !dialog->UseNewStyleForThisDialog()) { 266 if (dialog && !dialog->UseNewStyleForThisDialog()) {
318 set_background(views::Background::CreateSolidBackground(GetNativeTheme()-> 267 set_background(views::Background::CreateSolidBackground(GetNativeTheme()->
319 GetSystemColor(ui::NativeTheme::kColorId_DialogBackground))); 268 GetSystemColor(ui::NativeTheme::kColorId_DialogBackground)));
320 } 269 }
321 } 270 }
(...skipping 14 matching lines...) Expand all
336 NOTREACHED(); 285 NOTREACHED();
337 } 286 }
338 287
339 //////////////////////////////////////////////////////////////////////////////// 288 ////////////////////////////////////////////////////////////////////////////////
340 // DialogClientView, protected: 289 // DialogClientView, protected:
341 290
342 DialogClientView::DialogClientView(View* contents_view) 291 DialogClientView::DialogClientView(View* contents_view)
343 : ClientView(NULL, contents_view), 292 : ClientView(NULL, contents_view),
344 ok_button_(NULL), 293 ok_button_(NULL),
345 cancel_button_(NULL), 294 cancel_button_(NULL),
346 default_button_(NULL),
347 focus_manager_(NULL),
348 extra_view_(NULL), 295 extra_view_(NULL),
349 footnote_view_(NULL), 296 footnote_view_(NULL),
350 notified_delegate_(false) {} 297 notified_delegate_(false) {}
351 298
352 DialogDelegate* DialogClientView::GetDialogDelegate() const { 299 DialogDelegate* DialogClientView::GetDialogDelegate() const {
353 return GetWidget()->widget_delegate()->AsDialogDelegate(); 300 return GetWidget()->widget_delegate()->AsDialogDelegate();
354 } 301 }
355 302
356 void DialogClientView::CreateExtraView() { 303 void DialogClientView::CreateExtraView() {
357 if (extra_view_) 304 if (extra_view_)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 button->SetMinSize(gfx::Size(kDialogMinButtonWidth, 0)); 349 button->SetMinSize(gfx::Size(kDialogMinButtonWidth, 0));
403 button->SetGroup(kButtonGroup); 350 button->SetGroup(kButtonGroup);
404 return button; 351 return button;
405 } 352 }
406 353
407 void DialogClientView::UpdateButton(LabelButton* button, 354 void DialogClientView::UpdateButton(LabelButton* button,
408 ui::DialogButton type) { 355 ui::DialogButton type) {
409 DialogDelegate* dialog = GetDialogDelegate(); 356 DialogDelegate* dialog = GetDialogDelegate();
410 button->SetText(dialog->GetDialogButtonLabel(type)); 357 button->SetText(dialog->GetDialogButtonLabel(type));
411 button->SetEnabled(dialog->IsDialogButtonEnabled(type)); 358 button->SetEnabled(dialog->IsDialogButtonEnabled(type));
412 359 button->SetIsDefault(type == dialog->GetDefaultDialogButton());
413 if (type == dialog->GetDefaultDialogButton()) {
414 default_button_ = button;
415 button->SetIsDefault(true);
416 }
417 } 360 }
418 361
419 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { 362 int DialogClientView::GetButtonsAndExtraViewRowHeight() const {
420 int extra_view_height = ShouldShow(extra_view_) ? 363 int extra_view_height = ShouldShow(extra_view_) ?
421 extra_view_->GetPreferredSize().height() : 0; 364 extra_view_->GetPreferredSize().height() : 0;
422 int buttons_height = std::max( 365 int buttons_height = std::max(
423 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, 366 ok_button_ ? ok_button_->GetPreferredSize().height() : 0,
424 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); 367 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0);
425 return std::max(extra_view_height, buttons_height); 368 return std::max(extra_view_height, buttons_height);
426 } 369 }
427 370
428 gfx::Insets DialogClientView::GetButtonRowInsets() const { 371 gfx::Insets DialogClientView::GetButtonRowInsets() const {
429 // NOTE: The insets only apply to the buttons, extra view, and footnote view. 372 // NOTE: The insets only apply to the buttons, extra view, and footnote view.
430 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() : 373 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() :
431 gfx::Insets(0, kButtonHEdgeMarginNew, 374 gfx::Insets(0, kButtonHEdgeMarginNew,
432 kButtonVEdgeMarginNew, kButtonHEdgeMarginNew); 375 kButtonVEdgeMarginNew, kButtonHEdgeMarginNew);
433 } 376 }
434 377
435 void DialogClientView::Close() { 378 void DialogClientView::Close() {
436 GetWidget()->Close(); 379 GetWidget()->Close();
437 GetDialogDelegate()->OnClosed(); 380 GetDialogDelegate()->OnClosed();
438 } 381 }
439 382
440 } // namespace views 383 } // 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