Home | History | Annotate | Download | only in tray
      1 // Copyright (c) 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 "ash/system/tray/special_popup_row.h"
      6 
      7 #include "ash/system/tray/hover_highlight_view.h"
      8 #include "ash/system/tray/throbber_view.h"
      9 #include "ash/system/tray/tray_constants.h"
     10 #include "ash/system/tray/tray_popup_header_button.h"
     11 #include "grit/ash_resources.h"
     12 #include "grit/ash_strings.h"
     13 #include "ui/base/resource/resource_bundle.h"
     14 #include "ui/gfx/canvas.h"
     15 #include "ui/gfx/rect.h"
     16 #include "ui/views/background.h"
     17 #include "ui/views/border.h"
     18 #include "ui/views/layout/box_layout.h"
     19 #include "ui/views/painter.h"
     20 
     21 namespace ash {
     22 namespace internal {
     23 
     24 namespace {
     25 
     26 const int kIconPaddingLeft = 5;
     27 const int kSpecialPopupRowHeight = 55;
     28 const int kBorderHeight = 1;
     29 const SkColor kBorderColor = SkColorSetRGB(0xaa, 0xaa, 0xaa);
     30 
     31 views::View* CreatePopupHeaderButtonsContainer() {
     32   views::View* view = new views::View;
     33   view->SetLayoutManager(new
     34       views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, -1));
     35   view->set_border(views::Border::CreateEmptyBorder(0, 0, 0, 5));
     36   return view;
     37 }
     38 
     39 }  // namespace
     40 
     41 SpecialPopupRow::SpecialPopupRow()
     42     : content_(NULL),
     43       button_container_(NULL) {
     44   set_background(views::Background::CreateSolidBackground(
     45       kHeaderBackgroundColor));
     46   set_border(views::Border::CreateSolidSidedBorder(
     47       kBorderHeight, 0, 0, 0, kBorderColor));
     48   SetLayoutManager(
     49       new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
     50 }
     51 
     52 SpecialPopupRow::~SpecialPopupRow() {
     53 }
     54 
     55 void SpecialPopupRow::SetTextLabel(int string_id, ViewClickListener* listener) {
     56   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
     57   HoverHighlightView* container = new HoverHighlightView(listener);
     58   container->SetLayoutManager(new
     59       views::BoxLayout(views::BoxLayout::kHorizontal, 0, 3, kIconPaddingLeft));
     60 
     61   container->set_highlight_color(SkColorSetARGB(0, 0, 0, 0));
     62   container->set_default_color(SkColorSetARGB(0, 0, 0, 0));
     63   container->set_text_highlight_color(kHeaderTextColorHover);
     64   container->set_text_default_color(kHeaderTextColorNormal);
     65 
     66   container->AddIconAndLabel(
     67       *rb.GetImageNamed(IDR_AURA_UBER_TRAY_LESS).ToImageSkia(),
     68       rb.GetLocalizedString(string_id),
     69       gfx::Font::BOLD);
     70 
     71   container->set_border(views::Border::CreateEmptyBorder(0,
     72       kTrayPopupPaddingHorizontal, 0, 0));
     73 
     74   container->SetAccessibleName(
     75       rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_PREVIOUS_MENU));
     76   SetContent(container);
     77 }
     78 
     79 void SpecialPopupRow::SetContent(views::View* view) {
     80   CHECK(!content_);
     81   content_ = view;
     82   AddChildViewAt(content_, 0);
     83 }
     84 
     85 void SpecialPopupRow::AddButton(TrayPopupHeaderButton* button) {
     86   if (!button_container_) {
     87     button_container_ = CreatePopupHeaderButtonsContainer();
     88     AddChildView(button_container_);
     89   }
     90   button_container_->AddChildView(button);
     91 }
     92 
     93 void SpecialPopupRow::AddThrobber(ThrobberView* throbber) {
     94   if (!button_container_) {
     95     button_container_ = CreatePopupHeaderButtonsContainer();
     96     AddChildView(button_container_);
     97   }
     98   button_container_->AddChildView(throbber);
     99 }
    100 
    101 gfx::Size SpecialPopupRow::GetPreferredSize() {
    102   gfx::Size size = views::View::GetPreferredSize();
    103   size.set_height(kSpecialPopupRowHeight);
    104   return size;
    105 }
    106 
    107 int SpecialPopupRow::GetHeightForWidth(int width) {
    108   return kSpecialPopupRowHeight;
    109 }
    110 
    111 void SpecialPopupRow::Layout() {
    112   views::View::Layout();
    113   gfx::Rect content_bounds = GetContentsBounds();
    114   if (content_bounds.IsEmpty())
    115     return;
    116   if (!button_container_) {
    117     content_->SetBoundsRect(GetContentsBounds());
    118     return;
    119   }
    120 
    121   gfx::Rect bounds(button_container_->GetPreferredSize());
    122   bounds.set_height(content_bounds.height());
    123   gfx::Rect container_bounds = content_bounds;
    124   container_bounds.ClampToCenteredSize(bounds.size());
    125   container_bounds.set_x(content_bounds.width() - container_bounds.width());
    126   button_container_->SetBoundsRect(container_bounds);
    127 
    128   bounds = content_->bounds();
    129   bounds.set_width(button_container_->x());
    130   content_->SetBoundsRect(bounds);
    131 }
    132 
    133 }  // namespace internal
    134 }  // namespace ash
    135