| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/extensions/bookmark_app_confirmation_view.h" |
| 6 |
| 7 #include "base/callback_helpers.h" |
| 8 #include "base/strings/string16.h" |
| 9 #include "base/strings/string_util.h" |
| 10 #include "build/build_config.h" |
| 11 #include "chrome/grit/generated_resources.h" |
| 12 #include "components/constrained_window/constrained_window_views.h" |
| 13 #include "content/public/browser/web_contents.h" |
| 14 #include "extensions/common/constants.h" |
| 15 #include "grit/components_strings.h" |
| 16 #include "ui/accessibility/ax_view_state.h" |
| 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/gfx/image/image_skia.h" |
| 20 #include "ui/gfx/image/image_skia_source.h" |
| 21 #include "ui/views/controls/button/checkbox.h" |
| 22 #include "ui/views/controls/image_view.h" |
| 23 #include "ui/views/controls/textfield/textfield.h" |
| 24 #include "ui/views/layout/box_layout.h" |
| 25 #include "ui/views/layout/layout_constants.h" |
| 26 #include "ui/views/widget/widget.h" |
| 27 #include "ui/views/window/dialog_client_view.h" |
| 28 |
| 29 namespace { |
| 30 |
| 31 // Minimum width of the the bubble. |
| 32 const int kMinBubbleWidth = 300; |
| 33 // Size of the icon. |
| 34 const int kIconSize = extension_misc::EXTENSION_ICON_MEDIUM; |
| 35 |
| 36 class WebAppInfoImageSource : public gfx::ImageSkiaSource { |
| 37 public: |
| 38 WebAppInfoImageSource(int dip_size, const WebApplicationInfo& info) |
| 39 : dip_size_(dip_size), info_(info) {} |
| 40 ~WebAppInfoImageSource() override {} |
| 41 |
| 42 private: |
| 43 gfx::ImageSkiaRep GetImageForScale(float scale) override { |
| 44 int size = base::saturated_cast<int>(dip_size_ * scale); |
| 45 for (const auto& icon_info : info_.icons) { |
| 46 if (icon_info.width == size) |
| 47 return gfx::ImageSkiaRep(icon_info.data, scale); |
| 48 } |
| 49 return gfx::ImageSkiaRep(); |
| 50 } |
| 51 |
| 52 int dip_size_; |
| 53 WebApplicationInfo info_; |
| 54 }; |
| 55 |
| 56 } // namespace |
| 57 |
| 58 BookmarkAppConfirmationView::~BookmarkAppConfirmationView() {} |
| 59 |
| 60 // static |
| 61 void BookmarkAppConfirmationView::CreateAndShow( |
| 62 gfx::NativeWindow parent, |
| 63 const WebApplicationInfo& web_app_info, |
| 64 const BrowserWindow::ShowBookmarkAppBubbleCallback& callback) { |
| 65 constrained_window::CreateBrowserModalDialogViews( |
| 66 new BookmarkAppConfirmationView(web_app_info, callback), parent) |
| 67 ->Show(); |
| 68 } |
| 69 |
| 70 BookmarkAppConfirmationView::BookmarkAppConfirmationView( |
| 71 const WebApplicationInfo& web_app_info, |
| 72 const BrowserWindow::ShowBookmarkAppBubbleCallback& callback) |
| 73 : web_app_info_(web_app_info), |
| 74 callback_(callback), |
| 75 open_as_window_checkbox_(nullptr), |
| 76 title_tf_(nullptr) { |
| 77 views::BoxLayout* layout = new views::BoxLayout( |
| 78 views::BoxLayout::kHorizontal, views::kButtonHEdgeMarginNew, |
| 79 views::kButtonHEdgeMarginNew, views::kButtonHEdgeMarginNew); |
| 80 layout->set_cross_axis_alignment( |
| 81 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
| 82 SetLayoutManager(layout); |
| 83 |
| 84 views::ImageView* icon_image_view = new views::ImageView(); |
| 85 gfx::Size image_size(kIconSize, kIconSize); |
| 86 gfx::ImageSkia image(new WebAppInfoImageSource(kIconSize, web_app_info_), |
| 87 image_size); |
| 88 icon_image_view->SetImageSize(image_size); |
| 89 icon_image_view->SetImage(image); |
| 90 AddChildView(icon_image_view); |
| 91 |
| 92 title_tf_ = new views::Textfield(); |
| 93 title_tf_->SetText(web_app_info_.title); |
| 94 title_tf_->SetAccessibleName( |
| 95 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_AX_BUBBLE_NAME_LABEL)); |
| 96 title_tf_->set_controller(this); |
| 97 AddChildView(title_tf_); |
| 98 layout->SetFlexForView(title_tf_, 1); |
| 99 |
| 100 title_tf_->SelectAll(true); |
| 101 } |
| 102 |
| 103 views::View* BookmarkAppConfirmationView::GetInitiallyFocusedView() { |
| 104 return title_tf_; |
| 105 } |
| 106 |
| 107 base::string16 BookmarkAppConfirmationView::GetWindowTitle() const { |
| 108 #if defined(USE_ASH) |
| 109 int ids = IDS_ADD_TO_SHELF_BUBBLE_TITLE; |
| 110 #else |
| 111 int ids = IDS_ADD_TO_DESKTOP_BUBBLE_TITLE; |
| 112 #endif |
| 113 |
| 114 return l10n_util::GetStringUTF16(ids); |
| 115 } |
| 116 |
| 117 bool BookmarkAppConfirmationView::ShouldShowCloseButton() const { |
| 118 return false; |
| 119 } |
| 120 |
| 121 void BookmarkAppConfirmationView::WindowClosing() { |
| 122 if (!callback_.is_null()) |
| 123 callback_.Run(false, web_app_info_); |
| 124 } |
| 125 |
| 126 views::View* BookmarkAppConfirmationView::CreateExtraView() { |
| 127 open_as_window_checkbox_ = new views::Checkbox( |
| 128 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_BUBBLE_OPEN_AS_WINDOW)); |
| 129 open_as_window_checkbox_->SetChecked(web_app_info_.open_as_window); |
| 130 return open_as_window_checkbox_; |
| 131 } |
| 132 |
| 133 bool BookmarkAppConfirmationView::Accept() { |
| 134 web_app_info_.title = GetTrimmedTitle(); |
| 135 web_app_info_.open_as_window = open_as_window_checkbox_->checked(); |
| 136 base::ResetAndReturn(&callback_).Run(true, web_app_info_); |
| 137 return true; |
| 138 } |
| 139 |
| 140 base::string16 BookmarkAppConfirmationView::GetDialogButtonLabel( |
| 141 ui::DialogButton button) const { |
| 142 return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK ? IDS_ADD |
| 143 : IDS_CANCEL); |
| 144 } |
| 145 |
| 146 bool BookmarkAppConfirmationView::IsDialogButtonEnabled( |
| 147 ui::DialogButton button) const { |
| 148 return button == ui::DIALOG_BUTTON_OK ? !GetTrimmedTitle().empty() : true; |
| 149 } |
| 150 |
| 151 gfx::Size BookmarkAppConfirmationView::GetMinimumSize() const { |
| 152 gfx::Size size(views::DialogDelegateView::GetPreferredSize()); |
| 153 size.SetToMax(gfx::Size(kMinBubbleWidth, 0)); |
| 154 return size; |
| 155 } |
| 156 |
| 157 void BookmarkAppConfirmationView::ContentsChanged( |
| 158 views::Textfield* sender, |
| 159 const base::string16& new_contents) { |
| 160 DCHECK_EQ(title_tf_, sender); |
| 161 GetDialogClientView()->UpdateDialogButtons(); |
| 162 } |
| 163 |
| 164 base::string16 BookmarkAppConfirmationView::GetTrimmedTitle() const { |
| 165 base::string16 title(title_tf_->text()); |
| 166 base::TrimWhitespace(title, base::TRIM_ALL, &title); |
| 167 return title; |
| 168 } |
| OLD | NEW |