Skip to content

Commit

Permalink
fix: Correctly calculate size when using Modifier.size on Paginated a…
Browse files Browse the repository at this point in the history
…nd Scrollable composables
  • Loading branch information
0ffz committed Aug 20, 2024
1 parent 1e63fa9 commit bb34982
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.mineinabyss.guiy.inventory.LocalGuiyOwner
import com.mineinabyss.guiy.modifiers.Modifier
import com.mineinabyss.guiy.modifiers.click.clickable
import com.mineinabyss.guiy.modifiers.fillMaxSize
import com.mineinabyss.guiy.modifiers.size
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
Expand All @@ -35,7 +36,7 @@ fun PaginatedMenu(player: Player) {
previousButton = { Item(Material.RED_CONCRETE, "Previous", modifier = Modifier.clickable { page-- }) },
nextButton = { Item(Material.BLUE_CONCRETE, "Next", modifier = Modifier.clickable { page++ }) },
) { pageItems ->
HorizontalGrid {
HorizontalGrid(Modifier.size(4, 5)) {
pageItems.forEach { item ->
Item(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun ScrollingMenu(player: Player) {
val owner = LocalGuiyOwner.current
Chest(
setOf(player),
"Pagination example",
"Scrolling example",
onClose = { owner.exit() },
modifier = Modifier.fillMaxSize()
) {
Expand All @@ -37,7 +37,7 @@ fun ScrollingMenu(player: Player) {
previousButton = { Item(Material.RED_CONCRETE, "Previous", modifier = Modifier.clickable { line-- }) },
nextButton = { Item(Material.BLUE_CONCRETE, "Next", modifier = Modifier.clickable { line++ }) },
) { pageItems ->
VerticalGrid {
VerticalGrid(Modifier.fillMaxSize()) {
pageItems.forEach { item ->
Item(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import com.mineinabyss.idofront.items.editItemMeta
import org.bukkit.Material
import org.bukkit.inventory.ItemStack

/**
* A paginated list of items, with buttons to go to the next and previous pages.
*
* Content must set a size or fillMaxSize Modifier to be visible.
*/
@Composable
fun <T> Paginated(
items: List<T>,
Expand Down Expand Up @@ -49,7 +54,7 @@ fun <T> Paginated(
}
},
content = {
Box(Modifier.fillMaxSize().onSizeChanged {
Box(Modifier.onSizeChanged {
size = it
}) {
content(pageItems)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ enum class ScrollDirection {
VERTICAL, HORIZONTAL;
}

/**
* A scrollable list of items, with buttons to go to the next and previous lines.
*
* Content must set a size or fillMaxSize Modifier to be visible.
*/
@Composable
fun <T> Scrollable(
items: List<T>,
Expand All @@ -35,7 +40,7 @@ fun <T> Scrollable(
val totalLines = if (scrollDirection == ScrollDirection.VERTICAL) size.height else size.width
Box(Modifier.fillMaxSize()) {
val start = line * itemsPerLine
val end = (line + 1) * itemsPerLine * totalLines
val end = start + (itemsPerLine * totalLines)
val pageItems = remember(start, end) {
if (start < 0) emptyList()
else items.subList(start, end.coerceAtMost(items.size))
Expand All @@ -51,7 +56,7 @@ fun <T> Scrollable(
}
},
content = {
Box(Modifier.fillMaxSize().onSizeChanged {
Box(Modifier.onSizeChanged {
size = it
}) {
content(pageItems)
Expand Down

0 comments on commit bb34982

Please sign in to comment.