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. 4 5 #include "chrome/browser/chromeos/power/idle_action_warning_dialog_view.h" 6 7 #include <algorithm> 8 9 #include "ash/shell.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/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" 24 25 namespace chromeos { 26 27 namespace { 28 29 const int kIdleActionWarningContentWidth = 300; 30 31 const int kCountdownUpdateIntervalMs = 1000; // 1 second. 32 33 class FixedWidthLabel : public views::Label { 34 public: 35 explicit FixedWidthLabel(int width); 36 virtual ~FixedWidthLabel(); 37 38 virtual gfx::Size GetPreferredSize() const OVERRIDE; 39 40 private: 41 int width_; 42 43 DISALLOW_COPY_AND_ASSIGN(FixedWidthLabel); 44 }; 45 46 FixedWidthLabel::FixedWidthLabel(int width) : width_(width) { 47 SetHorizontalAlignment(gfx::ALIGN_LEFT); 48 SetMultiLine(true); 49 } 50 51 FixedWidthLabel::~FixedWidthLabel() { 52 } 53 54 gfx::Size FixedWidthLabel::GetPreferredSize() const { 55 return gfx::Size(width_, GetHeightForWidth(width_)); 56 } 57 58 } // namespace 59 60 IdleActionWarningDialogView::IdleActionWarningDialogView( 61 base::TimeTicks idle_action_time) 62 : idle_action_time_(idle_action_time), 63 label_(NULL) { 64 label_ = new FixedWidthLabel(kIdleActionWarningContentWidth); 65 label_->SetBorder( 66 views::Border::CreateEmptyBorder(views::kPanelVertMargin, 67 views::kButtonHEdgeMarginNew, 68 views::kPanelVertMargin, 69 views::kButtonHEdgeMarginNew)); 70 AddChildView(label_); 71 SetLayoutManager(new views::FillLayout()); 72 73 UpdateLabel(); 74 75 views::DialogDelegate::CreateDialogWidget( 76 this, ash::Shell::GetPrimaryRootWindow(), NULL)->Show(); 77 78 update_timer_.Start( 79 FROM_HERE, 80 base::TimeDelta::FromMilliseconds(kCountdownUpdateIntervalMs), 81 this, 82 &IdleActionWarningDialogView::UpdateLabel); 83 } 84 85 void IdleActionWarningDialogView::CloseDialog() { 86 update_timer_.Stop(); 87 GetDialogClientView()->CancelWindow(); 88 } 89 90 void IdleActionWarningDialogView::Update(base::TimeTicks idle_action_time) { 91 idle_action_time_ = idle_action_time; 92 UpdateLabel(); 93 } 94 95 ui::ModalType IdleActionWarningDialogView::GetModalType() const { 96 return ui::MODAL_TYPE_SYSTEM; 97 } 98 99 base::string16 IdleActionWarningDialogView::GetWindowTitle() const { 100 return l10n_util::GetStringUTF16(IDS_IDLE_WARNING_TITLE); 101 } 102 103 int IdleActionWarningDialogView::GetDialogButtons() const { 104 return ui::DIALOG_BUTTON_NONE; 105 } 106 107 bool IdleActionWarningDialogView::Cancel() { 108 return !update_timer_.IsRunning(); 109 } 110 111 IdleActionWarningDialogView::~IdleActionWarningDialogView() { 112 } 113 114 void IdleActionWarningDialogView::UpdateLabel() { 115 const base::TimeDelta time_until_idle_action = 116 std::max(idle_action_time_ - base::TimeTicks::Now(), 117 base::TimeDelta()); 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, 122 10, 123 time_until_idle_action))); 124 } 125 126 } // namespace chromeos 127