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.
5 #include "chrome/browser/ui/views/accessibility/invert_bubble_view.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/views/frame/browser_view.h"
11 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
12 #include "chrome/common/pref_names.h"
13 #include "content/public/browser/page_navigator.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/base/window_open_disposition.h"
18 #include "ui/views/bubble/bubble_delegate.h"
19 #include "ui/views/controls/label.h"
20 #include "ui/views/controls/link.h"
21 #include "ui/views/controls/link_listener.h"
22 #include "ui/views/layout/grid_layout.h"
23 #include "ui/views/layout/layout_constants.h"
24 #include "ui/views/widget/widget.h"
28 const char kHighContrastExtensionUrl[] =
29 "https://chrome.google.com/webstore/detail/djcfdncoelnlbldjfhinnjlhdjlikmph";
30 const char kDarkThemeSearchUrl[] =
31 "https://chrome.google.com/webstore/search-themes/dark";
32 const char kLearnMoreUrl[] =
33 "https://groups.google.com/a/googleproductforums.com/d/topic/chrome/Xrco2HsXS-8/discussion";
35 class InvertBubbleView : public views::BubbleDelegateView,
36 public views::LinkListener {
38 InvertBubbleView(Browser* browser, views::View* anchor_view);
39 virtual ~InvertBubbleView();
42 // Overridden from views::BubbleDelegateView:
43 virtual void Init() OVERRIDE;
45 // Overridden from views::LinkListener:
46 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE;
48 void OpenLink(const std::string& url, int event_flags);
51 views::Link* high_contrast_;
52 views::Link* dark_theme_;
53 views::Link* learn_more_;
56 DISALLOW_COPY_AND_ASSIGN(InvertBubbleView);
59 InvertBubbleView::InvertBubbleView(Browser* browser, views::View* anchor_view)
60 : views::BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT),
68 InvertBubbleView::~InvertBubbleView() {
71 void InvertBubbleView::Init() {
72 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
73 const gfx::FontList& original_font_list =
74 rb.GetFontList(ui::ResourceBundle::MediumFont);
76 views::Label* title = new views::Label(
77 base::string16(), original_font_list.Derive(2, gfx::Font::BOLD));
78 title->SetMultiLine(true);
80 learn_more_ = new views::Link(l10n_util::GetStringUTF16(IDS_LEARN_MORE));
81 learn_more_->SetFontList(original_font_list);
82 learn_more_->set_listener(this);
85 new views::Link(l10n_util::GetStringUTF16(IDS_HIGH_CONTRAST_EXT));
86 high_contrast_->SetFontList(original_font_list);
87 high_contrast_->set_listener(this);
89 dark_theme_ = new views::Link(l10n_util::GetStringUTF16(IDS_DARK_THEME));
90 dark_theme_->SetFontList(original_font_list);
91 dark_theme_->set_listener(this);
93 close_ = new views::Link(l10n_util::GetStringUTF16(IDS_CLOSE));
94 close_->SetFontList(original_font_list);
95 close_->set_listener(this);
97 views::GridLayout* layout = views::GridLayout::CreatePanel(this);
98 SetLayoutManager(layout);
100 views::ColumnSet* columns = layout->AddColumnSet(0);
101 for (int i = 0; i < 4; i++) {
102 columns->AddColumn(views::GridLayout::LEADING,
103 views::GridLayout::LEADING, 0,
104 views::GridLayout::USE_PREF, 0, 0);
107 layout->StartRow(0, 0);
108 layout->AddView(title, 4, 1);
109 layout->StartRowWithPadding(0, 0, 0,
110 views::kRelatedControlSmallVerticalSpacing);
111 layout->AddView(high_contrast_);
112 layout->AddView(dark_theme_);
113 layout->AddView(learn_more_);
114 layout->AddView(close_);
116 // Fit the message to the width of the links in the bubble.
117 const gfx::Size size(GetPreferredSize());
118 title->SetText(l10n_util::GetStringUTF16(IDS_HIGH_CONTRAST_NOTIFICATION));
119 title->SizeToFit(size.width());
121 // Switching to high-contrast mode has a nasty habit of causing Chrome
122 // top-level windows to lose focus, so closing the bubble on deactivate
123 // makes it disappear before the user has even seen it. This forces the
124 // user to close it explicitly, which should be okay because it affects
125 // a small minority of users, and only once.
126 set_close_on_deactivate(false);
127 set_move_with_anchor(true);
130 void InvertBubbleView::LinkClicked(views::Link* source, int event_flags) {
131 if (source == high_contrast_)
132 OpenLink(kHighContrastExtensionUrl, event_flags);
133 else if (source == dark_theme_)
134 OpenLink(kDarkThemeSearchUrl, event_flags);
135 else if (source == learn_more_)
136 OpenLink(kLearnMoreUrl, event_flags);
137 else if (source == close_)
138 GetWidget()->Close();
143 void InvertBubbleView::OpenLink(const std::string& url, int event_flags) {
144 WindowOpenDisposition disposition =
145 ui::DispositionFromEventFlags(event_flags);
146 content::OpenURLParams params(
147 GURL(url), content::Referrer(),
148 disposition == CURRENT_TAB ? NEW_FOREGROUND_TAB : disposition,
149 content::PAGE_TRANSITION_LINK, false);
150 browser_->OpenURL(params);
157 void MaybeShowInvertBubbleView(BrowserView* browser_view) {
158 Browser* browser = browser_view->browser();
159 PrefService* pref_service = browser->profile()->GetPrefs();
160 views::View* anchor = browser_view->toolbar()->app_menu();
161 if (gfx::IsInvertedColorScheme() && anchor && anchor->GetWidget() &&
162 !pref_service->GetBoolean(prefs::kInvertNotificationShown)) {
163 pref_service->SetBoolean(prefs::kInvertNotificationShown, true);
164 InvertBubbleView* delegate = new InvertBubbleView(browser, anchor);
165 views::BubbleDelegateView::CreateBubble(delegate)->Show();
169 } // namespace chrome