-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
VerticalListFixedRowLayout.hx
139 lines (126 loc) · 3.72 KB
/
VerticalListFixedRowLayout.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Feathers
Copyright 2019 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.layout;
import feathers.core.IValidating;
import feathers.events.FeathersEvent;
import openfl.display.DisplayObject;
import openfl.events.Event;
import openfl.events.EventDispatcher;
/**
A simple list layout that positions items from top to bottom, in a single
column, where every item fills the entire width of the container.
@since 1.0.0
**/
class VerticalListFixedRowLayout extends EventDispatcher implements IScrollLayout {
public var scrollX(default, set):Float = 0.0;
private function set_scrollX(value:Float):Float {
if (this.scrollX == value) {
return this.scrollX;
}
FeathersEvent.dispatch(this, Event.CHANGE);
return this.scrollX;
}
public var scrollY(default, set):Float = 0.0;
private function set_scrollY(value:Float):Float {
if (this.scrollY == value) {
return this.scrollY;
}
FeathersEvent.dispatch(this, Event.CHANGE);
return this.scrollY;
}
/**
The height to set on each item, or `null`, if the row height should be
calculated automatically.
**/
public var rowHeight(default, set):Null<Float> = null;
private function set_rowHeight(value:Null<Float>):Null<Float> {
if (this.rowHeight == value) {
return this.rowHeight;
}
this.rowHeight = value;
FeathersEvent.dispatch(this, Event.CHANGE);
return this.rowHeight;
}
/**
The number of rows to render, if the height of the container has not
been set explicitly. If `null`, shows all rows.
**/
public var requestedRowCount(default, set):Null<Int> = 5;
private function set_requestedRowCount(value:Null<Int>):Null<Int> {
if (this.requestedRowCount == value) {
return this.requestedRowCount;
}
this.requestedRowCount = value;
FeathersEvent.dispatch(this, Event.CHANGE);
return this.requestedRowCount;
}
public function layout(items:Array<DisplayObject>, measurements:Measurements, ?result:LayoutBoundsResult):LayoutBoundsResult {
var viewPortWidth = measurements.width;
var maxWidth:Float = 0.0;
var maxHeight:Float = 0.0;
if (viewPortWidth == null || this.rowHeight == null) {
for (i in 0...items.length) {
var item = items[i];
if (Std.is(item, ILayoutObject)) {
if (!cast(item, ILayoutObject).includeInLayout) {
continue;
}
}
if (Std.is(item, IValidating)) {
cast(item, IValidating).validateNow();
}
if (viewPortWidth == null && item.width > maxWidth) {
maxWidth = item.width;
}
if (this.rowHeight == null && item.height > maxHeight) {
maxHeight = item.height;
}
}
}
var itemWidth = maxWidth;
if (viewPortWidth != null) {
itemWidth = viewPortWidth;
}
var itemHeight = maxHeight;
if (this.rowHeight != null) {
itemHeight = this.rowHeight;
}
var positionY = 0.0;
for (i in 0...items.length) {
var item = items[i];
if (Std.is(item, ILayoutObject)) {
if (!cast(item, ILayoutObject).includeInLayout) {
continue;
}
}
item.x = 0;
item.y = positionY;
item.width = itemWidth;
item.height = itemHeight;
positionY += itemHeight;
}
if (result == null) {
result = new LayoutBoundsResult();
}
result.contentWidth = itemWidth;
result.contentHeight = positionY;
if (viewPortWidth != null) {
result.viewPortWidth = viewPortWidth;
} else {
result.viewPortWidth = itemWidth;
}
var viewPortHeight = measurements.height;
if (viewPortHeight != null) {
result.viewPortHeight = viewPortHeight;
} else if (this.requestedRowCount != null) {
result.viewPortHeight = itemHeight * this.requestedRowCount;
} else {
result.viewPortHeight = positionY;
}
return result;
}
}