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 {
23
24 const int kIconPaddingLeft = 5;
25 const int kSpecialPopupRowHeight = 55;
26 const int kBorderHeight = 1;
27 const SkColor kBorderColor = SkColorSetRGB(0xaa, 0xaa, 0xaa);
28
CreatePopupHeaderButtonsContainer()29 views::View* CreatePopupHeaderButtonsContainer() {
30 views::View* view = new views::View;
31 view->SetLayoutManager(new
32 views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, -1));
33 view->SetBorder(views::Border::CreateEmptyBorder(0, 0, 0, 5));
34 return view;
35 }
36
37 } // namespace
38
SpecialPopupRow()39 SpecialPopupRow::SpecialPopupRow()
40 : content_(NULL),
41 button_container_(NULL) {
42 set_background(views::Background::CreateSolidBackground(
43 kHeaderBackgroundColor));
44 SetBorder(views::Border::CreateSolidSidedBorder(
45 kBorderHeight, 0, 0, 0, kBorderColor));
46 SetLayoutManager(
47 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
48 }
49
~SpecialPopupRow()50 SpecialPopupRow::~SpecialPopupRow() {
51 }
52
SetTextLabel(int string_id,ViewClickListener * listener)53 void SpecialPopupRow::SetTextLabel(int string_id, ViewClickListener* listener) {
54 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
55 HoverHighlightView* container = new HoverHighlightView(listener);
56 container->SetLayoutManager(new
57 views::BoxLayout(views::BoxLayout::kHorizontal, 0, 3, kIconPaddingLeft));
58
59 container->set_highlight_color(SkColorSetARGB(0, 0, 0, 0));
60 container->set_default_color(SkColorSetARGB(0, 0, 0, 0));
61 container->set_text_highlight_color(kHeaderTextColorHover);
62 container->set_text_default_color(kHeaderTextColorNormal);
63
64 container->AddIconAndLabel(
65 *rb.GetImageNamed(IDR_AURA_UBER_TRAY_LESS).ToImageSkia(),
66 rb.GetLocalizedString(string_id),
67 gfx::Font::BOLD);
68
69 container->SetBorder(
70 views::Border::CreateEmptyBorder(0, kTrayPopupPaddingHorizontal, 0, 0));
71
72 container->SetAccessibleName(
73 rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_PREVIOUS_MENU));
74 SetContent(container);
75 }
76
SetContent(views::View * view)77 void SpecialPopupRow::SetContent(views::View* view) {
78 CHECK(!content_);
79 content_ = view;
80 AddChildViewAt(content_, 0);
81 }
82
AddButton(TrayPopupHeaderButton * button)83 void SpecialPopupRow::AddButton(TrayPopupHeaderButton* button) {
84 if (!button_container_) {
85 button_container_ = CreatePopupHeaderButtonsContainer();
86 AddChildView(button_container_);
87 }
88 button_container_->AddChildView(button);
89 }
90
AddThrobber(ThrobberView * throbber)91 void SpecialPopupRow::AddThrobber(ThrobberView* throbber) {
92 if (!button_container_) {
93 button_container_ = CreatePopupHeaderButtonsContainer();
94 AddChildView(button_container_);
95 }
96 button_container_->AddChildView(throbber);
97 }
98
GetPreferredSize() const99 gfx::Size SpecialPopupRow::GetPreferredSize() const {
100 gfx::Size size = views::View::GetPreferredSize();
101 size.set_height(kSpecialPopupRowHeight);
102 return size;
103 }
104
GetHeightForWidth(int width) const105 int SpecialPopupRow::GetHeightForWidth(int width) const {
106 return kSpecialPopupRowHeight;
107 }
108
Layout()109 void SpecialPopupRow::Layout() {
110 views::View::Layout();
111 gfx::Rect content_bounds = GetContentsBounds();
112 if (content_bounds.IsEmpty())
113 return;
114 if (!button_container_) {
115 content_->SetBoundsRect(GetContentsBounds());
116 return;
117 }
118
119 gfx::Rect bounds(button_container_->GetPreferredSize());
120 bounds.set_height(content_bounds.height());
121 gfx::Rect container_bounds = content_bounds;
122 container_bounds.ClampToCenteredSize(bounds.size());
123 container_bounds.set_x(content_bounds.width() - container_bounds.width());
124 button_container_->SetBoundsRect(container_bounds);
125
126 bounds = content_->bounds();
127 bounds.set_width(button_container_->x());
128 content_->SetBoundsRect(bounds);
129 }
130
131 } // namespace ash
132