Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/ui/views/toolbar/toolbar_actions_bar_bubble_views.h" | 5 #include "chrome/browser/ui/views/toolbar/toolbar_actions_bar_bubble_views.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_bubble_delegate.h" | 7 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_bubble_delegate.h" |
| 8 #include "chrome/grit/locale_settings.h" | 8 #include "chrome/grit/locale_settings.h" |
| 9 #include "ui/base/resource/resource_bundle.h" | 9 #include "ui/base/resource/resource_bundle.h" |
| 10 #include "ui/views/controls/button/label_button.h" | 10 #include "ui/views/controls/button/label_button.h" |
| 11 #include "ui/views/controls/label.h" | 11 #include "ui/views/controls/label.h" |
| 12 #include "ui/views/layout/grid_layout.h" | 12 #include "ui/views/layout/fill_layout.h" |
| 13 #include "ui/views/layout/layout_constants.h" | |
| 14 | 13 |
| 15 ToolbarActionsBarBubbleViews::ToolbarActionsBarBubbleViews( | 14 ToolbarActionsBarBubbleViews::ToolbarActionsBarBubbleViews( |
| 16 views::View* anchor_view, | 15 views::View* anchor_view, |
| 17 std::unique_ptr<ToolbarActionsBarBubbleDelegate> delegate) | 16 std::unique_ptr<ToolbarActionsBarBubbleDelegate> delegate) |
| 18 : views::BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), | 17 : views::BubbleDialogDelegateView(anchor_view, |
| 19 delegate_(std::move(delegate)), | 18 views::BubbleBorder::TOP_RIGHT), |
| 20 heading_label_(nullptr), | 19 delegate_(std::move(delegate)) {} |
| 21 content_label_(nullptr), | |
| 22 dismiss_button_(nullptr), | |
| 23 action_button_(nullptr), | |
| 24 acknowledged_(false) {} | |
| 25 | 20 |
| 26 ToolbarActionsBarBubbleViews::~ToolbarActionsBarBubbleViews() {} | 21 ToolbarActionsBarBubbleViews::~ToolbarActionsBarBubbleViews() {} |
| 27 | 22 |
| 28 void ToolbarActionsBarBubbleViews::Show() { | 23 void ToolbarActionsBarBubbleViews::Show() { |
| 29 delegate_->OnBubbleShown(); | 24 delegate_->OnBubbleShown(); |
| 30 GetWidget()->Show(); | 25 GetWidget()->Show(); |
| 31 } | 26 } |
| 32 | 27 |
| 28 base::string16 ToolbarActionsBarBubbleViews::GetWindowTitle() const { | |
| 29 return delegate_->GetHeadingText(); | |
| 30 } | |
| 31 | |
| 32 bool ToolbarActionsBarBubbleViews::Cancel() { | |
| 33 // Technically we don't know if it's an explicit user action or deactivation, | |
| 34 // but this reason isn't used for anything currently. | |
|
Devlin
2016/04/12 23:01:05
This will be used after https://codereview.chromiu
Evan Stade
2016/04/12 23:33:58
Done.
| |
| 35 delegate_->OnBubbleClosed( | |
| 36 ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS_USER_ACTION); | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 bool ToolbarActionsBarBubbleViews::Accept() { | |
| 41 delegate_->OnBubbleClosed(ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE); | |
| 42 return true; | |
| 43 } | |
| 44 | |
| 45 int ToolbarActionsBarBubbleViews::GetDialogButtons() const { | |
| 46 int buttons = ui::DIALOG_BUTTON_OK; | |
| 47 if (!delegate_->GetDismissButtonText().empty()) | |
| 48 buttons |= ui::DIALOG_BUTTON_CANCEL; | |
| 49 return buttons; | |
| 50 } | |
| 51 | |
| 52 base::string16 ToolbarActionsBarBubbleViews::GetDialogButtonLabel( | |
| 53 ui::DialogButton button) const { | |
| 54 return button == ui::DIALOG_BUTTON_OK ? delegate_->GetActionButtonText() | |
|
Devlin
2016/04/12 23:01:05
optional nit: I'd slightly prefer a switch here so
Evan Stade
2016/04/12 23:33:58
I see what you're saying, but this pattern is pret
| |
| 55 : delegate_->GetDismissButtonText(); | |
| 56 } | |
| 57 | |
| 33 void ToolbarActionsBarBubbleViews::Init() { | 58 void ToolbarActionsBarBubbleViews::Init() { |
| 34 views::GridLayout* layout = new views::GridLayout(this); | 59 SetLayoutManager(new views::FillLayout()); |
| 35 layout->SetInsets(views::kPanelVertMargin, views::kPanelHorizMargin, | |
| 36 views::kPanelVertMargin, views::kPanelHorizMargin); | |
| 37 SetLayoutManager(layout); | |
| 38 | 60 |
| 39 enum ColumnSetId { | |
| 40 HEADER_AND_BODY_COLUMN_SET = 0, | |
| 41 BUTTON_STRIP_COLUMN_SET, | |
| 42 }; | |
| 43 | |
| 44 views::ColumnSet* header_and_body_cs = | |
| 45 layout->AddColumnSet(HEADER_AND_BODY_COLUMN_SET); | |
| 46 header_and_body_cs->AddColumn(views::GridLayout::TRAILING, | |
| 47 views::GridLayout::LEADING, 1, | |
| 48 views::GridLayout::USE_PREF, 0, 0); | |
| 49 views::ColumnSet* buttons_cs = layout->AddColumnSet(BUTTON_STRIP_COLUMN_SET); | |
| 50 buttons_cs->AddPaddingColumn(1, 0); | |
| 51 buttons_cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::LEADING, | |
| 52 0, views::GridLayout::USE_PREF, 0, 0); | |
| 53 buttons_cs->AddPaddingColumn(0, views::kRelatedButtonHSpacing); | |
| 54 buttons_cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::LEADING, | |
| 55 0, views::GridLayout::USE_PREF, 0, 0); | |
| 56 | |
| 57 // Add a header. | |
| 58 layout->StartRow(0, HEADER_AND_BODY_COLUMN_SET); | |
| 59 heading_label_ = new views::Label(delegate_->GetHeadingText()); | |
| 60 heading_label_->SetFontList( | |
| 61 ui::ResourceBundle::GetSharedInstance().GetFontList( | |
| 62 ui::ResourceBundle::MediumFont)); | |
| 63 heading_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 64 layout->AddView(heading_label_); | |
| 65 layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing); | |
| 66 int width = views::Widget::GetLocalizedContentsWidth( | 61 int width = views::Widget::GetLocalizedContentsWidth( |
| 67 IDS_EXTENSION_TOOLBAR_REDESIGN_NOTIFICATION_BUBBLE_WIDTH_CHARS); | 62 IDS_EXTENSION_TOOLBAR_REDESIGN_NOTIFICATION_BUBBLE_WIDTH_CHARS); |
| 68 | |
| 69 // Add the content string. | 63 // Add the content string. |
| 70 layout->StartRow(0, HEADER_AND_BODY_COLUMN_SET); | 64 views::Label* content_label = new views::Label(delegate_->GetBodyText()); |
| 71 content_label_ = new views::Label(delegate_->GetBodyText()); | 65 content_label->SetMultiLine(true); |
| 72 content_label_->SetMultiLine(true); | 66 content_label->SizeToFit(width); |
| 73 content_label_->SizeToFit(width); | 67 content_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 74 content_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 68 AddChildView(content_label); |
| 75 layout->AddView(content_label_); | |
| 76 layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing); | |
| 77 | |
| 78 layout->StartRow(0, BUTTON_STRIP_COLUMN_SET); | |
| 79 base::string16 dismiss_button_text = delegate_->GetDismissButtonText(); | |
| 80 if (!dismiss_button_text.empty()) { | |
| 81 dismiss_button_ = new views::LabelButton(this, dismiss_button_text); | |
| 82 dismiss_button_->SetStyle(views::Button::STYLE_BUTTON); | |
| 83 layout->AddView(dismiss_button_, 1, 1, views::GridLayout::TRAILING, | |
| 84 views::GridLayout::FILL); | |
| 85 } else { | |
| 86 layout->SkipColumns(1); | |
| 87 } | |
| 88 | |
| 89 action_button_ = | |
| 90 new views::LabelButton(this, delegate_->GetActionButtonText()); | |
| 91 action_button_->SetStyle(views::Button::STYLE_BUTTON); | |
| 92 layout->AddView(action_button_, 1, 1, views::GridLayout::TRAILING, | |
| 93 views::GridLayout::FILL); | |
| 94 } | 69 } |
| 95 | |
| 96 void ToolbarActionsBarBubbleViews::OnWidgetDestroying(views::Widget* widget) { | |
| 97 BubbleDelegateView::OnWidgetDestroying(widget); | |
| 98 if (!acknowledged_) { | |
| 99 ToolbarActionsBarBubbleDelegate::CloseAction close_action = | |
| 100 close_reason() == CloseReason::DEACTIVATION | |
| 101 ? ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS_DEACTIVATION | |
| 102 : ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS_USER_ACTION; | |
| 103 delegate_->OnBubbleClosed(close_action); | |
| 104 acknowledged_ = true; | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 void ToolbarActionsBarBubbleViews::ButtonPressed(views::Button* sender, | |
| 109 const ui::Event& event) { | |
| 110 delegate_->OnBubbleClosed(ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE); | |
| 111 acknowledged_ = true; | |
| 112 GetWidget()->Close(); | |
| 113 } | |
| OLD | NEW |