1 // Copyright 2017 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/chromeos/lock_screen_apps/toast_dialog_view.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "ash/public/cpp/shell_window_ids.h"
11 #include "chrome/browser/ui/ash/ash_util.h"
12 #include "chrome/browser/ui/browser_dialogs.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "ui/aura/window.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/gfx/geometry/size.h"
18 #include "ui/views/bubble/bubble_border.h"
19 #include "ui/views/bubble/bubble_frame_view.h"
20 #include "ui/views/controls/label.h"
21 #include "ui/views/layout/fill_layout.h"
22 
23 namespace lock_screen_apps {
24 
25 namespace {
26 
27 constexpr int kDialogWidthDp = 292;
28 
29 constexpr int kDialogMessageMarginTopDp = 0;
30 constexpr int kDialogMessageMarginStartDp = 16;
31 constexpr int kDialogMessageMarginBottomDp = 18;
32 constexpr int kDialogMessageMarginEndDp = 12;
33 constexpr int kDialogMessageLineHeightDp = 20;
34 
35 constexpr int kDialogTitleMarginTopDp = 14;
36 constexpr int kDialogTitleMarginStartDp = 16;
37 constexpr int kDialogTitleMarginBottomDp = 5;
38 constexpr int kDialogTitleMarginEndDp = 0;
39 
40 }  // namespace
41 
ToastDialogView(const base::string16 & app_name,base::OnceClosure dismissed_callback)42 ToastDialogView::ToastDialogView(const base::string16& app_name,
43                                  base::OnceClosure dismissed_callback)
44     : app_name_(app_name) {
45   DialogDelegate::SetButtons(ui::DIALOG_BUTTON_NONE);
46   DialogDelegate::SetCloseCallback(std::move(dismissed_callback));
47 
48   chrome::RecordDialogCreation(
49       chrome::DialogIdentifier::LOCK_SCREEN_NOTE_APP_TOAST);
50 
51   SetArrow(views::BubbleBorder::NONE);
52   set_margins(
53       gfx::Insets(kDialogMessageMarginTopDp, kDialogMessageMarginStartDp,
54                   kDialogMessageMarginBottomDp, kDialogMessageMarginEndDp));
55   set_title_margins(
56       gfx::Insets(kDialogTitleMarginTopDp, kDialogTitleMarginStartDp,
57                   kDialogTitleMarginBottomDp, kDialogTitleMarginEndDp));
58   set_shadow(views::BubbleBorder::SMALL_SHADOW);
59 
60   SetLayoutManager(std::make_unique<views::FillLayout>());
61   auto* label = new views::Label(l10n_util::GetStringFUTF16(
62       IDS_LOCK_SCREEN_NOTE_APP_TOAST_DIALOG_MESSAGE, app_name_));
63   label->SetMultiLine(true);
64   label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
65   label->SetEnabledColor(SkColorSetARGB(138, 0, 0, 0));
66   label->SetLineHeight(kDialogMessageLineHeightDp);
67   label->SetFontList(views::Label::GetDefaultFontList().Derive(
68       1, gfx::Font::NORMAL, gfx::Font::Weight::NORMAL));
69   label->SetPreferredSize(
70       gfx::Size(kDialogWidthDp, label->GetHeightForWidth(kDialogWidthDp)));
71   label->SizeToPreferredSize();
72 
73   AddChildView(label);
74 }
75 
76 ToastDialogView::~ToastDialogView() = default;
77 
GetModalType() const78 ui::ModalType ToastDialogView::GetModalType() const {
79   return ui::MODAL_TYPE_NONE;
80 }
81 
GetWindowTitle() const82 base::string16 ToastDialogView::GetWindowTitle() const {
83   return l10n_util::GetStringFUTF16(IDS_LOCK_SCREEN_NOTE_APP_TOAST_DIALOG_TITLE,
84                                     app_name_);
85 }
86 
AddedToWidget()87 void ToastDialogView::AddedToWidget() {
88   std::unique_ptr<views::Label> title =
89       views::BubbleFrameView::CreateDefaultTitleLabel(GetWindowTitle());
90   title->SetFontList(views::Label::GetDefaultFontList().Derive(
91       3, gfx::Font::NORMAL, gfx::Font::Weight::MEDIUM));
92   GetBubbleFrameView()->SetTitleView(std::move(title));
93 }
94 
ShouldShowCloseButton() const95 bool ToastDialogView::ShouldShowCloseButton() const {
96   return true;
97 }
98 
OnBeforeBubbleWidgetInit(views::Widget::InitParams * params,views::Widget * widget) const99 void ToastDialogView::OnBeforeBubbleWidgetInit(
100     views::Widget::InitParams* params,
101     views::Widget* widget) const {
102   ash_util::SetupWidgetInitParamsForContainer(
103       params, ash::kShellWindowId_SettingBubbleContainer);
104 }
105 
106 }  // namespace lock_screen_apps
107