From 60d7cf84c64789e813279646cd4bb4870ecccffe Mon Sep 17 00:00:00 2001 From: Thomas Breil Date: Fri, 12 Jan 2024 15:34:59 +0100 Subject: [PATCH] Add ScrollBar on ProjectList --- src/main/kotlin/main/Glossary.kt | 45 ++++++++++------ src/main/kotlin/main/Projects.kt | 88 ++++++++++++++++++-------------- 2 files changed, 79 insertions(+), 54 deletions(-) diff --git a/src/main/kotlin/main/Glossary.kt b/src/main/kotlin/main/Glossary.kt index 198e1a5..86b6eab 100644 --- a/src/main/kotlin/main/Glossary.kt +++ b/src/main/kotlin/main/Glossary.kt @@ -188,23 +188,34 @@ fun newGlossary( .fillMaxWidth() .onKeyEvent { event -> if (event.key == Key.Enter && event.type == KeyEventType.KeyDown) { - if (nouveauGlossaireName.isEmpty()) { - println("Veuillez saisir un nom pour le nouveau glossaire") - isEmptySnackbarVisibleState.value = true - } else if (nouveauGlossaireName.contains(" ")) { - println("Le nom du glossaire ne doit pas contenir d'espace") - containsSpaceSnackbarVisibleState.value = true - } else if (glossaries.any { it.name == nouveauGlossaireName }) { - println("Le nom du glossaire existe déjà") - glossaryAlreadyExistsSnackbarVisibleState.value = true - } else if (!isValidFileName(nouveauGlossaireName)) { - println("Le nom du glossaire contient des caractères non autorisés") - invalidCharacterSnackbarVisibleState.value = true - } else { - val directory = File("src/main/resources/projects/${appState.selectedProject}/$nouveauGlossaireName") - directory.mkdirs() - println("Project $nouveauGlossaireName created") - currentPage.value = "projects" + when { + nouveauGlossaireName.isEmpty() -> { + println("Veuillez saisir un nom pour le nouveau glossaire") + isEmptySnackbarVisibleState.value = true + } + nouveauGlossaireName.contains(" ") -> { + println("Le nom du glossaire ne doit pas contenir d'espace") + containsSpaceSnackbarVisibleState.value = true + } + glossaries.any { it.name == nouveauGlossaireName } -> { + println("Le nom du glossaire existe déjà") + glossaryAlreadyExistsSnackbarVisibleState.value = true + } + !isValidFileName(nouveauGlossaireName) -> { + println("Le nom du glossaire contient des caractères non autorisés") + invalidCharacterSnackbarVisibleState.value = true + } + else -> { + val newGlossary = Glossary(nouveauGlossaireName, "$nouveauGlossaireName.json") + glossaries = glossaries + newGlossary + // create new json file + val newFile = + File(glossaryPath + (appState.selectedProject?.name) + "/" + newGlossary.jsonFilePath) + newFile.createNewFile() + // update glossaries list + glossaries = loadGlossaries(appState.selectedProject!!) + currentPage.value = "glossaires" // Revenir à la liste des glossaires + } } true } else { diff --git a/src/main/kotlin/main/Projects.kt b/src/main/kotlin/main/Projects.kt index da23a77..fd00d2e 100644 --- a/src/main/kotlin/main/Projects.kt +++ b/src/main/kotlin/main/Projects.kt @@ -1,8 +1,11 @@ package main +import androidx.compose.foundation.VerticalScrollbar import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.lazy.rememberLazyListState +import androidx.compose.foundation.rememberScrollbarAdapter import androidx.compose.material.* import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Delete @@ -39,54 +42,65 @@ fun projectsPage( Spacer(modifier = Modifier.height(16.dp)) - LazyColumn( + Box( modifier = Modifier + .height(300.dp) + .width(250.dp) .padding(10.dp) - .width(200.dp) - .height(300.dp), - verticalArrangement = Arrangement.spacedBy(10.dp) ) { + val scrollState = rememberLazyListState() + projects = loadProjects() if (projects.isEmpty()) { - item { - Text("Aucun projet n'a été créé") - } + Text("Aucun proje n'a été créé") } else { - items(projects, key = { project -> project.name }) { project -> - Row( - modifier = Modifier.width(200.dp).fillMaxWidth(), - horizontalArrangement = Arrangement.SpaceBetween - ) { - Button( - onClick = { - appState.selectedProject = project - currentPage.value = "glossaires" - - }, - modifier = Modifier - .width(150.dp), - colors = ButtonDefaults.buttonColors( - backgroundColor = customRedColor, - contentColor = Color.White - ) + LazyColumn( + state = scrollState, + modifier = Modifier.padding(10.dp), + verticalArrangement = Arrangement.spacedBy(10.dp) + ) { + println(projects) + items(projects) { project -> + Row( + modifier = Modifier.width(200.dp).fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceBetween ) { - Text(project.name) - } - - IconButton( - onClick = { - // Handle delete project action - projects = projects.filterNot { it.name == project.name } - println(projects) - val directory = File("src/main/resources/projects/${project.name}/") - directory.deleteRecursively() - currentPage.value = "projects" + Button( + onClick = { + main.appState.selectedProject = project + currentPage.value = "glossaires" + }, + modifier = Modifier + .width(150.dp), + colors = ButtonDefaults.buttonColors( + backgroundColor = customRedColor, + contentColor = Color.White + ) + ) { + Text(project.name) + } + + IconButton( + onClick = { + // Handle delete glossary action + projects = projects.filterNot { it == project } + val directory = File("src/main/resources/projects/${project.name}/") + directory.deleteRecursively() + currentPage.value = "projects" + } + ) { + Icon( + imageVector = Icons.Default.Delete, + contentDescription = "Delete Project" + ) } - ) { - Icon(imageVector = Icons.Default.Delete, contentDescription = "Delete Project") } } } } + VerticalScrollbar( + modifier = Modifier.align(Alignment.CenterEnd).fillMaxHeight(), + adapter = rememberScrollbarAdapter(scrollState) + ) } Spacer(modifier = Modifier.height(16.dp))