Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/main/kotlin/com/mineinabyss/guiy/components/Grid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.mineinabyss.guiy.layout.Layout
import com.mineinabyss.guiy.layout.MeasurePolicy
import com.mineinabyss.guiy.layout.MeasureResult
import com.mineinabyss.guiy.modifiers.Modifier
import kotlin.math.ceil

/**
* A grid layout component that finds the largest child size, and then places children in a grid based on this size.
Expand Down Expand Up @@ -47,22 +48,19 @@ fun gridMeasurePolicy(vertical: Boolean) = MeasurePolicy { measurables, constrai
constraints.minWidth,
constraints.minHeight
) {}

// Get width and height divisible by cellWidth, cellHeight
val itemsPerLine =
if (vertical) constraints.maxWidth / cellWidth
else constraints.maxHeight / cellHeight

val (width, height) = if (vertical) {
val w = itemsPerLine * cellWidth
val h = ((placeables.size / itemsPerLine) + 1) * cellHeight
val h = (ceil(placeables.size / itemsPerLine.toFloat()).toInt()) * cellHeight
w to h
} else {
val w = ((placeables.size / itemsPerLine) + 1) * cellWidth
val w = (ceil(placeables.size / itemsPerLine.toFloat()).toInt()) * cellWidth
val h = itemsPerLine * cellHeight
w to h
}

MeasureResult(width, height) {
var placeAtX = 0
var placeAtY = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mineinabyss.guiy.components.lists
import androidx.compose.runtime.*
import com.mineinabyss.guiy.components.Item
import com.mineinabyss.guiy.components.Spacer
import com.mineinabyss.guiy.components.VerticalGrid
import com.mineinabyss.guiy.jetpack.Alignment
import com.mineinabyss.guiy.jetpack.Arrangement
import com.mineinabyss.guiy.layout.Box
Expand Down Expand Up @@ -37,6 +38,7 @@ fun <T> Paginated(
content: @Composable (page: List<T>) -> Unit,
) {
var size by remember { mutableStateOf(Size(0, 0)) }
var clearSize by remember { mutableStateOf(Size(0, 0)) }
val itemsPerPage = size.width * size.height
val pageCount = if (itemsPerPage == 0) 1 else (-((-items.size).floorDiv(itemsPerPage))).coerceAtLeast(1)
val coercedPage = page.coerceIn(0, pageCount - 1)
Expand All @@ -47,6 +49,23 @@ fun <T> Paginated(
if (start < 0 || start >= items.size) emptyList()
else items.subList(start, end.coerceAtMost(items.size))
}

// Extract original size of contents
Box(Modifier.onSizeChanged{
size = it
}) {
content(pageItems)
}

// Clear out the previous Box
Box(Modifier.onSizeChanged{
clearSize = it
}.fillMaxSize()) {
VerticalGrid(){
MutableList(clearSize.width * clearSize.height) {Item(null)}
}
}

NavbarLayout(
position = navbarPosition,
navbar = {
Expand All @@ -62,9 +81,8 @@ fun <T> Paginated(
}
},
content = {
Box(Modifier.onSizeChanged {
size = it
}) {
// Actually render the correct amount of items into a box that can fit them including offsets
Box(Modifier.fillMaxSize()) {
content(pageItems)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.mineinabyss.guiy.components.lists

import androidx.compose.runtime.*
import com.mineinabyss.guiy.components.Item
import com.mineinabyss.guiy.components.Spacer
import com.mineinabyss.guiy.components.VerticalGrid
import com.mineinabyss.guiy.layout.Box
import com.mineinabyss.guiy.layout.Size
import com.mineinabyss.guiy.modifiers.*
import com.mineinabyss.guiy.modifiers.Modifier
import com.mineinabyss.guiy.modifiers.click.clickable
import com.mineinabyss.guiy.modifiers.fillMaxSize
Expand Down Expand Up @@ -38,38 +41,61 @@ fun <T> Scrollable(
content: @Composable (page: List<T>) -> Unit,
) {
var size by remember { mutableStateOf(Size(0, 0)) }
var clearSize by remember { mutableStateOf(Size(0, 0)) }

val itemsPerLine = if (scrollDirection == ScrollDirection.VERTICAL) size.width else size.height
val totalLines = if (scrollDirection == ScrollDirection.VERTICAL) size.height else size.width
val lineCount = if(itemsPerLine == 0) 1 else (-((-items.size).floorDiv(itemsPerLine))).coerceAtLeast(1)
val coercedLine = line.coerceIn(0, lineCount - 1)
Box(Modifier.fillMaxSize()) {
val start = coercedLine * itemsPerLine
val end = start + (itemsPerLine * totalLines)
val pageItems = remember(items, start, end) {
if (start < 0 || start >= items.size) emptyList()
else items.subList(start, end.coerceAtMost(items.size))
}
NavbarLayout(
position = navbarPosition,
navbar = {
NavbarButtons(navbarPosition, navbarBackground) {
if (coercedLine > 0) Box(Modifier.clickable { onLineChange(coercedLine - 1) }) {
previousButton()

Box(Modifier.fillMaxSize().onSizeChanged { println("Exterior box size: $it") }) {
val start = line * itemsPerLine
val lineCount = if (itemsPerLine == 0) 1 else (-((-items.size).floorDiv(itemsPerLine))).coerceAtLeast(1)
val coercedLine = line.coerceIn(0, lineCount - 1)
Box(Modifier.fillMaxSize()) {
val start = coercedLine * itemsPerLine
val end = start + (itemsPerLine * totalLines)
val pageItems = remember(items, start, end) {
if (start < 0 || start >= items.size) emptyList()
else items.subList(start, end.coerceAtMost(items.size))
}

// Extract original size of contents
Box(Modifier.onSizeChanged {
size = it
}) {
content(pageItems)
}

// Clear out the previous Box
Box(Modifier.onSizeChanged {
clearSize = it
}.fillMaxSize()) {
VerticalGrid() {
MutableList(clearSize.width * clearSize.height) { Item(null) }
}
}

NavbarLayout(
position = navbarPosition,
navbar = {
NavbarButtons(navbarPosition, navbarBackground) {
if (coercedLine > 0) Box(Modifier.clickable { onLineChange(coercedLine - 1) }) {
previousButton()
}
else Spacer(1, 1)
if (end < items.size) Box(Modifier.clickable { onLineChange(coercedLine + 1) }) {
nextButton()
}
else Spacer(1, 1)
}
else Spacer(1, 1)
if (end < items.size) Box(Modifier.clickable { onLineChange(coercedLine + 1) }) {
nextButton()
},
content = {
// Actually render the correct amount of items into a box that can fit them including offsets
Box(Modifier.fillMaxSize()) {
content(pageItems)
}
else Spacer(1, 1)
}
},
content = {
Box(Modifier.onSizeChanged {
size = it
}) {
content(pageItems)
}
}
)

)
}
}
}
}
Loading