-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/paginated dialog #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: version/1.21.10
Are you sure you want to change the base?
Changes from all commits
bf19e35
917e75b
9235c4d
7c5d9c1
d025851
0ce0cbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,303 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @file:Suppress("UnstableApiUsage") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package dev.slne.surf.surfapi.bukkit.api.dialog.builder | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import dev.slne.surf.surfapi.bukkit.api.dialog.base | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import dev.slne.surf.surfapi.bukkit.api.dialog.dialog | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import dev.slne.surf.surfapi.bukkit.api.dialog.type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import dev.slne.surf.surfapi.core.api.messages.adventure.buildText | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import dev.slne.surf.surfapi.core.api.messages.adventure.text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import io.papermc.paper.registry.data.dialog.ActionButton | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import net.kyori.adventure.text.Component | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private const val PAGINATION_BUTTON_DEFAULT_WIDTH = 50 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fun <T> PaginatedDialog( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| block: DialogPaginationBuilder<T>.() -> Unit, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) = DialogPaginationBuilder<T>().apply(block).build() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fun <T> paginatedDialog( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| block: DialogPaginationBuilder<T>.() -> Unit, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) = PaginatedDialog(block) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fun interface DialogPageAction { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fun newPage(currentPage: Int, maxPages: Int): Int | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private enum class DialogPaginationBaseAction( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val label: (Int, Int) -> Component, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val tooltip: (Int, Int) -> Component, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val pageAction: DialogPageAction, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FIRST( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label = { _, _ -> text("«") }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tooltip = { _, _ -> buildText { info("Erste Seite") } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pageAction = { _, _ -> 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BACK( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label = { _, _ -> text("‹") }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tooltip = { _, _ -> buildText { info("Vorherige Seite") } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pageAction = { currentPage, _ -> (currentPage - 1).coerceAtLeast(0) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CURRENT( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label = { currentPage, maxPages -> text("${currentPage + 1} / $maxPages") }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tooltip = { currentPage, maxPages -> buildText { info("Seite ${currentPage + 1} von $maxPages") } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pageAction = { currentPage, _ -> currentPage }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NEXT( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label = { _, _ -> text("›") }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tooltip = { _, _ -> buildText { info("Nächste Seite") } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pageAction = { currentPage, maxPages -> (currentPage + 1).coerceAtMost(maxPages - 1) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LAST( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label = { _, _ -> text("»") }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tooltip = { _, _ -> buildText { info("Letzte Seite") } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pageAction = { _, maxPages -> maxPages - 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fun actionButton( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentPage: Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| maxPages: Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width: Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) = actionButton { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label(this@DialogPaginationBaseAction.label(currentPage, maxPages)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tooltip(this@DialogPaginationBaseAction.tooltip(currentPage, maxPages)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width(width) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class DialogPaginationBuilder<T> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var base: (DialogBaseBuilder.() -> Unit)? = null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var exitAction: ActionButton? = null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var buttonBuilder: (DialogActionButtonBuilder.(T) -> Unit)? = null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private val elements = ObjectLinkedOpenHashSet<T>() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var elementsPerPage = 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private val currentPageElements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get() = elements.drop(currentPage * elementsPerPage).take(elementsPerPage) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var paginationButtonWidth = PAGINATION_BUTTON_DEFAULT_WIDTH | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Suppress("SuspiciousVarProperty") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var minElementButtonWidth = 200 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private fun calculateElementButtonWidth(): Int { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var width = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hasPreviousPage) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width += paginationButtonWidth * 2 // first + back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hasNextPage) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width += paginationButtonWidth * 2 // next + last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width += paginationButtonWidth // current page button | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return width.coerceAtLeast(minElementButtonWidth) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var currentPage = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val maxPages: Int | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get() = (elements.size + elementsPerPage - 1) / elementsPerPage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val hasNextPage: Boolean | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get() = currentPage < maxPages - 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val hasPreviousPage: Boolean | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get() = currentPage > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var firstPageButton = DialogPaginationBaseAction.FIRST.actionButton( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentPage, maxPages, paginationButtonWidth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var backButton = DialogPaginationBaseAction.BACK.actionButton( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentPage, maxPages, paginationButtonWidth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var currentPageButton = DialogPaginationBaseAction.CURRENT.actionButton( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentPage, maxPages, paginationButtonWidth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var nextButton = DialogPaginationBaseAction.NEXT.actionButton( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentPage, maxPages, paginationButtonWidth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var lastPageButton = DialogPaginationBaseAction.LAST.actionButton( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+114
to
+130
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private var firstPageButton = DialogPaginationBaseAction.FIRST.actionButton( | |
| currentPage, maxPages, paginationButtonWidth | |
| ) | |
| private var backButton = DialogPaginationBaseAction.BACK.actionButton( | |
| currentPage, maxPages, paginationButtonWidth | |
| ) | |
| private var currentPageButton = DialogPaginationBaseAction.CURRENT.actionButton( | |
| currentPage, maxPages, paginationButtonWidth | |
| ) | |
| private var nextButton = DialogPaginationBaseAction.NEXT.actionButton( | |
| currentPage, maxPages, paginationButtonWidth | |
| ) | |
| private var lastPageButton = DialogPaginationBaseAction.LAST.actionButton( | |
| private val firstPageButton get() = DialogPaginationBaseAction.FIRST.actionButton( | |
| currentPage, maxPages, paginationButtonWidth | |
| ) | |
| private val backButton get() = DialogPaginationBaseAction.BACK.actionButton( | |
| currentPage, maxPages, paginationButtonWidth | |
| ) | |
| private val currentPageButton get() = DialogPaginationBaseAction.CURRENT.actionButton( | |
| currentPage, maxPages, paginationButtonWidth | |
| ) | |
| private val nextButton get() = DialogPaginationBaseAction.NEXT.actionButton( | |
| currentPage, maxPages, paginationButtonWidth | |
| ) | |
| private val lastPageButton get() = DialogPaginationBaseAction.LAST.actionButton( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function returns a calculated width but this value is never used to update the button widths. The variable
widthaccumulates pagination button widths but doesn't account for the actual element button width calculation logic.