Skip to content

Commit bb34982

Browse files
committed
fix: Correctly calculate size when using Modifier.size on Paginated and Scrollable composables
1 parent 1e63fa9 commit bb34982

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/gui/PaginatedMenu.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.mineinabyss.guiy.inventory.LocalGuiyOwner
1010
import com.mineinabyss.guiy.modifiers.Modifier
1111
import com.mineinabyss.guiy.modifiers.click.clickable
1212
import com.mineinabyss.guiy.modifiers.fillMaxSize
13+
import com.mineinabyss.guiy.modifiers.size
1314
import org.bukkit.Material
1415
import org.bukkit.entity.Player
1516
import org.bukkit.inventory.ItemStack
@@ -35,7 +36,7 @@ fun PaginatedMenu(player: Player) {
3536
previousButton = { Item(Material.RED_CONCRETE, "Previous", modifier = Modifier.clickable { page-- }) },
3637
nextButton = { Item(Material.BLUE_CONCRETE, "Next", modifier = Modifier.clickable { page++ }) },
3738
) { pageItems ->
38-
HorizontalGrid {
39+
HorizontalGrid(Modifier.size(4, 5)) {
3940
pageItems.forEach { item ->
4041
Item(item)
4142
}

guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/gui/ScrollingMenu.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fun ScrollingMenu(player: Player) {
2020
val owner = LocalGuiyOwner.current
2121
Chest(
2222
setOf(player),
23-
"Pagination example",
23+
"Scrolling example",
2424
onClose = { owner.exit() },
2525
modifier = Modifier.fillMaxSize()
2626
) {
@@ -37,7 +37,7 @@ fun ScrollingMenu(player: Player) {
3737
previousButton = { Item(Material.RED_CONCRETE, "Previous", modifier = Modifier.clickable { line-- }) },
3838
nextButton = { Item(Material.BLUE_CONCRETE, "Next", modifier = Modifier.clickable { line++ }) },
3939
) { pageItems ->
40-
VerticalGrid {
40+
VerticalGrid(Modifier.fillMaxSize()) {
4141
pageItems.forEach { item ->
4242
Item(item)
4343
}

src/main/kotlin/com/mineinabyss/guiy/components/lists/Paginated.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import com.mineinabyss.idofront.items.editItemMeta
1515
import org.bukkit.Material
1616
import org.bukkit.inventory.ItemStack
1717

18+
/**
19+
* A paginated list of items, with buttons to go to the next and previous pages.
20+
*
21+
* Content must set a size or fillMaxSize Modifier to be visible.
22+
*/
1823
@Composable
1924
fun <T> Paginated(
2025
items: List<T>,
@@ -49,7 +54,7 @@ fun <T> Paginated(
4954
}
5055
},
5156
content = {
52-
Box(Modifier.fillMaxSize().onSizeChanged {
57+
Box(Modifier.onSizeChanged {
5358
size = it
5459
}) {
5560
content(pageItems)

src/main/kotlin/com/mineinabyss/guiy/components/lists/Scrollable.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ enum class ScrollDirection {
1515
VERTICAL, HORIZONTAL;
1616
}
1717

18+
/**
19+
* A scrollable list of items, with buttons to go to the next and previous lines.
20+
*
21+
* Content must set a size or fillMaxSize Modifier to be visible.
22+
*/
1823
@Composable
1924
fun <T> Scrollable(
2025
items: List<T>,
@@ -35,7 +40,7 @@ fun <T> Scrollable(
3540
val totalLines = if (scrollDirection == ScrollDirection.VERTICAL) size.height else size.width
3641
Box(Modifier.fillMaxSize()) {
3742
val start = line * itemsPerLine
38-
val end = (line + 1) * itemsPerLine * totalLines
43+
val end = start + (itemsPerLine * totalLines)
3944
val pageItems = remember(start, end) {
4045
if (start < 0) emptyList()
4146
else items.subList(start, end.coerceAtMost(items.size))
@@ -51,7 +56,7 @@ fun <T> Scrollable(
5156
}
5257
},
5358
content = {
54-
Box(Modifier.fillMaxSize().onSizeChanged {
59+
Box(Modifier.onSizeChanged {
5560
size = it
5661
}) {
5762
content(pageItems)

0 commit comments

Comments
 (0)