1 // Copyright 2014 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 "ash/system/chromeos/multi_user/user_switch_util.h"
8 #include "ash/system/chromeos/screen_security/screen_tray_item.h"
9 #include "ash/system/tray/system_tray.h"
10 #include "grit/ash_strings.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/views/controls/label.h"
14 #include "ui/views/layout/grid_layout.h"
15 #include "ui/views/widget/widget.h"
16 #include "ui/views/window/dialog_delegate.h"
22 // Default width/height of the dialog.
23 const int kDefaultWidth
= 500;
24 const int kDefaultHeight
= 150;
26 const int kPaddingToMessage
= 30;
27 const int kInset
= 40;
28 const int kTopInset
= 10;
30 ////////////////////////////////////////////////////////////////////////////////
31 // Dialog for multi-profiles desktop casting warning.
32 class DesktopCastingWarningView
: public views::DialogDelegateView
{
34 DesktopCastingWarningView(base::Callback
<void()> on_accept
);
35 ~DesktopCastingWarningView() override
;
37 static void ShowDialog(const base::Callback
<void()> on_accept
);
39 // views::DialogDelegate overrides.
40 bool Accept() override
;
41 base::string16
GetDialogButtonLabel(ui::DialogButton button
) const override
;
42 bool IsDialogButtonEnabled(ui::DialogButton button
) const override
;
43 int GetDefaultDialogButton() const override
;
45 // views::WidgetDelegate overrides.
46 ui::ModalType
GetModalType() const override
;
48 // views::View overrides.
49 gfx::Size
GetPreferredSize() const override
;
54 const base::Callback
<void()> on_switch_
;
56 DISALLOW_COPY_AND_ASSIGN(DesktopCastingWarningView
);
59 // The current instance of the running dialog - or NULL. This is used for
60 // unittest related functions.
61 static DesktopCastingWarningView
* instance_for_test
;
63 ////////////////////////////////////////////////////////////////////////////////
64 // DesktopCastingWarningView implementation.
66 DesktopCastingWarningView::DesktopCastingWarningView(
67 const base::Callback
<void()> on_switch
)
68 : on_switch_(on_switch
) {
69 DCHECK(!instance_for_test
);
70 instance_for_test
= this;
73 DesktopCastingWarningView::~DesktopCastingWarningView() {
74 DCHECK(instance_for_test
);
75 instance_for_test
= NULL
;
79 void DesktopCastingWarningView::ShowDialog(
80 const base::Callback
<void()> on_accept
) {
81 DesktopCastingWarningView
* dialog_view
=
82 new DesktopCastingWarningView(on_accept
);
83 views::DialogDelegate::CreateDialogWidget(
84 dialog_view
, ash::Shell::GetTargetRootWindow(), NULL
);
85 dialog_view
->InitDialog();
86 views::Widget
* widget
= dialog_view
->GetWidget();
91 bool DesktopCastingWarningView::Accept() {
92 // Stop screen sharing and capturing.
93 SystemTray
* system_tray
= ash::Shell::GetInstance()->GetPrimarySystemTray();
94 if (system_tray
->GetScreenShareItem()->is_started())
95 system_tray
->GetScreenShareItem()->Stop();
96 if (system_tray
->GetScreenCaptureItem()->is_started())
97 system_tray
->GetScreenCaptureItem()->Stop();
103 base::string16
DesktopCastingWarningView::GetDialogButtonLabel(
104 ui::DialogButton button
) const {
105 return l10n_util::GetStringUTF16(
106 button
== ui::DIALOG_BUTTON_OK
?
107 IDS_DESKTOP_CASTING_ACTIVE_BUTTON_SWITCH_USER
:
108 IDS_DESKTOP_CASTING_ACTIVE_BUTTON_ABORT_USER_SWITCH
);
111 bool DesktopCastingWarningView::IsDialogButtonEnabled(
112 ui::DialogButton button
) const {
113 return button
== ui::DIALOG_BUTTON_OK
|| button
== ui::DIALOG_BUTTON_CANCEL
;
116 int DesktopCastingWarningView::GetDefaultDialogButton() const {
117 // The default should turn off the casting.
118 return ui::DIALOG_BUTTON_CANCEL
;
121 ui::ModalType
DesktopCastingWarningView::GetModalType() const {
122 return ui::MODAL_TYPE_SYSTEM
;
125 gfx::Size
DesktopCastingWarningView::GetPreferredSize() const {
126 return gfx::Size(kDefaultWidth
, kDefaultHeight
);
129 void DesktopCastingWarningView::InitDialog() {
130 const gfx::Insets
kDialogInsets(kTopInset
, kInset
, kInset
, kInset
);
132 // Create the views and layout manager and set them up.
133 views::GridLayout
* grid_layout
= views::GridLayout::CreatePanel(this);
134 grid_layout
->SetInsets(kDialogInsets
);
136 views::ColumnSet
* column_set
= grid_layout
->AddColumnSet(0);
137 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::FILL
, 1,
138 views::GridLayout::USE_PREF
, 0, 0);
141 views::Label
* title_label_
= new views::Label(
142 l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_TITLE
));
143 title_label_
->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList(
144 ui::ResourceBundle::MediumBoldFont
));
145 title_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
146 grid_layout
->StartRow(0, 0);
147 grid_layout
->AddView(title_label_
);
148 grid_layout
->AddPaddingRow(0, kPaddingToMessage
);
150 // Explanation string
151 views::Label
* label
= new views::Label(
152 l10n_util::GetStringUTF16(IDS_DESKTOP_CASTING_ACTIVE_MESSAGE
));
153 label
->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList(
154 ui::ResourceBundle::MediumFont
));
155 label
->SetMultiLine(true);
156 label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
157 label
->SetAllowCharacterBreak(true);
158 grid_layout
->StartRow(0, 0);
159 grid_layout
->AddView(label
);
161 SetLayoutManager(grid_layout
);
167 ////////////////////////////////////////////////////////////////////////////////
170 void TrySwitchingActiveUser(
171 const base::Callback
<void()> on_switch
) {
172 // Some unit tests do not have a shell. In that case simply execute.
173 if (!ash::Shell::HasInstance()) {
177 // If neither screen sharing nor capturing is going on we can immediately
179 SystemTray
* system_tray
= ash::Shell::GetInstance()->GetPrimarySystemTray();
180 if (!system_tray
->GetScreenShareItem()->is_started() &&
181 !system_tray
->GetScreenCaptureItem()->is_started()) {
185 DesktopCastingWarningView::ShowDialog(on_switch
);
188 bool TestAndTerminateDesktopCastingWarningForTest(bool accept
) {
189 if (!instance_for_test
)
192 instance_for_test
->Accept();
193 delete instance_for_test
->GetWidget()->GetNativeWindow();
194 CHECK(!instance_for_test
);
198 } // namespace chromeos