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

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 in milliseconds that the bubble should stay on the screen if
49 // it will close automatically.
50 const int kBubbleCloseDelayDefaultMs = 1500;
51
52 // A longer timeout used for how long the bubble should stay on the screen
53 // before it will close automatically after + or - buttons have been used.
54 const int kBubbleCloseDelayLongMs = 5000;
55
56 } // namespace
57
39 // static 58 // static
40 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = nullptr; 59 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = nullptr;
41 60
42 // static 61 // static
43 void ZoomBubbleView::ShowBubble(content::WebContents* web_contents, 62 void ZoomBubbleView::ShowBubble(content::WebContents* web_contents,
44 const gfx::Point& anchor_point, 63 const gfx::Point& anchor_point,
45 DisplayReason reason) { 64 DisplayReason reason) {
46 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); 65 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
47 DCHECK(browser && browser->window() && 66 DCHECK(browser && browser->window() &&
48 browser->exclusive_access_manager()->fullscreen_controller()); 67 browser->exclusive_access_manager()->fullscreen_controller());
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 if (zoom_bubble_) 142 if (zoom_bubble_)
124 zoom_bubble_->CloseBubble(); 143 zoom_bubble_->CloseBubble();
125 } 144 }
126 145
127 // static 146 // static
128 ZoomBubbleView* ZoomBubbleView::GetZoomBubble() { 147 ZoomBubbleView* ZoomBubbleView::GetZoomBubble() {
129 return zoom_bubble_; 148 return zoom_bubble_;
130 } 149 }
131 150
132 void ZoomBubbleView::Refresh() { 151 void ZoomBubbleView::Refresh() {
133 zoom::ZoomController* zoom_controller = 152 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(); 153 StartTimerIfNecessary();
139 } 154 }
140 155
141 ZoomBubbleView::ZoomBubbleView( 156 ZoomBubbleView::ZoomBubbleView(
142 views::View* anchor_view, 157 views::View* anchor_view,
143 const gfx::Point& anchor_point, 158 const gfx::Point& anchor_point,
144 content::WebContents* web_contents, 159 content::WebContents* web_contents,
145 DisplayReason reason, 160 DisplayReason reason,
146 ImmersiveModeController* immersive_mode_controller) 161 ImmersiveModeController* immersive_mode_controller)
147 : LocationBarBubbleDelegateView(anchor_view, anchor_point, web_contents), 162 : LocationBarBubbleDelegateView(anchor_view, anchor_point, web_contents),
163 timer_duration_(kBubbleCloseDelayDefaultMs),
148 image_button_(nullptr), 164 image_button_(nullptr),
149 label_(nullptr), 165 label_(nullptr),
166 zoom_out_button_(nullptr),
167 zoom_in_button_(nullptr),
168 reset_button_(nullptr),
150 web_contents_(web_contents), 169 web_contents_(web_contents),
151 auto_close_(reason == AUTOMATIC), 170 auto_close_(reason == AUTOMATIC),
171 ignore_close_bubble_(false),
152 immersive_mode_controller_(immersive_mode_controller) { 172 immersive_mode_controller_(immersive_mode_controller) {
153 set_notify_enter_exit_on_child(true); 173 set_notify_enter_exit_on_child(true);
154 if (immersive_mode_controller_) 174 if (immersive_mode_controller_)
155 immersive_mode_controller_->AddObserver(this); 175 immersive_mode_controller_->AddObserver(this);
156 UseCompactMargins(); 176 UseCompactMargins();
157 } 177 }
158 178
159 ZoomBubbleView::~ZoomBubbleView() { 179 ZoomBubbleView::~ZoomBubbleView() {
160 if (immersive_mode_controller_) 180 if (immersive_mode_controller_)
161 immersive_mode_controller_->RemoveObserver(this); 181 immersive_mode_controller_->RemoveObserver(this);
(...skipping 12 matching lines...) Expand all
174 194
175 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) { 195 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) {
176 StopTimer(); 196 StopTimer();
177 } 197 }
178 198
179 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) { 199 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) {
180 StartTimerIfNecessary(); 200 StartTimerIfNecessary();
181 } 201 }
182 202
183 void ZoomBubbleView::Init() { 203 void ZoomBubbleView::Init() {
184 // Set up the layout of the zoom bubble. A grid layout is used because 204 // Set up the layout of the zoom bubble.
185 // sometimes an extension icon is shown next to the zoom label. 205 ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
186 views::GridLayout* grid_layout = new views::GridLayout(this); 206 const int margin =
187 SetLayoutManager(grid_layout); 207 provider->GetDistanceMetric(views::DISTANCE_RELATED_BUTTON_HORIZONTAL);
188 views::ColumnSet* columns = grid_layout->AddColumnSet(0); 208 views::BoxLayout* box_layout =
189 // First row. 209 new views::BoxLayout(views::BoxLayout::kHorizontal, margin, 0, 0);
190 if (extension_info_.icon_image) { 210
191 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 2, 211 box_layout->set_main_axis_alignment(
192 views::GridLayout::USE_PREF, 0, 0); 212 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
193 } 213 box_layout->set_cross_axis_alignment(
194 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, 214 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
195 views::GridLayout::USE_PREF, 0, 0); 215 box_layout->SetDefaultFlex(1);
196 grid_layout->StartRow(0, 0); 216 SetLayoutManager(box_layout);
197 217
198 // If this zoom change was initiated by an extension, that extension will be 218 // If this zoom change was initiated by an extension, that extension will be
199 // attributed by showing its icon in the zoom bubble. 219 // attributed by showing its icon in the zoom bubble.
200 if (extension_info_.icon_image) { 220 if (extension_info_.icon_image) {
201 image_button_ = new views::ImageButton(this); 221 image_button_ = new views::ImageButton(this);
202 image_button_->SetTooltipText( 222 image_button_->SetTooltipText(
203 l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON, 223 l10n_util::GetStringFUTF16(IDS_TOOLTIP_ZOOM_EXTENSION_ICON,
204 base::UTF8ToUTF16(extension_info_.name))); 224 base::UTF8ToUTF16(extension_info_.name)));
205 image_button_->SetImage(views::Button::STATE_NORMAL, 225 image_button_->SetImage(views::Button::STATE_NORMAL,
206 &extension_info_.icon_image->image_skia()); 226 &extension_info_.icon_image->image_skia());
207 grid_layout->AddView(image_button_); 227 AddChildView(image_button_);
208 } 228 }
209 229
230 // Add Zoom Out ("-") button.
231 std::unique_ptr<views::Button> zoom_out_button =
232 CreateZoomButton(kRemoveIcon, IDS_ACCNAME_ZOOM_MINUS2);
233 zoom_out_button_ = zoom_out_button.get();
234 AddChildView(zoom_out_button.release());
235
210 // Add zoom label with the new zoom percent. 236 // Add zoom label with the new zoom percent.
211 zoom::ZoomController* zoom_controller = 237 label_ = new views::Label();
212 zoom::ZoomController::FromWebContents(web_contents_); 238 UpdateZoomPercent();
213 int zoom_percent = zoom_controller->GetZoomPercent(); 239 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 240
219 // Second row. 241 // Add Zoom In ("+") button with extra padding on the right because "Reset"
220 grid_layout->AddPaddingRow(0, 8); 242 // button has fixed size.
221 columns = grid_layout->AddColumnSet(1); 243 std::unique_ptr<views::Button> zoom_in_button =
222 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, 244 CreateZoomButton(kAddIcon, IDS_ACCNAME_ZOOM_PLUS2);
223 views::GridLayout::USE_PREF, 0, 0); 245 views::View* zoom_in_button_padding = new views::View();
tapted 2017/05/02 01:28:02 Rather than a container View with a border and lay
varkha 2017/05/03 01:50:27 Yes, I actually went back and forth on something l
224 grid_layout->StartRow(0, 1); 246 zoom_in_button_padding->SetLayoutManager(new views::FillLayout());
247 zoom_in_button_padding->SetBorder(views::CreateEmptyBorder(0, 0, 0, margin));
248 zoom_in_button_ = zoom_in_button.get();
249 zoom_in_button_padding->AddChildView(zoom_in_button.release());
250 AddChildView(zoom_in_button_padding);
225 251
226 // Add "Reset to Default" button. 252 // Add "Reset" button.
227 grid_layout->AddView( 253 reset_button_ = views::MdTextButton::CreateSecondaryUiButton(
228 views::MdTextButton::CreateSecondaryUiButton( 254 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT));
229 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT))); 255 reset_button_->SetTooltipText(
256 l10n_util::GetStringUTF16(IDS_ACCNAME_ZOOM_SET_DEFAULT));
257 AddChildView(reset_button_);
258 box_layout->SetFlexForView(reset_button_, 0);
230 259
231 StartTimerIfNecessary(); 260 StartTimerIfNecessary();
232 } 261 }
233 262
234 void ZoomBubbleView::WindowClosing() { 263 void ZoomBubbleView::WindowClosing() {
235 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't 264 // |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. 265 // call this right away). Only set to nullptr when it's this bubble.
237 if (zoom_bubble_ == this) 266 if (zoom_bubble_ == this)
238 zoom_bubble_ = nullptr; 267 zoom_bubble_ = nullptr;
239 } 268 }
240 269
241 void ZoomBubbleView::CloseBubble() { 270 void ZoomBubbleView::CloseBubble() {
271 if (ignore_close_bubble_)
272 return;
273
242 // Widget's Close() is async, but we don't want to use zoom_bubble_ after 274 // Widget's Close() is async, but we don't want to use zoom_bubble_ after
243 // this. Additionally web_contents_ may have been destroyed. 275 // this. Additionally web_contents_ may have been destroyed.
244 zoom_bubble_ = nullptr; 276 zoom_bubble_ = nullptr;
245 web_contents_ = nullptr; 277 web_contents_ = nullptr;
246 LocationBarBubbleDelegateView::CloseBubble(); 278 LocationBarBubbleDelegateView::CloseBubble();
247 } 279 }
248 280
281 bool ZoomBubbleView::ShouldSnapFrameWidth() const {
282 return false;
283 }
284
285 gfx::Size ZoomBubbleView::GetPreferredSize() const {
286 const gfx::Size kPreferredZoomBubbleSize = gfx::Size(208, 32);
tapted 2017/05/02 01:28:02 constexpr? (the geometry classes have the syntax
varkha 2017/05/03 01:50:27 Done.
287 return kPreferredZoomBubbleSize;
288 }
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 // No buttons presses in this dialog should cause the dialog to close,
293 // including when the zoom level is set to 100% as a result of a button press.
294 base::AutoReset<bool> auto_ignore_close_bubble(&ignore_close_bubble_, true);
295
296 // Extend the timer to give a user more time after any button is pressed.
297 StopTimer();
298 timer_duration_ = kBubbleCloseDelayLongMs;
299 StartTimerIfNecessary();
300
251 if (sender == image_button_) { 301 if (sender == image_button_) {
252 DCHECK(extension_info_.icon_image) << "Invalid button press."; 302 DCHECK(extension_info_.icon_image) << "Invalid button press.";
253 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); 303 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
254 chrome::AddSelectedTabWithURL( 304 chrome::AddSelectedTabWithURL(
255 browser, GURL(base::StringPrintf("chrome://extensions?id=%s", 305 browser, GURL(base::StringPrintf("chrome://extensions?id=%s",
256 extension_info_.id.c_str())), 306 extension_info_.id.c_str())),
257 ui::PAGE_TRANSITION_FROM_API); 307 ui::PAGE_TRANSITION_FROM_API);
308 } else if (sender == zoom_out_button_) {
309 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_OUT);
310 } else if (sender == zoom_in_button_) {
311 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_IN);
312 } else if (sender == reset_button_) {
313 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET);
258 } else { 314 } else {
259 zoom::PageZoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET); 315 NOTREACHED();
260 } 316 }
261 } 317 }
262 318
263 void ZoomBubbleView::OnImmersiveRevealStarted() { 319 void ZoomBubbleView::OnImmersiveRevealStarted() {
264 CloseBubble(); 320 CloseBubble();
265 } 321 }
266 322
267 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() { 323 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
268 immersive_mode_controller_ = nullptr; 324 immersive_mode_controller_ = nullptr;
269 } 325 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 icon_size = browser_action->default_icon.map().begin()->first; 368 icon_size = browser_action->default_icon.map().begin()->first;
313 extension_info_.icon_image.reset( 369 extension_info_.icon_image.reset(
314 new extensions::IconImage(web_contents_->GetBrowserContext(), 370 new extensions::IconImage(web_contents_->GetBrowserContext(),
315 extension, 371 extension,
316 browser_action->default_icon, 372 browser_action->default_icon,
317 icon_size, 373 icon_size,
318 default_extension_icon_image, 374 default_extension_icon_image,
319 this)); 375 this));
320 } 376 }
321 377
378 std::unique_ptr<views::Button> ZoomBubbleView::CreateZoomButton(
379 const gfx::VectorIcon& icon,
380 int tooltip_id) {
381 std::unique_ptr<views::ImageButton> button(
382 views::CreateVectorImageButton(this));
383 views::SetImageFromVectorIcon(button.get(), icon);
384 button->SetFocusForPlatform();
385 button->SetTooltipText(l10n_util::GetStringUTF16(tooltip_id));
386 return button;
387 }
388
389 void ZoomBubbleView::UpdateZoomPercent() {
390 label_->SetText(base::FormatPercent(
391 zoom::ZoomController::FromWebContents(web_contents_)->GetZoomPercent()));
392 }
393
322 void ZoomBubbleView::StartTimerIfNecessary() { 394 void ZoomBubbleView::StartTimerIfNecessary() {
323 if (!auto_close_) 395 if (!auto_close_)
324 return; 396 return;
325 397
326 if (timer_.IsRunning()) { 398 if (timer_.IsRunning()) {
tapted 2017/05/02 01:28:02 I don't think this case adds anything. Timer::Star
varkha 2017/05/03 01:50:27 Done.
327 timer_.Reset(); 399 timer_.Reset();
328 } else { 400 } else {
329 // The number of milliseconds the bubble should stay on the screen if it 401 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(timer_duration_),
330 // will close automatically. 402 this, &ZoomBubbleView::CloseBubble);
331 const int kBubbleCloseDelay = 1500;
332 timer_.Start(FROM_HERE,
333 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), this,
334 &ZoomBubbleView::CloseBubble);
335 } 403 }
336 } 404 }
337 405
338 void ZoomBubbleView::StopTimer() { 406 void ZoomBubbleView::StopTimer() {
339 timer_.Stop(); 407 timer_.Stop();
340 } 408 }
341 409
342 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {} 410 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {}
343 411
344 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {} 412 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698