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