Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(623)

Side by Side Diff: chrome/browser/ui/views/extensions/bookmark_app_bubble_view.cc

Issue 1878963003: Convert BookmarkBubbleAppView to a DialogDelegateView (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_bubble_view.h"
6
7 #include "base/numerics/safe_conversions.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "build/build_config.h"
12 #include "chrome/grit/generated_resources.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/events/keycodes/keyboard_codes.h"
20 #include "ui/gfx/image/image_skia.h"
21 #include "ui/gfx/image/image_skia_source.h"
22 #include "ui/views/controls/button/checkbox.h"
23 #include "ui/views/controls/button/label_button.h"
24 #include "ui/views/controls/image_view.h"
25 #include "ui/views/controls/label.h"
26 #include "ui/views/controls/textfield/textfield.h"
27 #include "ui/views/layout/grid_layout.h"
28 #include "ui/views/layout/layout_constants.h"
29 #include "ui/views/widget/widget.h"
30
31 #if defined(OS_WIN)
32 #include "base/win/shortcut.h"
33 #endif // defined(OS_WIN)
34
35 using views::ColumnSet;
36 using views::GridLayout;
37
38 namespace {
39
40 // Minimum width of the the bubble.
41 const int kMinBubbleWidth = 300;
42 // Minimum width of the the textfield.
43 const int kMinTextfieldWidth = 200;
44 // Size of the icon.
45 const int kIconSize = extension_misc::EXTENSION_ICON_MEDIUM;
46
47 class WebAppInfoImageSource : public gfx::ImageSkiaSource {
48 public:
49 WebAppInfoImageSource(int dip_size, const WebApplicationInfo& info)
50 : dip_size_(dip_size), info_(info) {}
51 ~WebAppInfoImageSource() override {}
52
53 private:
54 gfx::ImageSkiaRep GetImageForScale(float scale) override {
55 int size = base::saturated_cast<int>(dip_size_ * scale);
56 for (const auto& icon_info : info_.icons) {
57 if (icon_info.width == size) {
58 return gfx::ImageSkiaRep(icon_info.data, scale);
59 }
60 }
61 return gfx::ImageSkiaRep();
62 }
63
64 int dip_size_;
65 WebApplicationInfo info_;
66 };
67
68 } // namespace
69
70 BookmarkAppBubbleView::~BookmarkAppBubbleView() {
71 }
72
73 // static
74 void BookmarkAppBubbleView::ShowBubble(
75 views::View* anchor_view,
76 const WebApplicationInfo& web_app_info,
77 const BrowserWindow::ShowBookmarkAppBubbleCallback& callback) {
78 // |bookmark_app_bubble| becomes owned by the BubbleDelegateView through the
79 // views system, and is freed when the BubbleDelegateView is closed and
80 // subsequently destroyed.
81 BookmarkAppBubbleView* bookmark_app_bubble =
82 new BookmarkAppBubbleView(anchor_view, web_app_info, callback);
83 views::BubbleDelegateView::CreateBubble(bookmark_app_bubble)->Show();
84 // Select the entire title textfield contents when the bubble is first shown.
85 bookmark_app_bubble->title_tf_->SelectAll(true);
86 bookmark_app_bubble->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
87 }
88
89 BookmarkAppBubbleView::BookmarkAppBubbleView(
90 views::View* anchor_view,
91 const WebApplicationInfo& web_app_info,
92 const BrowserWindow::ShowBookmarkAppBubbleCallback& callback)
93 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT),
94 web_app_info_(web_app_info),
95 user_accepted_(false),
96 callback_(callback),
97 add_button_(NULL),
98 cancel_button_(NULL),
99 open_as_window_checkbox_(NULL),
100 title_tf_(NULL) {
101 const SkColor background_color = GetNativeTheme()->GetSystemColor(
102 ui::NativeTheme::kColorId_DialogBackground);
103 set_arrow(views::BubbleBorder::TOP_CENTER);
104 set_color(background_color);
105 set_background(views::Background::CreateSolidBackground(background_color));
106 set_margins(gfx::Insets(views::kPanelVertMargin, 0, 0, 0));
107 }
108
109 void BookmarkAppBubbleView::Init() {
110 views::Label* title_label = new views::Label(
111 l10n_util::GetStringUTF16(TitleStringId()));
112 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
113 title_label->SetFontList(rb->GetFontList(ui::ResourceBundle::MediumFont));
114 title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
115
116 add_button_ =
117 new views::LabelButton(this, l10n_util::GetStringUTF16(IDS_ADD));
Evan Stade 2016/04/12 22:20:13 oops, this string needs to be preserved
118 add_button_->SetStyle(views::Button::STYLE_BUTTON);
119 add_button_->SetIsDefault(true);
120
121 cancel_button_ =
122 new views::LabelButton(this, l10n_util::GetStringUTF16(IDS_CANCEL));
123 cancel_button_->SetStyle(views::Button::STYLE_BUTTON);
124
125 GridLayout* layout = new GridLayout(this);
126 SetLayoutManager(layout);
127
128 // Column sets used in the layout of the bubble.
129 enum ColumnSetID {
130 TITLE_COLUMN_SET_ID,
131 TITLE_TEXT_COLUMN_SET_ID,
132 CONTENT_COLUMN_SET_ID
133 };
134
135 // The column layout used for the title and checkbox.
136 ColumnSet* cs = layout->AddColumnSet(TITLE_COLUMN_SET_ID);
137 cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
138 cs->AddColumn(
139 GridLayout::LEADING, GridLayout::CENTER, 0, GridLayout::USE_PREF, 0, 0);
140 cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
141
142 // The column layout used for the icon and text box.
143 cs = layout->AddColumnSet(TITLE_TEXT_COLUMN_SET_ID);
144 cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
145 cs->AddColumn(GridLayout::LEADING,
146 GridLayout::CENTER,
147 0,
148 GridLayout::USE_PREF,
149 0,
150 0);
151 cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
152 cs->AddColumn(GridLayout::FILL,
153 GridLayout::CENTER,
154 1,
155 GridLayout::USE_PREF,
156 0,
157 kMinTextfieldWidth);
158 cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
159
160 // The column layout used for the row with buttons.
161 cs = layout->AddColumnSet(CONTENT_COLUMN_SET_ID);
162 cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
163 cs->AddColumn(
164 GridLayout::LEADING, GridLayout::CENTER, 0, GridLayout::USE_PREF, 0, 0);
165 cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing);
166 cs->AddColumn(
167 GridLayout::LEADING, GridLayout::CENTER, 0, GridLayout::USE_PREF, 0, 0);
168 cs->AddPaddingColumn(0, views::kRelatedButtonHSpacing);
169 cs->AddColumn(
170 GridLayout::LEADING, GridLayout::CENTER, 0, GridLayout::USE_PREF, 0, 0);
171 cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
172
173 layout->StartRow(0, TITLE_COLUMN_SET_ID);
174 layout->AddView(title_label);
175 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
176
177 layout->StartRow(0, TITLE_TEXT_COLUMN_SET_ID);
178 icon_image_view_ = new views::ImageView();
179
180 gfx::Size image_size(kIconSize, kIconSize);
181 gfx::ImageSkia image(new WebAppInfoImageSource(kIconSize, web_app_info_),
182 image_size);
183 icon_image_view_->SetImageSize(image_size);
184 icon_image_view_->SetImage(image);
185 layout->AddView(icon_image_view_);
186
187 title_tf_ = new views::Textfield();
188 title_tf_->SetText(web_app_info_.title);
189 title_tf_->SetAccessibleName(l10n_util::GetStringUTF16(
190 IDS_BOOKMARK_APP_AX_BUBBLE_NAME_LABEL));
191 title_tf_->set_controller(this);
192 layout->AddView(title_tf_);
193 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
194
195 layout->StartRow(0, CONTENT_COLUMN_SET_ID);
196 open_as_window_checkbox_ = new views::Checkbox(
197 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_BUBBLE_OPEN_AS_WINDOW));
198 open_as_window_checkbox_->SetChecked(web_app_info_.open_as_window);
199 layout->AddView(open_as_window_checkbox_);
200 layout->AddView(add_button_);
201 layout->AddView(cancel_button_);
202 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
203
204 AddAccelerator(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
205 UpdateAddButtonState();
206 }
207
208 views::View* BookmarkAppBubbleView::GetInitiallyFocusedView() {
209 return title_tf_;
210 }
211
212 void BookmarkAppBubbleView::WindowClosing() {
213 callback_.Run(user_accepted_, web_app_info_);
214 }
215
216 bool BookmarkAppBubbleView::AcceleratorPressed(
217 const ui::Accelerator& accelerator) {
218 if (accelerator.key_code() == ui::VKEY_RETURN) {
219 HandleButtonPressed(add_button_);
220 }
221
222 return BubbleDelegateView::AcceleratorPressed(accelerator);
223 }
224
225 void BookmarkAppBubbleView::GetAccessibleState(ui::AXViewState* state) {
226 views::BubbleDelegateView::GetAccessibleState(state);
227 state->name = l10n_util::GetStringUTF16(TitleStringId());
228 }
229
230 gfx::Size BookmarkAppBubbleView::GetMinimumSize() const {
231 gfx::Size size(views::BubbleDelegateView::GetPreferredSize());
232 size.SetToMax(gfx::Size(kMinBubbleWidth, 0));
233 return size;
234 }
235
236 void BookmarkAppBubbleView::ButtonPressed(views::Button* sender,
237 const ui::Event& event) {
238 HandleButtonPressed(sender);
239 }
240
241 void BookmarkAppBubbleView::ContentsChanged(
242 views::Textfield* sender,
243 const base::string16& new_contents) {
244 DCHECK_EQ(title_tf_, sender);
245 UpdateAddButtonState();
246 }
247
248 void BookmarkAppBubbleView::HandleButtonPressed(views::Button* sender) {
249 if (sender == add_button_) {
250 user_accepted_ = true;
251 web_app_info_.title = GetTrimmedTitle();
252 web_app_info_.open_as_window = open_as_window_checkbox_->checked();
253 }
254
255 GetWidget()->Close();
256 }
257
258 void BookmarkAppBubbleView::UpdateAddButtonState() {
259 add_button_->SetEnabled(!GetTrimmedTitle().empty());
260 }
261
262 int BookmarkAppBubbleView::TitleStringId() {
263 #if defined(USE_ASH)
264 return IDS_ADD_TO_SHELF_BUBBLE_TITLE;
265 #else
266 return IDS_ADD_TO_DESKTOP_BUBBLE_TITLE;
267 #endif
268 }
269
270 base::string16 BookmarkAppBubbleView::GetTrimmedTitle() {
271 base::string16 title(title_tf_->text());
272 base::TrimWhitespace(title, base::TRIM_ALL, &title);
273 return title;
274 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698