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

Side by Side Diff: chrome/browser/ui/views/location_bar/zoom_bubble_view.cc

Issue 2845593002: Updates Zoom bubble layout and adds +/- buttons (Closed)
Patch Set: Updates Zoom bubble layout and adds +/- buttons (comments) Created 3 years, 7 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/location_bar/zoom_bubble_view.h" 5 #include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
6 6
7 #include "base/auto_reset.h"
7 #include "base/i18n/number_formatting.h" 8 #include "base/i18n/number_formatting.h"
8 #include "base/i18n/rtl.h" 9 #include "base/i18n/rtl.h"
9 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "chrome/app/vector_icons/vector_icons.h"
10 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/platform_util.h" 13 #include "chrome/browser/platform_util.h"
12 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h" 15 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_tabstrip.h" 16 #include "chrome/browser/ui/browser_tabstrip.h"
15 #include "chrome/browser/ui/browser_window.h" 17 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/views/frame/browser_view.h" 18 #include "chrome/browser/ui/views/frame/browser_view.h"
17 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" 19 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
18 #include "chrome/browser/ui/views/location_bar/zoom_view.h" 20 #include "chrome/browser/ui/views/location_bar/zoom_view.h"
19 #include "chrome/common/extensions/api/extension_action/action_info.h" 21 #include "chrome/common/extensions/api/extension_action/action_info.h"
20 #include "chrome/grit/generated_resources.h" 22 #include "chrome/grit/generated_resources.h"
21 #include "chrome/grit/theme_resources.h" 23 #include "chrome/grit/theme_resources.h"
22 #include "components/zoom/page_zoom.h" 24 #include "components/zoom/page_zoom.h"
23 #include "components/zoom/zoom_controller.h" 25 #include "components/zoom/zoom_controller.h"
24 #include "content/public/browser/notification_source.h" 26 #include "content/public/browser/notification_source.h"
25 #include "extensions/browser/extension_zoom_request_client.h" 27 #include "extensions/browser/extension_zoom_request_client.h"
26 #include "extensions/common/manifest_handlers/icons_handler.h" 28 #include "extensions/common/manifest_handlers/icons_handler.h"
27 #include "ui/base/l10n/l10n_util.h" 29 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/base/material_design/material_design_controller.h" 30 #include "ui/base/material_design/material_design_controller.h"
29 #include "ui/base/resource/resource_bundle.h" 31 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/base/ui_features.h" 32 #include "ui/base/ui_features.h"
31 #include "ui/gfx/favicon_size.h" 33 #include "ui/gfx/favicon_size.h"
34 #include "ui/gfx/vector_icon_types.h"
35 #include "ui/native_theme/native_theme.h"
32 #include "ui/views/controls/button/image_button.h" 36 #include "ui/views/controls/button/image_button.h"
37 #include "ui/views/controls/button/image_button_factory.h"
33 #include "ui/views/controls/button/md_text_button.h" 38 #include "ui/views/controls/button/md_text_button.h"
34 #include "ui/views/controls/separator.h" 39 #include "ui/views/controls/separator.h"
35 #include "ui/views/layout/grid_layout.h" 40 #include "ui/views/layout/grid_layout.h"
36 #include "ui/views/layout/layout_constants.h" 41 #include "ui/views/layout/layout_constants.h"
37 #include "ui/views/widget/widget.h" 42 #include "ui/views/widget/widget.h"
38 43
39 // static 44 // static
40 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = nullptr; 45 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = nullptr;
41 46
42 // static 47 // static
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 if (zoom_bubble_) 128 if (zoom_bubble_)
124 zoom_bubble_->CloseBubble(); 129 zoom_bubble_->CloseBubble();
125 } 130 }
126 131
127 // static 132 // static
128 ZoomBubbleView* ZoomBubbleView::GetZoomBubble() { 133 ZoomBubbleView* ZoomBubbleView::GetZoomBubble() {
129 return zoom_bubble_; 134 return zoom_bubble_;
130 } 135 }
131 136
132 void ZoomBubbleView::Refresh() { 137 void ZoomBubbleView::Refresh() {
133 zoom::ZoomController* zoom_controller = 138 UpdateZoomPercent();
134 zoom::ZoomController::FromWebContents(web_contents_);
135 int zoom_percent = zoom_controller->GetZoomPercent();
136 label_->SetText(l10n_util::GetStringFUTF16(
137 IDS_TOOLTIP_ZOOM, base::FormatPercent(zoom_percent)));
138 StartTimerIfNecessary(); 139 StartTimerIfNecessary();
139 } 140 }
140 141
141 ZoomBubbleView::ZoomBubbleView( 142 ZoomBubbleView::ZoomBubbleView(
142 views::View* anchor_view, 143 views::View* anchor_view,
143 const gfx::Point& anchor_point, 144 const gfx::Point& anchor_point,
144 content::WebContents* web_contents, 145 content::WebContents* web_contents,
145 DisplayReason reason, 146 DisplayReason reason,
146 ImmersiveModeController* immersive_mode_controller) 147 ImmersiveModeController* immersive_mode_controller)
147 : LocationBarBubbleDelegateView(anchor_view, anchor_point, web_contents), 148 : LocationBarBubbleDelegateView(anchor_view, anchor_point, web_contents),
148 image_button_(nullptr), 149 image_button_(nullptr),
149 label_(nullptr), 150 label_(nullptr),
151 zoom_out_button_(nullptr),
152 zoom_in_button_(nullptr),
153 reset_button_(nullptr),
150 web_contents_(web_contents), 154 web_contents_(web_contents),
151 auto_close_(reason == AUTOMATIC), 155 auto_close_(reason == AUTOMATIC),
156 ignore_close_bubble_(false),
152 immersive_mode_controller_(immersive_mode_controller) { 157 immersive_mode_controller_(immersive_mode_controller) {
153 set_notify_enter_exit_on_child(true); 158 set_notify_enter_exit_on_child(true);
154 if (immersive_mode_controller_) 159 if (immersive_mode_controller_)
155 immersive_mode_controller_->AddObserver(this); 160 immersive_mode_controller_->AddObserver(this);
156 UseCompactMargins(); 161 UseCompactMargins();
157 } 162 }
158 163
159 ZoomBubbleView::~ZoomBubbleView() { 164 ZoomBubbleView::~ZoomBubbleView() {
160 if (immersive_mode_controller_) 165 if (immersive_mode_controller_)
161 immersive_mode_controller_->RemoveObserver(this); 166 immersive_mode_controller_->RemoveObserver(this);
(...skipping 24 matching lines...) Expand all
186 views::GridLayout* grid_layout = new views::GridLayout(this); 191 views::GridLayout* grid_layout = new views::GridLayout(this);
187 SetLayoutManager(grid_layout); 192 SetLayoutManager(grid_layout);
188 views::ColumnSet* columns = grid_layout->AddColumnSet(0); 193 views::ColumnSet* columns = grid_layout->AddColumnSet(0);
189 // First row. 194 // First row.
190 if (extension_info_.icon_image) { 195 if (extension_info_.icon_image) {
191 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 2, 196 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 2,
192 views::GridLayout::USE_PREF, 0, 0); 197 views::GridLayout::USE_PREF, 0, 0);
193 } 198 }
194 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, 199 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
195 views::GridLayout::USE_PREF, 0, 0); 200 views::GridLayout::USE_PREF, 0, 0);
201 const bool material = ui::MaterialDesignController::IsSecondaryUiMaterial();
Peter Kasting 2017/04/28 05:58:48 Do we have to key this off secondary-ui-material?
varkha 2017/04/28 06:21:19 Yes, I'll do that if this is not blocked on Harmon
varkha 2017/05/01 10:12:00 Done.
202 if (material) {
203 const int kResetButtonPadding = 10;
Peter Kasting 2017/04/28 05:58:48 This should be using a constant from the layout pr
varkha 2017/04/28 06:21:19 Acknowledged.
varkha 2017/05/01 10:12:00 Done.
204 const gfx::Insets kBubbleContentsInsets(2, 0, 2, 10);
Peter Kasting 2017/04/28 05:58:48 These numbers are very magic (and I don't recogniz
varkha 2017/04/28 06:21:19 Acknowledged.
varkha 2017/05/01 10:11:59 Done.
205
206 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
207 views::GridLayout::USE_PREF, 0, 0);
208 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
209 views::GridLayout::USE_PREF, 0, 0);
210 columns->AddPaddingColumn(0, kResetButtonPadding);
211 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL, 0,
212 views::GridLayout::USE_PREF, 0, 0);
213 grid_layout->SetInsets(kBubbleContentsInsets);
214 }
196 grid_layout->StartRow(0, 0); 215 grid_layout->StartRow(0, 0);
197 216
198 // If this zoom change was initiated by an extension, that extension will be 217 // If this zoom change was initiated by an extension, that extension will be
199 // attributed by showing its icon in the zoom bubble. 218 // attributed by showing its icon in the zoom bubble.
200 if (extension_info_.icon_image) { 219 if (extension_info_.icon_image) {
201 image_button_ = new views::ImageButton(this); 220 image_button_ = new views::ImageButton(this);
202 image_button_->SetTooltipText( 221 image_button_->SetTooltipText(
203 l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON, 222 l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON,
204 base::UTF8ToUTF16(extension_info_.name))); 223 base::UTF8ToUTF16(extension_info_.name)));
205 image_button_->SetImage(views::Button::STATE_NORMAL, 224 image_button_->SetImage(views::Button::STATE_NORMAL,
206 &extension_info_.icon_image->image_skia()); 225 &extension_info_.icon_image->image_skia());
207 grid_layout->AddView(image_button_); 226 grid_layout->AddView(image_button_);
208 } 227 }
209 228
210 // Add zoom label with the new zoom percent. 229 // Add zoom label with the new zoom percent.
211 zoom::ZoomController* zoom_controller = 230 label_ = new views::Label();
212 zoom::ZoomController::FromWebContents(web_contents_); 231 UpdateZoomPercent();
213 int zoom_percent = zoom_controller->GetZoomPercent(); 232 if (material) {
214 label_ = new views::Label(l10n_util::GetStringFUTF16( 233 zoom_out_button_ = CreateZoomButton(kRemoveIcon, IDS_ACCNAME_ZOOM_MINUS2);
215 IDS_TOOLTIP_ZOOM, base::FormatPercent(zoom_percent))); 234 grid_layout->AddView(zoom_out_button_);
216 label_->SetFontList(GetTitleFontList()); 235
236 label_->SetEnabledColor(GetNativeTheme()->GetSystemColor(
237 ui::NativeTheme::kColorId_ButtonEnabledColor));
Peter Kasting 2017/04/28 05:58:48 The latest Harmony mock I see shows this in normal
varkha 2017/04/28 06:21:19 Acknowledged. On Mac the labels are all muted gray
Peter Kasting 2017/04/28 06:47:26 What do you mean by "on Mac the labels are all mut
varkha 2017/05/01 10:12:00 Done.
238 } else {
239 label_->SetFontList(GetTitleFontList());
240 }
217 grid_layout->AddView(label_); 241 grid_layout->AddView(label_);
218 242
219 // Second row. 243 if (material) {
220 grid_layout->AddPaddingRow(0, 8); 244 zoom_in_button_ = CreateZoomButton(kAddIcon, IDS_ACCNAME_ZOOM_PLUS2);
221 columns = grid_layout->AddColumnSet(1); 245 grid_layout->AddView(zoom_in_button_);
222 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, 246 } else {
223 views::GridLayout::USE_PREF, 0, 0); 247 // Second row.
224 grid_layout->StartRow(0, 1); 248 grid_layout->AddPaddingRow(0, 8);
249 columns = grid_layout->AddColumnSet(1);
250 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
251 views::GridLayout::USE_PREF, 0, 0);
252 grid_layout->StartRow(0, 1);
253 }
225 254
226 // Add "Reset to Default" button. 255 // Add "Reset to Default" button.
227 grid_layout->AddView( 256 reset_button_ = views::MdTextButton::CreateSecondaryUiButton(
228 views::MdTextButton::CreateSecondaryUiButton( 257 this, l10n_util::GetStringUTF16(material ? IDS_ZOOM_SET_DEFAULT_SHORT
229 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT))); 258 : IDS_ZOOM_SET_DEFAULT));
259 if (material) {
260 // TODO(varkha): When cleaning up pre-material code path and
261 // IDS_ZOOM_SET_DEFAULT is not used as the button text anymore, rename the
262 // IDS and change the string to a more explanatory version, e.g. "Reset to
263 // default zoom level".
264 reset_button_->SetTooltipText(
265 l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT));
266 }
267 grid_layout->AddView(reset_button_);
230 268
231 StartTimerIfNecessary(); 269 StartTimerIfNecessary();
232 } 270 }
233 271
234 void ZoomBubbleView::WindowClosing() { 272 void ZoomBubbleView::WindowClosing() {
235 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't 273 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't
236 // call this right away). Only set to nullptr when it's this bubble. 274 // call this right away). Only set to nullptr when it's this bubble.
237 if (zoom_bubble_ == this) 275 if (zoom_bubble_ == this)
238 zoom_bubble_ = nullptr; 276 zoom_bubble_ = nullptr;
239 } 277 }
240 278
241 void ZoomBubbleView::CloseBubble() { 279 void ZoomBubbleView::CloseBubble() {
280 if (ignore_close_bubble_)
281 return;
282
242 // Widget's Close() is async, but we don't want to use zoom_bubble_ after 283 // Widget's Close() is async, but we don't want to use zoom_bubble_ after
243 // this. Additionally web_contents_ may have been destroyed. 284 // this. Additionally web_contents_ may have been destroyed.
244 zoom_bubble_ = nullptr; 285 zoom_bubble_ = nullptr;
245 web_contents_ = nullptr; 286 web_contents_ = nullptr;
246 LocationBarBubbleDelegateView::CloseBubble(); 287 LocationBarBubbleDelegateView::CloseBubble();
247 } 288 }
248 289
249 void ZoomBubbleView::ButtonPressed(views::Button* sender, 290 void ZoomBubbleView::ButtonPressed(views::Button* sender,
250 const ui::Event& event) { 291 const ui::Event& event) {
292 base::AutoReset<bool> auto_ignore_close_bubble(&ignore_close_bubble_, true);
Peter Kasting 2017/04/28 05:58:48 Is this so if the timer is already running, it won
varkha 2017/04/28 06:21:19 No, this is to skip over 100% without closing the
Peter Kasting 2017/04/28 06:47:26 My brain hurts trying to understand how closing wo
varkha 2017/05/01 10:11:59 I've added a comment explaining the reasoning. I w
293 auto_close_ = false;
Peter Kasting 2017/04/28 05:58:48 Won't this mean once you click a button the bubble
varkha 2017/04/28 06:21:19 Yes. This is similar to ET_GESTURE_TAP handling th
Peter Kasting 2017/04/28 06:47:26 Stopping auto-close permanently is potentially ann
varkha 2017/05/01 10:12:00 I've played a bit more with it and I find that 5 s
294 StopTimer();
251 if (sender == image_button_) { 295 if (sender == image_button_) {
252 DCHECK(extension_info_.icon_image) << "Invalid button press."; 296 DCHECK(extension_info_.icon_image) << "Invalid button press.";
253 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); 297 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
254 chrome::AddSelectedTabWithURL( 298 chrome::AddSelectedTabWithURL(
255 browser, GURL(base::StringPrintf("chrome://extensions?id=%s", 299 browser, GURL(base::StringPrintf("chrome://extensions?id=%s",
256 extension_info_.id.c_str())), 300 extension_info_.id.c_str())),
257 ui::PAGE_TRANSITION_FROM_API); 301 ui::PAGE_TRANSITION_FROM_API);
302 } else if (sender == zoom_out_button_) {
303 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_OUT);
304 } else if (sender == zoom_in_button_) {
305 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_IN);
306 } else if (sender == reset_button_) {
307 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET);
258 } else { 308 } else {
259 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET); 309 NOTREACHED();
260 } 310 }
261 } 311 }
262 312
263 void ZoomBubbleView::OnImmersiveRevealStarted() { 313 void ZoomBubbleView::OnImmersiveRevealStarted() {
264 CloseBubble(); 314 CloseBubble();
265 } 315 }
266 316
267 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() { 317 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
268 immersive_mode_controller_ = nullptr; 318 immersive_mode_controller_ = nullptr;
269 } 319 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 icon_size = browser_action->default_icon.map().begin()->first; 362 icon_size = browser_action->default_icon.map().begin()->first;
313 extension_info_.icon_image.reset( 363 extension_info_.icon_image.reset(
314 new extensions::IconImage(web_contents_->GetBrowserContext(), 364 new extensions::IconImage(web_contents_->GetBrowserContext(),
315 extension, 365 extension,
316 browser_action->default_icon, 366 browser_action->default_icon,
317 icon_size, 367 icon_size,
318 default_extension_icon_image, 368 default_extension_icon_image,
319 this)); 369 this));
320 } 370 }
321 371
372 views::Button* ZoomBubbleView::CreateZoomButton(const gfx::VectorIcon& icon,
Peter Kasting 2017/04/28 05:58:48 Nit: It would be nice if this returned a unique_pt
varkha 2017/04/28 06:21:19 Acknowledged.
varkha 2017/05/01 10:12:00 Done.
373 int tooltip_id) {
374 views::ImageButton* button = views::CreateVectorImageButton(this);
375 views::SetImageFromVectorIcon(button, icon);
376 button->SetFocusForPlatform();
377 button->SetTooltipText(l10n_util::GetStringUTF16(tooltip_id));
378 return button;
379 }
380
381 void ZoomBubbleView::UpdateZoomPercent() {
382 zoom::ZoomController* zoom_controller =
383 zoom::ZoomController::FromWebContents(web_contents_);
384 const int zoom_percent = zoom_controller->GetZoomPercent();
385 const base::string16 label_text =
386 ui::MaterialDesignController::IsSecondaryUiMaterial()
387 ? base::FormatPercent(zoom_percent)
388 : l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM,
389 base::FormatPercent(zoom_percent));
390 label_->SetText(label_text);
391 }
392
322 void ZoomBubbleView::StartTimerIfNecessary() { 393 void ZoomBubbleView::StartTimerIfNecessary() {
323 if (!auto_close_) 394 if (!auto_close_)
324 return; 395 return;
325 396
326 if (timer_.IsRunning()) { 397 if (timer_.IsRunning()) {
327 timer_.Reset(); 398 timer_.Reset();
328 } else { 399 } else {
329 // The number of milliseconds the bubble should stay on the screen if it 400 // The number of milliseconds the bubble should stay on the screen if it
330 // will close automatically. 401 // will close automatically.
331 const int kBubbleCloseDelay = 1500; 402 const int kBubbleCloseDelay = 1500;
332 timer_.Start(FROM_HERE, 403 timer_.Start(FROM_HERE,
333 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), this, 404 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), this,
334 &ZoomBubbleView::CloseBubble); 405 &ZoomBubbleView::CloseBubble);
335 } 406 }
336 } 407 }
337 408
338 void ZoomBubbleView::StopTimer() { 409 void ZoomBubbleView::StopTimer() {
339 timer_.Stop(); 410 timer_.Stop();
340 } 411 }
341 412
342 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {} 413 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {}
343 414
344 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {} 415 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698