Skip to content

Commit

Permalink
feat(abcdecoder):添加了打开文件的历史记录功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Yricky committed Dec 12, 2024
1 parent ec28cc8 commit 1db4a61
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 86 deletions.
26 changes: 26 additions & 0 deletions abcdecoder/src/jvmMain/kotlin/me/yricky/abcde/AppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ package me.yricky.abcde
import androidx.compose.runtime.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.yricky.abcde.desktop.config.AppCommonConfig
import me.yricky.abcde.desktop.config.HistoryConfig
import me.yricky.abcde.page.*
import me.yricky.abcde.util.SelectedAbcFile
import me.yricky.abcde.util.SelectedFile
import me.yricky.abcde.util.SelectedHapFile
import me.yricky.abcde.util.SelectedIndexFile
import kotlin.io.path.Path
import kotlin.io.path.isRegularFile

val LocalAppCommonConfig = compositionLocalOf<AppCommonConfig> { error("No default value") }

class AppState {
val coroutineScope = CoroutineScope(Dispatchers.Default)
Expand All @@ -24,19 +31,38 @@ class AppState {
if(!file.valid()){
return
}
var opened = false
when(file){
is SelectedAbcFile -> AbcView(file.abcBuf).also {
session.openPage(it)
currHapSession = session
opened = true
}
is SelectedHapFile -> HapView(file).also{
currHapSession = hapSessions.firstOrNull { s -> s.hapView == it } ?: HapSession(it).also { s ->
hapSessions.add(s)
}
opened = true
}
is SelectedIndexFile -> ResIndexView(file.resBuf, file.tag).also{
session.openPage(it)
currHapSession = session
opened = true
}
}
if(opened){
coroutineScope.launch(Dispatchers.IO){
HistoryConfig.edit { config ->
val history = config.openedFile.toMutableList()
history.removeIf { t ->
t.path == file.file.absolutePath || !Path(file.file.absolutePath).isRegularFile()
}
history.add(HistoryConfig.OpenedFile(
System.currentTimeMillis(),
file.file.absolutePath
))
config.copy(openedFile = history)
}
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions abcdecoder/src/jvmMain/kotlin/me/yricky/abcde/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kotlinx.coroutines.withContext
import me.yricky.abcde.cli.CliEntry
import me.yricky.abcde.content.SettingsPanel
import me.yricky.abcde.desktop.DesktopUtils
import me.yricky.abcde.desktop.config.AppCommonConfig
import me.yricky.abcde.page.*
import me.yricky.abcde.ui.*
import me.yricky.abcde.util.SelectedFile
Expand Down Expand Up @@ -260,16 +261,22 @@ fun main(args: Array<String>) = if(args.firstOrNull() == "--cli") {
}
}
}
ABCDEWindow(onCloseRequest = ::exitApplication, title = "ABCDecoder") {
LaunchedEffect(null){
DesktopUtils.AppStatus.renderApi = window.renderApi
window.minimumSize = Dimension(1280,800)
}
val cfg by AppCommonConfig.flow.collectAsState()
CompositionLocalProvider(
LocalAppCommonConfig provides cfg
) {
ABCDEWindow(onCloseRequest = ::exitApplication, title = "ABCDecoder") {
LaunchedEffect(null){
DesktopUtils.AppStatus.renderApi = window.renderApi
window.minimumSize = Dimension(1280,800)
}

AbcdeFrame(appState) {
App(appState)
AbcdeFrame(appState) {
App(appState)
}
}
SettingsPanel(appState.showSettings){ appState.showSettings = false }
}
SettingsPanel(appState.showSettings){ appState.showSettings = false }

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.yricky.abcde.content

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
Expand All @@ -11,7 +10,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import me.yricky.abcde.desktop.DesktopUtils
import me.yricky.abcde.LocalAppCommonConfig
import me.yricky.abcde.desktop.config.AppCommonConfig
import me.yricky.abcde.ui.*

@Composable
Expand All @@ -22,7 +22,7 @@ fun SettingsPanel(show:Boolean,onDismiss:()->Unit){
icon = Icons.editorConfig(),
title = "设置"
){
val cfg = LocalAppConfig.current
val cfg = LocalAppCommonConfig.current
val scope = rememberCoroutineScope()
Column(Modifier.fillMaxSize().padding(horizontal = 8.dp)) {
ConfigGroup("外观",Modifier.padding(top=8.dp)) {
Expand All @@ -37,7 +37,7 @@ fun SettingsPanel(show:Boolean,onDismiss:()->Unit){
onValueChange = { newDensity = it },
onValueChangeFinished = {
scope.launch {
DesktopUtils.AppConfig.edit {
AppCommonConfig.edit {
it.copy(density = newDensity)
}
}
Expand All @@ -50,7 +50,7 @@ fun SettingsPanel(show:Boolean,onDismiss:()->Unit){
Row(
Modifier.height(40.dp).clickable {
scope.launch {
DesktopUtils.AppConfig.edit{
AppCommonConfig.edit{
it.copy(darkTheme = it.darkTheme?.not() ?: false)
}
}
Expand All @@ -70,7 +70,7 @@ fun SettingsPanel(show:Boolean,onDismiss:()->Unit){
Row(
Modifier.height(40.dp).clickable {
scope.launch {
DesktopUtils.AppConfig.edit{
AppCommonConfig.edit{
it.copy(futureFeature = it.futureFeature.not())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.nio.file.Files
import java.util.*

object DesktopUtils {
private val json = Json{
val json = Json{
encodeDefaults = true
}
private val projectFiles = ProjectDirectories.from("me","yricky","abcdecoder")
Expand Down Expand Up @@ -67,41 +67,4 @@ object DesktopUtils {
object AppStatus{
var renderApi:GraphicsApi? = null
}

@Serializable
data class AppConfig(
@SerialName("density")
val density:Float = GraphicsEnvironment.getLocalGraphicsEnvironment()
?.defaultScreenDevice
?.defaultConfiguration
?.defaultTransform
?.scaleX?.toFloat() ?: 1f,
@SerialName("historyList")
val historyList:List<String> = emptyList(),
@SerialName("darkTheme")
val darkTheme:Boolean? = true,
@SerialName("futureFeature")
val futureFeature:Boolean = false,
){
companion object{
suspend fun edit(action: (AppConfig) -> AppConfig){
withContext(Dispatchers.IO){
val appConfig = action(inst.value)
file.writeText(json.encodeToString(appConfig))
inst.value = appConfig
}
}


private val file = File(dataDir,"cfg.json")
private val inst = MutableStateFlow(kotlin.runCatching {
json.decodeFromString<AppConfig>(file.readText())
}.onFailure {
it.printStackTrace()
}.getOrNull() ?: AppConfig().also {
file.writeText(json.encodeToString(it))
})
val flow :StateFlow<AppConfig> = inst.asStateFlow()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package me.yricky.abcde.desktop.config

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.withContext
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import me.yricky.abcde.desktop.DesktopUtils.dataDir
import me.yricky.abcde.desktop.DesktopUtils.json
import java.awt.GraphicsEnvironment
import java.io.File

@Serializable
data class AppCommonConfig(
@SerialName("density")
val density:Float = GraphicsEnvironment.getLocalGraphicsEnvironment()
?.defaultScreenDevice
?.defaultConfiguration
?.defaultTransform
?.scaleX?.toFloat() ?: 1f,
@SerialName("historyList")
val historyList:List<String> = emptyList(),
@SerialName("darkTheme")
val darkTheme:Boolean? = true,
@SerialName("futureFeature")
val futureFeature:Boolean = false
){
companion object{
suspend fun edit(action: (AppCommonConfig) -> AppCommonConfig){
withContext(Dispatchers.IO){
println("edit!")
val appConfig = action(inst.value)
file.writeText(json.encodeToString(appConfig))
inst.value = appConfig
}
}


private val file = File(dataDir,"cfg.json")
private val inst = MutableStateFlow(kotlin.runCatching {
json.decodeFromString<AppCommonConfig>(file.readText())
}.onFailure {
it.printStackTrace()
}.getOrNull() ?: AppCommonConfig().also {
file.writeText(json.encodeToString(it))
})
val flow :StateFlow<AppCommonConfig> = inst.asStateFlow()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package me.yricky.abcde.desktop.config

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.withContext
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import me.yricky.abcde.desktop.DesktopUtils.dataDir
import me.yricky.abcde.desktop.DesktopUtils.json
import java.awt.GraphicsEnvironment
import java.io.File

@Serializable
data class HistoryConfig(
@SerialName("openedFile")
val openedFile: List<OpenedFile> = emptyList()
){
@Serializable
class OpenedFile(
@SerialName("lastTime")
val lastTime: Long,
@SerialName("path")
val path: String
)
companion object{
suspend fun edit(action: (HistoryConfig) -> HistoryConfig){
withContext(Dispatchers.IO){
val appConfig = action(inst.value)
file.writeText(json.encodeToString(appConfig))
inst.value = appConfig
}
}


private val file = File(dataDir,"history.json")
private val inst = MutableStateFlow(kotlin.runCatching {
json.decodeFromString<HistoryConfig>(file.readText())
}.onFailure {
it.printStackTrace()
}.getOrNull() ?: HistoryConfig().also {
file.writeText(json.encodeToString(it))
})
val flow :StateFlow<HistoryConfig> = inst.asStateFlow()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class AbcView(val abc: AbcBuf,override val hap:HapView? = null):AttachHapPage()
@Composable
override fun Page(modifier: Modifier, hapSession: HapSession, appState: AppState) {
val scope = rememberCoroutineScope()
val appCfg = LocalAppConfig.current
VerticalTabAndContent(modifier, listOfNotNull(composeSelectContent{ _: Boolean ->
Image(Icons.clazz(), null, Modifier.fillMaxSize(), colorFilter = grayColorFilter)
} to composeContent{
Expand Down
10 changes: 2 additions & 8 deletions abcdecoder/src/jvmMain/kotlin/me/yricky/abcde/page/Page.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import me.yricky.abcde.AppState
import me.yricky.abcde.HapSession
import me.yricky.abcde.ui.short

sealed class Page{
companion object{
Expand Down Expand Up @@ -33,11 +34,4 @@ sealed class AttachHapPage:Page(){
abstract val hap:HapView?
}

val Page.shortName get() = name.let {
if (it.length > 35) "...${
it.substring(
it.length - 32,
it.length
)
}" else it
}
val Page.shortName get() = name.short(35)
Loading

0 comments on commit 1db4a61

Please sign in to comment.