Skip to content

Commit

Permalink
Small changes to fix ui refreshes that go missing
Browse files Browse the repository at this point in the history
  • Loading branch information
smilefx committed Jan 28, 2025
1 parent 6bdb21e commit edb0319
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
8 changes: 5 additions & 3 deletions bgw-gui/src/jsMain/kotlin/tools/aqua/bgw/elements/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ internal val App =
width = 100.cqw
height = (100.0 / props.data.width * props.data.height).cqw
position = Position.relative
backgroundColor = rgb(0, 0, 0, 1.0)
backgroundColor = rgb(0, 0, 0, 0.0)
overflow = Overflow.hidden
display = Display.block
}
Expand Down Expand Up @@ -517,13 +517,14 @@ internal val App =
zIndex = zIndex(1000)
}

if (menuScene != null) {
if (menuScene != undefined) {
ariaExpanded = true

+SceneBuilder.build(menuScene)
}
}
if (menuScene != null) {

if (menuScene != undefined) {
bgwBlur {
css {
position = Position.absolute
Expand All @@ -535,6 +536,7 @@ internal val App =
}
}
}

val gameScene = props.data.gameScene
bgwGameScene {
css { position = Position.absolute }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ internal fun Application.configureRouting() {
}
}

internal val messageQueue = mutableListOf<ActionProp>()
private val messageQueueMutex = Mutex()

internal fun CoroutineScope.launchPeriodicAsync(repeatMillis: Long, action: (suspend () -> Unit)) =
this.async {
if (repeatMillis > 0) {
Expand All @@ -131,8 +128,13 @@ private val debounceTimeMillis = 5L // Adjust this value as needed
private val debounceMutex = Mutex()
private var debounceJob: Job? = null

private var refreshJob: Job? = null

private var finalUpdate: String = ""

internal fun enqueueUpdate(data: AppData) {
updateStack.add(data)
refreshJob?.cancel()
debounceJob?.cancel()
debounceJob =
CoroutineScope(Dispatchers.IO).launch {
Expand All @@ -147,26 +149,46 @@ private suspend fun processLastUpdate() {
lastUpdate?.let {
try {
val json = jsonMapper.encodeToString(PropData(lastUpdate))
finalUpdate = json
// println("Processing update: $json")
componentChannel.sendToAllClients(json)
refreshJob = CoroutineScope(Dispatchers.IO).launch {
delay(50)
checkForMissingState()
}
} catch (e: Exception) {
println("Error sending update: $e")
}
}
}

internal fun markDirty(prop: ActionProp) {
val isSceneLoaded = Frontend.boardGameScene != null
runCatching {
internal fun checkForMissingState() {
val appData = collectAppData()
val json = jsonMapper.encodeToString(PropData(appData))
if (json != finalUpdate) {
// println("Missing state detected. Sending update...")
enqueueUpdate(appData)
}
finalUpdate = ""

refreshJob?.cancel()
}

internal fun collectAppData(): AppData {
val appData =
SceneMapper.map(menuScene = Frontend.menuScene, gameScene = Frontend.boardGameScene).apply {
fonts =
Frontend.loadedFonts.map { (path, fontName, weight) ->
Triple(path, fontName, weight.toInt())
}
fonts =
Frontend.loadedFonts.map { (path, fontName, weight) ->
Triple(path, fontName, weight.toInt())
}
}
appData.action = ActionProp.UPDATE_COMPONENT
// val json = jsonMapper.encodeToString(PropData(appData))
return appData
}

internal fun markDirty(prop: ActionProp) {
runCatching {
val appData = collectAppData()
// println("Collecting updates... Size: " + updateStack.size)
enqueueUpdate(appData)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,24 @@ internal class CardLayoutScene : BoardGameScene() {
visual = ColorVisual(Color(255, 0, 0)))
.apply {
onMouseClicked = {
val dialog =
FileDialog(
mode = FileDialogMode.OPEN_MULTIPLE_FILES,
title = "Open BGW File",
initialDirectoryPath = "F:\\Test",
initialFileName = "test.bgw",
extensionFilters =
listOf(
ExtensionFilter("Images", "png", "jpg", "jpeg"),
ExtensionFilter("BoardGameWork File", "bgw"),
ExtensionFilter("All Files", "*")))

Application.showFileDialog(dialog)

dialog.onPathsSelected = { paths -> println("Selected paths: $paths") }

dialog.onSelectionCancelled = { println("Selection cancelled") }
Application.showMenuScene(Application.menuScene)
// val dialog =
// FileDialog(
// mode = FileDialogMode.OPEN_MULTIPLE_FILES,
// title = "Open BGW File",
// initialDirectoryPath = "F:\\Test",
// initialFileName = "test.bgw",
// extensionFilters =
// listOf(
// ExtensionFilter("Images", "png", "jpg", "jpeg"),
// ExtensionFilter("BoardGameWork File", "bgw"),
// ExtensionFilter("All Files", "*")))
//
// Application.showFileDialog(dialog)
//
// dialog.onPathsSelected = { paths -> println("Selected paths: $paths") }
//
// dialog.onSelectionCancelled = { println("Selection cancelled") }
}
}

Expand All @@ -110,6 +111,7 @@ internal class CardLayoutScene : BoardGameScene() {
.apply {
onMouseClicked = { event ->
if (event.button == MouseButtonType.LEFT_BUTTON) {
this.visual = ColorVisual(Color(0, 255, 0))
this.add(
CardView(
posX = 0,
Expand Down

0 comments on commit edb0319

Please sign in to comment.