1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/gtk/zoom_bubble_gtk.h"
#include "base/i18n/rtl.h"
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/gtk/gtk_theme_service.h"
#include "chrome/browser/ui/gtk/location_bar_view_gtk.h"
#include "chrome/browser/ui/zoom/zoom_controller.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "grit/generated_resources.h"
#include "ui/base/gtk/gtk_hig_constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/rect.h"
namespace {
// Pointer to singleton object (NULL if no bubble is open).
ZoomBubbleGtk* g_bubble = NULL;
// Number of milliseconds the bubble should stay open for if it will auto-close.
const int kBubbleCloseDelay = 1500;
// Need to manually set anchor width and height to ensure that the bubble shows
// in the correct spot the first time it is displayed when no icon is present.
const int kBubbleAnchorWidth = 20;
const int kBubbleAnchorHeight = 25;
} // namespace
// static
void ZoomBubbleGtk::ShowBubble(content::WebContents* web_contents,
bool auto_close) {
Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
DCHECK(browser && browser->window() && browser->fullscreen_controller());
LocationBar* location_bar = browser->window()->GetLocationBar();
GtkWidget* anchor = browser->window()->IsFullscreen() ?
GTK_WIDGET(browser->window()->GetNativeWindow()) :
static_cast<LocationBarViewGtk*>(location_bar)->zoom_widget();
// If the bubble is already showing and its |auto_close_| value is equal to
// |auto_close|, the bubble can be reused and only the label text needs to
// be updated.
if (g_bubble &&
g_bubble->auto_close_ == auto_close &&
g_bubble->bubble_->anchor_widget() == anchor) {
g_bubble->Refresh();
} else {
// If the bubble is already showing but its |auto_close_| value is not equal
// to |auto_close|, the bubble's focus properties must change, so the
// current bubble must be closed and a new one created.
CloseBubble();
DCHECK(!g_bubble);
g_bubble = new ZoomBubbleGtk(anchor,
web_contents,
auto_close,
browser->fullscreen_controller());
}
}
// static
void ZoomBubbleGtk::CloseBubble() {
if (g_bubble)
g_bubble->Close();
}
// static
bool ZoomBubbleGtk::IsShowing() {
return g_bubble != NULL;
}
ZoomBubbleGtk::ZoomBubbleGtk(GtkWidget* anchor,
content::WebContents* web_contents,
bool auto_close,
FullscreenController* fullscreen_controller)
: auto_close_(auto_close),
mouse_inside_(false),
web_contents_(web_contents) {
GtkThemeService* theme_service =
GtkThemeService::GetFrom(Profile::FromBrowserContext(
web_contents_->GetBrowserContext()));
event_box_ = gtk_event_box_new();
gtk_event_box_set_visible_window(GTK_EVENT_BOX(event_box_), FALSE);
GtkWidget* container = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(event_box_), container);
ZoomController* zoom_controller =
ZoomController::FromWebContents(web_contents_);
int zoom_percent = zoom_controller->zoom_percent();
std::string percentage_text = UTF16ToUTF8(l10n_util::GetStringFUTF16Int(
IDS_TOOLTIP_ZOOM, zoom_percent));
label_ = theme_service->BuildLabel(percentage_text, ui::kGdkBlack);
gtk_widget_modify_font(label_, pango_font_description_from_string("13"));
gtk_misc_set_padding(GTK_MISC(label_),
ui::kControlSpacing, ui::kControlSpacing);
gtk_box_pack_start(GTK_BOX(container), label_, FALSE, FALSE, 0);
GtkWidget* set_default_button = gtk_button_new_with_label(
l10n_util::GetStringUTF8(IDS_ZOOM_SET_DEFAULT).c_str());
GtkWidget* alignment = gtk_alignment_new(0, 0, 1, 1);
gtk_alignment_set_padding(GTK_ALIGNMENT(alignment),
0,
ui::kControlSpacing,
ui::kControlSpacing,
ui::kControlSpacing);
gtk_container_add(GTK_CONTAINER(alignment), set_default_button);
gtk_box_pack_start(GTK_BOX(container), alignment, FALSE, FALSE, 0);
g_signal_connect(set_default_button, "clicked",
G_CALLBACK(&OnSetDefaultLinkClickThunk), this);
gtk_container_set_focus_child(GTK_CONTAINER(container), NULL);
gfx::Rect rect = gfx::Rect(kBubbleAnchorWidth, kBubbleAnchorHeight);
BubbleGtk::FrameStyle frame_style = gtk_widget_is_toplevel(anchor) ?
BubbleGtk::FIXED_TOP_RIGHT : BubbleGtk::ANCHOR_TOP_MIDDLE;
int bubble_options = BubbleGtk::MATCH_SYSTEM_THEME | BubbleGtk::POPUP_WINDOW;
bubble_ = BubbleGtk::Show(anchor, &rect, event_box_, frame_style,
auto_close ? bubble_options : bubble_options | BubbleGtk::GRAB_INPUT,
theme_service, NULL);
if (!bubble_) {
NOTREACHED();
return;
}
g_signal_connect(event_box_, "destroy",
G_CALLBACK(&OnDestroyThunk), this);
if (auto_close_) {
// If this is an auto-closing bubble, listen to leave/enter to keep the
// bubble alive if the mouse stays anywhere inside the bubble.
g_signal_connect_after(event_box_, "enter-notify-event",
G_CALLBACK(&OnMouseEnterThunk), this);
g_signal_connect(event_box_, "leave-notify-event",
G_CALLBACK(&OnMouseLeaveThunk), this);
// This is required as a leave is fired when the mouse goes from inside the
// bubble's container to inside the set default button.
gtk_widget_add_events(set_default_button, GDK_ENTER_NOTIFY_MASK);
g_signal_connect_after(set_default_button, "enter-notify-event",
G_CALLBACK(&OnMouseEnterThunk), this);
g_signal_connect(set_default_button, "leave-notify-event",
G_CALLBACK(&OnMouseLeaveThunk), this);
}
registrar_.Add(this,
chrome::NOTIFICATION_FULLSCREEN_CHANGED,
content::Source<FullscreenController>(fullscreen_controller));
StartTimerIfNecessary();
}
ZoomBubbleGtk::~ZoomBubbleGtk() {
DCHECK_EQ(g_bubble, this);
// Set singleton pointer to NULL.
g_bubble = NULL;
}
void ZoomBubbleGtk::Refresh() {
ZoomController* zoom_controller =
ZoomController::FromWebContents(web_contents_);
int zoom_percent = zoom_controller->zoom_percent();
base::string16 text =
l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent);
gtk_label_set_text(GTK_LABEL(g_bubble->label_), UTF16ToUTF8(text).c_str());
StartTimerIfNecessary();
}
void ZoomBubbleGtk::StartTimerIfNecessary() {
if (!auto_close_ || mouse_inside_)
return;
if (timer_.IsRunning()) {
timer_.Reset();
} else {
timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kBubbleCloseDelay),
this,
&ZoomBubbleGtk::Close);
}
}
void ZoomBubbleGtk::StopTimerIfNecessary() {
if (timer_.IsRunning())
timer_.Stop();
}
void ZoomBubbleGtk::Close() {
DCHECK(bubble_);
bubble_->Close();
}
void ZoomBubbleGtk::OnDestroy(GtkWidget* widget) {
// Listen to the destroy signal and delete this instance when it is caught.
delete this;
}
void ZoomBubbleGtk::OnSetDefaultLinkClick(GtkWidget* widget) {
double default_zoom_level = Profile::FromBrowserContext(
web_contents_->GetBrowserContext())->GetPrefs()->GetDouble(
prefs::kDefaultZoomLevel);
web_contents_->GetRenderViewHost()->SetZoomLevel(default_zoom_level);
}
gboolean ZoomBubbleGtk::OnMouseEnter(GtkWidget* widget,
GdkEventCrossing* event) {
mouse_inside_ = true;
StopTimerIfNecessary();
return FALSE;
}
gboolean ZoomBubbleGtk::OnMouseLeave(GtkWidget* widget,
GdkEventCrossing* event) {
mouse_inside_ = false;
StartTimerIfNecessary();
return FALSE;
}
void ZoomBubbleGtk::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(type, chrome::NOTIFICATION_FULLSCREEN_CHANGED);
CloseBubble();
}
|