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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
// Copyright (c) 2011 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/download_shelf_gtk.h"
#include <string>
#include "chrome/browser/download/download_item.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/gtk/browser_window_gtk.h"
#include "chrome/browser/ui/gtk/custom_button.h"
#include "chrome/browser/ui/gtk/download_item_gtk.h"
#include "chrome/browser/ui/gtk/gtk_chrome_link_button.h"
#include "chrome/browser/ui/gtk/gtk_chrome_shrinkable_hbox.h"
#include "chrome/browser/ui/gtk/gtk_theme_provider.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "chrome/common/notification_service.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/gtk_util.h"
#include "ui/gfx/insets.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
namespace {
// The height of the download items.
const int kDownloadItemHeight = download_util::kSmallProgressIconSize;
// Padding between the download widgets.
const int kDownloadItemPadding = 10;
// Padding between the top/bottom of the download widgets and the edge of the
// shelf.
const int kTopBottomPadding = 4;
// Padding between the left side of the shelf and the first download item.
const int kLeftPadding = 2;
// Padding between the right side of the shelf and the close button.
const int kRightPadding = 10;
// Speed of the shelf show/hide animation.
const int kShelfAnimationDurationMs = 120;
// The time between when the user mouses out of the download shelf zone and
// when the shelf closes (when auto-close is enabled).
const int kAutoCloseDelayMs = 300;
// The area to the top of the shelf that is considered part of its "zone".
const int kShelfAuraSize = 40;
} // namespace
DownloadShelfGtk::DownloadShelfGtk(Browser* browser, GtkWidget* parent)
: browser_(browser),
is_showing_(false),
theme_provider_(GtkThemeProvider::GetFrom(browser->profile())),
close_on_mouse_out_(false),
mouse_in_shelf_(false),
auto_close_factory_(this) {
// Logically, the shelf is a vbox that contains two children: a one pixel
// tall event box, which serves as the top border, and an hbox, which holds
// the download items and other shelf widgets (close button, show-all-
// downloads link).
// To make things pretty, we have to add a few more widgets. To get padding
// right, we stick the hbox in an alignment. We put that alignment in an
// event box so we can color the background.
// Create the top border.
top_border_ = gtk_event_box_new();
gtk_widget_set_size_request(GTK_WIDGET(top_border_), 0, 1);
// Create |items_hbox_|. We use GtkChromeShrinkableHBox, so that download
// items can be hid automatically when there is no enough space to show them.
items_hbox_.Own(gtk_chrome_shrinkable_hbox_new(
TRUE, FALSE, kDownloadItemPadding));
// We want the download shelf to be horizontally shrinkable, so that the
// Chrome window can be resized freely even with many download items.
gtk_widget_set_size_request(items_hbox_.get(), 0, kDownloadItemHeight);
// Create a hbox that holds |items_hbox_| and other shelf widgets.
GtkWidget* outer_hbox = gtk_hbox_new(FALSE, kDownloadItemPadding);
// Pack the |items_hbox_| in the outer hbox.
gtk_box_pack_start(GTK_BOX(outer_hbox), items_hbox_.get(), TRUE, TRUE, 0);
// Get the padding and background color for |outer_hbox| right.
GtkWidget* padding = gtk_alignment_new(0, 0, 1, 1);
// Subtract 1 from top spacing to account for top border.
gtk_alignment_set_padding(GTK_ALIGNMENT(padding),
kTopBottomPadding - 1, kTopBottomPadding, kLeftPadding, kRightPadding);
padding_bg_ = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(padding_bg_), padding);
gtk_container_add(GTK_CONTAINER(padding), outer_hbox);
GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), top_border_, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), padding_bg_, FALSE, FALSE, 0);
// Put the shelf in an event box so it gets its own window, which makes it
// easier to get z-ordering right.
shelf_.Own(gtk_event_box_new());
gtk_container_add(GTK_CONTAINER(shelf_.get()), vbox);
// Create and pack the close button.
close_button_.reset(CustomDrawButton::CloseButton(theme_provider_));
gtk_util::CenterWidgetInHBox(outer_hbox, close_button_->widget(), true, 0);
g_signal_connect(close_button_->widget(), "clicked",
G_CALLBACK(OnButtonClickThunk), this);
// Create the "Show all downloads..." link and connect to the click event.
std::string link_text =
l10n_util::GetStringUTF8(IDS_SHOW_ALL_DOWNLOADS);
link_button_ = gtk_chrome_link_button_new(link_text.c_str());
g_signal_connect(link_button_, "clicked",
G_CALLBACK(OnButtonClickThunk), this);
gtk_util::SetButtonTriggersNavigation(link_button_);
// Until we switch to vector graphics, force the font size.
// 13.4px == 10pt @ 96dpi
gtk_util::ForceFontSizePixels(GTK_CHROME_LINK_BUTTON(link_button_)->label,
13.4);
// Make the download arrow icon.
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
GdkPixbuf* download_pixbuf = rb.GetPixbufNamed(IDR_DOWNLOADS_FAVICON);
GtkWidget* download_image = gtk_image_new_from_pixbuf(download_pixbuf);
// Pack the link and the icon in outer hbox.
gtk_util::CenterWidgetInHBox(outer_hbox, link_button_, true, 0);
gtk_util::CenterWidgetInHBox(outer_hbox, download_image, true, 0);
slide_widget_.reset(new SlideAnimatorGtk(shelf_.get(),
SlideAnimatorGtk::UP,
kShelfAnimationDurationMs,
false, true, this));
theme_provider_->InitThemesFor(this);
registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
NotificationService::AllSources());
gtk_widget_show_all(shelf_.get());
// Stick ourselves at the bottom of the parent browser.
gtk_box_pack_end(GTK_BOX(parent), slide_widget_->widget(),
FALSE, FALSE, 0);
// Make sure we are at the very end.
gtk_box_reorder_child(GTK_BOX(parent), slide_widget_->widget(), 0);
Show();
}
DownloadShelfGtk::~DownloadShelfGtk() {
for (std::vector<DownloadItemGtk*>::iterator iter = download_items_.begin();
iter != download_items_.end(); ++iter) {
delete *iter;
}
shelf_.Destroy();
items_hbox_.Destroy();
// Make sure we're no longer an observer of the message loop.
SetCloseOnMouseOut(false);
}
void DownloadShelfGtk::AddDownload(BaseDownloadItemModel* download_model_) {
download_items_.push_back(new DownloadItemGtk(this, download_model_));
Show();
}
bool DownloadShelfGtk::IsShowing() const {
return slide_widget_->IsShowing();
}
bool DownloadShelfGtk::IsClosing() const {
return slide_widget_->IsClosing();
}
void DownloadShelfGtk::Show() {
slide_widget_->Open();
browser_->UpdateDownloadShelfVisibility(true);
CancelAutoClose();
}
void DownloadShelfGtk::Close() {
// When we are closing, we can vertically overlap the render view. Make sure
// we are on top.
gdk_window_raise(shelf_.get()->window);
slide_widget_->Close();
browser_->UpdateDownloadShelfVisibility(false);
SetCloseOnMouseOut(false);
}
Browser* DownloadShelfGtk::browser() const {
return browser_;
}
void DownloadShelfGtk::Closed() {
// When the close animation is complete, remove all completed downloads.
size_t i = 0;
while (i < download_items_.size()) {
DownloadItem* download = download_items_[i]->get_download();
bool is_transfer_done = download->state() == DownloadItem::COMPLETE ||
download->state() == DownloadItem::CANCELLED;
if (is_transfer_done &&
download->safety_state() != DownloadItem::DANGEROUS) {
RemoveDownloadItem(download_items_[i]);
} else {
// We set all remaining items as "opened", so that the shelf will auto-
// close in the future without the user clicking on them.
download->set_opened(true);
++i;
}
}
}
void DownloadShelfGtk::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == NotificationType::BROWSER_THEME_CHANGED) {
GdkColor color = theme_provider_->GetGdkColor(
BrowserThemeProvider::COLOR_TOOLBAR);
gtk_widget_modify_bg(padding_bg_, GTK_STATE_NORMAL, &color);
color = theme_provider_->GetBorderColor();
gtk_widget_modify_bg(top_border_, GTK_STATE_NORMAL, &color);
gtk_chrome_link_button_set_use_gtk_theme(
GTK_CHROME_LINK_BUTTON(link_button_), theme_provider_->UseGtkTheme());
// When using a non-standard, non-gtk theme, we make the link color match
// the bookmark text color. Otherwise, standard link blue can look very
// bad for some dark themes.
bool use_default_color = theme_provider_->GetColor(
BrowserThemeProvider::COLOR_BOOKMARK_TEXT) ==
BrowserThemeProvider::GetDefaultColor(
BrowserThemeProvider::COLOR_BOOKMARK_TEXT);
GdkColor bookmark_color = theme_provider_->GetGdkColor(
BrowserThemeProvider::COLOR_BOOKMARK_TEXT);
gtk_chrome_link_button_set_normal_color(
GTK_CHROME_LINK_BUTTON(link_button_),
use_default_color ? NULL : &bookmark_color);
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
close_button_->SetBackground(
theme_provider_->GetColor(BrowserThemeProvider::COLOR_TAB_TEXT),
rb.GetBitmapNamed(IDR_CLOSE_BAR),
rb.GetBitmapNamed(IDR_CLOSE_BAR_MASK));
}
}
int DownloadShelfGtk::GetHeight() const {
return slide_widget_->widget()->allocation.height;
}
void DownloadShelfGtk::RemoveDownloadItem(DownloadItemGtk* download_item) {
DCHECK(download_item);
std::vector<DownloadItemGtk*>::iterator i =
find(download_items_.begin(), download_items_.end(), download_item);
DCHECK(i != download_items_.end());
download_items_.erase(i);
delete download_item;
if (download_items_.empty()) {
slide_widget_->CloseWithoutAnimation();
browser_->UpdateDownloadShelfVisibility(false);
} else {
AutoCloseIfPossible();
}
}
GtkWidget* DownloadShelfGtk::GetHBox() const {
return items_hbox_.get();
}
void DownloadShelfGtk::MaybeShowMoreDownloadItems() {
// Show all existing download items. It'll trigger "size-allocate" signal,
// which will hide download items that don't have enough space to show.
gtk_widget_show_all(items_hbox_.get());
}
void DownloadShelfGtk::OnButtonClick(GtkWidget* button) {
if (button == close_button_->widget()) {
Close();
} else {
// The link button was clicked.
browser_->ShowDownloadsTab();
}
}
void DownloadShelfGtk::AutoCloseIfPossible() {
for (std::vector<DownloadItemGtk*>::iterator iter = download_items_.begin();
iter != download_items_.end(); ++iter) {
if (!(*iter)->get_download()->opened())
return;
}
SetCloseOnMouseOut(true);
}
void DownloadShelfGtk::CancelAutoClose() {
SetCloseOnMouseOut(false);
auto_close_factory_.RevokeAll();
}
void DownloadShelfGtk::ItemOpened() {
AutoCloseIfPossible();
}
void DownloadShelfGtk::SetCloseOnMouseOut(bool close) {
if (close_on_mouse_out_ == close)
return;
close_on_mouse_out_ = close;
mouse_in_shelf_ = close;
if (close)
MessageLoopForUI::current()->AddObserver(this);
else
MessageLoopForUI::current()->RemoveObserver(this);
}
void DownloadShelfGtk::WillProcessEvent(GdkEvent* event) {
}
void DownloadShelfGtk::DidProcessEvent(GdkEvent* event) {
gfx::Point cursor_screen_coords;
switch (event->type) {
case GDK_MOTION_NOTIFY:
cursor_screen_coords =
gfx::Point(event->motion.x_root, event->motion.y_root);
break;
case GDK_LEAVE_NOTIFY:
cursor_screen_coords =
gfx::Point(event->crossing.x_root, event->crossing.y_root);
break;
default:
return;
}
bool mouse_in_shelf = IsCursorInShelfZone(cursor_screen_coords);
if (mouse_in_shelf == mouse_in_shelf_)
return;
mouse_in_shelf_ = mouse_in_shelf;
if (mouse_in_shelf)
MouseEnteredShelf();
else
MouseLeftShelf();
}
bool DownloadShelfGtk::IsCursorInShelfZone(
const gfx::Point& cursor_screen_coords) {
gfx::Rect bounds(gtk_util::GetWidgetScreenPosition(shelf_.get()),
gfx::Size(shelf_.get()->allocation.width,
shelf_.get()->allocation.height));
// Negative insets expand the rectangle. We only expand the top.
bounds.Inset(gfx::Insets(-kShelfAuraSize, 0, 0, 0));
return bounds.Contains(cursor_screen_coords);
}
void DownloadShelfGtk::MouseLeftShelf() {
DCHECK(close_on_mouse_out_);
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
auto_close_factory_.NewRunnableMethod(&DownloadShelfGtk::Close),
kAutoCloseDelayMs);
}
void DownloadShelfGtk::MouseEnteredShelf() {
auto_close_factory_.RevokeAll();
}
|