update_view.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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/login/update_view.h" 6 7#include <string> 8 9#include "app/l10n_util.h" 10#include "app/resource_bundle.h" 11#include "chrome/browser/chromeos/login/rounded_rect_painter.h" 12#include "chrome/browser/chromeos/login/update_screen.h" 13#include "grit/chromium_strings.h" 14#include "grit/generated_resources.h" 15#include "views/border.h" 16#include "views/controls/label.h" 17#include "views/controls/progress_bar.h" 18#include "views/focus/focus_manager.h" 19#include "views/widget/widget.h" 20 21using views::Background; 22using views::Label; 23using views::View; 24using views::Widget; 25 26namespace { 27 28// Y offset for the 'installing updates' label. 29const int kInstallingUpdatesLabelY = 200; 30// Y offset for the progress bar. 31const int kProgressBarY = 250; 32// Y offset for the 'ESCAPE to skip' label. 33const int kEscapeToSkipLabelY = 290; 34// Progress bar width. 35const int kProgressBarWidth = 450; 36// Horizontal spacing (ex. min left and right margins for label on the screen). 37const int kHorizontalSpacing = 25; 38 39// Label color. 40const SkColor kLabelColor = 0xFF000000; 41const SkColor kSkipLabelColor = 0xFFAA0000; 42 43} // namespace 44 45namespace chromeos { 46 47UpdateView::UpdateView(chromeos::ScreenObserver* observer) 48 : escape_accelerator_(base::VKEY_ESCAPE, false, false, false), 49 installing_updates_label_(NULL), 50 progress_bar_(NULL), 51 observer_(observer) { 52} 53 54UpdateView::~UpdateView() { 55} 56 57void UpdateView::Init() { 58 // Use rounded-rect background. 59 views::Painter* painter = chromeos::CreateWizardPainter( 60 &chromeos::BorderDefinition::kScreenBorder); 61 set_background(views::Background::CreateBackgroundPainter(true, painter)); 62 63 ResourceBundle& res_bundle = ResourceBundle::GetSharedInstance(); 64 gfx::Font base_font = res_bundle.GetFont(ResourceBundle::BaseFont); 65 66 InitLabel(&installing_updates_label_); 67 installing_updates_label_->SetFont(base_font); 68 69 progress_bar_ = new views::ProgressBar(); 70 AddChildView(progress_bar_); 71 72 UpdateLocalizedStrings(); 73 74#if !defined(OFFICIAL_BUILD) 75 escape_to_skip_label_ = new views::Label(); 76 escape_to_skip_label_->SetColor(kSkipLabelColor); 77 escape_to_skip_label_->SetFont(base_font); 78 escape_to_skip_label_->SetText( 79 L"Press ESCAPE to skip (Non-official builds only)"); 80 AddChildView(escape_to_skip_label_); 81#endif 82} 83 84void UpdateView::Reset() { 85 progress_bar_->SetProgress(0); 86#if !defined(OFFICIAL_BUILD) 87 AddAccelerator(escape_accelerator_); 88#endif 89} 90 91void UpdateView::UpdateLocalizedStrings() { 92 installing_updates_label_->SetText( 93 l10n_util::GetStringF(IDS_INSTALLING_UPDATE, 94 l10n_util::GetString(IDS_PRODUCT_OS_NAME))); 95} 96 97void UpdateView::AddProgress(int ticks) { 98 progress_bar_->AddProgress(ticks); 99} 100 101// Sets the bounds of the view, placing it at the center of the screen 102// with the |y| coordinate provided. |width| could specify desired view width 103// otherwise preferred width/height are used. 104// |x_center| specifies screen center. 105static void setViewBounds( 106 views::View* view, int x_center, int y, int width = -1) { 107 int preferred_width = (width >= 0) ? width : view->GetPreferredSize().width(); 108 int preferred_height = view->GetPreferredSize().height(); 109 view->SetBounds( 110 x_center - preferred_width / 2, 111 y, 112 preferred_width, 113 preferred_height); 114} 115 116void UpdateView::Layout() { 117 int x_center = width() / 2; 118 int max_width = width() - GetInsets().width() - 2 * kHorizontalSpacing; 119 installing_updates_label_->SizeToFit(max_width); 120 setViewBounds(installing_updates_label_, x_center, kInstallingUpdatesLabelY); 121 setViewBounds(progress_bar_, x_center, kProgressBarY, kProgressBarWidth); 122#if !defined(OFFICIAL_BUILD) 123 setViewBounds(escape_to_skip_label_, x_center, kEscapeToSkipLabelY); 124#endif 125 SchedulePaint(); 126} 127 128bool UpdateView::AcceleratorPressed(const views::Accelerator& a) { 129#if !defined(OFFICIAL_BUILD) 130 RemoveAccelerator(escape_accelerator_); 131 if (a.GetKeyCode() == base::VKEY_ESCAPE) { 132 if (controller_ != NULL) { 133 controller_->CancelUpdate(); 134 } 135 return true; 136 } 137#endif 138 return false; 139} 140 141void UpdateView::InitLabel(views::Label** label) { 142 *label = new views::Label(); 143 (*label)->SetColor(kLabelColor); 144 (*label)->SetHorizontalAlignment(views::Label::ALIGN_CENTER); 145 (*label)->SetMultiLine(true); 146 AddChildView(*label); 147} 148 149} // namespace chromeos 150