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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
7 #include "base/i18n/rtl.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/chrome_page_zoom.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
15 #include "chrome/browser/ui/views/location_bar/zoom_view.h"
16 #include "chrome/browser/ui/zoom/zoom_controller.h"
17 #include "content/public/browser/notification_source.h"
18 #include "content/public/browser/web_contents_view.h"
19 #include "grit/generated_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/views/controls/button/label_button.h"
23 #include "ui/views/controls/separator.h"
24 #include "ui/views/layout/box_layout.h"
25 #include "ui/views/layout/layout_constants.h"
26 #include "ui/views/widget/widget.h"
30 // The number of milliseconds the bubble should stay on the screen if it will
31 // close automatically.
32 const int kBubbleCloseDelay
= 1500;
34 // The bubble's padding from the screen edge, used in fullscreen.
35 const int kFullscreenPaddingEnd
= 20;
40 ZoomBubbleView
* ZoomBubbleView::zoom_bubble_
= NULL
;
43 void ZoomBubbleView::ShowBubble(content::WebContents
* web_contents
,
45 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents
);
46 DCHECK(browser
&& browser
->window() && browser
->fullscreen_controller());
48 BrowserView
* browser_view
= BrowserView::GetBrowserViewForBrowser(browser
);
49 bool is_fullscreen
= browser_view
->IsFullscreen();
50 bool anchor_to_view
= !is_fullscreen
||
51 browser_view
->immersive_mode_controller()->IsRevealed();
52 views::View
* anchor_view
= anchor_to_view
?
53 browser_view
->GetLocationBarView()->zoom_view() : NULL
;
55 // If the bubble is already showing in this window and its |auto_close_| value
56 // is equal to |auto_close|, the bubble can be reused and only the label text
57 // needs to be updated.
59 zoom_bubble_
->GetAnchorView() == anchor_view
&&
60 zoom_bubble_
->auto_close_
== auto_close
) {
61 zoom_bubble_
->Refresh();
63 // If the bubble is already showing but its |auto_close_| value is not equal
64 // to |auto_close|, the bubble's focus properties must change, so the
65 // current bubble must be closed and a new one created.
68 zoom_bubble_
= new ZoomBubbleView(anchor_view
,
71 browser_view
->immersive_mode_controller(),
72 browser
->fullscreen_controller());
74 // If we do not have an anchor view, parent the bubble to the content area.
75 if (!anchor_to_view
) {
76 zoom_bubble_
->set_parent_window(
77 web_contents
->GetView()->GetTopLevelNativeWindow());
80 views::BubbleDelegateView::CreateBubble(zoom_bubble_
);
82 // Adjust for fullscreen after creation as it relies on the content size.
84 zoom_bubble_
->AdjustForFullscreen(browser_view
->GetBoundsInScreen());
86 if (zoom_bubble_
->use_focusless())
87 zoom_bubble_
->GetWidget()->ShowInactive();
89 zoom_bubble_
->GetWidget()->Show();
94 void ZoomBubbleView::CloseBubble() {
96 zoom_bubble_
->Close();
100 bool ZoomBubbleView::IsShowing() {
101 // The bubble may be in the process of closing.
102 return zoom_bubble_
!= NULL
&& zoom_bubble_
->GetWidget()->IsVisible();
106 const ZoomBubbleView
* ZoomBubbleView::GetZoomBubbleForTest() {
110 ZoomBubbleView::ZoomBubbleView(
111 views::View
* anchor_view
,
112 content::WebContents
* web_contents
,
114 ImmersiveModeController
* immersive_mode_controller
,
115 FullscreenController
* fullscreen_controller
)
116 : BubbleDelegateView(anchor_view
, anchor_view
?
117 views::BubbleBorder::TOP_RIGHT
: views::BubbleBorder::NONE
),
119 web_contents_(web_contents
),
120 auto_close_(auto_close
),
121 immersive_mode_controller_(immersive_mode_controller
) {
122 // Compensate for built-in vertical padding in the anchor view's image.
123 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0));
124 set_use_focusless(auto_close
);
125 set_notify_enter_exit_on_child(true);
127 // Add observers to close the bubble if the fullscreen state or immersive
128 // fullscreen revealed state changes.
130 chrome::NOTIFICATION_FULLSCREEN_CHANGED
,
131 content::Source
<FullscreenController
>(fullscreen_controller
));
132 immersive_mode_controller_
->AddObserver(this);
135 ZoomBubbleView::~ZoomBubbleView() {
136 if (immersive_mode_controller_
)
137 immersive_mode_controller_
->RemoveObserver(this);
140 void ZoomBubbleView::AdjustForFullscreen(const gfx::Rect
& screen_bounds
) {
144 // TODO(dbeam): should RTL logic be done in views::BubbleDelegateView?
145 const size_t bubble_half_width
= width() / 2;
146 const int x_pos
= base::i18n::IsRTL() ?
147 screen_bounds
.x() + bubble_half_width
+ kFullscreenPaddingEnd
:
148 screen_bounds
.right() - bubble_half_width
- kFullscreenPaddingEnd
;
149 SetAnchorRect(gfx::Rect(x_pos
, screen_bounds
.y(), 0, 0));
152 void ZoomBubbleView::Refresh() {
153 ZoomController
* zoom_controller
=
154 ZoomController::FromWebContents(web_contents_
);
155 int zoom_percent
= zoom_controller
->zoom_percent();
157 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM
, zoom_percent
));
158 StartTimerIfNecessary();
161 void ZoomBubbleView::Close() {
162 GetWidget()->Close();
165 void ZoomBubbleView::StartTimerIfNecessary() {
167 if (timer_
.IsRunning()) {
172 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay
),
174 &ZoomBubbleView::Close
);
179 void ZoomBubbleView::StopTimer() {
183 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent
& event
) {
184 set_use_focusless(false);
188 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent
& event
) {
189 set_use_focusless(auto_close_
);
190 StartTimerIfNecessary();
193 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent
* event
) {
194 if (!zoom_bubble_
|| !zoom_bubble_
->auto_close_
||
195 event
->type() != ui::ET_GESTURE_TAP
) {
199 // If an auto-closing bubble was tapped, show a non-auto-closing bubble in
201 ShowBubble(zoom_bubble_
->web_contents_
, false);
205 void ZoomBubbleView::ButtonPressed(views::Button
* sender
,
206 const ui::Event
& event
) {
207 chrome_page_zoom::Zoom(web_contents_
, content::PAGE_ZOOM_RESET
);
210 void ZoomBubbleView::Init() {
211 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical
,
212 0, 0, views::kRelatedControlVerticalSpacing
));
214 ZoomController
* zoom_controller
=
215 ZoomController::FromWebContents(web_contents_
);
216 int zoom_percent
= zoom_controller
->zoom_percent();
217 label_
= new views::Label(
218 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM
, zoom_percent
));
220 ui::ResourceBundle::GetSharedInstance().GetFontList(
221 ui::ResourceBundle::MediumFont
));
222 AddChildView(label_
);
224 views::LabelButton
* set_default_button
= new views::LabelButton(
225 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT
));
226 set_default_button
->SetStyle(views::Button::STYLE_BUTTON
);
227 set_default_button
->SetHorizontalAlignment(gfx::ALIGN_CENTER
);
228 AddChildView(set_default_button
);
230 StartTimerIfNecessary();
233 void ZoomBubbleView::Observe(int type
,
234 const content::NotificationSource
& source
,
235 const content::NotificationDetails
& details
) {
236 DCHECK_EQ(type
, chrome::NOTIFICATION_FULLSCREEN_CHANGED
);
240 void ZoomBubbleView::OnImmersiveRevealStarted() {
244 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
245 immersive_mode_controller_
= NULL
;
248 void ZoomBubbleView::WindowClosing() {
249 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't
250 // call this right away). Only set to NULL when it's this bubble.
251 if (zoom_bubble_
== this)