| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/extensions/bookmark_app_confirmation_view.h" | 5 #include "chrome/browser/ui/views/extensions/bookmark_app_confirmation_view.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" | 11 #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" |
| 12 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" |
| 13 #include "components/constrained_window/constrained_window_views.h" | 13 #include "components/constrained_window/constrained_window_views.h" |
| 14 #include "components/strings/grit/components_strings.h" | 14 #include "components/strings/grit/components_strings.h" |
| 15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 16 #include "extensions/common/constants.h" | 16 #include "extensions/common/constants.h" |
| 17 #include "ui/accessibility/ax_node_data.h" | 17 #include "ui/accessibility/ax_node_data.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 20 #include "ui/gfx/image/image_skia.h" | 20 #include "ui/gfx/image/image_skia.h" |
| 21 #include "ui/gfx/image/image_skia_source.h" | 21 #include "ui/gfx/image/image_skia_source.h" |
| 22 #include "ui/views/controls/button/checkbox.h" | 22 #include "ui/views/controls/button/checkbox.h" |
| 23 #include "ui/views/controls/image_view.h" | |
| 24 #include "ui/views/controls/textfield/textfield.h" | 23 #include "ui/views/controls/textfield/textfield.h" |
| 25 #include "ui/views/layout/box_layout.h" | 24 #include "ui/views/layout/grid_layout.h" |
| 25 #include "ui/views/layout/layout_provider.h" |
| 26 #include "ui/views/widget/widget.h" | 26 #include "ui/views/widget/widget.h" |
| 27 #include "ui/views/window/dialog_client_view.h" | 27 #include "ui/views/window/dialog_client_view.h" |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // Minimum width of the the bubble. | 31 // Minimum width of the the bubble. |
| 32 const int kMinBubbleWidth = 300; | 32 const int kMinBubbleWidth = 300; |
| 33 // Size of the icon. | |
| 34 const int kIconSize = extension_misc::EXTENSION_ICON_MEDIUM; | |
| 35 | 33 |
| 36 class WebAppInfoImageSource : public gfx::ImageSkiaSource { | 34 class WebAppInfoImageSource : public gfx::ImageSkiaSource { |
| 37 public: | 35 public: |
| 38 WebAppInfoImageSource(int dip_size, const WebApplicationInfo& info) | 36 WebAppInfoImageSource(int dip_size, const WebApplicationInfo& info) |
| 39 : dip_size_(dip_size), info_(info) {} | 37 : dip_size_(dip_size), info_(info) {} |
| 40 ~WebAppInfoImageSource() override {} | 38 ~WebAppInfoImageSource() override {} |
| 41 | 39 |
| 42 private: | 40 private: |
| 43 gfx::ImageSkiaRep GetImageForScale(float scale) override { | 41 gfx::ImageSkiaRep GetImageForScale(float scale) override { |
| 44 int size = base::saturated_cast<int>(dip_size_ * scale); | 42 int size = base::saturated_cast<int>(dip_size_ * scale); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 67 ->Show(); | 65 ->Show(); |
| 68 } | 66 } |
| 69 | 67 |
| 70 BookmarkAppConfirmationView::BookmarkAppConfirmationView( | 68 BookmarkAppConfirmationView::BookmarkAppConfirmationView( |
| 71 const WebApplicationInfo& web_app_info, | 69 const WebApplicationInfo& web_app_info, |
| 72 const BrowserWindow::ShowBookmarkAppBubbleCallback& callback) | 70 const BrowserWindow::ShowBookmarkAppBubbleCallback& callback) |
| 73 : web_app_info_(web_app_info), | 71 : web_app_info_(web_app_info), |
| 74 callback_(callback), | 72 callback_(callback), |
| 75 open_as_window_checkbox_(nullptr), | 73 open_as_window_checkbox_(nullptr), |
| 76 title_tf_(nullptr) { | 74 title_tf_(nullptr) { |
| 77 ChromeLayoutProvider* provider = ChromeLayoutProvider::Get(); | 75 const ChromeLayoutProvider* layout_provider = ChromeLayoutProvider::Get(); |
| 78 // Align the contents with the dialog buttons. | 76 views::GridLayout* layout = views::GridLayout::CreatePanel(this); |
| 79 views::BoxLayout* layout = new views::BoxLayout( | 77 const int column_set_id = 0; |
| 80 views::BoxLayout::kHorizontal, | |
| 81 provider->GetDistanceMetric(DISTANCE_DIALOG_BUTTON_MARGIN), | |
| 82 provider->GetDistanceMetric(DISTANCE_PANEL_CONTENT_MARGIN), | |
| 83 provider->GetDistanceMetric(DISTANCE_UNRELATED_CONTROL_HORIZONTAL_LARGE)); | |
| 84 layout->set_cross_axis_alignment( | |
| 85 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); | |
| 86 SetLayoutManager(layout); | |
| 87 | 78 |
| 79 views::ColumnSet* column_set = layout->AddColumnSet(column_set_id); |
| 80 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 0, |
| 81 views::GridLayout::USE_PREF, 0, 0); |
| 82 column_set->AddPaddingColumn(0, |
| 83 layout_provider->GetDistanceMetric( |
| 84 views::DISTANCE_RELATED_CONTROL_HORIZONTAL)); |
| 85 constexpr int textfield_width = 320; |
| 86 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 0, |
| 87 views::GridLayout::FIXED, textfield_width, 0); |
| 88 |
| 89 const int icon_size = layout_provider->IsHarmonyMode() |
| 90 ? extension_misc::EXTENSION_ICON_SMALL |
| 91 : extension_misc::EXTENSION_ICON_MEDIUM; |
| 88 views::ImageView* icon_image_view = new views::ImageView(); | 92 views::ImageView* icon_image_view = new views::ImageView(); |
| 89 gfx::Size image_size(kIconSize, kIconSize); | 93 gfx::Size image_size(icon_size, icon_size); |
| 90 gfx::ImageSkia image(new WebAppInfoImageSource(kIconSize, web_app_info_), | 94 gfx::ImageSkia image(new WebAppInfoImageSource(icon_size, web_app_info_), |
| 91 image_size); | 95 image_size); |
| 92 icon_image_view->SetImageSize(image_size); | 96 icon_image_view->SetImageSize(image_size); |
| 93 icon_image_view->SetImage(image); | 97 icon_image_view->SetImage(image); |
| 94 AddChildView(icon_image_view); | 98 layout->StartRow(0, column_set_id); |
| 99 layout->AddView(icon_image_view); |
| 95 | 100 |
| 96 title_tf_ = new views::Textfield(); | 101 title_tf_ = new views::Textfield(); |
| 97 title_tf_->SetText(web_app_info_.title); | 102 title_tf_->SetText(web_app_info_.title); |
| 98 title_tf_->SetAccessibleName( | 103 title_tf_->SetAccessibleName( |
| 99 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_AX_BUBBLE_NAME_LABEL)); | 104 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_AX_BUBBLE_NAME_LABEL)); |
| 100 title_tf_->set_controller(this); | 105 title_tf_->set_controller(this); |
| 101 AddChildView(title_tf_); | 106 layout->AddView(title_tf_); |
| 102 layout->SetFlexForView(title_tf_, 1); | 107 |
| 108 layout->AddPaddingRow( |
| 109 0, layout_provider->GetDistanceMetric(DISTANCE_CONTROL_LIST_VERTICAL)); |
| 110 |
| 111 open_as_window_checkbox_ = new views::Checkbox( |
| 112 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_BUBBLE_OPEN_AS_WINDOW)); |
| 113 open_as_window_checkbox_->SetChecked(web_app_info_.open_as_window); |
| 114 layout->StartRow(0, column_set_id); |
| 115 layout->SkipColumns(1); |
| 116 layout->AddView(open_as_window_checkbox_); |
| 103 | 117 |
| 104 title_tf_->SelectAll(true); | 118 title_tf_->SelectAll(true); |
| 105 } | 119 } |
| 106 | 120 |
| 107 views::View* BookmarkAppConfirmationView::GetInitiallyFocusedView() { | 121 views::View* BookmarkAppConfirmationView::GetInitiallyFocusedView() { |
| 108 return title_tf_; | 122 return title_tf_; |
| 109 } | 123 } |
| 110 | 124 |
| 111 ui::ModalType BookmarkAppConfirmationView::GetModalType() const { | 125 ui::ModalType BookmarkAppConfirmationView::GetModalType() const { |
| 112 return ui::MODAL_TYPE_WINDOW; | 126 return ui::MODAL_TYPE_WINDOW; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 124 | 138 |
| 125 bool BookmarkAppConfirmationView::ShouldShowCloseButton() const { | 139 bool BookmarkAppConfirmationView::ShouldShowCloseButton() const { |
| 126 return false; | 140 return false; |
| 127 } | 141 } |
| 128 | 142 |
| 129 void BookmarkAppConfirmationView::WindowClosing() { | 143 void BookmarkAppConfirmationView::WindowClosing() { |
| 130 if (!callback_.is_null()) | 144 if (!callback_.is_null()) |
| 131 callback_.Run(false, web_app_info_); | 145 callback_.Run(false, web_app_info_); |
| 132 } | 146 } |
| 133 | 147 |
| 134 views::View* BookmarkAppConfirmationView::CreateExtraView() { | |
| 135 open_as_window_checkbox_ = new views::Checkbox( | |
| 136 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_BUBBLE_OPEN_AS_WINDOW)); | |
| 137 open_as_window_checkbox_->SetChecked(web_app_info_.open_as_window); | |
| 138 return open_as_window_checkbox_; | |
| 139 } | |
| 140 | |
| 141 bool BookmarkAppConfirmationView::Accept() { | 148 bool BookmarkAppConfirmationView::Accept() { |
| 142 web_app_info_.title = GetTrimmedTitle(); | 149 web_app_info_.title = GetTrimmedTitle(); |
| 143 web_app_info_.open_as_window = open_as_window_checkbox_->checked(); | 150 web_app_info_.open_as_window = open_as_window_checkbox_->checked(); |
| 144 base::ResetAndReturn(&callback_).Run(true, web_app_info_); | 151 base::ResetAndReturn(&callback_).Run(true, web_app_info_); |
| 145 return true; | 152 return true; |
| 146 } | 153 } |
| 147 | 154 |
| 148 base::string16 BookmarkAppConfirmationView::GetDialogButtonLabel( | 155 base::string16 BookmarkAppConfirmationView::GetDialogButtonLabel( |
| 149 ui::DialogButton button) const { | 156 ui::DialogButton button) const { |
| 150 return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK ? IDS_ADD | 157 return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK ? IDS_ADD |
| (...skipping 16 matching lines...) Expand all Loading... |
| 167 const base::string16& new_contents) { | 174 const base::string16& new_contents) { |
| 168 DCHECK_EQ(title_tf_, sender); | 175 DCHECK_EQ(title_tf_, sender); |
| 169 GetDialogClientView()->UpdateDialogButtons(); | 176 GetDialogClientView()->UpdateDialogButtons(); |
| 170 } | 177 } |
| 171 | 178 |
| 172 base::string16 BookmarkAppConfirmationView::GetTrimmedTitle() const { | 179 base::string16 BookmarkAppConfirmationView::GetTrimmedTitle() const { |
| 173 base::string16 title(title_tf_->text()); | 180 base::string16 title(title_tf_->text()); |
| 174 base::TrimWhitespace(title, base::TRIM_ALL, &title); | 181 base::TrimWhitespace(title, base::TRIM_ALL, &title); |
| 175 return title; | 182 return title; |
| 176 } | 183 } |
| OLD | NEW |