File: invert_bubble_view.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (201 lines) | stat: -rw-r--r-- 7,434 bytes parent folder | download | duplicates (2)
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
// 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/accessibility/invert_bubble_view.h"

#include "base/macros.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/frame/app_menu_button.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/page_navigator.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/window_open_disposition.h"
#include "ui/gfx/color_utils.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/controls/link_listener.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/widget/widget.h"

namespace {

constexpr char kHighContrastExtensionUrl[] =
    "https://chrome.google.com/webstore/detail/"
    "djcfdncoelnlbldjfhinnjlhdjlikmph";
constexpr char kDarkThemeSearchUrl[] =
    "https://chrome.google.com/webstore/category/collection/dark_themes";
constexpr char kLearnMoreUrl[] =
    "https://groups.google.com/a/googleproductforums.com/d/topic/chrome/"
    "Xrco2HsXS-8/discussion";

// Tag value used to uniquely identify the "learn more" (?) button.
constexpr int kLearnMoreButton = 100;

class InvertBubbleView : public views::BubbleDialogDelegateView,
                         public views::LinkListener,
                         public views::ButtonListener {
 public:
  InvertBubbleView(Browser* browser, views::View* anchor_view);
  ~InvertBubbleView() override;

 private:
  // Overridden from views::BubbleDialogDelegateView:
  views::View* CreateExtraView() override;
  int GetDialogButtons() const override;
  base::string16 GetDialogButtonLabel(ui::DialogButton button) const override;
  void Init() override;

  // Overridden from views::WidgetDelegate:
  base::string16 GetWindowTitle() const override;
  bool ShouldShowCloseButton() const override;

  // Overridden from views::LinkListener:
  void LinkClicked(views::Link* source, int event_flags) override;

  // Overridden from views::ButtonListener:
  void ButtonPressed(views::Button* sender, const ui::Event& event) override;

  void OpenLink(const std::string& url, int event_flags);

  Browser* browser_;
  views::Link* high_contrast_;
  views::Link* dark_theme_;

  DISALLOW_COPY_AND_ASSIGN(InvertBubbleView);
};

InvertBubbleView::InvertBubbleView(Browser* browser, views::View* anchor_view)
    : views::BubbleDialogDelegateView(anchor_view,
                                      views::BubbleBorder::TOP_RIGHT),
      browser_(browser),
      high_contrast_(nullptr),
      dark_theme_(nullptr) {
  set_margins(gfx::Insets());
  chrome::RecordDialogCreation(chrome::DialogIdentifier::INVERT);
}

InvertBubbleView::~InvertBubbleView() {
}

views::View* InvertBubbleView::CreateExtraView() {
  views::ImageButton* learn_more = views::CreateVectorImageButton(this);
  views::SetImageFromVectorIcon(learn_more, vector_icons::kHelpOutlineIcon);
  learn_more->SetTooltipText(l10n_util::GetStringUTF16(IDS_LEARN_MORE));
  learn_more->set_tag(kLearnMoreButton);
  return learn_more;
}

int InvertBubbleView::GetDialogButtons() const {
  return ui::DIALOG_BUTTON_OK;
}

base::string16 InvertBubbleView::GetDialogButtonLabel(
    ui::DialogButton button) const {
  DCHECK_EQ(button, ui::DialogButton::DIALOG_BUTTON_OK);
  return l10n_util::GetStringUTF16(IDS_DONE);
}

void InvertBubbleView::Init() {
  const ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
  SetBorder(views::CreateEmptyBorder(
      provider->GetInsetsMetric(views::INSETS_DIALOG)));

  views::Label* header =
      new views::Label(l10n_util::GetStringUTF16(IDS_HIGH_CONTRAST_HEADER),
                       CONTEXT_BODY_TEXT_LARGE);

  high_contrast_ =
      new views::Link(l10n_util::GetStringUTF16(IDS_HIGH_CONTRAST_EXT),
                      CONTEXT_BODY_TEXT_LARGE);
  high_contrast_->set_listener(this);

  dark_theme_ = new views::Link(l10n_util::GetStringUTF16(IDS_DARK_THEME),
                                CONTEXT_BODY_TEXT_LARGE);
  dark_theme_->set_listener(this);

  views::BoxLayout* layout = SetLayoutManager(
      std::make_unique<views::BoxLayout>(views::BoxLayout::kVertical));
  layout->set_cross_axis_alignment(
      views::BoxLayout::CROSS_AXIS_ALIGNMENT_START);

  AddChildView(header);
  AddChildView(high_contrast_);
  AddChildView(dark_theme_);

  // Switching to high-contrast mode has a nasty habit of causing Chrome
  // top-level windows to lose focus, so closing the bubble on deactivate
  // makes it disappear before the user has even seen it. This forces the
  // user to close it explicitly, which should be okay because it affects
  // a small minority of users, and only once.
  set_close_on_deactivate(false);
}

base::string16 InvertBubbleView::GetWindowTitle() const {
  return l10n_util::GetStringUTF16(IDS_HIGH_CONTRAST_TITLE);
}

bool InvertBubbleView::ShouldShowCloseButton() const {
  return true;
}

void InvertBubbleView::LinkClicked(views::Link* source, int event_flags) {
  if (source == high_contrast_)
    OpenLink(kHighContrastExtensionUrl, event_flags);
  else if (source == dark_theme_)
    OpenLink(kDarkThemeSearchUrl, event_flags);
  else
    NOTREACHED();
}

void InvertBubbleView::ButtonPressed(views::Button* sender,
                                     const ui::Event& event) {
  if (sender->tag() == kLearnMoreButton)
    OpenLink(kLearnMoreUrl, event.flags());
}

void InvertBubbleView::OpenLink(const std::string& url, int event_flags) {
  WindowOpenDisposition disposition =
      ui::DispositionFromEventFlags(event_flags);
  content::OpenURLParams params(
      GURL(url), content::Referrer(),
      disposition == WindowOpenDisposition::CURRENT_TAB
          ? WindowOpenDisposition::NEW_FOREGROUND_TAB
          : disposition,
      ui::PAGE_TRANSITION_LINK, false);
  browser_->OpenURL(params);
}

}  // namespace

void MaybeShowInvertBubbleView(BrowserView* browser_view) {
  Browser* browser = browser_view->browser();
  PrefService* pref_service = browser->profile()->GetPrefs();
  views::View* anchor =
      browser_view->toolbar_button_provider()->GetAppMenuButton();
  if (color_utils::IsInvertedColorScheme() && anchor && anchor->GetWidget() &&
      !pref_service->GetBoolean(prefs::kInvertNotificationShown)) {
    pref_service->SetBoolean(prefs::kInvertNotificationShown, true);
    ShowInvertBubbleView(browser, anchor);
  }
}

void ShowInvertBubbleView(Browser* browser, views::View* anchor) {
  InvertBubbleView* delegate = new InvertBubbleView(browser, anchor);
  views::BubbleDialogDelegateView::CreateBubble(delegate)->Show();
}