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

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

Powered by Google App Engine
This is Rietveld 408576698