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.
5 #include "ash/system/tray/tray_details_view.h"
7 #include "ash/system/tray/fixed_sized_scroll_view.h"
8 #include "ash/system/tray/system_tray.h"
9 #include "ash/system/tray/system_tray_item.h"
10 #include "ash/system/tray/tray_constants.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/views/background.h"
13 #include "ui/views/border.h"
14 #include "ui/views/controls/scroll_view.h"
15 #include "ui/views/layout/box_layout.h"
19 class ScrollSeparator
: public views::View
{
23 ~ScrollSeparator() override
{}
26 // Overriden from views::View.
27 void OnPaint(gfx::Canvas
* canvas
) override
{
28 canvas
->FillRect(gfx::Rect(0, height() / 2, width(), 1), kBorderLightColor
);
30 gfx::Size
GetPreferredSize() const override
{
31 return gfx::Size(1, kTrayPopupScrollSeparatorHeight
);
34 DISALLOW_COPY_AND_ASSIGN(ScrollSeparator
);
37 class ScrollBorder
: public views::Border
{
40 ~ScrollBorder() override
{}
42 void set_visible(bool visible
) { visible_
= visible
; }
45 // Overridden from views::Border.
46 void Paint(const views::View
& view
, gfx::Canvas
* canvas
) override
{
49 canvas
->FillRect(gfx::Rect(0, view
.height() - 1, view
.width(), 1),
53 gfx::Insets
GetInsets() const override
{ return gfx::Insets(0, 0, 1, 0); }
55 gfx::Size
GetMinimumSize() const override
{ return gfx::Size(0, 1); }
59 DISALLOW_COPY_AND_ASSIGN(ScrollBorder
);
62 TrayDetailsView::TrayDetailsView(SystemTrayItem
* owner
)
66 scroll_content_(NULL
),
67 scroll_border_(NULL
) {
68 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical
,
70 set_background(views::Background::CreateSolidBackground(kBackgroundColor
));
73 TrayDetailsView::~TrayDetailsView() {
76 void TrayDetailsView::CreateSpecialRow(int string_id
,
77 ViewClickListener
* listener
) {
79 footer_
= new SpecialPopupRow();
80 footer_
->SetTextLabel(string_id
, listener
);
81 AddChildViewAt(footer_
, child_count());
84 void TrayDetailsView::CreateScrollableList() {
86 scroll_content_
= new views::View
;
87 scroll_content_
->SetLayoutManager(new views::BoxLayout(
88 views::BoxLayout::kVertical
, 0, 0, 1));
89 scroller_
= new FixedSizedScrollView
;
90 scroller_
->SetContentsView(scroll_content_
);
92 // Note: |scroller_| takes ownership of |scroll_border_|.
93 scroll_border_
= new ScrollBorder
;
94 scroller_
->SetBorder(scoped_ptr
<views::Border
>(scroll_border_
));
96 AddChildView(scroller_
);
99 void TrayDetailsView::AddScrollSeparator() {
100 DCHECK(scroll_content_
);
101 // Do not draw the separator if it is the very first item
102 // in the scrollable list.
103 if (scroll_content_
->has_children())
104 scroll_content_
->AddChildView(new ScrollSeparator
);
107 void TrayDetailsView::Reset() {
108 RemoveAllChildViews(true);
111 scroll_content_
= NULL
;
114 void TrayDetailsView::TransitionToDefaultView() {
115 // Cache pointer to owner in this function scope. TrayDetailsView will be
116 // deleted after called ShowDefaultView.
117 SystemTrayItem
* owner
= owner_
;
118 if (footer_
&& footer_
->content() && footer_
->content()->HasFocus())
119 owner
->set_restore_focus(true);
120 owner
->system_tray()->ShowDefaultView(BUBBLE_USE_EXISTING
);
121 owner
->set_restore_focus(false);
124 void TrayDetailsView::Layout() {
125 if (bounds().IsEmpty()) {
126 views::View::Layout();
131 scroller_
->set_fixed_size(gfx::Size());
132 gfx::Size size
= GetPreferredSize();
134 // Set the scroller to fill the space above the bottom row, so that the
135 // bottom row of the detailed view will always stay just above the footer.
136 gfx::Size scroller_size
= scroll_content_
->GetPreferredSize();
137 scroller_
->set_fixed_size(
138 gfx::Size(width() + scroller_
->GetScrollBarWidth(),
139 scroller_size
.height() - (size
.height() - height())));
142 views::View::Layout();
145 // Always make sure the footer element is bottom aligned.
146 gfx::Rect fbounds
= footer_
->bounds();
147 fbounds
.set_y(height() - footer_
->height());
148 footer_
->SetBoundsRect(fbounds
);
152 void TrayDetailsView::OnPaintBorder(gfx::Canvas
* canvas
) {
153 if (scroll_border_
) {
154 int index
= GetIndexOf(scroller_
);
155 if (index
< child_count() - 1 && child_at(index
+ 1) != footer_
)
156 scroll_border_
->set_visible(true);
158 scroll_border_
->set_visible(false);
161 views::View::OnPaintBorder(canvas
);