summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/power/idle_action_warning_dialog_view.cc
blob: df5b64cd426c7e50a0e249065ee4f600ae71a228 (plain)
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
// Copyright 2013 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/chromeos/power/idle_action_warning_dialog_view.h"

#include <algorithm>

#include "ash/shell.h"
#include "base/location.h"
#include "grit/generated_resources.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/time_format.h"
#include "ui/base/ui_base_types.h"
#include "ui/gfx/size.h"
#include "ui/gfx/text_constants.h"
#include "ui/views/border.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_client_view.h"

namespace chromeos {

namespace {

const int kIdleActionWarningContentWidth = 300;

const int kCountdownUpdateIntervalMs = 1000;  // 1 second.

class FixedWidthLabel : public views::Label {
 public:
  explicit FixedWidthLabel(int width);
  virtual ~FixedWidthLabel();

  virtual gfx::Size GetPreferredSize() const OVERRIDE;

 private:
  int width_;

  DISALLOW_COPY_AND_ASSIGN(FixedWidthLabel);
};

FixedWidthLabel::FixedWidthLabel(int width) : width_(width) {
  SetHorizontalAlignment(gfx::ALIGN_LEFT);
  SetMultiLine(true);
}

FixedWidthLabel::~FixedWidthLabel() {
}

gfx::Size FixedWidthLabel::GetPreferredSize() const {
  return gfx::Size(width_, GetHeightForWidth(width_));
}

}  // namespace

IdleActionWarningDialogView::IdleActionWarningDialogView(
    base::TimeTicks idle_action_time)
    : idle_action_time_(idle_action_time),
      label_(NULL) {
  label_ = new FixedWidthLabel(kIdleActionWarningContentWidth);
  label_->SetBorder(
      views::Border::CreateEmptyBorder(views::kPanelVertMargin,
                                       views::kButtonHEdgeMarginNew,
                                       views::kPanelVertMargin,
                                       views::kButtonHEdgeMarginNew));
  AddChildView(label_);
  SetLayoutManager(new views::FillLayout());

  UpdateLabel();

  views::DialogDelegate::CreateDialogWidget(
      this, ash::Shell::GetPrimaryRootWindow(), NULL)->Show();

  update_timer_.Start(
      FROM_HERE,
      base::TimeDelta::FromMilliseconds(kCountdownUpdateIntervalMs),
      this,
      &IdleActionWarningDialogView::UpdateLabel);
}

void IdleActionWarningDialogView::CloseDialog() {
  update_timer_.Stop();
  GetDialogClientView()->CancelWindow();
}

void IdleActionWarningDialogView::Update(base::TimeTicks idle_action_time) {
  idle_action_time_ = idle_action_time;
  UpdateLabel();
}

ui::ModalType IdleActionWarningDialogView::GetModalType() const {
  return ui::MODAL_TYPE_SYSTEM;
}

base::string16 IdleActionWarningDialogView::GetWindowTitle() const {
  return l10n_util::GetStringUTF16(IDS_IDLE_WARNING_TITLE);
}

int IdleActionWarningDialogView::GetDialogButtons() const {
  return ui::DIALOG_BUTTON_NONE;
}

bool IdleActionWarningDialogView::Cancel() {
  return !update_timer_.IsRunning();
}

IdleActionWarningDialogView::~IdleActionWarningDialogView() {
}

void IdleActionWarningDialogView::UpdateLabel() {
  const base::TimeDelta time_until_idle_action =
      std::max(idle_action_time_ - base::TimeTicks::Now(),
               base::TimeDelta());
  label_->SetText(l10n_util::GetStringFUTF16(
      IDS_IDLE_WARNING_LOGOUT_WARNING,
      ui::TimeFormat::Detailed(ui::TimeFormat::FORMAT_DURATION,
                               ui::TimeFormat::LENGTH_LONG,
                               10,
                               time_until_idle_action)));
}

}  // namespace chromeos