diff --git a/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/config/Config.kt b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/config/Config.kt index 2e31851..c436717 100644 --- a/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/config/Config.kt +++ b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/config/Config.kt @@ -6,6 +6,7 @@ import com.github.purofle.nmsl.utils.os.OS import kotlin.io.path.createDirectories import kotlin.io.path.readText import kotlin.io.path.writeText +import kotlin.reflect.KProperty object Config { private val configPath by lazy { @@ -18,4 +19,12 @@ object Config { } fun readConfig(): NmslConfig = configPath.readText().toJsonObject() + + operator fun getValue(nothing: Nothing?, property: KProperty<*>): NmslConfig { + return readConfig() + } + + operator fun setValue(nothing: Nothing?, property: KProperty<*>, nmslConfig: NmslConfig) { + createConfig(nmslConfig) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/purofle/nmsl/pages/SettingPage.kt b/src/main/kotlin/com/github/purofle/nmsl/pages/SettingPage.kt index d92c469..57cd8f3 100644 --- a/src/main/kotlin/com/github/purofle/nmsl/pages/SettingPage.kt +++ b/src/main/kotlin/com/github/purofle/nmsl/pages/SettingPage.kt @@ -1,9 +1,93 @@ package com.github.purofle.nmsl.pages -import androidx.compose.runtime.Composable +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.ArrowDropDown +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.DpOffset +import androidx.compose.ui.unit.dp +import com.github.purofle.nmsl.config.Config +import com.github.purofle.nmsl.config.LauncherConfig +import com.github.purofle.nmsl.download.DownloadProvider class SettingPage : Page { @Composable override fun render() { + + var config by Config + var gameSource by remember { mutableStateOf("${config.launcherConfig.provider}源") } + + Scaffold { + LazyColumn { + item { + SettingSelectItem( + title = "游戏资源下载源", + items = DownloadProvider.providers.keys.map { + SettingItem("${it}源") { + gameSource = "${it}源" + config = config.copy(launcherConfig = LauncherConfig(provider = it)) + } + }, + defaultItem = gameSource + ) + } + } + } + } +} + +data class SettingItem( + val title: String, + val onClick: () -> Unit +) + +@Composable +fun SettingSelectItem( + title: String, + items: List, + defaultItem: String +) { + + var expanded by remember { mutableStateOf(false) } + + Column(modifier = Modifier.padding(12.dp)) { + Text(title) + Spacer(modifier = Modifier.height(6.dp)) + Row( + modifier = Modifier + .height(32.dp) + .background(MaterialTheme.colorScheme.secondaryContainer, RoundedCornerShape(2.dp)) + .clickable { expanded = !expanded }, + verticalAlignment = Alignment.CenterVertically + ) { + Text(defaultItem, modifier = Modifier.padding(start = 12.dp)) + Icon( + imageVector = Icons.Default.ArrowDropDown, + contentDescription = null, + modifier = Modifier.padding(start = 48.dp, end = 12.dp) + ) + } + } + DropdownMenu( + expanded = expanded, + onDismissRequest = { expanded = false }, + offset = DpOffset(12.dp, 0.dp) + ) { + items.forEach { + DropdownMenuItem( + text = { Text(it.title) }, + onClick = { + expanded = false + it.onClick() + } + ) + } } } \ No newline at end of file