Chromium Code Reviews| 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 } | |
| 50 return gfx::ImageSkiaRep(); | |
| 51 } | |
| 52 | |
| 53 int dip_size_; | |
| 54 WebApplicationInfo info_; | |
| 55 }; | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 BookmarkAppConfirmationView::~BookmarkAppConfirmationView() {} | |
| 60 | |
| 61 // static | |
| 62 void BookmarkAppConfirmationView::ShowBubble( | |
| 63 gfx::NativeWindow parent, | |
| 64 const WebApplicationInfo& web_app_info, | |
| 65 const BrowserWindow::ShowBookmarkAppBubbleCallback& callback) { | |
| 66 constrained_window::CreateBrowserModalDialogViews( | |
| 67 new BookmarkAppConfirmationView(web_app_info, callback), parent) | |
| 68 ->Show(); | |
| 69 } | |
| 70 | |
| 71 BookmarkAppConfirmationView::BookmarkAppConfirmationView( | |
| 72 const WebApplicationInfo& web_app_info, | |
| 73 const BrowserWindow::ShowBookmarkAppBubbleCallback& callback) | |
| 74 : web_app_info_(web_app_info), | |
| 75 callback_(callback), | |
| 76 open_as_window_checkbox_(nullptr), | |
| 77 title_tf_(nullptr) { | |
| 78 views::BoxLayout* layout = new views::BoxLayout( | |
| 79 views::BoxLayout::kHorizontal, views::kButtonHEdgeMarginNew, | |
| 80 views::kButtonHEdgeMarginNew, views::kButtonHEdgeMarginNew); | |
| 81 layout->set_cross_axis_alignment( | |
| 82 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); | |
| 83 SetLayoutManager(layout); | |
| 84 | |
| 85 views::ImageView* icon_image_view = new views::ImageView(); | |
| 86 gfx::Size image_size(kIconSize, kIconSize); | |
| 87 gfx::ImageSkia image(new WebAppInfoImageSource(kIconSize, web_app_info_), | |
| 88 image_size); | |
| 89 icon_image_view->SetImageSize(image_size); | |
| 90 icon_image_view->SetImage(image); | |
| 91 AddChildView(icon_image_view); | |
| 92 | |
| 93 title_tf_ = new views::Textfield(); | |
| 94 title_tf_->SetText(web_app_info_.title); | |
| 95 title_tf_->SetAccessibleName( | |
| 96 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_AX_BUBBLE_NAME_LABEL)); | |
| 97 title_tf_->set_controller(this); | |
| 98 AddChildView(title_tf_); | |
| 99 layout->SetFlexForView(title_tf_, 1); | |
| 100 | |
| 101 title_tf_->SelectAll(true); | |
| 102 } | |
| 103 | |
| 104 views::View* BookmarkAppConfirmationView::GetInitiallyFocusedView() { | |
| 105 return title_tf_; | |
| 106 } | |
| 107 | |
| 108 base::string16 BookmarkAppConfirmationView::GetWindowTitle() const { | |
| 109 #if defined(USE_ASH) | |
| 110 int ids = IDS_ADD_TO_SHELF_BUBBLE_TITLE; | |
| 111 #else | |
| 112 int ids = IDS_ADD_TO_DESKTOP_BUBBLE_TITLE; | |
| 113 #endif | |
| 114 | |
| 115 return l10n_util::GetStringUTF16(ids); | |
| 116 } | |
| 117 | |
| 118 bool BookmarkAppConfirmationView::ShouldShowCloseButton() const { | |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 void BookmarkAppConfirmationView::WindowClosing() { | |
| 123 if (!callback_.is_null()) | |
| 124 callback_.Run(false, web_app_info_); | |
| 125 } | |
| 126 | |
| 127 views::View* BookmarkAppConfirmationView::CreateExtraView() { | |
| 128 open_as_window_checkbox_ = new views::Checkbox( | |
| 129 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_BUBBLE_OPEN_AS_WINDOW)); | |
| 130 open_as_window_checkbox_->SetChecked(web_app_info_.open_as_window); | |
| 131 return open_as_window_checkbox_; | |
| 132 } | |
| 133 | |
| 134 bool BookmarkAppConfirmationView::Accept() { | |
| 135 web_app_info_.title = GetTrimmedTitle(); | |
| 136 web_app_info_.open_as_window = open_as_window_checkbox_->checked(); | |
| 137 base::ResetAndReturn(&callback_).Run(true, web_app_info_); | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 bool BookmarkAppConfirmationView::IsDialogButtonEnabled( | |
| 142 ui::DialogButton button) const { | |
| 143 return button == ui::DIALOG_BUTTON_OK ? !GetTrimmedTitle().empty() : true; | |
|
msw
2016/04/13 00:54:37
ok was previously IDS_ADD, now it's the default ID
Evan Stade
2016/04/13 20:40:44
nope, that's what my patchset 1 comment was intend
| |
| 144 } | |
| 145 | |
| 146 gfx::Size BookmarkAppConfirmationView::GetMinimumSize() const { | |
| 147 gfx::Size size(views::DialogDelegateView::GetPreferredSize()); | |
| 148 size.SetToMax(gfx::Size(kMinBubbleWidth, 0)); | |
| 149 return size; | |
| 150 } | |
| 151 | |
| 152 void BookmarkAppConfirmationView::ContentsChanged( | |
| 153 views::Textfield* sender, | |
| 154 const base::string16& new_contents) { | |
| 155 DCHECK_EQ(title_tf_, sender); | |
| 156 GetDialogClientView()->UpdateDialogButtons(); | |
| 157 } | |
| 158 | |
| 159 base::string16 BookmarkAppConfirmationView::GetTrimmedTitle() const { | |
| 160 base::string16 title(title_tf_->text()); | |
| 161 base::TrimWhitespace(title, base::TRIM_ALL, &title); | |
| 162 return title; | |
| 163 } | |
| OLD | NEW |