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
|
// Copyright 2013 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/toolbar/home_button.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/user_prefs.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/bubble/bubble_delegate.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/controls/link_listener.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/widget/widget.h"
// HomePageUndoBubble --------------------------------------------------------
namespace {
class HomePageUndoBubble : public views::BubbleDelegateView,
public views::LinkListener {
public:
static void ShowBubble(Browser* browser,
bool undo_value_is_ntp,
const GURL& undo_url,
views::View* anchor_view);
static void HideBubble();
private:
HomePageUndoBubble(Browser* browser, bool undo_value_is_ntp,
const GURL& undo_url, views::View* anchor_view);
virtual ~HomePageUndoBubble();
// views::BubbleDelegateView:
virtual void Init() OVERRIDE;
virtual void WindowClosing() OVERRIDE;
// views::LinkListener:
virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE;
static HomePageUndoBubble* home_page_undo_bubble_;
Browser* browser_;
bool undo_value_is_ntp_;
GURL undo_url_;
DISALLOW_COPY_AND_ASSIGN(HomePageUndoBubble);
};
// static
HomePageUndoBubble* HomePageUndoBubble::home_page_undo_bubble_ = NULL;
void HomePageUndoBubble::ShowBubble(Browser* browser,
bool undo_value_is_ntp,
const GURL& undo_url,
views::View* anchor_view) {
HideBubble();
home_page_undo_bubble_ = new HomePageUndoBubble(browser,
undo_value_is_ntp,
undo_url,
anchor_view);
views::BubbleDelegateView::CreateBubble(home_page_undo_bubble_);
home_page_undo_bubble_->StartFade(true);
}
void HomePageUndoBubble::HideBubble() {
if (home_page_undo_bubble_)
home_page_undo_bubble_->GetWidget()->Close();
}
HomePageUndoBubble::HomePageUndoBubble(
Browser* browser,
bool undo_value_is_ntp,
const GURL& undo_url,
views::View* anchor_view)
: BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT),
browser_(browser),
undo_value_is_ntp_(undo_value_is_ntp),
undo_url_(undo_url) {
}
HomePageUndoBubble::~HomePageUndoBubble() {
}
void HomePageUndoBubble::Init() {
views::GridLayout* layout = new views::GridLayout(this);
SetLayoutManager(layout);
// Create two columns for the message and the undo link.
views::ColumnSet* cs = layout->AddColumnSet(0);
cs = layout->AddColumnSet(1);
cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::BASELINE, 0,
views::GridLayout::USE_PREF, 0, 0);
cs->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::BASELINE, 0,
views::GridLayout::USE_PREF, 0, 0);
views::Label* message_label = new views::Label(
l10n_util::GetStringUTF16(IDS_TOOLBAR_INFORM_SET_HOME_PAGE));
message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
layout->StartRow(0, 1);
layout->AddView(message_label);
views::Link* undo_link = new views::Link(
l10n_util::GetStringUTF16(IDS_ONE_CLICK_BUBBLE_UNDO));
undo_link->set_listener(this);
layout->AddView(undo_link);
}
void HomePageUndoBubble::LinkClicked(views::Link* source, int event_flags) {
PrefService* prefs = user_prefs::UserPrefs::Get(browser_->profile());
prefs->SetBoolean(prefs::kHomePageIsNewTabPage, undo_value_is_ntp_);
prefs->SetString(prefs::kHomePage, undo_url_.spec());
HideBubble();
}
void HomePageUndoBubble::WindowClosing() {
// We have to reset |home_page_undo_bubble_| here, not in our destructor,
// because we'll be hidden first, then destroyed asynchronously. If we wait
// to reset this, and the user triggers a call to ShowBubble() while the
// window is hidden but not destroyed, GetWidget()->Close() would be
// called twice.
DCHECK_EQ(this, home_page_undo_bubble_);
home_page_undo_bubble_ = NULL;
}
} // namespace
// HomeButton -----------------------------------------------------------
HomeButton::HomeButton(
views::ButtonListener* listener,
Browser* browser)
: ToolbarButton(listener, NULL),
browser_(browser) {
}
HomeButton::~HomeButton() {
}
bool HomeButton::GetDropFormats(
int* formats,
std::set<OSExchangeData::CustomFormat>* custom_formats) {
*formats = ui::OSExchangeData::URL;
return true;
}
bool HomeButton::CanDrop(const OSExchangeData& data) {
return data.HasURL();
}
int HomeButton::OnDragUpdated(const ui::DropTargetEvent& event) {
return (event.source_operations() & ui::DragDropTypes::DRAG_LINK) ?
ui::DragDropTypes::DRAG_LINK : ui::DragDropTypes::DRAG_NONE;
}
int HomeButton::OnPerformDrop(const ui::DropTargetEvent& event) {
GURL new_homepage_url;
string16 title;
if (event.data().GetURLAndTitle(&new_homepage_url, &title) &&
new_homepage_url.is_valid()) {
PrefService* prefs = browser_->profile()->GetPrefs();
bool old_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage);
GURL old_homepage(prefs->GetString(prefs::kHomePage));
prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false);
prefs->SetString(prefs::kHomePage, new_homepage_url.spec());
HomePageUndoBubble::ShowBubble(browser_, old_is_ntp, old_homepage, this);
}
return ui::DragDropTypes::DRAG_NONE;
}
|