Home | History | Annotate | Download | only in views
      1 // Copyright (c) 2012 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/app_list_background.h"
      6 
      7 #include "third_party/skia/include/core/SkPaint.h"
      8 #include "third_party/skia/include/core/SkPath.h"
      9 #include "ui/app_list/app_list_constants.h"
     10 #include "ui/app_list/views/app_list_main_view.h"
     11 #include "ui/app_list/views/search_box_view.h"
     12 #include "ui/gfx/canvas.h"
     13 #include "ui/gfx/rect.h"
     14 #include "ui/gfx/skia_util.h"
     15 #include "ui/views/view.h"
     16 
     17 namespace {
     18 
     19 // Size of top separator between searchbox and grid view.
     20 const int kTopSeparatorSize = 1;
     21 
     22 }  // namespace
     23 
     24 namespace app_list {
     25 
     26 AppListBackground::AppListBackground(int corner_radius,
     27                                      AppListMainView* main_view)
     28     : corner_radius_(corner_radius),
     29       main_view_(main_view) {
     30 }
     31 
     32 AppListBackground::~AppListBackground() {
     33 }
     34 
     35 void AppListBackground::Paint(gfx::Canvas* canvas,
     36                               views::View* view) const {
     37   gfx::Rect bounds = view->GetContentsBounds();
     38 
     39   canvas->Save();
     40   SkPath path;
     41   // Contents corner radius is 1px smaller than border corner radius.
     42   SkScalar radius = SkIntToScalar(corner_radius_ - 1);
     43   path.addRoundRect(gfx::RectToSkRect(bounds), radius, radius);
     44   canvas->ClipPath(path);
     45 
     46   SkPaint paint;
     47   paint.setStyle(SkPaint::kFill_Style);
     48 
     49   int contents_top = bounds.y();
     50   if (main_view_->visible()) {
     51     views::View* search_box_view = main_view_->search_box_view();
     52     const gfx::Rect search_box_view_bounds =
     53         search_box_view->ConvertRectToWidget(search_box_view->GetLocalBounds());
     54     gfx::Rect search_box_rect(bounds.x(),
     55                               bounds.y(),
     56                               bounds.width(),
     57                               search_box_view_bounds.bottom() - bounds.y());
     58 
     59     paint.setColor(kSearchBoxBackground);
     60     canvas->DrawRect(search_box_rect, paint);
     61 
     62     gfx::Rect seperator_rect(search_box_rect);
     63     seperator_rect.set_y(seperator_rect.bottom());
     64     seperator_rect.set_height(kTopSeparatorSize);
     65     canvas->FillRect(seperator_rect, kTopSeparatorColor);
     66     contents_top = seperator_rect.bottom();
     67   }
     68 
     69   gfx::Rect contents_rect(bounds.x(),
     70                           contents_top,
     71                           bounds.width(),
     72                           bounds.bottom() - contents_top);
     73 
     74   paint.setColor(kContentsBackgroundColor);
     75   canvas->DrawRect(contents_rect, paint);
     76   canvas->Restore();
     77 }
     78 
     79 }  // namespace app_list
     80