OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016 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/common/system/tray/header_list_scroll_view.h" | |
6 #include "ui/views/border.h" | |
7 #include "ui/views/layout/box_layout.h" | |
8 | |
9 namespace ash { | |
10 namespace { | |
11 | |
12 const int kHeaderRowSeparatorThickness = 1; | |
13 const SkColor kHeaderRowSeparatorColor = SkColorSetA(SK_ColorBLACK, 0x1F); | |
14 | |
15 } // namespace | |
16 | |
17 // Variation of a BoxLayout that keeps header rows at the end of children_ in | |
18 // order to stack them above the other rows while maintaining the order (group | |
19 // headers first, the rest of the group rows in their original order). | |
20 // To use this layout tag header rows with set_id(kHeaderRowId) and tag groups | |
21 // including the header row with SetGroup(group_id). | |
22 class HeaderListScrollView::HeaderListLayout : public views::BoxLayout { | |
23 public: | |
24 class Header { | |
25 public: | |
26 explicit Header(views::View* header) : view(header) {} | |
27 bool operator==(views::View* other) { return view == other; } | |
28 bool operator==(int group_id) { return view->GetGroup() == group_id; } | |
sadrul
2016/11/01 18:50:16
These operator overloads don't actually seem usefu
varkha
2016/11/02 02:03:21
Done. Used lambda instead as you have suggested.
| |
29 | |
30 views::View* view; | |
31 }; | |
32 | |
33 HeaderListLayout() : views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1) {} | |
34 ~HeaderListLayout() override {} | |
35 const std::vector<Header>& headers() const { return headers_; } | |
36 | |
37 void SetHeaderView(views::View* child) { | |
sadrul
2016/11/01 18:50:16
AddHeaderView
varkha
2016/11/02 02:03:21
Acknowledged.
| |
38 auto header = std::find(headers_.begin(), headers_.end(), child); | |
39 if (header == headers_.end()) | |
40 headers_.push_back(Header(child)); | |
41 } | |
42 | |
43 protected: | |
44 // views::BoxLayout: | |
45 void LayoutChildren(views::View* host, | |
46 const gfx::Rect& child_area, | |
47 int main_free_space, | |
48 int flex_sum, | |
49 int main_position, | |
50 int current_flex, | |
51 int* total_padding) override { | |
52 // Ensure that the header rows are stacked at the end of |host|'s children. | |
53 for (const auto& header : headers_) | |
54 host->ReorderChildView(header.view, -1); | |
sadrul
2016/11/01 18:50:16
Do you need to do this for all layout? Can this be
varkha
2016/11/02 02:03:21
Acknowledged.
| |
55 | |
56 auto headers(headers_); | |
57 int group_id = -1; | |
58 for (int i = 0, count = host->child_count(); i < count; ++i) { | |
59 views::View* child = host->child_at(i); | |
60 if (!child->visible() || | |
61 headers_.end() != | |
62 std::find(headers_.begin(), headers_.end(), child)) { | |
63 continue; | |
64 } | |
65 if (child->GetGroup() != group_id) { | |
66 // Find a header for the new group and place it above the group. | |
67 group_id = child->GetGroup(); | |
68 auto header = std::find(headers.begin(), headers.end(), group_id); | |
69 if (header != headers.end()) { | |
70 LayoutChild(header->view, child_area, main_free_space, flex_sum, | |
71 &main_position, ¤t_flex, total_padding); | |
72 headers.erase(header); | |
73 } | |
74 } | |
75 LayoutChild(child, child_area, main_free_space, flex_sum, &main_position, | |
76 ¤t_flex, total_padding); | |
77 } | |
78 // In the end layout unclaimed headers. | |
79 for (const auto& header : headers) { | |
80 LayoutChild(header.view, child_area, main_free_space, flex_sum, | |
81 &main_position, ¤t_flex, total_padding); | |
82 } | |
83 } | |
84 | |
85 void ViewRemoved(views::View* host, views::View* view) override { | |
86 views::BoxLayout::ViewRemoved(host, view); | |
87 auto header = std::find(headers_.begin(), headers_.end(), view); | |
88 if (header != headers_.end()) | |
89 headers_.erase(header); | |
90 } | |
91 | |
92 private: | |
93 // Header child views that stick to the top of visible viewport when scrolled. | |
94 std::vector<Header> headers_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(HeaderListLayout); | |
97 }; | |
98 | |
99 HeaderListScrollView::HeaderListScrollView() { | |
100 header_list_layout_ = new HeaderListLayout(); | |
101 SetLayoutManager(header_list_layout_); | |
102 } | |
103 HeaderListScrollView::~HeaderListScrollView() {} | |
104 | |
105 // Sets |view| as a sticky header view for all items with the same group. | |
106 void HeaderListScrollView::SetHeaderView(views::View* child) { | |
107 header_list_layout_->SetHeaderView(child); | |
108 } | |
109 | |
110 void HeaderListScrollView::Layout() { | |
111 views::View::Layout(); | |
112 headers_.clear(); | |
113 for (auto& header : header_list_layout_->headers()) | |
114 headers_.push_back(Header(header.view)); | |
115 ScrollChildren(); | |
116 } | |
117 | |
118 void HeaderListScrollView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
119 ScrollChildren(); | |
120 } | |
121 | |
122 void HeaderListScrollView::ViewHierarchyChanged( | |
123 const ViewHierarchyChangedDetails& details) { | |
124 if (!details.is_add && details.parent == this) { | |
125 auto header = std::find(headers_.begin(), headers_.end(), details.child); | |
126 if (header != headers_.end()) | |
127 headers_.erase(header); | |
128 } | |
129 } | |
130 | |
131 const char* HeaderListScrollView::GetClassName() const { | |
132 return "HeaderListScrollView"; | |
133 } | |
134 | |
135 // static | |
136 void HeaderListScrollView::ShowHeaderSticky(views::View* header, | |
137 bool show_sticky) { | |
138 if (show_sticky) { | |
139 header->SetBorder(views::Border::CreateSolidSidedBorder( | |
140 0, 0, kHeaderRowSeparatorThickness, 0, kHeaderRowSeparatorColor)); | |
141 } else { | |
142 header->SetBorder(views::Border::CreateSolidSidedBorder( | |
143 kHeaderRowSeparatorThickness, 0, 0, 0, kHeaderRowSeparatorColor)); | |
144 } | |
145 } | |
146 | |
147 // Adjusts y-position of header rows allowing one or two rows to stick to the | |
148 // top of the visible viewport. | |
149 void HeaderListScrollView::ScrollChildren() { | |
150 const int scroll_offset = -bounds().y(); | |
151 Header* previous_header = nullptr; | |
152 for (auto& header : headers_) { | |
153 gfx::Rect header_bounds = header.view->bounds(); | |
154 if (scroll_offset > header.offset) { | |
155 header_bounds.set_y(scroll_offset); | |
156 header.view->SetBoundsRect(header_bounds); | |
157 ShowHeaderSticky(header.view, true); | |
158 header.view->Layout(); | |
159 header.view->SchedulePaint(); | |
160 if (previous_header) { | |
161 header_bounds = previous_header->view->bounds(); | |
162 header_bounds.set_y(previous_header->offset); | |
163 previous_header->view->SetBoundsRect(header_bounds); | |
164 ShowHeaderSticky(previous_header->view, false); | |
165 } | |
166 previous_header = &header; | |
167 } else if (previous_header && | |
168 header_bounds.y() < previous_header->view->bounds().bottom()) { | |
169 gfx::Rect previous_header_bounds = previous_header->view->bounds(); | |
170 previous_header_bounds.set_y(header_bounds.y() - | |
171 previous_header->view->bounds().height()); | |
172 previous_header->view->SetBoundsRect(previous_header_bounds); | |
173 ShowHeaderSticky(previous_header->view, false); | |
174 ShowHeaderSticky(header.view, false); | |
175 } else { | |
176 ShowHeaderSticky(header.view, false); | |
177 } | |
178 } | |
179 } | |
180 | |
181 } // namespace ash | |
OLD | NEW |