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
|
// 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/views/bookmarks/bookmark_bubble_view.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/bookmarks/bookmark_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/bookmarks/bookmark_editor.h"
#include "chrome/browser/ui/sync/sync_promo_ui.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_bubble_view_observer.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_sync_promo_view.h"
#include "content/public/browser/user_metrics.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/combobox/combobox.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/widget/widget.h"
using base::UserMetricsAction;
using views::ColumnSet;
using views::GridLayout;
namespace {
// Minimum width of the the bubble.
const int kMinBubbleWidth = 350;
// Width of the border of a button.
const int kControlBorderWidth = 2;
} // namespace
BookmarkBubbleView* BookmarkBubbleView::bookmark_bubble_ = NULL;
// static
void BookmarkBubbleView::ShowBubble(views::View* anchor_view,
BookmarkBubbleViewObserver* observer,
scoped_ptr<BookmarkBubbleDelegate> delegate,
Profile* profile,
const GURL& url,
bool newly_bookmarked) {
if (IsShowing())
return;
bookmark_bubble_ = new BookmarkBubbleView(anchor_view,
observer,
delegate.Pass(),
profile,
url,
newly_bookmarked);
views::BubbleDelegateView::CreateBubble(bookmark_bubble_)->Show();
// Select the entire title textfield contents when the bubble is first shown.
bookmark_bubble_->title_tf_->SelectAll(true);
bookmark_bubble_->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
if (bookmark_bubble_->observer_)
bookmark_bubble_->observer_->OnBookmarkBubbleShown(url);
}
// static
bool BookmarkBubbleView::IsShowing() {
return bookmark_bubble_ != NULL;
}
void BookmarkBubbleView::Hide() {
if (IsShowing())
bookmark_bubble_->GetWidget()->Close();
}
BookmarkBubbleView::~BookmarkBubbleView() {
if (apply_edits_) {
ApplyEdits();
} else if (remove_bookmark_) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
const BookmarkNode* node = model->GetMostRecentlyAddedNodeForURL(url_);
if (node)
model->Remove(node->parent(), node->parent()->GetIndexOf(node));
}
// |parent_combobox_| needs to be destroyed before |parent_model_| as it
// uses |parent_model_| in its destructor.
delete parent_combobox_;
}
views::View* BookmarkBubbleView::GetInitiallyFocusedView() {
return title_tf_;
}
void BookmarkBubbleView::WindowClosing() {
// We have to reset |bubble_| here, not in our destructor, because we'll be
// destroyed asynchronously and the shown state will be checked before then.
DCHECK_EQ(bookmark_bubble_, this);
bookmark_bubble_ = NULL;
if (observer_)
observer_->OnBookmarkBubbleHidden();
}
bool BookmarkBubbleView::AcceleratorPressed(
const ui::Accelerator& accelerator) {
if (accelerator.key_code() == ui::VKEY_RETURN) {
if (edit_button_->HasFocus())
HandleButtonPressed(edit_button_);
else
HandleButtonPressed(close_button_);
return true;
} else if (accelerator.key_code() == ui::VKEY_ESCAPE) {
remove_bookmark_ = newly_bookmarked_;
apply_edits_ = false;
}
return BubbleDelegateView::AcceleratorPressed(accelerator);
}
void BookmarkBubbleView::Init() {
views::Label* title_label = new views::Label(
l10n_util::GetStringUTF16(
newly_bookmarked_ ? IDS_BOOKMARK_BUBBLE_PAGE_BOOKMARKED :
IDS_BOOKMARK_BUBBLE_PAGE_BOOKMARK));
ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
title_label->SetFontList(rb->GetFontList(ui::ResourceBundle::MediumFont));
remove_button_ = new views::LabelButton(this, l10n_util::GetStringUTF16(
IDS_BOOKMARK_BUBBLE_REMOVE_BOOKMARK));
remove_button_->SetStyle(views::Button::STYLE_BUTTON);
edit_button_ = new views::LabelButton(
this, l10n_util::GetStringUTF16(IDS_BOOKMARK_BUBBLE_OPTIONS));
edit_button_->SetStyle(views::Button::STYLE_BUTTON);
close_button_ = new views::LabelButton(
this, l10n_util::GetStringUTF16(IDS_DONE));
close_button_->SetStyle(views::Button::STYLE_BUTTON);
close_button_->SetIsDefault(true);
views::Label* combobox_label = new views::Label(
l10n_util::GetStringUTF16(IDS_BOOKMARK_BUBBLE_FOLDER_TEXT));
parent_combobox_ = new views::Combobox(&parent_model_);
parent_combobox_->set_listener(this);
parent_combobox_->SetAccessibleName(
l10n_util::GetStringUTF16(IDS_BOOKMARK_AX_BUBBLE_FOLDER_TEXT));
GridLayout* layout = new GridLayout(this);
SetLayoutManager(layout);
// Column sets used in the layout of the bubble.
enum ColumnSetID {
TITLE_COLUMN_SET_ID,
CONTENT_COLUMN_SET_ID,
SYNC_PROMO_COLUMN_SET_ID
};
ColumnSet* cs = layout->AddColumnSet(TITLE_COLUMN_SET_ID);
cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
cs->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0, GridLayout::USE_PREF,
0, 0);
cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
// The column layout used for middle and bottom rows.
cs = layout->AddColumnSet(CONTENT_COLUMN_SET_ID);
cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
cs->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
GridLayout::USE_PREF, 0, 0);
cs->AddPaddingColumn(0, views::kUnrelatedControlHorizontalSpacing);
cs->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
GridLayout::USE_PREF, 0, 0);
cs->AddPaddingColumn(1, views::kUnrelatedControlLargeHorizontalSpacing);
cs->AddColumn(GridLayout::LEADING, GridLayout::TRAILING, 0,
GridLayout::USE_PREF, 0, 0);
cs->AddPaddingColumn(0, views::kRelatedButtonHSpacing);
cs->AddColumn(GridLayout::LEADING, GridLayout::TRAILING, 0,
GridLayout::USE_PREF, 0, 0);
cs->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
layout->StartRow(0, TITLE_COLUMN_SET_ID);
layout->AddView(title_label);
layout->AddPaddingRow(0, views::kUnrelatedControlHorizontalSpacing);
layout->StartRow(0, CONTENT_COLUMN_SET_ID);
views::Label* label = new views::Label(
l10n_util::GetStringUTF16(IDS_BOOKMARK_BUBBLE_TITLE_TEXT));
layout->AddView(label);
title_tf_ = new views::Textfield();
title_tf_->SetText(GetTitle());
title_tf_->SetAccessibleName(
l10n_util::GetStringUTF16(IDS_BOOKMARK_AX_BUBBLE_TITLE_TEXT));
layout->AddView(title_tf_, 5, 1);
layout->AddPaddingRow(0, views::kUnrelatedControlHorizontalSpacing);
layout->StartRow(0, CONTENT_COLUMN_SET_ID);
layout->AddView(combobox_label);
layout->AddView(parent_combobox_, 5, 1);
layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
layout->StartRow(0, CONTENT_COLUMN_SET_ID);
layout->SkipColumns(2);
layout->AddView(remove_button_);
layout->AddView(edit_button_);
layout->AddView(close_button_);
layout->AddPaddingRow(
0,
views::kUnrelatedControlVerticalSpacing - kControlBorderWidth);
if (SyncPromoUI::ShouldShowSyncPromo(profile_)) {
// The column layout used for the sync promo.
cs = layout->AddColumnSet(SYNC_PROMO_COLUMN_SET_ID);
cs->AddColumn(GridLayout::FILL,
GridLayout::FILL,
1,
GridLayout::USE_PREF,
0,
0);
layout->StartRow(0, SYNC_PROMO_COLUMN_SET_ID);
sync_promo_view_ = new BookmarkSyncPromoView(delegate_.get());
layout->AddView(sync_promo_view_);
}
AddAccelerator(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
}
BookmarkBubbleView::BookmarkBubbleView(
views::View* anchor_view,
BookmarkBubbleViewObserver* observer,
scoped_ptr<BookmarkBubbleDelegate> delegate,
Profile* profile,
const GURL& url,
bool newly_bookmarked)
: BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT),
observer_(observer),
delegate_(delegate.Pass()),
profile_(profile),
url_(url),
newly_bookmarked_(newly_bookmarked),
parent_model_(
BookmarkModelFactory::GetForProfile(profile_),
BookmarkModelFactory::GetForProfile(profile_)->
GetMostRecentlyAddedNodeForURL(url)),
remove_button_(NULL),
edit_button_(NULL),
close_button_(NULL),
title_tf_(NULL),
parent_combobox_(NULL),
sync_promo_view_(NULL),
remove_bookmark_(false),
apply_edits_(true) {
const SkColor background_color = GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_DialogBackground);
set_color(background_color);
set_move_with_anchor(true);
set_background(views::Background::CreateSolidBackground(background_color));
set_margins(gfx::Insets(views::kPanelVertMargin, 0, 0, 0));
// Compensate for built-in vertical padding in the anchor view's image.
set_anchor_view_insets(gfx::Insets(2, 0, 2, 0));
}
base::string16 BookmarkBubbleView::GetTitle() {
BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForProfile(profile_);
const BookmarkNode* node =
bookmark_model->GetMostRecentlyAddedNodeForURL(url_);
if (node)
return node->GetTitle();
else
NOTREACHED();
return base::string16();
}
gfx::Size BookmarkBubbleView::GetMinimumSize() {
gfx::Size size(views::BubbleDelegateView::GetPreferredSize());
size.SetToMax(gfx::Size(kMinBubbleWidth, 0));
return size;
}
void BookmarkBubbleView::GetAccessibleState(ui::AccessibleViewState* state) {
BubbleDelegateView::GetAccessibleState(state);
state->name =
l10n_util::GetStringUTF16(
newly_bookmarked_ ? IDS_BOOKMARK_BUBBLE_PAGE_BOOKMARKED :
IDS_BOOKMARK_AX_BUBBLE_PAGE_BOOKMARK);
}
void BookmarkBubbleView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
HandleButtonPressed(sender);
}
void BookmarkBubbleView::OnPerformAction(views::Combobox* combobox) {
if (combobox->selected_index() + 1 == parent_model_.GetItemCount()) {
content::RecordAction(UserMetricsAction("BookmarkBubble_EditFromCombobox"));
ShowEditor();
}
}
void BookmarkBubbleView::HandleButtonPressed(views::Button* sender) {
if (sender == remove_button_) {
content::RecordAction(UserMetricsAction("BookmarkBubble_Unstar"));
// Set this so we remove the bookmark after the window closes.
remove_bookmark_ = true;
apply_edits_ = false;
StartFade(false);
} else if (sender == edit_button_) {
content::RecordAction(UserMetricsAction("BookmarkBubble_Edit"));
ShowEditor();
} else {
DCHECK_EQ(close_button_, sender);
StartFade(false);
}
}
void BookmarkBubbleView::ShowEditor() {
const BookmarkNode* node = BookmarkModelFactory::GetForProfile(
profile_)->GetMostRecentlyAddedNodeForURL(url_);
views::Widget* parent = anchor_widget();
DCHECK(parent);
Profile* profile = profile_;
ApplyEdits();
GetWidget()->Close();
if (node && parent)
BookmarkEditor::Show(parent->GetNativeWindow(), profile,
BookmarkEditor::EditDetails::EditNode(node),
BookmarkEditor::SHOW_TREE);
}
void BookmarkBubbleView::ApplyEdits() {
// Set this to make sure we don't attempt to apply edits again.
apply_edits_ = false;
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
const BookmarkNode* node = model->GetMostRecentlyAddedNodeForURL(url_);
if (node) {
const base::string16 new_title = title_tf_->text();
if (new_title != node->GetTitle()) {
model->SetTitle(node, new_title);
content::RecordAction(
UserMetricsAction("BookmarkBubble_ChangeTitleInBubble"));
}
parent_model_.MaybeChangeParent(node, parent_combobox_->selected_index());
}
}
|