OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/common/system/tray/tray_details_view.h" | 5 #include "ash/common/system/tray/tray_details_view.h" |
6 | 6 |
7 #include "ash/common/material_design/material_design_controller.h" | 7 #include "ash/common/material_design/material_design_controller.h" |
8 #include "ash/common/system/tray/fixed_sized_scroll_view.h" | 8 #include "ash/common/system/tray/fixed_sized_scroll_view.h" |
9 #include "ash/common/system/tray/system_tray.h" | 9 #include "ash/common/system/tray/system_tray.h" |
10 #include "ash/common/system/tray/system_tray_item.h" | 10 #include "ash/common/system/tray/system_tray_item.h" |
11 #include "ash/common/system/tray/tray_constants.h" | 11 #include "ash/common/system/tray/tray_constants.h" |
12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
13 #include "ui/views/background.h" | 13 #include "ui/views/background.h" |
14 #include "ui/views/border.h" | 14 #include "ui/views/border.h" |
15 #include "ui/views/controls/progress_bar.h" | 15 #include "ui/views/controls/progress_bar.h" |
16 #include "ui/views/controls/scroll_view.h" | 16 #include "ui/views/controls/scroll_view.h" |
17 #include "ui/views/controls/separator.h" | 17 #include "ui/views/controls/separator.h" |
18 #include "ui/views/layout/box_layout.h" | 18 #include "ui/views/layout/box_layout.h" |
19 #include "ui/views/view_targeter.h" | |
20 #include "ui/views/view_targeter_delegate.h" | |
19 | 21 |
20 namespace ash { | 22 namespace ash { |
21 namespace { | 23 namespace { |
22 | 24 |
25 const int kHeaderRowId = -1; | |
Evan Stade
2016/11/02 13:41:27
Should this be exposed in tray_details_view.h?
varkha
2016/11/02 22:42:34
I've moved it to tray_constants.h instead.
| |
26 const int kHeaderRowSeparatorThickness = 1; | |
27 const SkColor kHeaderRowSeparatorColor = SkColorSetA(SK_ColorBLACK, 0x1F); | |
28 | |
29 // A view that is used as ScrollView contents. It supports designating some of | |
30 // the children as sticky header rows. The sticky header rows are not scrolled | |
Evan Stade
2016/11/02 13:41:27
nit: until the next one "pushes" it up
varkha
2016/11/02 22:42:34
Done.
| |
31 // above the top of the visible viewport and are painted above other children. | |
32 // To indicate that a child is a sticky header row use set_id(kHeaderRowId). | |
33 class ScrollContentsView : public views::View, | |
34 public views::ViewTargeterDelegate { | |
35 public: | |
36 ScrollContentsView(ash::TrayDetailsView* tray_details_view) { | |
Evan Stade
2016/11/02 13:41:27
doesn't look like you need the param
varkha
2016/11/02 22:42:34
Yes, I've removed it but merging brought it back.
| |
37 SetEventTargeter(base::MakeUnique<views::ViewTargeter>(this)); | |
38 } | |
39 ~ScrollContentsView() override {} | |
40 | |
41 protected: | |
42 // views::View: | |
43 void OnBoundsChanged(const gfx::Rect& previous_bounds) override { | |
44 PositionHeaderRows(); | |
Evan Stade
2016/11/02 23:12:25
why not InvalidateLayout()?
varkha
2016/11/03 02:32:57
I thought this would be more expensive. Now I am o
| |
45 } | |
46 | |
47 void PaintChildren(const ui::PaintContext& context) override { | |
48 for (int i = 0; i < child_count(); ++i) { | |
49 if (child_at(i)->id() != kHeaderRowId && !child_at(i)->layer()) | |
50 child_at(i)->Paint(context); | |
51 } | |
52 // Paint header rows above other children in Z-order. | |
53 for (auto& header : headers_) { | |
54 if (!header.view->layer()) | |
55 header.view->Paint(context); | |
56 } | |
57 } | |
58 | |
59 void Layout() override { | |
60 views::View::Layout(); | |
61 headers_.clear(); | |
62 for (int i = 0; i < child_count(); ++i) { | |
63 views::View* view = child_at(i); | |
64 if (view->id() == kHeaderRowId) | |
65 headers_.push_back(Header(view)); | |
Evan Stade
2016/11/02 13:41:27
seems like you could/should keep this list up to d
varkha
2016/11/02 22:42:34
<Header> list is not just keeping the list of head
| |
66 } | |
67 PositionHeaderRows(); | |
68 } | |
69 | |
70 void ViewHierarchyChanged( | |
71 const ViewHierarchyChangedDetails& details) override { | |
72 if (!details.is_add && details.parent == this) { | |
73 auto header_it = std::find_if(headers_.begin(), headers_.end(), | |
74 [details](const Header& header) { | |
75 return header.view == details.child; | |
76 }); | |
77 if (header_it != headers_.end()) | |
78 headers_.erase(header_it); | |
Evan Stade
2016/11/02 13:41:27
isn't there a std::remove_if?
varkha
2016/11/02 22:42:34
Yes, effectively generalizing this for the case wh
| |
79 } | |
80 } | |
81 | |
82 View* TargetForRect(View* root, const gfx::Rect& rect) override { | |
83 // Give header rows first dibs on events. | |
84 for (auto& header : headers_) { | |
85 views::View* view = header.view; | |
86 gfx::Rect local_to_header = rect; | |
87 local_to_header.Offset(-view->x(), -view->y()); | |
88 if (ViewTargeterDelegate::DoesIntersectRect(view, local_to_header)) | |
89 return ViewTargeterDelegate::TargetForRect(view, local_to_header); | |
90 } | |
91 return ViewTargeterDelegate::TargetForRect(root, rect); | |
92 } | |
93 | |
94 private: | |
95 class Header { | |
Evan Stade
2016/11/02 13:41:27
struct? not sure what this is buying us either way
varkha
2016/11/02 22:42:34
I need this to have an offset saved in Layout and
Evan Stade
2016/11/02 23:12:25
this is not at all obvious from the code. This war
varkha
2016/11/03 02:32:57
Done. I've also moved a bit more into this so this
| |
96 public: | |
97 Header(views::View* header) : view(header), offset(header->bounds().y()) {} | |
Evan Stade
2016/11/02 13:41:27
you can just do header->y()
varkha
2016/11/02 22:42:34
Done.
| |
98 | |
99 views::View* view; | |
100 int offset; | |
101 }; | |
102 | |
103 // Sets decorations on a header row to indicate whether it is sticky. | |
104 static void ShowHeaderSticky(views::View* header, bool show_sticky) { | |
Evan Stade
2016/11/02 13:41:27
nit: s/ShowHeaderSticky/DecorateAsSticky/
or SetS
varkha
2016/11/02 22:42:34
I like your first suggestion the most. Done.
| |
105 if (show_sticky) { | |
106 header->SetBorder(views::Border::CreateSolidSidedBorder( | |
107 0, 0, kHeaderRowSeparatorThickness, 0, kHeaderRowSeparatorColor)); | |
108 } else { | |
109 header->SetBorder(views::Border::CreateSolidSidedBorder( | |
110 kHeaderRowSeparatorThickness, 0, 0, 0, kHeaderRowSeparatorColor)); | |
111 } | |
112 } | |
113 | |
114 // Adjusts y-position of header rows allowing one or two rows to stick to the | |
115 // top of the visible viewport. | |
116 void PositionHeaderRows() { | |
117 const int scroll_offset = -bounds().y(); | |
118 Header* previous_header = nullptr; | |
119 for (auto& header : headers_) { | |
Evan Stade
2016/11/02 23:12:25
I feel like this loop could be simpler if you iter
varkha
2016/11/03 02:32:57
Done (although I am still not sure how you would e
| |
120 gfx::Rect header_bounds = header.view->bounds(); | |
121 if (scroll_offset > header.offset) { | |
122 header_bounds.set_y(scroll_offset); | |
123 header.view->SetBoundsRect(header_bounds); | |
124 ShowHeaderSticky(header.view, true); | |
125 header.view->Layout(); | |
126 header.view->SchedulePaint(); | |
127 if (previous_header) { | |
128 header_bounds = previous_header->view->bounds(); | |
Evan Stade
2016/11/02 23:12:25
I don't see the purpose in reusing header_bounds h
varkha
2016/11/03 02:32:57
Done.
| |
129 header_bounds.set_y(previous_header->offset); | |
130 previous_header->view->SetBoundsRect(header_bounds); | |
Evan Stade
2016/11/02 23:12:25
this stanza looks equivalent to
previous_header
varkha
2016/11/03 02:32:57
Done. Here and elsewhere.
| |
131 ShowHeaderSticky(previous_header->view, false); | |
132 } | |
133 previous_header = &header; | |
134 } else if (previous_header && | |
135 header_bounds.y() < previous_header->view->bounds().bottom()) { | |
136 gfx::Rect previous_header_bounds = previous_header->view->bounds(); | |
137 previous_header_bounds.set_y(header_bounds.y() - | |
138 previous_header->view->bounds().height()); | |
139 previous_header->view->SetBoundsRect(previous_header_bounds); | |
140 ShowHeaderSticky(previous_header->view, false); | |
141 ShowHeaderSticky(header.view, false); | |
142 } else { | |
143 ShowHeaderSticky(header.view, false); | |
144 } | |
145 } | |
146 } | |
147 | |
148 // Header child views that stick to the top of visible viewport when scrolled. | |
149 std::vector<Header> headers_; | |
150 | |
151 DISALLOW_COPY_AND_ASSIGN(ScrollContentsView); | |
152 }; | |
153 | |
23 // Constants for the title row in material design. | 154 // Constants for the title row in material design. |
24 const int kTitleRowVerticalPadding = 4; | 155 const int kTitleRowVerticalPadding = 4; |
25 const int kTitleRowSeparatorBorderHeight = 1; | 156 const int kTitleRowSeparatorBorderHeight = 1; |
26 const int kTitleRowProgressBarHeight = 2; | 157 const int kTitleRowProgressBarHeight = 2; |
27 // The separator's height should be same as kTitleRowProgressBarHeight, and | 158 // The separator's height should be same as kTitleRowProgressBarHeight, and |
28 // should not be larger than kTitleRowSeparatorBorderHeight. | 159 // should not be larger than kTitleRowSeparatorBorderHeight. |
29 const int kTitleRowSeparatorHeight = kTitleRowProgressBarHeight; | 160 const int kTitleRowSeparatorHeight = kTitleRowProgressBarHeight; |
30 const int kTitleRowPaddingTop = kTitleRowVerticalPadding; | 161 const int kTitleRowPaddingTop = kTitleRowVerticalPadding; |
31 const int kTitleRowPaddingBottom = | 162 const int kTitleRowPaddingBottom = |
32 kTitleRowVerticalPadding - kTitleRowSeparatorHeight; | 163 kTitleRowVerticalPadding - kTitleRowSeparatorHeight; |
(...skipping 25 matching lines...) Expand all Loading... | |
58 | 189 |
59 private: | 190 private: |
60 int GetMaxHeight(const views::View* host) const { | 191 int GetMaxHeight(const views::View* host) const { |
61 int max_height = 0; | 192 int max_height = 0; |
62 for (int i = 0; i < host->child_count(); ++i) | 193 for (int i = 0; i < host->child_count(); ++i) |
63 max_height = std::max(max_height, host->child_at(i)->height()); | 194 max_height = std::max(max_height, host->child_at(i)->height()); |
64 return max_height; | 195 return max_height; |
65 } | 196 } |
66 }; | 197 }; |
67 | 198 |
68 } // namespace | |
69 | |
70 class ScrollSeparator : public views::View { | 199 class ScrollSeparator : public views::View { |
71 public: | 200 public: |
72 ScrollSeparator() {} | 201 ScrollSeparator() {} |
73 | 202 |
74 ~ScrollSeparator() override {} | 203 ~ScrollSeparator() override {} |
75 | 204 |
76 private: | 205 private: |
77 // Overriden from views::View. | 206 // views::View: |
78 void OnPaint(gfx::Canvas* canvas) override { | 207 void OnPaint(gfx::Canvas* canvas) override { |
79 canvas->FillRect(gfx::Rect(0, height() / 2, width(), 1), kBorderLightColor); | 208 canvas->FillRect(gfx::Rect(0, height() / 2, width(), 1), |
209 ash::kBorderLightColor); | |
Evan Stade
2016/11/02 13:41:27
aren't we already in ash
varkha
2016/11/02 22:42:34
Done.
| |
80 } | 210 } |
81 gfx::Size GetPreferredSize() const override { | 211 gfx::Size GetPreferredSize() const override { |
82 return gfx::Size(1, kTrayPopupScrollSeparatorHeight); | 212 return gfx::Size(1, ash::kTrayPopupScrollSeparatorHeight); |
83 } | 213 } |
84 | 214 |
85 DISALLOW_COPY_AND_ASSIGN(ScrollSeparator); | 215 DISALLOW_COPY_AND_ASSIGN(ScrollSeparator); |
86 }; | 216 }; |
87 | 217 |
218 } // namespace | |
219 | |
88 class ScrollBorder : public views::Border { | 220 class ScrollBorder : public views::Border { |
89 public: | 221 public: |
90 ScrollBorder() {} | 222 ScrollBorder() {} |
91 ~ScrollBorder() override {} | 223 ~ScrollBorder() override {} |
92 | 224 |
93 void set_visible(bool visible) { visible_ = visible; } | 225 void set_visible(bool visible) { visible_ = visible; } |
94 | 226 |
95 private: | 227 private: |
96 // Overridden from views::Border. | 228 // views::Border: |
97 void Paint(const views::View& view, gfx::Canvas* canvas) override { | 229 void Paint(const views::View& view, gfx::Canvas* canvas) override { |
98 if (!visible_) | 230 if (!visible_) |
99 return; | 231 return; |
100 canvas->FillRect(gfx::Rect(0, view.height() - 1, view.width(), 1), | 232 canvas->FillRect(gfx::Rect(0, view.height() - 1, view.width(), 1), |
101 kBorderLightColor); | 233 kBorderLightColor); |
102 } | 234 } |
103 | 235 |
104 gfx::Insets GetInsets() const override { return gfx::Insets(0, 0, 1, 0); } | 236 gfx::Insets GetInsets() const override { return gfx::Insets(0, 0, 1, 0); } |
105 | 237 |
106 gfx::Size GetMinimumSize() const override { return gfx::Size(0, 1); } | 238 gfx::Size GetMinimumSize() const override { return gfx::Size(0, 1); } |
107 | 239 |
108 bool visible_; | 240 bool visible_ = false; |
109 | 241 |
110 DISALLOW_COPY_AND_ASSIGN(ScrollBorder); | 242 DISALLOW_COPY_AND_ASSIGN(ScrollBorder); |
111 }; | 243 }; |
112 | 244 |
113 TrayDetailsView::TrayDetailsView(SystemTrayItem* owner) | 245 TrayDetailsView::TrayDetailsView(SystemTrayItem* owner) |
114 : owner_(owner), | 246 : owner_(owner), |
115 title_row_(nullptr), | 247 title_row_(nullptr), |
116 scroller_(nullptr), | 248 scroller_(nullptr), |
117 scroll_content_(nullptr), | 249 scroll_content_(nullptr), |
118 progress_bar_(nullptr), | 250 progress_bar_(nullptr), |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 CreateExtraTitleRowButtons(); | 306 CreateExtraTitleRowButtons(); |
175 | 307 |
176 if (MaterialDesignController::IsSystemTrayMenuMaterial()) | 308 if (MaterialDesignController::IsSystemTrayMenuMaterial()) |
177 back_button_ = title_row_->AddBackButton(this); | 309 back_button_ = title_row_->AddBackButton(this); |
178 | 310 |
179 Layout(); | 311 Layout(); |
180 } | 312 } |
181 | 313 |
182 void TrayDetailsView::CreateScrollableList() { | 314 void TrayDetailsView::CreateScrollableList() { |
183 DCHECK(!scroller_); | 315 DCHECK(!scroller_); |
184 scroll_content_ = new views::View; | 316 scroll_content_ = new ScrollContentsView(this); |
185 scroll_content_->SetLayoutManager( | 317 scroll_content_->SetLayoutManager( |
Evan Stade
2016/11/02 13:41:27
perhaps this belongs in ScrollContentsView's ctor
varkha
2016/11/02 22:42:34
Done.
| |
186 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1)); | 318 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1)); |
187 scroller_ = new FixedSizedScrollView; | 319 scroller_ = new FixedSizedScrollView; |
188 scroller_->SetContentsView(scroll_content_); | 320 scroller_->SetContentsView(scroll_content_); |
189 | 321 |
190 // Note: |scroller_| takes ownership of |scroll_border_|. | 322 // Note: |scroller_| takes ownership of |scroll_border_|. |
191 scroll_border_ = new ScrollBorder; | 323 scroll_border_ = new ScrollBorder; |
192 scroller_->SetBorder(std::unique_ptr<views::Border>(scroll_border_)); | 324 scroller_->SetBorder(std::unique_ptr<views::Border>(scroll_border_)); |
193 | 325 |
194 AddChildView(scroller_); | 326 AddChildView(scroller_); |
195 } | 327 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 if (index < child_count() - 1 && child_at(index + 1) != title_row_) | 411 if (index < child_count() - 1 && child_at(index + 1) != title_row_) |
280 scroll_border_->set_visible(true); | 412 scroll_border_->set_visible(true); |
281 else | 413 else |
282 scroll_border_->set_visible(false); | 414 scroll_border_->set_visible(false); |
283 } | 415 } |
284 | 416 |
285 views::View::OnPaintBorder(canvas); | 417 views::View::OnPaintBorder(canvas); |
286 } | 418 } |
287 | 419 |
288 } // namespace ash | 420 } // namespace ash |
OLD | NEW |