Skip to content

Commit 06a9ba8

Browse files
authored
Add an IntConsumer to the PagedWidget so an external action can be called when a page is changed (#161)
1 parent f61dc8e commit 06a9ba8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/main/java/com/cleanroommc/modularui/widgets/PagedWidget.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,35 @@
44
import com.cleanroommc.modularui.widget.Widget;
55

66
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
78
import org.jetbrains.annotations.Unmodifiable;
89

910
import java.util.ArrayList;
1011
import java.util.List;
12+
import java.util.function.IntConsumer;
1113

1214
public class PagedWidget<W extends PagedWidget<W>> extends Widget<W> {
1315

1416
private final List<IWidget> pages = new ArrayList<>();
1517
private IWidget currentPage;
1618
private int currentPageIndex = 0;
19+
@Nullable
20+
private IntConsumer onPageChange;
1721

1822
@Override
1923
public void afterInit() {
2024
setPage(0);
2125
}
2226

27+
/**
28+
* Set a consumer that is accepted <b>right after</b> the page is actually changed and the next page widget is enabled. <br/>
29+
* Will also be called with {@code 0} when after this widget is initialized.
30+
*/
31+
public W onPageChange(@Nullable IntConsumer onPageChange) {
32+
this.onPageChange = onPageChange;
33+
return getThis();
34+
}
35+
2336
public void setPage(int page) {
2437
if (page < 0 || page >= this.pages.size()) {
2538
throw new IndexOutOfBoundsException();
@@ -30,6 +43,10 @@ public void setPage(int page) {
3043
}
3144
this.currentPage = this.pages.get(this.currentPageIndex);
3245
this.currentPage.setEnabled(true);
46+
47+
if (this.onPageChange != null) {
48+
this.onPageChange.accept(page);
49+
}
3350
}
3451

3552
public void nextPage() {

0 commit comments

Comments
 (0)