Home | History | Annotate | Download | only in views
      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 "ui/app_list/views/progress_bar_view.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "grit/ui_resources.h"
     10 #include "ui/base/resource/resource_bundle.h"
     11 #include "ui/gfx/image/image_skia.h"
     12 #include "ui/gfx/insets.h"
     13 #include "ui/views/painter.h"
     14 
     15 namespace app_list {
     16 
     17 namespace {
     18 
     19 // Image assets for the bar part.
     20 const int kProgressBarImages[] = {
     21   IDR_APP_LIST_ITEM_PROGRESS_LEFT,
     22   IDR_APP_LIST_ITEM_PROGRESS_CENTER,
     23   IDR_APP_LIST_ITEM_PROGRESS_RIGHT
     24 };
     25 
     26 }  // namespace
     27 
     28 ProgressBarView::ProgressBarView()
     29     : background_painter_(views::Painter::CreateImagePainter(
     30           *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
     31               IDR_APP_LIST_ITEM_PROGRESS_BACKGROUND),
     32           gfx::Insets(2, 2, 2, 2))),
     33       bar_painter_(new views::HorizontalPainter(kProgressBarImages)) {}
     34 
     35 ProgressBarView::~ProgressBarView() {}
     36 
     37 gfx::Size ProgressBarView::GetPreferredSize() const {
     38   return background_painter_->GetMinimumSize();
     39 }
     40 
     41 void ProgressBarView::OnPaint(gfx::Canvas* canvas) {
     42   gfx::Size paint_size = size();
     43 
     44   const gfx::Size min_size(background_painter_->GetMinimumSize());
     45   if (paint_size.width() < min_size.width() ||
     46       paint_size.height() < min_size.height()) {
     47     return;
     48   }
     49 
     50   background_painter_->Paint(canvas, paint_size);
     51 
     52   const int kBarEndWidth = 4;
     53   const int bar_width = paint_size.width() - kBarEndWidth;
     54   DCHECK_GE(bar_width, 0);
     55 
     56   // Map normalized value in [0,1] to the pixel width in
     57   // [kBarEndWidth, view_size.width()].
     58   paint_size.set_width(kBarEndWidth + bar_width * GetNormalizedValue());
     59   bar_painter_->Paint(canvas, paint_size);
     60 }
     61 
     62 }  // namespace app_list
     63