summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/login/update_view.cc
blob: 59e20dd8039295b9650a4a97a1c8f4600107ace4 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright (c) 2010 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/login/update_view.h"

#include <string>

#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "chrome/browser/chromeos/login/helper.h"
#include "chrome/browser/chromeos/login/rounded_rect_painter.h"
#include "chrome/browser/chromeos/login/update_screen.h"
#include "chrome/browser/chromeos/login/wizard_accessibility_helper.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "views/border.h"
#include "views/controls/label.h"
#include "views/controls/progress_bar.h"
#include "views/controls/throbber.h"
#include "views/focus/focus_manager.h"
#include "views/widget/widget.h"

using views::Background;
using views::Label;
using views::View;
using views::Widget;

namespace {

// TODO(nkostylev): Switch to GridLayout.

// Y offset for the 'installing updates' label.
const int kInstallingUpdatesLabelYBottomFromProgressBar = 18;
// Y offset for the progress bar.
const int kProgressBarY = 199;
// Y offset for the 'computer will restart' label.
const int kRebootLabelYFromProgressBar = 77;
// Y offset for the 'ESCAPE to skip' label.
const int kEscapeToSkipLabelY = 48;
// Progress bar width.
const int kProgressBarWidth = 550;
// Progress bar height.
const int kProgressBarHeight = 18;
// Horizontal spacing (ex. min left and right margins for label on the screen).
const int kHorizontalSpacing = 75;
// Horizontal spacing between spinner and label on the curtain screen.
const int kBetweenSpacing = 25;

// Label color.
const SkColor kLabelColor = 0xFF000000;
const SkColor kSkipLabelColor = 0xFFAA0000;
const SkColor kManualRebootLabelColor = 0xFFAA0000;

}  // namespace

namespace chromeos {

UpdateView::UpdateView(chromeos::ScreenObserver* observer)
    : escape_accelerator_(app::VKEY_ESCAPE, false, false, false),
      installing_updates_label_(NULL),
      reboot_label_(NULL),
      manual_reboot_label_(NULL),
      progress_bar_(NULL),
      show_curtain_(true),
      show_manual_reboot_label_(false),
      observer_(observer) {
}

UpdateView::~UpdateView() {
}

void UpdateView::Init() {
  // Use rounded-rect background.
  views::Painter* painter = chromeos::CreateWizardPainter(
      &chromeos::BorderDefinition::kScreenBorder);
  set_background(views::Background::CreateBackgroundPainter(true, painter));

  InitLabel(&installing_updates_label_);
  InitLabel(&reboot_label_);
  InitLabel(&manual_reboot_label_);
  manual_reboot_label_->SetVisible(false);
  manual_reboot_label_->SetColor(kManualRebootLabelColor);

  progress_bar_ = new views::ProgressBar();
  AddChildView(progress_bar_);

  // Curtain view.
  InitLabel(&checking_label_);
  throbber_ = CreateDefaultThrobber();
  AddChildView(throbber_);

#if !defined(OFFICIAL_BUILD)
  InitLabel(&escape_to_skip_label_);
  escape_to_skip_label_->SetColor(kSkipLabelColor);
  escape_to_skip_label_->SetText(
      L"Press ESCAPE to skip (Non-official builds only)");
#endif

  UpdateLocalizedStrings();
  UpdateVisibility();
}

void UpdateView::Reset() {
  progress_bar_->SetProgress(0);
#if !defined(OFFICIAL_BUILD)
  ResetAccelerators();
  AddAccelerator(escape_accelerator_);
#endif
}

void UpdateView::UpdateLocalizedStrings() {
  installing_updates_label_->SetText(
      l10n_util::GetStringF(IDS_INSTALLING_UPDATE,
                            l10n_util::GetString(IDS_PRODUCT_OS_NAME)));
  reboot_label_->SetText(l10n_util::GetString(IDS_INSTALLING_UPDATE_DESC));
  manual_reboot_label_->SetText(l10n_util::GetString(IDS_UPDATE_COMPLETED));
  checking_label_->SetText(l10n_util::GetString(IDS_CHECKING_FOR_UPDATES));
}

void UpdateView::AddProgress(int ticks) {
  progress_bar_->AddProgress(ticks);
}

void UpdateView::SetProgress(int progress) {
  progress_bar_->SetProgress(progress);
}

void UpdateView::ShowManualRebootInfo() {
  show_manual_reboot_label_ = true;
  UpdateVisibility();
}

void UpdateView::ShowCurtain(bool show_curtain) {
  if (show_curtain_ != show_curtain) {
    show_curtain_ = show_curtain;
    UpdateVisibility();
  }
}

// Sets the bounds of the view, placing center of the view at the given
// coordinates (|x| and |y|).
static void setViewBounds(views::View* view, int x, int y) {
  int preferred_width = view->GetPreferredSize().width();
  int preferred_height = view->GetPreferredSize().height();
  view->SetBounds(
      x - preferred_width / 2,
      y - preferred_height / 2,
      preferred_width,
      preferred_height);
}

void UpdateView::Layout() {
  int max_width = width() - GetInsets().width() - 2 * kHorizontalSpacing;
  int right_margin = GetInsets().right() + kHorizontalSpacing;
  int max_height = height() - GetInsets().height();
  int vertical_center = GetInsets().top() + max_height / 2;

  installing_updates_label_->SizeToFit(max_width);
  reboot_label_->SizeToFit(max_width);
  manual_reboot_label_->SizeToFit(max_width);

  progress_bar_->SetBounds(right_margin,
                           vertical_center - kProgressBarHeight / 2,
                           max_width,
                           kProgressBarHeight);

  installing_updates_label_->SetX(right_margin);
  installing_updates_label_->SetY(
      progress_bar_->y() -
      kInstallingUpdatesLabelYBottomFromProgressBar -
      installing_updates_label_->height());
  reboot_label_->SetX(right_margin);
  reboot_label_->SetY(
      progress_bar_->y() +
      progress_bar_->height() +
      kRebootLabelYFromProgressBar);
  manual_reboot_label_->SetX(reboot_label_->x());
  manual_reboot_label_->SetY(reboot_label_->y());
  // Curtain layout is independed.
  int x_center = width() / 2;
  int throbber_width = throbber_->GetPreferredSize().width();
  checking_label_->SizeToFit(max_width - throbber_width - kBetweenSpacing);
  int checking_label_width = checking_label_->GetPreferredSize().width();
  int space_half = (kBetweenSpacing + 1) / 2;
  setViewBounds(
      throbber_, x_center - checking_label_width / 2 - space_half,
      vertical_center);
  setViewBounds(
      checking_label_, x_center + (throbber_width + 1) / 2 + space_half,
      vertical_center);
#if !defined(OFFICIAL_BUILD)
  escape_to_skip_label_->SizeToFit(max_width);
  escape_to_skip_label_->SetX(right_margin);
  escape_to_skip_label_->SetY(kEscapeToSkipLabelY);
#endif
  SchedulePaint();
}

bool UpdateView::AcceleratorPressed(const views::Accelerator& a) {
#if !defined(OFFICIAL_BUILD)
  RemoveAccelerator(escape_accelerator_);
  if (a.GetKeyCode() == app::VKEY_ESCAPE) {
    if (controller_ != NULL) {
      controller_->CancelUpdate();
    }
    return true;
  }
#endif
  return false;
}

void UpdateView::ViewHierarchyChanged(bool is_add, View* parent, View* child) {
  if (is_add && this == child)
    WizardAccessibilityHelper::GetInstance()->MaybeEnableAccessibility(this);
}

void UpdateView::InitLabel(views::Label** label) {
  *label = new views::Label();
  (*label)->SetColor(kLabelColor);
  (*label)->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  (*label)->SetMultiLine(true);

  ResourceBundle& res_bundle = ResourceBundle::GetSharedInstance();
  gfx::Font label_font = res_bundle.GetFont(ResourceBundle::MediumBoldFont);
  (*label)->SetFont(label_font);

  AddChildView(*label);
}

void UpdateView::UpdateVisibility() {
  installing_updates_label_->SetVisible(
      !show_curtain_&& !show_manual_reboot_label_);
  reboot_label_->SetVisible(!show_curtain_&& !show_manual_reboot_label_);
  manual_reboot_label_->SetVisible(!show_curtain_ && show_manual_reboot_label_);
  progress_bar_->SetVisible(!show_curtain_);

  checking_label_->SetVisible(show_curtain_);
  throbber_->SetVisible(show_curtain_);
  if (show_curtain_) {
    throbber_->Start();
  } else {
    throbber_->Stop();
  }
}

}  // namespace chromeos