Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 namespace { | |
|
Peter Kasting
2013/01/18 18:19:27
Nit: When there are multiple classes in a file, I
Tom Cassiotis
2013/01/18 21:24:19
Done.
| |
| 23 class HomePageUndoBubble : public views::BubbleDelegateView, | |
|
Peter Kasting
2013/01/18 18:19:27
Nit: Go ahead and add a blank line here.
Tom Cassiotis
2013/01/18 21:24:19
Done.
| |
| 24 public views::LinkListener { | |
| 25 public: | |
| 26 static void ShowBubble(Browser* browser, | |
| 27 bool undo_value_is_ntp, | |
| 28 const GURL& undo_url, | |
| 29 views::View* anchor_view); | |
| 30 static void HideBubble(); | |
| 31 | |
| 32 private: | |
| 33 HomePageUndoBubble(Browser* browser, | |
| 34 bool undo_value_is_ntp, | |
|
Peter Kasting
2013/01/18 18:19:27
Nit: Args should be aligned
Tom Cassiotis
2013/01/18 21:24:19
I reread the style guides and have a better unders
Peter Kasting
2013/01/18 21:45:34
The only rule is, if you use multiple lines, the b
| |
| 35 const GURL &undo_url, | |
|
Peter Kasting
2013/01/18 18:19:27
Nit: '&' and '*' go on type name, not variable nam
Tom Cassiotis
2013/01/18 21:24:19
Done.
| |
| 36 views::View* anchor_view); | |
| 37 virtual ~HomePageUndoBubble(); | |
| 38 | |
| 39 // views::BubbleDelegateView methods. | |
|
Peter Kasting
2013/01/18 18:19:27
Nit: " methods." -> ":" for consistency with below
Tom Cassiotis
2013/01/18 21:24:19
Done.
| |
| 40 virtual void Init() OVERRIDE; | |
| 41 virtual void WindowClosing() OVERRIDE; | |
| 42 | |
| 43 // views::LinkListener: | |
| 44 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE; | |
| 45 | |
| 46 static HomePageUndoBubble* home_page_undo_bubble_; | |
| 47 | |
| 48 Browser* browser_; | |
| 49 bool undo_value_is_ntp_; | |
| 50 GURL undo_url_; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(HomePageUndoBubble); | |
| 53 }; | |
| 54 | |
| 55 // static | |
| 56 HomePageUndoBubble* HomePageUndoBubble::home_page_undo_bubble_ = NULL; | |
| 57 | |
| 58 void HomePageUndoBubble::ShowBubble(Browser* browser, | |
| 59 bool undo_value_is_ntp, | |
| 60 const GURL &undo_url, | |
| 61 views::View* anchor_view) { | |
| 62 HideBubble(); | |
| 63 home_page_undo_bubble_ = new HomePageUndoBubble(browser, undo_value_is_ntp, | |
| 64 undo_url, anchor_view); | |
| 65 views::BubbleDelegateView::CreateBubble(home_page_undo_bubble_); | |
| 66 home_page_undo_bubble_->StartFade(true); | |
| 67 } | |
| 68 | |
| 69 void HomePageUndoBubble::HideBubble() { | |
| 70 if (home_page_undo_bubble_ != NULL) | |
| 71 home_page_undo_bubble_->GetWidget()->Close(); | |
| 72 } | |
| 73 | |
| 74 HomePageUndoBubble::HomePageUndoBubble( | |
| 75 Browser* browser, | |
| 76 bool undo_value_is_ntp, | |
| 77 const GURL &undo_url, | |
| 78 views::View* anchor_view) | |
| 79 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT), | |
| 80 browser_(browser), | |
| 81 undo_value_is_ntp_(undo_value_is_ntp), | |
| 82 undo_url_(undo_url) { | |
| 83 } | |
| 84 | |
| 85 HomePageUndoBubble::~HomePageUndoBubble() { | |
| 86 } | |
| 87 | |
| 88 void HomePageUndoBubble::Init() { | |
| 89 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 90 views::GridLayout* layout = new views::GridLayout(this); | |
| 91 SetLayoutManager(layout); | |
| 92 | |
| 93 // Create two columns for the message and the undo link. | |
| 94 views::ColumnSet* cs = layout->AddColumnSet(0); | |
| 95 cs = layout->AddColumnSet(1); | |
| 96 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::BASELINE, 0, | |
| 97 views::GridLayout::USE_PREF, 0, 0); | |
| 98 cs->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | |
| 99 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::BASELINE, 0, | |
| 100 views::GridLayout::USE_PREF, 0, 0); | |
| 101 | |
| 102 views::Label* message_label = new views::Label( | |
| 103 l10n_util::GetStringUTF16(IDS_TOOLBAR_INFORM_SET_HOME_PAGE)); | |
| 104 message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 105 layout->StartRow(0, 1); | |
| 106 layout->AddView(message_label); | |
| 107 | |
| 108 views::Link* undo_link = new views::Link( | |
| 109 l10n_util::GetStringUTF16(IDS_ONE_CLICK_BUBBLE_UNDO)); | |
| 110 undo_link->set_listener(this); | |
| 111 layout->AddView(undo_link); | |
| 112 } | |
| 113 | |
| 114 void HomePageUndoBubble::LinkClicked(views::Link* source, int event_flags) { | |
| 115 browser_->profile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage, | |
|
Peter Kasting
2013/01/18 18:19:27
Nit: I'd probably pull this out into a temp as you
Tom Cassiotis
2013/01/18 21:24:19
Done.
| |
| 116 undo_value_is_ntp_); | |
| 117 browser_->profile()->GetPrefs()->SetString(prefs::kHomePage, | |
| 118 undo_url_.spec()); | |
| 119 HideBubble(); | |
| 120 } | |
| 121 | |
| 122 void HomePageUndoBubble::WindowClosing() { | |
| 123 // We have to reset |home_page_undo_bubble_| here, not in our destructor, | |
| 124 // because we'll be destroyed asynchronously. | |
|
Peter Kasting
2013/01/18 18:19:27
Nit: OK, but why? (I think I know why -- so that
Tom Cassiotis
2013/01/18 21:24:19
After closer inspection the only thing that would
| |
| 125 DCHECK_EQ(this, home_page_undo_bubble_); | |
| 126 home_page_undo_bubble_ = NULL; | |
| 127 } | |
| 128 | |
| 129 } // namespace | |
| 130 | |
| 131 HomeImageButton::HomeImageButton( | |
| 132 views::ButtonListener* listener, | |
| 133 Browser* browser) | |
| 134 : views::ImageButton(listener), | |
| 135 browser_(browser) { | |
| 136 } | |
| 137 | |
| 138 HomeImageButton::~HomeImageButton() { | |
| 139 } | |
| 140 | |
| 141 bool HomeImageButton::GetDropFormats( | |
| 142 int* formats, | |
|
Peter Kasting
2013/01/18 18:19:27
Nit: Indent 4, not 8
Tom Cassiotis
2013/01/18 21:24:19
Done.
| |
| 143 std::set<OSExchangeData::CustomFormat>* custom_formats) { | |
| 144 *formats = ui::OSExchangeData::URL; | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 bool HomeImageButton::CanDrop(const OSExchangeData& data) { | |
| 149 return data.HasURL(); | |
| 150 } | |
| 151 | |
| 152 int HomeImageButton::OnDragUpdated(const ui::DropTargetEvent& event) { | |
| 153 return (event.source_operations() & ui::DragDropTypes::DRAG_LINK) ? | |
| 154 ui::DragDropTypes::DRAG_LINK : | |
| 155 ui::DragDropTypes::DRAG_NONE; | |
|
Peter Kasting
2013/01/18 18:19:27
Nit: Welcome to put this on previous line if it fi
Tom Cassiotis
2013/01/18 21:24:19
Done.
| |
| 156 } | |
| 157 | |
| 158 int HomeImageButton::OnPerformDrop(const ui::DropTargetEvent& event) { | |
| 159 GURL new_homepage_url; | |
| 160 string16 title; | |
| 161 if (event.data().GetURLAndTitle(&new_homepage_url, &title) && | |
| 162 new_homepage_url.is_valid()) { | |
| 163 PrefService* prefs = browser_->profile()->GetPrefs(); | |
| 164 bool old_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage); | |
| 165 GURL old_homepage(prefs->GetString(prefs::kHomePage)); | |
| 166 | |
| 167 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false); | |
| 168 prefs->SetString(prefs::kHomePage, new_homepage_url.spec()); | |
| 169 | |
| 170 HomePageUndoBubble::ShowBubble(browser_, old_is_ntp, old_homepage, this); | |
| 171 } | |
| 172 return ui::DragDropTypes::DRAG_NONE; | |
| 173 } | |
| 174 | |
| OLD | NEW |