Skip to content

Commit

Permalink
Add a Progress Indicator
Browse files Browse the repository at this point in the history
I tried to add a popup dialog with a progress bar but I couldn't get the dialog to show. I added it to the status bar instead.
  • Loading branch information
alonalbert committed May 5, 2024
1 parent da90411 commit 891866c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ kotlin {
implementation("org.jetbrains.jewel:jewel-int-ui-decorated-window-241:${extra["jewel.version"] as String}")
implementation("org.jetbrains.skiko:skiko-awt-runtime-macos-arm64:${extra["skiko.version"] as String}")
implementation("org.jetbrains.compose.components:components-splitpane-desktop:${extra["compose.version"] as String}")
implementation("org.jetbrains.compose.material3:material3-desktop:${extra["compose.version"] as String}")
implementation("com.fifesoft:rsyntaxtextarea:${extra["rsyntaxtextarea.version"] as String}")
implementation("com.fifesoft:rstaui:${extra["rstaui.version"] as String}")
implementation("net.java.dev.jna:jna:${extra["jna.version"] as String}")
Expand Down
37 changes: 23 additions & 14 deletions src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/Disassembly.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,32 @@
package dev.romainguy.kotlin.explorer

import dev.romainguy.kotlin.explorer.dex.DexDumpParser
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.launch
import java.nio.file.Files
import java.nio.file.Path
import java.util.stream.Collectors
import kotlin.io.path.extension

private const val TotalSteps = 5
private const val Done = 1f

suspend fun disassemble(
toolPaths: ToolPaths,
source: String,
onDex: (String) -> Unit,
onOat: (String) -> Unit,
onStatusUpdate: (String) -> Unit,
onStatusUpdate: (String, Float) -> Unit,
optimize: Boolean
) = coroutineScope {
val ui = currentCoroutineContext()

launch(Dispatchers.IO) {
launch(ui) { onStatusUpdate("Compiling Kotlin…") }
var step = 0f

launch(ui) { onStatusUpdate("Compiling Kotlin…", step++ / TotalSteps) }

val directory = toolPaths.tempDirectory
cleanupClasses(directory)
Expand All @@ -50,7 +58,7 @@ suspend fun disassemble(
if (kotlinc.exitCode != 0) {
launch(ui) {
onDex(kotlinc.output.replace(path.parent.toString() + "/", ""))
onStatusUpdate("Ready")
onStatusUpdate("Ready", Done)
}
return@launch
}
Expand All @@ -60,7 +68,8 @@ suspend fun disassemble(
"Optimizing with R8…"
} else {
"Compiling with D8…"
})
}, step++ / TotalSteps
)
}

writeR8Rules(directory)
Expand All @@ -73,12 +82,12 @@ suspend fun disassemble(
if (r8.exitCode != 0) {
launch(ui) {
onDex(r8.output)
onStatusUpdate("Ready")
onStatusUpdate("Ready", Done)
}
return@launch
}

launch(ui) { onStatusUpdate("Disassembling DEX…") }
launch(ui) { onStatusUpdate("Disassembling DEX…", step++ / TotalSteps) }

val dexdump = process(
toolPaths.dexdump.toString(),
Expand All @@ -90,14 +99,14 @@ suspend fun disassemble(
if (dexdump.exitCode != 0) {
launch(ui) {
onDex(dexdump.output)
onStatusUpdate("Ready")
onStatusUpdate("Ready", Done)
}
return@launch
}

launch(ui) {
onDex(DexDumpParser(dexdump.output).parseDexDump())
onStatusUpdate("AOT compilation…")
onStatusUpdate("AOT compilation…", step++ / TotalSteps)
}

val push = process(
Expand All @@ -111,7 +120,7 @@ suspend fun disassemble(
if (push.exitCode != 0) {
launch(ui) {
onOat(push.output)
onStatusUpdate("Ready")
onStatusUpdate("Ready", Done)
}
return@launch
}
Expand All @@ -128,12 +137,12 @@ suspend fun disassemble(
if (dex2oat.exitCode != 0) {
launch(ui) {
onOat(dex2oat.output)
onStatusUpdate("Ready")
onStatusUpdate("Ready", Done)
}
return@launch
}

launch(ui) { onStatusUpdate("Disassembling OAT…") }
launch(ui) { onStatusUpdate("Disassembling OAT…", step++ / TotalSteps) }

val oatdump = process(
toolPaths.adb.toString(),
Expand All @@ -146,11 +155,11 @@ suspend fun disassemble(
launch(ui) { onOat(filterOat(oatdump.output)) }

if (oatdump.exitCode != 0) {
launch(ui) { onStatusUpdate("Ready") }
launch(ui) { onStatusUpdate("Ready", Done) }
return@launch
}

launch(ui) { onStatusUpdate("Ready") }
launch(ui) { onStatusUpdate("Ready", Done) }
}
}

Expand Down
31 changes: 22 additions & 9 deletions src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/KotlinExplorer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ package dev.romainguy.kotlin.explorer

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.SwingPanel
import androidx.compose.ui.graphics.Color
Expand Down Expand Up @@ -55,7 +57,10 @@ import org.jetbrains.jewel.intui.window.decoratedWindow
import org.jetbrains.jewel.intui.window.styling.dark
import org.jetbrains.jewel.intui.window.styling.light
import org.jetbrains.jewel.ui.ComponentStyling
import org.jetbrains.jewel.ui.component.*
import org.jetbrains.jewel.ui.component.DefaultButton
import org.jetbrains.jewel.ui.component.Icon
import org.jetbrains.jewel.ui.component.Text
import org.jetbrains.jewel.ui.component.TextField
import org.jetbrains.jewel.window.DecoratedWindow
import org.jetbrains.jewel.window.TitleBar
import org.jetbrains.jewel.window.newFullscreenControls
Expand All @@ -75,6 +80,7 @@ private fun FrameWindowScope.KotlinExplorer(
// TODO: Move all those remembers to an internal private state object
var activeTextArea by remember { mutableStateOf<RSyntaxTextArea?>(null) }
var status by remember { mutableStateOf("Ready") }
var progress by remember { mutableStateOf(1f) }

val searchListener = remember { object : SearchListener {
override fun searchEvent(e: SearchEvent?) {
Expand Down Expand Up @@ -124,7 +130,11 @@ private fun FrameWindowScope.KotlinExplorer(
val dexPanel: @Composable () -> Unit = { TextPanel("DEX", dexTextArea, explorerState) }
val oatPanel: @Composable () -> Unit = { TextPanel("OAT", oatTextArea, explorerState) }
var panels by remember { mutableStateOf(explorerState.getPanels(sourcePanel, dexPanel, oatPanel)) }

val onProgressUpdate: (String, Float) -> Unit = { newStatus: String, newProgress: Float ->
status = newStatus
progress = newProgress
}

MainMenu(
explorerState,
sourceTextArea,
Expand All @@ -136,7 +146,7 @@ private fun FrameWindowScope.KotlinExplorer(
}
},
{ oat -> updateTextArea(oatTextArea, oat) },
{ statusUpdate -> status = statusUpdate },
onProgressUpdate,
{ findDialog.isVisible = true },
{ SearchEngine.find(activeTextArea, findDialog.searchContext) },
{ showSettings = true },
Expand All @@ -153,14 +163,17 @@ private fun FrameWindowScope.KotlinExplorer(
modifier = Modifier.background(JewelTheme.globalColors.paneBackground)
) {
MultiSplitter(modifier = Modifier.weight(1.0f), panels)
Row {
Row(verticalAlignment = CenterVertically) {
val width = 160.dp
Text(
modifier = Modifier
.weight(1.0f, true)
.align(Alignment.CenterVertically)
.widthIn(min = width, max = width)
.padding(8.dp),
text = status
)
if (progress < 1) {
LinearProgressIndicator({ progress })
}
}
}
}
Expand Down Expand Up @@ -252,7 +265,7 @@ private fun FrameWindowScope.MainMenu(
sourceTextArea: RSyntaxTextArea,
onDexUpdate: (String?) -> Unit,
onOatUpdate: (String) -> Unit,
onStatusUpdate: (String) -> Unit,
onStatusUpdate: (String, Float) -> Unit,
onFindClicked: () -> Unit,
onFindNextClicked: () -> Unit,
onOpenSettings: () -> Unit,
Expand Down Expand Up @@ -352,7 +365,7 @@ private fun Settings(
Row {
Text(
"Android home directory: ",
modifier = Modifier.align(Alignment.CenterVertically)
modifier = Modifier.align(CenterVertically)
)
TextField(
androidHome,
Expand All @@ -371,7 +384,7 @@ private fun Settings(
Row {
Text(
"Kotlin home directory: ",
modifier = Modifier.align(Alignment.CenterVertically)
modifier = Modifier.align(CenterVertically)
)
TextField(
kotlinHome,
Expand Down

0 comments on commit 891866c

Please sign in to comment.