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

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: Always shows zoom icon, even when zoom is 100% (rebased) 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"
19 #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h"
17 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" 20 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
18 #include "chrome/browser/ui/views/location_bar/zoom_view.h" 21 #include "chrome/browser/ui/views/location_bar/zoom_view.h"
19 #include "chrome/common/extensions/api/extension_action/action_info.h" 22 #include "chrome/common/extensions/api/extension_action/action_info.h"
20 #include "chrome/grit/generated_resources.h" 23 #include "chrome/grit/generated_resources.h"
21 #include "chrome/grit/theme_resources.h" 24 #include "chrome/grit/theme_resources.h"
22 #include "components/zoom/page_zoom.h" 25 #include "components/zoom/page_zoom.h"
23 #include "components/zoom/zoom_controller.h" 26 #include "components/zoom/zoom_controller.h"
24 #include "content/public/browser/notification_source.h" 27 #include "content/public/browser/notification_source.h"
25 #include "extensions/browser/extension_zoom_request_client.h" 28 #include "extensions/browser/extension_zoom_request_client.h"
26 #include "extensions/common/manifest_handlers/icons_handler.h" 29 #include "extensions/common/manifest_handlers/icons_handler.h"
27 #include "ui/base/l10n/l10n_util.h" 30 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/base/material_design/material_design_controller.h" 31 #include "ui/base/material_design/material_design_controller.h"
29 #include "ui/base/resource/resource_bundle.h" 32 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/base/ui_features.h" 33 #include "ui/base/ui_features.h"
31 #include "ui/gfx/favicon_size.h" 34 #include "ui/gfx/favicon_size.h"
35 #include "ui/gfx/vector_icon_types.h"
36 #include "ui/native_theme/native_theme.h"
32 #include "ui/views/controls/button/image_button.h" 37 #include "ui/views/controls/button/image_button.h"
38 #include "ui/views/controls/button/image_button_factory.h"
33 #include "ui/views/controls/button/md_text_button.h" 39 #include "ui/views/controls/button/md_text_button.h"
34 #include "ui/views/controls/separator.h" 40 #include "ui/views/controls/separator.h"
35 #include "ui/views/layout/grid_layout.h" 41 #include "ui/views/layout/box_layout.h"
42 #include "ui/views/layout/fill_layout.h"
36 #include "ui/views/layout/layout_constants.h" 43 #include "ui/views/layout/layout_constants.h"
37 #include "ui/views/widget/widget.h" 44 #include "ui/views/widget/widget.h"
38 45
46 namespace {
47
48 // The default time that the bubble should stay on the screen if it will close
49 // automatically.
50 constexpr base::TimeDelta kBubbleCloseDelayDefault =
51 base::TimeDelta::FromMilliseconds(1500);
52
53 // A longer timeout used for how long the bubble should stay on the screen
54 // before it will close automatically after + or - buttons have been used.
55 constexpr base::TimeDelta kBubbleCloseDelayLong =
56 base::TimeDelta::FromMilliseconds(5000);
57
58 // Creates an ImageButton using vector |icon|, sets a tooltip with |tooltip_id|.
59 // Returns the button.
60 std::unique_ptr<views::Button> CreateZoomButton(views::ButtonListener* listener,
61 const gfx::VectorIcon& icon,
62 int tooltip_id) {
63 std::unique_ptr<views::ImageButton> button(
64 views::CreateVectorImageButton(listener));
65 views::SetImageFromVectorIcon(button.get(), icon);
66 button->SetFocusForPlatform();
67 button->SetTooltipText(l10n_util::GetStringUTF16(tooltip_id));
68 return button;
69 }
70
71 class ZoomValue : public views::Label {
72 public:
73 explicit ZoomValue(const content::WebContents* web_contents) {
74 SetText(base::FormatPercent(web_contents->GetMaximumZoomPercent()));
Peter Kasting 2017/05/16 00:14:35 I worry a little about this, in the sense that it'
tapted 2017/05/16 06:29:01 true it would be safer. Although I suspect it woul
tapted 2017/05/16 06:29:02 this should also take a font style (it currently w
varkha 2017/05/16 22:53:06 This is something we also do in [1] (we also cache
varkha 2017/05/16 22:53:07 Done.
varkha 2017/05/16 22:53:07 Acknowledged.
75 preferred_size_ = views::Label::GetPreferredSize();
tapted 2017/05/16 06:29:01 I tend to drop the views:: prefix (inheriting effe
varkha 2017/05/16 22:53:06 Acknowledged.
76 SetText(base::string16());
77 }
78 ~ZoomValue() override{};
tapted 2017/05/16 06:29:02 nit: space before {}, no semicolon after
varkha 2017/05/16 22:53:06 Done.
79
80 gfx::Size GetPreferredSize() const override { return preferred_size_; }
tapted 2017/05/16 06:29:02 nit: // Label:
varkha 2017/05/16 22:53:06 Done.
81
82 private:
83 gfx::Size preferred_size_;
84
85 DISALLOW_COPY_AND_ASSIGN(ZoomValue);
86 };
87
88 } // namespace
89
39 // static 90 // static
40 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = nullptr; 91 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = nullptr;
41 92
42 // static 93 // static
43 void ZoomBubbleView::ShowBubble(content::WebContents* web_contents, 94 void ZoomBubbleView::ShowBubble(content::WebContents* web_contents,
44 const gfx::Point& anchor_point, 95 const gfx::Point& anchor_point,
45 DisplayReason reason) { 96 DisplayReason reason) {
46 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); 97 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
47 DCHECK(browser && browser->window() && 98 DCHECK(browser && browser->window() &&
48 browser->exclusive_access_manager()->fullscreen_controller()); 99 browser->exclusive_access_manager()->fullscreen_controller());
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 if (zoom_bubble_) 174 if (zoom_bubble_)
124 zoom_bubble_->CloseBubble(); 175 zoom_bubble_->CloseBubble();
125 } 176 }
126 177
127 // static 178 // static
128 ZoomBubbleView* ZoomBubbleView::GetZoomBubble() { 179 ZoomBubbleView* ZoomBubbleView::GetZoomBubble() {
129 return zoom_bubble_; 180 return zoom_bubble_;
130 } 181 }
131 182
132 void ZoomBubbleView::Refresh() { 183 void ZoomBubbleView::Refresh() {
133 zoom::ZoomController* zoom_controller = 184 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(); 185 StartTimerIfNecessary();
139 } 186 }
140 187
141 ZoomBubbleView::ZoomBubbleView( 188 ZoomBubbleView::ZoomBubbleView(
142 views::View* anchor_view, 189 views::View* anchor_view,
143 const gfx::Point& anchor_point, 190 const gfx::Point& anchor_point,
144 content::WebContents* web_contents, 191 content::WebContents* web_contents,
145 DisplayReason reason, 192 DisplayReason reason,
146 ImmersiveModeController* immersive_mode_controller) 193 ImmersiveModeController* immersive_mode_controller)
147 : LocationBarBubbleDelegateView(anchor_view, anchor_point, web_contents), 194 : LocationBarBubbleDelegateView(anchor_view, anchor_point, web_contents),
195 auto_close_duration_(kBubbleCloseDelayDefault),
148 image_button_(nullptr), 196 image_button_(nullptr),
149 label_(nullptr), 197 label_(nullptr),
198 zoom_out_button_(nullptr),
199 zoom_in_button_(nullptr),
200 reset_button_(nullptr),
150 web_contents_(web_contents), 201 web_contents_(web_contents),
151 auto_close_(reason == AUTOMATIC), 202 auto_close_(reason == AUTOMATIC),
203 ignore_close_bubble_(false),
152 immersive_mode_controller_(immersive_mode_controller) { 204 immersive_mode_controller_(immersive_mode_controller) {
153 set_notify_enter_exit_on_child(true); 205 set_notify_enter_exit_on_child(true);
154 if (immersive_mode_controller_) 206 if (immersive_mode_controller_)
155 immersive_mode_controller_->AddObserver(this); 207 immersive_mode_controller_->AddObserver(this);
156 UseCompactMargins(); 208 UseCompactMargins();
157 } 209 }
158 210
159 ZoomBubbleView::~ZoomBubbleView() { 211 ZoomBubbleView::~ZoomBubbleView() {
160 if (immersive_mode_controller_) 212 if (immersive_mode_controller_)
161 immersive_mode_controller_->RemoveObserver(this); 213 immersive_mode_controller_->RemoveObserver(this);
(...skipping 12 matching lines...) Expand all
174 226
175 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) { 227 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) {
176 StopTimer(); 228 StopTimer();
177 } 229 }
178 230
179 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) { 231 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) {
180 StartTimerIfNecessary(); 232 StartTimerIfNecessary();
181 } 233 }
182 234
183 void ZoomBubbleView::Init() { 235 void ZoomBubbleView::Init() {
184 // Set up the layout of the zoom bubble. A grid layout is used because 236 // Set up the layout of the zoom bubble.
185 // sometimes an extension icon is shown next to the zoom label. 237 ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
186 views::GridLayout* grid_layout = new views::GridLayout(this); 238 const int margin =
187 SetLayoutManager(grid_layout); 239 provider->GetDistanceMetric(views::DISTANCE_RELATED_BUTTON_HORIZONTAL);
188 views::ColumnSet* columns = grid_layout->AddColumnSet(0); 240 views::BoxLayout* box_layout =
189 // First row. 241 new views::BoxLayout(views::BoxLayout::kHorizontal, margin, 0, margin);
190 if (extension_info_.icon_image) { 242
191 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 2, 243 box_layout->set_main_axis_alignment(
192 views::GridLayout::USE_PREF, 0, 0); 244 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
193 } 245 box_layout->set_cross_axis_alignment(
194 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, 246 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
195 views::GridLayout::USE_PREF, 0, 0); 247 SetLayoutManager(box_layout);
196 grid_layout->StartRow(0, 0);
197 248
198 // If this zoom change was initiated by an extension, that extension will be 249 // If this zoom change was initiated by an extension, that extension will be
199 // attributed by showing its icon in the zoom bubble. 250 // attributed by showing its icon in the zoom bubble.
200 if (extension_info_.icon_image) { 251 if (extension_info_.icon_image) {
201 image_button_ = new views::ImageButton(this); 252 image_button_ = new views::ImageButton(this);
202 image_button_->SetTooltipText( 253 image_button_->SetTooltipText(
203 l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON, 254 l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON,
204 base::UTF8ToUTF16(extension_info_.name))); 255 base::UTF8ToUTF16(extension_info_.name)));
205 image_button_->SetImage(views::Button::STATE_NORMAL, 256 image_button_->SetImage(views::Button::STATE_NORMAL,
206 &extension_info_.icon_image->image_skia()); 257 &extension_info_.icon_image->image_skia());
207 grid_layout->AddView(image_button_); 258 AddChildView(image_button_);
208 } 259 }
209 260
261 // Add Zoom Out ("-") button.
262 std::unique_ptr<views::Button> zoom_out_button =
263 CreateZoomButton(this, kRemoveIcon, IDS_ACCNAME_ZOOM_MINUS2);
264 zoom_out_button_ = zoom_out_button.get();
265 AddChildView(zoom_out_button.release());
266
210 // Add zoom label with the new zoom percent. 267 // Add zoom label with the new zoom percent.
211 zoom::ZoomController* zoom_controller = 268 label_ = new ZoomValue(web_contents_);
212 zoom::ZoomController::FromWebContents(web_contents_); 269 UpdateZoomPercent();
213 int zoom_percent = zoom_controller->GetZoomPercent(); 270 AddChildView(label_);
214 label_ = new views::Label(l10n_util::GetStringFUTF16(
215 IDS_TOOLTIP_ZOOM, base::FormatPercent(zoom_percent)));
216 label_->SetFontList(GetTitleFontList());
217 grid_layout->AddView(label_);
218 271
219 // Second row. 272 // Add Zoom In ("+") button.
220 grid_layout->AddPaddingRow(0, 8); 273 std::unique_ptr<views::Button> zoom_in_button =
221 columns = grid_layout->AddColumnSet(1); 274 CreateZoomButton(this, kAddIcon, IDS_ACCNAME_ZOOM_PLUS2);
222 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, 275 zoom_in_button_ = zoom_in_button.get();
223 views::GridLayout::USE_PREF, 0, 0); 276 AddChildView(zoom_in_button.release());
224 grid_layout->StartRow(0, 1);
225 277
226 // Add "Reset to Default" button. 278 // Add "Reset" button.
227 grid_layout->AddView( 279 reset_button_ = views::MdTextButton::CreateSecondaryUiButton(
228 views::MdTextButton::CreateSecondaryUiButton( 280 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT));
229 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT))); 281 reset_button_->SetTooltipText(
282 l10n_util::GetStringUTF16(IDS_ACCNAME_ZOOM_SET_DEFAULT));
283 AddChildView(reset_button_);
284 box_layout->SetFlexForView(reset_button_, 0);
Peter Kasting 2017/05/16 00:14:35 This call shouldn't be necessary, I don't think.
varkha 2017/05/16 22:53:06 Done.
230 285
231 StartTimerIfNecessary(); 286 StartTimerIfNecessary();
232 } 287 }
233 288
234 void ZoomBubbleView::WindowClosing() { 289 void ZoomBubbleView::WindowClosing() {
235 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't 290 // |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. 291 // call this right away). Only set to nullptr when it's this bubble.
237 if (zoom_bubble_ == this) 292 if (zoom_bubble_ == this)
238 zoom_bubble_ = nullptr; 293 zoom_bubble_ = nullptr;
239 } 294 }
240 295
241 void ZoomBubbleView::CloseBubble() { 296 void ZoomBubbleView::CloseBubble() {
297 if (ignore_close_bubble_)
298 return;
299
242 // Widget's Close() is async, but we don't want to use zoom_bubble_ after 300 // Widget's Close() is async, but we don't want to use zoom_bubble_ after
243 // this. Additionally web_contents_ may have been destroyed. 301 // this. Additionally web_contents_ may have been destroyed.
244 zoom_bubble_ = nullptr; 302 zoom_bubble_ = nullptr;
245 web_contents_ = nullptr; 303 web_contents_ = nullptr;
246 LocationBarBubbleDelegateView::CloseBubble(); 304 LocationBarBubbleDelegateView::CloseBubble();
247 } 305 }
248 306
307 bool ZoomBubbleView::ShouldSnapFrameWidth() const {
Peter Kasting 2017/05/16 00:14:35 You no longer need to provide this -- Elly's recen
varkha 2017/05/16 22:53:06 Done.
308 return false;
309 }
310
249 void ZoomBubbleView::ButtonPressed(views::Button* sender, 311 void ZoomBubbleView::ButtonPressed(views::Button* sender,
250 const ui::Event& event) { 312 const ui::Event& event) {
313 // No buttons presses in this dialog should cause the dialog to close,
tapted 2017/05/16 06:29:01 "No buttons pressed" or "No button presses" or "A
varkha 2017/05/16 22:53:06 Done.
314 // including when the zoom level is set to 100% as a result of a button press.
315 base::AutoReset<bool> auto_ignore_close_bubble(&ignore_close_bubble_, true);
316
317 // Extend the timer to give a user more time after any button is pressed.
318 auto_close_duration_ = kBubbleCloseDelayLong;
319 StartTimerIfNecessary();
320
251 if (sender == image_button_) { 321 if (sender == image_button_) {
252 DCHECK(extension_info_.icon_image) << "Invalid button press."; 322 DCHECK(extension_info_.icon_image) << "Invalid button press.";
253 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); 323 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
254 chrome::AddSelectedTabWithURL( 324 chrome::AddSelectedTabWithURL(
255 browser, GURL(base::StringPrintf("chrome://extensions?id=%s", 325 browser, GURL(base::StringPrintf("chrome://extensions?id=%s",
256 extension_info_.id.c_str())), 326 extension_info_.id.c_str())),
257 ui::PAGE_TRANSITION_FROM_API); 327 ui::PAGE_TRANSITION_FROM_API);
328 } else if (sender == zoom_out_button_) {
329 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_OUT);
330 } else if (sender == zoom_in_button_) {
331 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_IN);
332 } else if (sender == reset_button_) {
333 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET);
258 } else { 334 } else {
259 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET); 335 NOTREACHED();
260 } 336 }
261 } 337 }
262 338
263 void ZoomBubbleView::OnImmersiveRevealStarted() { 339 void ZoomBubbleView::OnImmersiveRevealStarted() {
264 CloseBubble(); 340 CloseBubble();
265 } 341 }
266 342
267 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() { 343 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
268 immersive_mode_controller_ = nullptr; 344 immersive_mode_controller_ = nullptr;
269 } 345 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 icon_size = browser_action->default_icon.map().begin()->first; 388 icon_size = browser_action->default_icon.map().begin()->first;
313 extension_info_.icon_image.reset( 389 extension_info_.icon_image.reset(
314 new extensions::IconImage(web_contents_->GetBrowserContext(), 390 new extensions::IconImage(web_contents_->GetBrowserContext(),
315 extension, 391 extension,
316 browser_action->default_icon, 392 browser_action->default_icon,
317 icon_size, 393 icon_size,
318 default_extension_icon_image, 394 default_extension_icon_image,
319 this)); 395 this));
320 } 396 }
321 397
398 void ZoomBubbleView::UpdateZoomPercent() {
399 label_->SetText(base::FormatPercent(
400 zoom::ZoomController::FromWebContents(web_contents_)->GetZoomPercent()));
401 }
402
322 void ZoomBubbleView::StartTimerIfNecessary() { 403 void ZoomBubbleView::StartTimerIfNecessary() {
323 if (!auto_close_) 404 if (!auto_close_)
324 return; 405 return;
325 406
326 if (timer_.IsRunning()) { 407 timer_.Start(FROM_HERE, auto_close_duration_, this,
327 timer_.Reset(); 408 &ZoomBubbleView::CloseBubble);
328 } else {
329 // The number of milliseconds the bubble should stay on the screen if it
330 // will close automatically.
331 const int kBubbleCloseDelay = 1500;
332 timer_.Start(FROM_HERE,
333 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), this,
334 &ZoomBubbleView::CloseBubble);
335 }
336 } 409 }
337 410
338 void ZoomBubbleView::StopTimer() { 411 void ZoomBubbleView::StopTimer() {
339 timer_.Stop(); 412 timer_.Stop();
340 } 413 }
341 414
342 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {} 415 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {}
343 416
344 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {} 417 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698