From 0e461e1419ad9dbcc4810898d059a63053e91666 Mon Sep 17 00:00:00 2001 From: Kagura Date: Fri, 1 Nov 2024 09:14:07 +0800 Subject: [PATCH] fix: UI Bug --- .idea/misc.xml | 3 + src/main/kotlin/Main.kt | 91 +++++++++++++++++++----------- src/main/kotlin/core/SourceFile.kt | 2 +- 3 files changed, 61 insertions(+), 35 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index eaedeea..f2a2ad2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,6 +4,9 @@ + + diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index f96ac80..1e533d7 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -21,6 +21,10 @@ import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.Window import androidx.compose.ui.window.application import core.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.runBlocking import ui.TextfieldWithLineNumber import utils.* @@ -47,39 +51,43 @@ fun App() { }) { AlertDialog( title = { - Text( - text = when (page) { - 0 -> "函数定义的变量" - 1 -> "函数内每条语句的定义和使用" - 2 -> "函数内每个变量 def-use 链" - 3 -> "函数间每个变量 def-use 链" - else -> "出错了" - }, - color = Color.Black, - fontWeight = FontWeight.Bold, - fontSize = 24.sp, - textAlign = TextAlign.Center, - modifier = Modifier.padding(top = 10.dp) - ) + Column(modifier = Modifier.fillMaxWidth(), + horizontalAlignment = Alignment.CenterHorizontally) { + Text( + text = when (page) { + 0 -> "函数定义的变量" + 1 -> "函数内每条语句的定义和使用" + 2 -> "函数内每个变量 def-use 链" + 3 -> "函数间每个变量 def-use 链" + else -> "出错了" + }, + color = Color.Black, + fontWeight = FontWeight.Bold, + fontSize = 24.sp, + textAlign = TextAlign.Center + ) + } }, text = { - Text( - text = when (page) { - 0 -> defineList - 1 -> useList - 2 -> traceTree - 3 -> funcTraceTree - else -> "出错了(ง •̀_•́)ง" - }, - modifier = Modifier - .fillMaxSize() - .verticalScroll(rememberScrollState()) - .wrapContentHeight(), - textAlign = TextAlign.Start, - color = Color.Black, - fontSize = 18.sp, - fontFamily = FontFamily("monospace") - ) + Box(modifier = Modifier.fillMaxWidth() + .fillMaxHeight(0.8f)) { + Text( + text = when (page) { + 0 -> defineList + 1 -> useList + 2 -> traceTree + 3 -> funcTraceTree + else -> "出错了(ง •̀_•́)ง" + }, + modifier = Modifier + .fillMaxSize() + .verticalScroll(rememberScrollState()), + textAlign = TextAlign.Start, + color = Color.Black, + fontSize = 18.sp, + fontFamily = FontFamily("monospace") + ) + } }, onDismissRequest = { showResult.value = false @@ -157,6 +165,7 @@ fun App() { val def = getDefineList(sf.getDef()) def.ifEmpty { "分析失败:可能输入有错误!" + return@TextfieldWithLineNumber } } catch (e: Exception) { e.printStackTrace().toString() @@ -166,12 +175,26 @@ fun App() { } catch (e: Exception) { e.printStackTrace().toString() } + + // 异步获取 traceTree val relations = mutableMapOf>() val traceTreeStr = StringBuilder() - sf.functions.forEach { func -> - traceTreeStr.append(getTraceTreeString(func.getTraceTree().getStringRepr(),func.name)) - relations.putAll(parseRelation(func.name,func.getTraceTree().getStringRepr())) + runBlocking { + val deferredResults = sf.functions.map { func -> + async(Dispatchers.Default) { + val traceStr = getTraceTreeString(func.getTraceTree().getStringRepr(), func.name) + val parsedRelations = parseRelation(func.name, func.getTraceTree().getStringRepr()) + Pair(traceStr, parsedRelations) + } + } + + // 等待所有协程结束 + deferredResults.awaitAll().forEach { (traceStr, parsedRelations) -> + traceTreeStr.append(traceStr) + relations.putAll(parsedRelations) + } } + traceTree = traceTreeStr.toString() funcTraceTree = diff --git a/src/main/kotlin/core/SourceFile.kt b/src/main/kotlin/core/SourceFile.kt index 18debf5..6d8aaf5 100644 --- a/src/main/kotlin/core/SourceFile.kt +++ b/src/main/kotlin/core/SourceFile.kt @@ -182,7 +182,7 @@ class SourceFile { } /** - * 任务1:获取使用的值的情况 + * 获取使用的值的情况 * @return Map of<函数 to List<使用值的情况>> * @see utils.getUseList 获取要求的格式 */