File: echo_dialog_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (151 lines) | stat: -rw-r--r-- 5,699 bytes parent folder | download | duplicates (6)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/notifications/echo_dialog_view.h"

#include <stddef.h>

#include "base/no_destructor.h"
#include "chrome/browser/ash/notifications/echo_dialog_listener.h"
#include "chrome/grit/generated_resources.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/gfx/font.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/widget/widget.h"

namespace ash {

namespace {

EchoDialogView::ShowCallback& GetShowCallbackForTesting() {
  static base::NoDestructor<EchoDialogView::ShowCallback> show_callback;
  return *show_callback;
}

}  // namespace

EchoDialogView::EchoDialogView(EchoDialogListener* listener,
                               const EchoDialogView::Params& params) {
  auto* learn_more_button = DialogDelegate::SetExtraView(
      views::CreateVectorImageButtonWithNativeTheme(
          base::BindRepeating(&EchoDialogListener::OnMoreInfoLinkClicked,
                              base::Unretained(listener)),
          vector_icons::kHelpOutlineIcon));
  learn_more_button->GetViewAccessibility().SetName(
      l10n_util::GetStringUTF16(IDS_CHROMEOS_ACC_LEARN_MORE));

  if (params.echo_enabled) {
    DialogDelegate::SetButtons(
        static_cast<int>(ui::mojom::DialogButton::kOk) |
        static_cast<int>(ui::mojom::DialogButton::kCancel));
    DialogDelegate::SetButtonLabel(
        ui::mojom::DialogButton::kOk,
        l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_ENABLE_BUTTON));
    DialogDelegate::SetButtonLabel(
        ui::mojom::DialogButton::kCancel,
        l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_DISABLE_BUTTON));
    InitForEnabledEcho(params.service_name, params.origin);
  } else {
    DialogDelegate::SetButtons(
        static_cast<int>(ui::mojom::DialogButton::kCancel));
    DialogDelegate::SetButtonLabel(
        ui::mojom::DialogButton::kCancel,
        l10n_util::GetStringUTF16(IDS_ECHO_CONSENT_DISMISS_BUTTON));
    InitForDisabledEcho();
  }

  DialogDelegate::SetAcceptCallback(base::BindOnce(
      &EchoDialogListener::OnAccept, base::Unretained(listener)));
  DialogDelegate::SetCancelCallback(base::BindOnce(
      &EchoDialogListener::OnCancel, base::Unretained(listener)));

  DialogDelegate::SetShowTitle(false);
  DialogDelegate::SetShowCloseButton(false);

  DialogDelegate::SetModalType(ui::mojom::ModalType::kWindow);
  DialogDelegate::set_fixed_width(
      views::LayoutProvider::Get()->GetDistanceMetric(
          views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH));
}

EchoDialogView::~EchoDialogView() = default;

void EchoDialogView::Show(gfx::NativeWindow parent) {
  views::DialogDelegate::CreateDialogWidget(this, parent, parent);
  GetWidget()->SetSize(GetWidget()->GetRootView()->GetPreferredSize());
  GetWidget()->Show();

  if (GetShowCallbackForTesting()) {
    std::move(GetShowCallbackForTesting()).Run(this);
  }
}

void EchoDialogView::AddShowCallbackForTesting(ShowCallback callback) {
  GetShowCallbackForTesting() = std::move(callback);
}

void EchoDialogView::InitForEnabledEcho(const std::u16string& service_name,
                                        const std::u16string& origin) {
  size_t offset;
  std::u16string text = l10n_util::GetStringFUTF16(IDS_ECHO_CONSENT_DIALOG_TEXT,
                                                   service_name, &offset);

  auto label = std::make_unique<views::StyledLabel>();
  label->SetText(text);

  views::StyledLabel::RangeStyleInfo service_name_style;
  gfx::FontList font_list = label->GetFontList();
  service_name_style.custom_font =
      font_list.DeriveWithStyle(gfx::Font::UNDERLINE);
  service_name_style.tooltip = origin;
  label->AddStyleRange(gfx::Range(offset, offset + service_name.length()),
                       service_name_style);

  SetBorderAndLabel(std::move(label), font_list);
}

void EchoDialogView::InitForDisabledEcho() {
  auto label = std::make_unique<views::Label>(
      l10n_util::GetStringUTF16(IDS_ECHO_DISABLED_CONSENT_DIALOG_TEXT));
  label->SetMultiLine(true);
  label->SetHorizontalAlignment(gfx::ALIGN_LEFT);

  // grab the font list before std::move(label) or it'll be nullptr
  gfx::FontList font_list = label->font_list();
  SetBorderAndLabel(std::move(label), font_list);
}

void EchoDialogView::SetBorderAndLabel(std::unique_ptr<views::View> label,
                                       const gfx::FontList& label_font_list) {
  SetLayoutManager(std::make_unique<views::FillLayout>());

  // Without a title, top padding isn't correctly calculated.  This adds the
  // text's internal leading to the top padding.  See
  // FontList::DeriveWithHeightUpperBound() for font padding details.
  int top_inset_padding =
      label_font_list.GetBaseline() - label_font_list.GetCapHeight();

  gfx::Insets insets =
      views::LayoutProvider::Get()->GetDialogInsetsForContentType(
          views::DialogContentType::kText, views::DialogContentType::kText);
  insets += gfx::Insets::TLBR(top_inset_padding, 0, 0, 0);
  SetBorder(views::CreateEmptyBorder(insets));

  AddChildView(std::move(label));
}

BEGIN_METADATA(EchoDialogView)
END_METADATA

}  // namespace ash