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

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"
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 } // namespace
72
39 // static 73 // static
40 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = nullptr; 74 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = nullptr;
41 75
42 // static 76 // static
43 void ZoomBubbleView::ShowBubble(content::WebContents* web_contents, 77 void ZoomBubbleView::ShowBubble(content::WebContents* web_contents,
44 const gfx::Point& anchor_point, 78 const gfx::Point& anchor_point,
45 DisplayReason reason) { 79 DisplayReason reason) {
46 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); 80 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
47 DCHECK(browser && browser->window() && 81 DCHECK(browser && browser->window() &&
48 browser->exclusive_access_manager()->fullscreen_controller()); 82 browser->exclusive_access_manager()->fullscreen_controller());
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 if (zoom_bubble_) 157 if (zoom_bubble_)
124 zoom_bubble_->CloseBubble(); 158 zoom_bubble_->CloseBubble();
125 } 159 }
126 160
127 // static 161 // static
128 ZoomBubbleView* ZoomBubbleView::GetZoomBubble() { 162 ZoomBubbleView* ZoomBubbleView::GetZoomBubble() {
129 return zoom_bubble_; 163 return zoom_bubble_;
130 } 164 }
131 165
132 void ZoomBubbleView::Refresh() { 166 void ZoomBubbleView::Refresh() {
133 zoom::ZoomController* zoom_controller = 167 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(); 168 StartTimerIfNecessary();
139 } 169 }
140 170
141 ZoomBubbleView::ZoomBubbleView( 171 ZoomBubbleView::ZoomBubbleView(
142 views::View* anchor_view, 172 views::View* anchor_view,
143 const gfx::Point& anchor_point, 173 const gfx::Point& anchor_point,
144 content::WebContents* web_contents, 174 content::WebContents* web_contents,
145 DisplayReason reason, 175 DisplayReason reason,
146 ImmersiveModeController* immersive_mode_controller) 176 ImmersiveModeController* immersive_mode_controller)
147 : LocationBarBubbleDelegateView(anchor_view, anchor_point, web_contents), 177 : LocationBarBubbleDelegateView(anchor_view, anchor_point, web_contents),
178 auto_close_duration_(kBubbleCloseDelayDefault),
148 image_button_(nullptr), 179 image_button_(nullptr),
149 label_(nullptr), 180 label_(nullptr),
181 zoom_out_button_(nullptr),
182 zoom_in_button_(nullptr),
183 reset_button_(nullptr),
150 web_contents_(web_contents), 184 web_contents_(web_contents),
151 auto_close_(reason == AUTOMATIC), 185 auto_close_(reason == AUTOMATIC),
186 ignore_close_bubble_(false),
152 immersive_mode_controller_(immersive_mode_controller) { 187 immersive_mode_controller_(immersive_mode_controller) {
153 set_notify_enter_exit_on_child(true); 188 set_notify_enter_exit_on_child(true);
154 if (immersive_mode_controller_) 189 if (immersive_mode_controller_)
155 immersive_mode_controller_->AddObserver(this); 190 immersive_mode_controller_->AddObserver(this);
156 UseCompactMargins(); 191 UseCompactMargins();
157 } 192 }
158 193
159 ZoomBubbleView::~ZoomBubbleView() { 194 ZoomBubbleView::~ZoomBubbleView() {
160 if (immersive_mode_controller_) 195 if (immersive_mode_controller_)
161 immersive_mode_controller_->RemoveObserver(this); 196 immersive_mode_controller_->RemoveObserver(this);
162 } 197 }
163 198
199 gfx::Size ZoomBubbleView::GetPreferredSize() const {
200 constexpr gfx::Size kPreferredZoomBubbleSize = gfx::Size(208, 32);
tapted 2017/05/04 00:24:11 update: we chatted offline about whether these num
varkha 2017/05/15 22:34:42 Switched to sizing the bubble based on the label w
201 return kPreferredZoomBubbleSize;
202 }
203
164 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) { 204 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) {
165 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ || 205 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ ||
166 event->type() != ui::ET_GESTURE_TAP) { 206 event->type() != ui::ET_GESTURE_TAP) {
167 return; 207 return;
168 } 208 }
169 209
170 auto_close_ = false; 210 auto_close_ = false;
171 StopTimer(); 211 StopTimer();
172 event->SetHandled(); 212 event->SetHandled();
173 } 213 }
174 214
175 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) { 215 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) {
176 StopTimer(); 216 StopTimer();
177 } 217 }
178 218
179 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) { 219 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) {
180 StartTimerIfNecessary(); 220 StartTimerIfNecessary();
181 } 221 }
182 222
183 void ZoomBubbleView::Init() { 223 void ZoomBubbleView::Init() {
184 // Set up the layout of the zoom bubble. A grid layout is used because 224 // Set up the layout of the zoom bubble.
185 // sometimes an extension icon is shown next to the zoom label. 225 ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
186 views::GridLayout* grid_layout = new views::GridLayout(this); 226 const int margin =
187 SetLayoutManager(grid_layout); 227 provider->GetDistanceMetric(views::DISTANCE_RELATED_BUTTON_HORIZONTAL);
188 views::ColumnSet* columns = grid_layout->AddColumnSet(0); 228 views::BoxLayout* box_layout =
189 // First row. 229 new views::BoxLayout(views::BoxLayout::kHorizontal, margin, 0, 0);
190 if (extension_info_.icon_image) { 230
191 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 2, 231 box_layout->set_main_axis_alignment(
192 views::GridLayout::USE_PREF, 0, 0); 232 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
193 } 233 box_layout->set_cross_axis_alignment(
194 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, 234 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
195 views::GridLayout::USE_PREF, 0, 0); 235 box_layout->SetDefaultFlex(1);
Peter Kasting 2017/05/05 23:50:40 Why does this layout need any flex?
varkha 2017/05/15 22:34:42 Not if the label is set to fixed size based on the
196 grid_layout->StartRow(0, 0); 236 SetLayoutManager(box_layout);
197 237
198 // If this zoom change was initiated by an extension, that extension will be 238 // If this zoom change was initiated by an extension, that extension will be
199 // attributed by showing its icon in the zoom bubble. 239 // attributed by showing its icon in the zoom bubble.
200 if (extension_info_.icon_image) { 240 if (extension_info_.icon_image) {
201 image_button_ = new views::ImageButton(this); 241 image_button_ = new views::ImageButton(this);
202 image_button_->SetTooltipText( 242 image_button_->SetTooltipText(
203 l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON, 243 l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON,
204 base::UTF8ToUTF16(extension_info_.name))); 244 base::UTF8ToUTF16(extension_info_.name)));
205 image_button_->SetImage(views::Button::STATE_NORMAL, 245 image_button_->SetImage(views::Button::STATE_NORMAL,
206 &extension_info_.icon_image->image_skia()); 246 &extension_info_.icon_image->image_skia());
207 grid_layout->AddView(image_button_); 247 AddChildView(image_button_);
208 } 248 }
209 249
250 // Add Zoom Out ("-") button.
251 std::unique_ptr<views::Button> zoom_out_button =
252 CreateZoomButton(this, kRemoveIcon, IDS_ACCNAME_ZOOM_MINUS2);
253 zoom_out_button_ = zoom_out_button.get();
254 AddChildView(zoom_out_button.release());
255
210 // Add zoom label with the new zoom percent. 256 // Add zoom label with the new zoom percent.
211 zoom::ZoomController* zoom_controller = 257 label_ = new views::Label();
212 zoom::ZoomController::FromWebContents(web_contents_); 258 UpdateZoomPercent();
213 int zoom_percent = zoom_controller->GetZoomPercent(); 259 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 260
219 // Second row. 261 // Add Zoom In ("+") button with extra padding on the right because "Reset"
220 grid_layout->AddPaddingRow(0, 8); 262 // button has fixed size.
Peter Kasting 2017/05/05 23:50:40 I don't really understand this. Why do we need a
varkha 2017/05/15 22:34:42 Yes, the updated mock seems symmetrical so I've ju
221 columns = grid_layout->AddColumnSet(1); 263 std::unique_ptr<views::Button> zoom_in_button =
222 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, 264 CreateZoomButton(this, kAddIcon, IDS_ACCNAME_ZOOM_PLUS2);
223 views::GridLayout::USE_PREF, 0, 0); 265 views::View* zoom_in_button_padding = new views::View();
224 grid_layout->StartRow(0, 1); 266 zoom_in_button_padding->SetLayoutManager(new views::FillLayout());
267 zoom_in_button_padding->SetBorder(views::CreateEmptyBorder(0, 0, 0, margin));
Peter Kasting 2017/05/05 23:50:40 Even if we did need a margin, BoxLayout can do per
varkha 2017/05/15 22:34:42 Done.
268 zoom_in_button_ = zoom_in_button.get();
269 zoom_in_button_padding->AddChildView(zoom_in_button.release());
270 AddChildView(zoom_in_button_padding);
225 271
226 // Add "Reset to Default" button. 272 // Add "Reset" button.
227 grid_layout->AddView( 273 reset_button_ = views::MdTextButton::CreateSecondaryUiButton(
228 views::MdTextButton::CreateSecondaryUiButton( 274 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT));
229 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT))); 275 reset_button_->SetTooltipText(
276 l10n_util::GetStringUTF16(IDS_ACCNAME_ZOOM_SET_DEFAULT));
277 AddChildView(reset_button_);
278 box_layout->SetFlexForView(reset_button_, 0);
230 279
231 StartTimerIfNecessary(); 280 StartTimerIfNecessary();
232 } 281 }
233 282
234 void ZoomBubbleView::WindowClosing() { 283 void ZoomBubbleView::WindowClosing() {
235 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't 284 // |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. 285 // call this right away). Only set to nullptr when it's this bubble.
237 if (zoom_bubble_ == this) 286 if (zoom_bubble_ == this)
238 zoom_bubble_ = nullptr; 287 zoom_bubble_ = nullptr;
239 } 288 }
240 289
241 void ZoomBubbleView::CloseBubble() { 290 void ZoomBubbleView::CloseBubble() {
291 if (ignore_close_bubble_)
292 return;
293
242 // Widget's Close() is async, but we don't want to use zoom_bubble_ after 294 // Widget's Close() is async, but we don't want to use zoom_bubble_ after
243 // this. Additionally web_contents_ may have been destroyed. 295 // this. Additionally web_contents_ may have been destroyed.
244 zoom_bubble_ = nullptr; 296 zoom_bubble_ = nullptr;
245 web_contents_ = nullptr; 297 web_contents_ = nullptr;
246 LocationBarBubbleDelegateView::CloseBubble(); 298 LocationBarBubbleDelegateView::CloseBubble();
247 } 299 }
248 300
301 bool ZoomBubbleView::ShouldSnapFrameWidth() const {
302 return false;
303 }
304
249 void ZoomBubbleView::ButtonPressed(views::Button* sender, 305 void ZoomBubbleView::ButtonPressed(views::Button* sender,
250 const ui::Event& event) { 306 const ui::Event& event) {
307 // No buttons presses in this dialog should cause the dialog to close,
308 // including when the zoom level is set to 100% as a result of a button press.
309 base::AutoReset<bool> auto_ignore_close_bubble(&ignore_close_bubble_, true);
310
311 // Extend the timer to give a user more time after any button is pressed.
312 auto_close_duration_ = kBubbleCloseDelayLong;
313 StartTimerIfNecessary();
314
251 if (sender == image_button_) { 315 if (sender == image_button_) {
252 DCHECK(extension_info_.icon_image) << "Invalid button press."; 316 DCHECK(extension_info_.icon_image) << "Invalid button press.";
253 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); 317 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
254 chrome::AddSelectedTabWithURL( 318 chrome::AddSelectedTabWithURL(
255 browser, GURL(base::StringPrintf("chrome://extensions?id=%s", 319 browser, GURL(base::StringPrintf("chrome://extensions?id=%s",
256 extension_info_.id.c_str())), 320 extension_info_.id.c_str())),
257 ui::PAGE_TRANSITION_FROM_API); 321 ui::PAGE_TRANSITION_FROM_API);
322 } else if (sender == zoom_out_button_) {
323 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_OUT);
324 } else if (sender == zoom_in_button_) {
325 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_IN);
326 } else if (sender == reset_button_) {
327 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET);
258 } else { 328 } else {
259 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET); 329 NOTREACHED();
260 } 330 }
261 } 331 }
262 332
263 void ZoomBubbleView::OnImmersiveRevealStarted() { 333 void ZoomBubbleView::OnImmersiveRevealStarted() {
264 CloseBubble(); 334 CloseBubble();
265 } 335 }
266 336
267 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() { 337 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
268 immersive_mode_controller_ = nullptr; 338 immersive_mode_controller_ = nullptr;
269 } 339 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 icon_size = browser_action->default_icon.map().begin()->first; 382 icon_size = browser_action->default_icon.map().begin()->first;
313 extension_info_.icon_image.reset( 383 extension_info_.icon_image.reset(
314 new extensions::IconImage(web_contents_->GetBrowserContext(), 384 new extensions::IconImage(web_contents_->GetBrowserContext(),
315 extension, 385 extension,
316 browser_action->default_icon, 386 browser_action->default_icon,
317 icon_size, 387 icon_size,
318 default_extension_icon_image, 388 default_extension_icon_image,
319 this)); 389 this));
320 } 390 }
321 391
392 void ZoomBubbleView::UpdateZoomPercent() {
393 label_->SetText(base::FormatPercent(
394 zoom::ZoomController::FromWebContents(web_contents_)->GetZoomPercent()));
395 }
396
322 void ZoomBubbleView::StartTimerIfNecessary() { 397 void ZoomBubbleView::StartTimerIfNecessary() {
323 if (!auto_close_) 398 if (!auto_close_)
324 return; 399 return;
325 400
326 if (timer_.IsRunning()) { 401 timer_.Start(FROM_HERE, auto_close_duration_, this,
327 timer_.Reset(); 402 &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 } 403 }
337 404
338 void ZoomBubbleView::StopTimer() { 405 void ZoomBubbleView::StopTimer() {
339 timer_.Stop(); 406 timer_.Stop();
340 } 407 }
341 408
342 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {} 409 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {}
343 410
344 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {} 411 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698