Skip to content

Commit 533164f

Browse files
authored
Allow skipping list updates (#88)
1 parent 8bc8bb6 commit 533164f

File tree

4 files changed

+86
-9
lines changed

4 files changed

+86
-9
lines changed

src/main/java/com/ldtteam/blockui/mod/ScrollingListsGui.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
import java.util.ArrayList;
1515
import java.util.List;
16+
import java.util.UUID;
17+
import java.util.concurrent.atomic.AtomicBoolean;
1618
import java.util.concurrent.atomic.AtomicInteger;
1719

1820
/**
@@ -127,5 +129,32 @@ public void updateElement(final int index, final Pane rowPane)
127129

128130
window.findPaneOfTypeByID("list4add", Button.class).setHandler(button -> renderAmount.getAndAdd(2));
129131
window.findPaneOfTypeByID("list4remove", Button.class).setHandler(button -> renderAmount.getAndAdd(-2));
132+
133+
// Case 5: A list that will not update
134+
final AtomicBoolean shouldRenderFlag = new AtomicBoolean();
135+
final ScrollingList list5 = window.findPaneOfTypeByID("list5", ScrollingList.class);
136+
list5.setDataProvider(new DataProvider()
137+
{
138+
@Override
139+
public int getElementCount()
140+
{
141+
return 10;
142+
}
143+
144+
@Override
145+
public boolean shouldUpdate()
146+
{
147+
return shouldRenderFlag.get();
148+
}
149+
150+
@Override
151+
public void updateElement(final int index, final Pane rowPane)
152+
{
153+
shouldRenderFlag.set(false);
154+
rowPane.findPaneByType(Text.class).setText(Component.literal("Hi " + index + " " + UUID.randomUUID()));
155+
}
156+
});
157+
158+
window.findPaneOfTypeByID("list5update", Button.class).setHandler(button -> shouldRenderFlag.set(true));
130159
}
131160
}

src/main/java/com/ldtteam/blockui/views/ScrollingList.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,32 @@ public void updateElement(final int index, final Pane rowPane)
121121
public void setDataProvider(final DataProvider p)
122122
{
123123
dataProvider = p;
124-
refreshElementPanes();
124+
refreshElementPanes(true);
125125
}
126126

127127
/**
128128
* Use the data provider to update all the element panes.
129129
*/
130130
public void refreshElementPanes()
131131
{
132-
((ScrollingListContainer) container).refreshElementPanes(dataProvider, maxHeight, childSpacing);
132+
refreshElementPanes(true);
133+
}
134+
135+
/**
136+
* Use the data provider to update all the element panes.
137+
*
138+
* @param force should the list be forcefully updated.
139+
*/
140+
public void refreshElementPanes(final boolean force)
141+
{
142+
((ScrollingListContainer) container).refreshElementPanes(dataProvider, maxHeight, childSpacing, force);
133143
}
134144

135145
@Override
136146
public void onUpdate()
137147
{
138148
super.onUpdate();
139-
refreshElementPanes();
149+
refreshElementPanes(false);
140150
}
141151

142152
@Override
@@ -185,13 +195,33 @@ public interface DataProvider
185195
*/
186196
int getElementCount();
187197

198+
/**
199+
* Should all the children update again?
200+
*
201+
* @return true if the updates should be made
202+
*/
203+
default boolean shouldUpdate()
204+
{
205+
return true;
206+
}
207+
208+
/**
209+
* Should the specific child update again?
210+
*
211+
* @return true if the updates should be made
212+
*/
213+
default boolean shouldUpdate(final int index)
214+
{
215+
return true;
216+
}
217+
188218
/**
189219
* Override this to pick a custom size for this element. Event contains the logic to modify the old size.
190220
*
191221
* @param index the index of the row/list element.
192222
* @param modifier the object used to modify the size.
193223
*/
194-
default void modifyRowSize(int index, final RowSizeModifier modifier)
224+
default void modifyRowSize(final int index, final RowSizeModifier modifier)
195225
{
196226
// No implementation by default
197227
}
@@ -214,6 +244,6 @@ public interface IPaneUpdater
214244
* @param index The index to update.
215245
* @param rowPane The pane to fill.
216246
*/
217-
void apply(int index, Pane rowPane);
247+
void apply(final int index, final Pane rowPane);
218248
}
219249
}

src/main/java/com/ldtteam/blockui/views/ScrollingListContainer.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,15 @@ public void setListNodeParams(final @NotNull PaneParams listNodeParams)
9696
* @param dataProvider data provider object, shouldn't be null.
9797
* @param height the maximum height of the parent.
9898
* @param childSpacing the spacing between each row.
99+
* @param force should the list be forcefully updated.
99100
*/
100-
public void refreshElementPanes(final DataProvider dataProvider, final int height, final int childSpacing)
101+
public void refreshElementPanes(final DataProvider dataProvider, final int height, final int childSpacing, final boolean force)
101102
{
103+
if (dataProvider == null)
104+
{
105+
return;
106+
}
107+
102108
int currentYpos = 0;
103109

104110
if (listNodeParams == null)
@@ -107,12 +113,17 @@ public void refreshElementPanes(final DataProvider dataProvider, final int heigh
107113
return;
108114
}
109115

116+
if (!force && !dataProvider.shouldUpdate())
117+
{
118+
return;
119+
}
120+
110121
if (this.width != emptyTextComponent.getWidth() || this.height != emptyTextComponent.getHeight())
111122
{
112123
emptyTextComponent.setSize(this.width, this.height);
113124
}
114125

115-
final int numElements = (dataProvider != null) ? dataProvider.getElementCount() : 0;
126+
final int numElements = dataProvider.getElementCount();
116127
if (numElements > 0)
117128
{
118129
if (emptyTextComponent.getParent() != null)
@@ -150,7 +161,10 @@ public void refreshElementPanes(final DataProvider dataProvider, final int heigh
150161
child.setSize(modifier.width, modifier.height);
151162
}
152163

153-
dataProvider.updateElement(i, child);
164+
if (force || dataProvider.shouldUpdate(i))
165+
{
166+
dataProvider.updateElement(i, child);
167+
}
154168
}
155169

156170
currentYpos += elementHeight + childSpacing;

src/main/resources/assets/blockui/gui/test4.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<window xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="block_ui.xsd" size="640 240" pause="true" lightbox="true">
1+
<window xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="block_ui.xsd" size="640 480" type="FIXED_VANILLA" pause="true" lightbox="true">
22
<list id="list1" size="160 240">
33
<text size="152 20"/>
44
</list>
@@ -21,4 +21,8 @@
2121
</list>
2222
<button id="list4add" pos="480 200" size="160 20" label="Add item"/>
2323
<button id="list4remove" pos="480 220" size="160 20" label="Remove item"/>
24+
<list id="list5" size="160 200" pos="0 240" emptytext="This list is empty" emptyscale="0.8" emptycolor="white">
25+
<label size="152 20"/>
26+
</list>
27+
<button id="list5update" pos="0 440" size="160 20" label="Update list"/>
2428
</window>

0 commit comments

Comments
 (0)