Skip to content

Commit

Permalink
Add Open Recent functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-x committed Jul 30, 2024
1 parent d6af1ec commit 6fa31cf
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ scripts
test
test2
compiled
recent_paths.dat

binary
sources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import dawn.cs2.*
import dawn.cs2.CS2Type.*
import dawn.cs2.util.FunctionDatabase
import javafx.application.Platform
import javafx.event.EventHandler
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.Node
Expand All @@ -26,6 +27,7 @@ import javafx.stage.DirectoryChooser
import javafx.stage.FileChooser
import javafx.stage.Window
import javafx.util.Callback
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.fxmisc.flowless.VirtualizedScrollPane
Expand All @@ -34,6 +36,7 @@ import org.fxmisc.richtext.LineNumberFactory
import org.fxmisc.richtext.model.StyleSpans
import org.fxmisc.richtext.model.StyleSpansBuilder
import java.io.File
import java.io.IOException
import java.io.PrintWriter
import java.io.StringWriter
import java.net.URL
Expand All @@ -43,14 +46,16 @@ import java.util.*
import java.util.regex.Matcher
import java.util.regex.Pattern
import kotlin.io.path.div
import kotlin.io.path.writeBytes
import kotlin.io.path.writeText

class MainController : Initializable {

@FXML
private lateinit var openMenuItem: MenuItem

@FXML
private lateinit var openRecentMenu: Menu

@FXML
private lateinit var saveScriptAs: MenuItem

Expand Down Expand Up @@ -90,9 +95,6 @@ class MainController : Initializable {
@FXML
private lateinit var tabPane: TabPane

@FXML
private lateinit var compilePane: BorderPane

@FXML
private lateinit var compileArea: TextArea

Expand All @@ -111,7 +113,14 @@ class MainController : Initializable {

private var currentScript: CS2? = null

private val recentPaths = mutableListOf<File>()
private val maxRecentPaths = 10 // Set how many recent paths that is allowed to be cached.
private val recentPathsFile = File("recent_paths.dat")

override fun initialize(location: URL?, resources: ResourceBundle?) {
// Load recent paths
loadRecentPaths()

rootPane.addEventHandler(KeyEvent.KEY_PRESSED) { e: KeyEvent ->
when {
e.isControlDown && e.code == KeyCode.N -> {
Expand All @@ -129,6 +138,7 @@ class MainController : Initializable {
}
}
}

openMenuItem.setOnAction {
openCache()
}
Expand Down Expand Up @@ -159,6 +169,7 @@ class MainController : Initializable {
aboutMenuItem.setOnAction {
AboutWindow()
}

scriptList.cellFactory = object : Callback<ListView<Int>, ListCell<Int>> {
override fun call(param: ListView<Int>): ListCell<Int> {
return object : ListCell<Int>() {
Expand All @@ -172,6 +183,7 @@ class MainController : Initializable {
}
}
}

scriptList.selectionModel.selectedItemProperty().addListener { observable, oldValue, newValue ->
if (newValue == null) {
return@addListener
Expand Down Expand Up @@ -201,12 +213,14 @@ class MainController : Initializable {
refreshAssemblyCode()
compileScript()
}

searchField.textProperty().addListener { observable, oldValue, newValue ->
if (newValue == null) {
return@addListener
}
search(newValue)
}

tabPane.selectionModel.selectedItemProperty().addListener { observable, oldValue, newValue ->
if (oldValue != null) {
val codeArea =
Expand All @@ -222,6 +236,7 @@ class MainController : Initializable {
currentScript = newValue.userData as CS2
refreshAssemblyCode()
}

assemblyCodePane.center = BorderPane(VirtualizedScrollPane(createCodeArea("", editable = false)))

// Disable assembly pane by default
Expand All @@ -230,14 +245,19 @@ class MainController : Initializable {

// init singleton
AutoCompleteUtils

// Update recent files menu
updateRecentPathMenu()
}

@OptIn(DelicateCoroutinesApi::class)
private fun openCache(f: File? = null) {
var mehFile = f
if (mehFile == null) {
val chooser = DirectoryChooser()
mehFile = chooser.showDialog(mainWindow()) ?: return
}
addRecentPath(mehFile)
scriptList.isDisable = true
GlobalScope.launch {
try {
Expand Down Expand Up @@ -272,6 +292,59 @@ class MainController : Initializable {
}
}

private fun addRecentPath(file: File) {
if (recentPaths.contains(file)) {
recentPaths.remove(file)
}
recentPaths.add(0, file)
if (recentPaths.size > maxRecentPaths) {
recentPaths.removeAt(maxRecentPaths)
}
updateRecentPathMenu()
saveRecentPaths()
}

private fun updateRecentPathMenu() {
openRecentMenu.items.clear()
recentPaths.forEach { file ->
val menuItem = MenuItem(file.absolutePath)
menuItem.onAction = EventHandler { openCache(file) }
openRecentMenu.items.add(menuItem)
}
}

private fun saveRecentPaths() {
try {
recentPathsFile.bufferedWriter().use { writer ->
recentPaths.forEach { file ->
writer.write(file.absolutePath)
writer.newLine()
}
}
} catch (e: IOException) {
e.printStackTrace()
Notification.error("Failed to save recent path: ${e.message}")
}
}

private fun loadRecentPaths() {
try {
if (recentPathsFile.exists()) {
recentPathsFile.bufferedReader().useLines { lines ->
lines.forEach { path ->
val file = File(path)
if (file.exists()) {
recentPaths.add(file)
}
}
}
}
} catch (e: IOException) {
e.printStackTrace()
Notification.error("Failed to load recent paths from recent_paths.dat: ${e.message}")
}
}

private fun loadParams() {
status("Populating params...")
val paramsSize = populateAttributes(cacheLibrary)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/main/resources/fxml/main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="openMenuItem" mnemonicParsing="false" text="Open…"/>
<Menu mnemonicParsing="false" text="Open Recent"/>
<Menu fx:id="openRecentMenu" mnemonicParsing="false" text="Open Recent"/>
<SeparatorMenuItem mnemonicParsing="false"/>
<MenuItem fx:id="newMenuItem" disable="true" mnemonicParsing="false" text="New Script (Ctrl + N)"/>
<MenuItem fx:id="importScriptMenuItem" disable="true" mnemonicParsing="false" text="Import Script (Ctrl + I)"/>
Expand Down

0 comments on commit 6fa31cf

Please sign in to comment.