Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: chrome/browser/ui/views/home_button.cc

Issue 11742003: Implemented drop hander for the Home toolbar button that accepts (only) links (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/ui/views/home_button.h"
6
7 #include "chrome/browser/prefs/pref_service.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/common/pref_names.h"
11 #include "grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/views/bubble/bubble_delegate.h"
15 #include "ui/views/controls/label.h"
16 #include "ui/views/controls/link.h"
17 #include "ui/views/controls/link_listener.h"
18 #include "ui/views/layout/grid_layout.h"
19 #include "ui/views/layout/layout_constants.h"
20 #include "ui/views/widget/widget.h"
21
22 // HomePageUndoBubble --------------------------------------------------------
23
24 namespace {
25
26 class HomePageUndoBubble : public views::BubbleDelegateView,
27 public views::LinkListener {
28 public:
29 static void ShowBubble(Browser* browser,
30 bool undo_value_is_ntp,
31 const GURL& undo_url,
32 views::View* anchor_view);
33 static void HideBubble();
34
35 private:
36 HomePageUndoBubble(Browser* browser, bool undo_value_is_ntp,
37 const GURL& undo_url, views::View* anchor_view);
38 virtual ~HomePageUndoBubble();
39
40 // views::BubbleDelegateView:
41 virtual void Init() OVERRIDE;
42 virtual void WindowClosing() OVERRIDE;
43
44 // views::LinkListener:
45 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE;
46
47 static HomePageUndoBubble* home_page_undo_bubble_;
48
49 Browser* browser_;
50 bool undo_value_is_ntp_;
51 GURL undo_url_;
52
53 DISALLOW_COPY_AND_ASSIGN(HomePageUndoBubble);
54 };
55
56 // static
57 HomePageUndoBubble* HomePageUndoBubble::home_page_undo_bubble_ = NULL;
58
59 void HomePageUndoBubble::ShowBubble(Browser* browser,
60 bool undo_value_is_ntp,
61 const GURL& undo_url,
62 views::View* anchor_view) {
63 HideBubble();
64 home_page_undo_bubble_ = new HomePageUndoBubble(browser,
65 undo_value_is_ntp,
66 undo_url,
67 anchor_view);
68 views::BubbleDelegateView::CreateBubble(home_page_undo_bubble_);
69 home_page_undo_bubble_->StartFade(true);
70 }
71
72 void HomePageUndoBubble::HideBubble() {
73 if (home_page_undo_bubble_ != NULL)
tfarina 2013/01/28 02:37:11 if (home_page_undo_bubble_)
Tom Cassiotis 2013/01/28 15:34:33 Done.
74 home_page_undo_bubble_->GetWidget()->Close();
75 }
76
77 HomePageUndoBubble::HomePageUndoBubble(
78 Browser* browser,
79 bool undo_value_is_ntp,
80 const GURL& undo_url,
81 views::View* anchor_view)
82 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT),
83 browser_(browser),
84 undo_value_is_ntp_(undo_value_is_ntp),
85 undo_url_(undo_url) {
86 }
87
88 HomePageUndoBubble::~HomePageUndoBubble() {
89 }
90
91 void HomePageUndoBubble::Init() {
92 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
93 views::GridLayout* layout = new views::GridLayout(this);
94 SetLayoutManager(layout);
95
96 // Create two columns for the message and the undo link.
97 views::ColumnSet* cs = layout->AddColumnSet(0);
98 cs = layout->AddColumnSet(1);
99 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::BASELINE, 0,
100 views::GridLayout::USE_PREF, 0, 0);
101 cs->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
102 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::BASELINE, 0,
103 views::GridLayout::USE_PREF, 0, 0);
104
105 views::Label* message_label = new views::Label(
106 l10n_util::GetStringUTF16(IDS_TOOLBAR_INFORM_SET_HOME_PAGE));
107 message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
108 layout->StartRow(0, 1);
109 layout->AddView(message_label);
110
111 views::Link* undo_link = new views::Link(
112 l10n_util::GetStringUTF16(IDS_ONE_CLICK_BUBBLE_UNDO));
113 undo_link->set_listener(this);
114 layout->AddView(undo_link);
115 }
116
117 void HomePageUndoBubble::LinkClicked(views::Link* source, int event_flags) {
118 PrefService* prefs = browser_->profile()->GetPrefs();
tfarina 2013/01/28 02:37:11 Better: PrefServiceBase* prefs = PrefServiceBase:
Tom Cassiotis 2013/01/28 15:34:33 Done.
119
Peter Kasting 2013/01/18 21:45:35 Nit: Newline probably not needed
Tom Cassiotis 2013/01/28 15:34:33 Done.
120 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, undo_value_is_ntp_);
121 prefs->SetString(prefs::kHomePage, undo_url_.spec());
122
123 HideBubble();
124 }
125
126 void HomePageUndoBubble::WindowClosing() {
127 // We have to reset |home_page_undo_bubble_| here, not in our destructor,
Peter Kasting 2013/01/18 21:45:35 Nit: How about this for clarity: We have to reset
Tom Cassiotis 2013/01/28 15:34:33 Done.
128 // because we'll be destroyed asynchronously. If we wait to reset
129 // in our destructor the user could initiated the display of this bubble
130 // between WindowClosing() and the destructor call which would result in
131 // the bubble's GetWidget()->Close() to be called a second time.
132 // We prevent this situation by resetting |home_page_undo_bubble_| here.
133 DCHECK_EQ(this, home_page_undo_bubble_);
134 home_page_undo_bubble_ = NULL;
135 }
136
137 } // namespace
138
139
140 // HomeImageButton -----------------------------------------------------------
141
142 HomeImageButton::HomeImageButton(
143 views::ButtonListener* listener,
tfarina 2013/01/28 02:37:11 I bet this will fit above!
144 Browser* browser)
145 : views::ImageButton(listener),
146 browser_(browser) {
147 }
148
149 HomeImageButton::~HomeImageButton() {
150 }
151
152 bool HomeImageButton::GetDropFormats(
153 int* formats,
154 std::set<OSExchangeData::CustomFormat>* custom_formats) {
155 *formats = ui::OSExchangeData::URL;
156 return true;
157 }
158
159 bool HomeImageButton::CanDrop(const OSExchangeData& data) {
160 return data.HasURL();
161 }
162
163 int HomeImageButton::OnDragUpdated(const ui::DropTargetEvent& event) {
164 return (event.source_operations() & ui::DragDropTypes::DRAG_LINK) ?
165 ui::DragDropTypes::DRAG_LINK : ui::DragDropTypes::DRAG_NONE;
166 }
167
168 int HomeImageButton::OnPerformDrop(const ui::DropTargetEvent& event) {
169 GURL new_homepage_url;
170 string16 title;
171 if (event.data().GetURLAndTitle(&new_homepage_url, &title) &&
172 new_homepage_url.is_valid()) {
173 PrefService* prefs = browser_->profile()->GetPrefs();
174 bool old_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage);
175 GURL old_homepage(prefs->GetString(prefs::kHomePage));
176
177 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false);
178 prefs->SetString(prefs::kHomePage, new_homepage_url.spec());
179
180 HomePageUndoBubble::ShowBubble(browser_, old_is_ntp, old_homepage, this);
181 }
182 return ui::DragDropTypes::DRAG_NONE;
183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698