1 // Copyright 2013 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/chromeos/power/idle_action_warning_dialog_view.h"
10 #include "base/location.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/l10n/time_format.h"
15 #include "ui/base/ui_base_types.h"
16 #include "ui/gfx/geometry/size.h"
17 #include "ui/gfx/text_constants.h"
18 #include "ui/views/border.h"
19 #include "ui/views/controls/label.h"
20 #include "ui/views/layout/fill_layout.h"
21 #include "ui/views/layout/layout_constants.h"
22 #include "ui/views/widget/widget.h"
23 #include "ui/views/window/dialog_client_view.h"
29 const int kIdleActionWarningContentWidth
= 300;
31 const int kCountdownUpdateIntervalMs
= 1000; // 1 second.
33 class FixedWidthLabel
: public views::Label
{
35 explicit FixedWidthLabel(int width
);
36 ~FixedWidthLabel() override
;
38 gfx::Size
GetPreferredSize() const override
;
43 DISALLOW_COPY_AND_ASSIGN(FixedWidthLabel
);
46 FixedWidthLabel::FixedWidthLabel(int width
) : width_(width
) {
47 SetHorizontalAlignment(gfx::ALIGN_LEFT
);
51 FixedWidthLabel::~FixedWidthLabel() {
54 gfx::Size
FixedWidthLabel::GetPreferredSize() const {
55 return gfx::Size(width_
, GetHeightForWidth(width_
));
60 IdleActionWarningDialogView::IdleActionWarningDialogView(
61 base::TimeTicks idle_action_time
)
62 : idle_action_time_(idle_action_time
),
64 label_
= new FixedWidthLabel(kIdleActionWarningContentWidth
);
66 views::Border::CreateEmptyBorder(views::kPanelVertMargin
,
67 views::kButtonHEdgeMarginNew
,
68 views::kPanelVertMargin
,
69 views::kButtonHEdgeMarginNew
));
71 SetLayoutManager(new views::FillLayout());
75 views::DialogDelegate::CreateDialogWidget(
76 this, ash::Shell::GetPrimaryRootWindow(), NULL
)->Show();
80 base::TimeDelta::FromMilliseconds(kCountdownUpdateIntervalMs
),
82 &IdleActionWarningDialogView::UpdateLabel
);
85 void IdleActionWarningDialogView::CloseDialog() {
87 GetDialogClientView()->CancelWindow();
90 void IdleActionWarningDialogView::Update(base::TimeTicks idle_action_time
) {
91 idle_action_time_
= idle_action_time
;
95 ui::ModalType
IdleActionWarningDialogView::GetModalType() const {
96 return ui::MODAL_TYPE_SYSTEM
;
99 base::string16
IdleActionWarningDialogView::GetWindowTitle() const {
100 return l10n_util::GetStringUTF16(IDS_IDLE_WARNING_TITLE
);
103 int IdleActionWarningDialogView::GetDialogButtons() const {
104 return ui::DIALOG_BUTTON_NONE
;
107 bool IdleActionWarningDialogView::Cancel() {
108 return !update_timer_
.IsRunning();
111 IdleActionWarningDialogView::~IdleActionWarningDialogView() {
114 void IdleActionWarningDialogView::UpdateLabel() {
115 const base::TimeDelta time_until_idle_action
=
116 std::max(idle_action_time_
- base::TimeTicks::Now(),
118 label_
->SetText(l10n_util::GetStringFUTF16(
119 IDS_IDLE_WARNING_LOGOUT_WARNING
,
120 ui::TimeFormat::Detailed(ui::TimeFormat::FORMAT_DURATION
,
121 ui::TimeFormat::LENGTH_LONG
,
123 time_until_idle_action
)));
126 } // namespace chromeos