From 8926db86d7808dd644d1c1d19586d3e34c976c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Ladst=C3=A4tter?= Date: Tue, 3 Oct 2023 18:02:57 +0200 Subject: [PATCH] #125: adds functionality to remember the parent directory of last opened file via menu --- .../app/logorrr/conf/LogoRRRGlobals.scala | 24 ++++++++++------- .../scala/app/logorrr/conf/Settings.scala | 18 ++++++++----- .../app/logorrr/conf/mut/MutSettings.scala | 27 ++++++++++++++----- .../app/logorrr/util/LogoRRRFileChooser.scala | 7 ++++- .../logorrr/conf/mut/MutSettingsSpec.scala | 2 +- .../app/logorrr/conf/mut/SettingsSpec.scala | 2 +- 6 files changed, 53 insertions(+), 27 deletions(-) diff --git a/app/src/main/scala/app/logorrr/conf/LogoRRRGlobals.scala b/app/src/main/scala/app/logorrr/conf/LogoRRRGlobals.scala index 8759a29e..e28486c8 100644 --- a/app/src/main/scala/app/logorrr/conf/LogoRRRGlobals.scala +++ b/app/src/main/scala/app/logorrr/conf/LogoRRRGlobals.scala @@ -11,6 +11,8 @@ import javafx.beans.property.SimpleObjectProperty import javafx.stage.Window import pureconfig.ConfigWriter +import java.nio.file.Path + /** * Place LogoRRR's settings. * @@ -26,7 +28,7 @@ object LogoRRRGlobals extends CanLog { Fs.write(FilePaths.settingsFilePath, ConfigWriter[Settings].to(LogoRRRGlobals.getSettings).render(renderOptions)) } - def getOrderedLogFileSettings: Seq[LogFileSettings] = mutSettings.getOrderedLogFileSettings() + def getOrderedLogFileSettings: Seq[LogFileSettings] = mutSettings.getOrderedLogFileSettings def bindWindow(window: Window): Unit = { window.setX(LogoRRRGlobals.getStageX) @@ -39,13 +41,13 @@ object LogoRRRGlobals extends CanLog { def unbindWindow(): Unit = mutSettings.unbindWindow() - def getStageWidth: Int = mutSettings.getStageWidth() + def getStageWidth: Int = mutSettings.getStageWidth - def getStageHeight: Int = mutSettings.getStageHeight() + def getStageHeight: Int = mutSettings.getStageHeight - def getStageX: Double = mutSettings.getStageX() + def getStageX: Double = mutSettings.getStageX - def getStageY: Double = mutSettings.getStageY() + def getStageY: Double = mutSettings.getStageY def setHostServices(hostServices: HostServices): Unit = hostServicesProperty.set(hostServices) @@ -60,12 +62,16 @@ object LogoRRRGlobals extends CanLog { def setSomeActive(sActive: Option[String]): Unit = mutSettings.setSomeActive(sActive) - def getSomeActive: Option[String] = mutSettings.getSomeActive() + def getSomeActive: Option[String] = mutSettings.getSomeActive + + def getSomeLastUsedDirectory: Option[Path] = mutSettings.getSomeLastUsedDirectory + + def setSomeLastUsedDirectory(someDirectory: Option[Path]): Unit = mutSettings.setSomeLastUsedDirectory(someDirectory) def removeLogFile(pathAsString: String): Unit = { mutSettings.removeLogFileSetting(pathAsString) - mutSettings.setSomeActive(mutSettings.getSomeActive() match { + mutSettings.setSomeActive(mutSettings.getSomeActive match { case Some(value) if value == pathAsString => None case x => x }) @@ -90,8 +96,6 @@ object LogoRRRGlobals extends CanLog { } - def setSelectedIndex(pathAsString: String, index: Int): Unit = getLogFileSettings(pathAsString).selectedLineNumber(index) - def setBlockSettings(pathAsString: String, bs: BlockSettings): Unit = mupdate({ lfs: MutLogFileSettings => lfs.setBlockSettings(bs) })(pathAsString) @@ -99,6 +103,6 @@ object LogoRRRGlobals extends CanLog { def updateLogFile(fs: LogFileSettings): Unit = mutSettings.putMutLogFileSetting(MutLogFileSettings(fs)) - def logVisualCanvasWidth(pathAsString: String): Int = (mutSettings.getStageWidth() * LogoRRRGlobals.getLogFileSettings(pathAsString).getDividerPosition()).intValue + def logVisualCanvasWidth(pathAsString: String): Int = (mutSettings.getStageWidth * LogoRRRGlobals.getLogFileSettings(pathAsString).getDividerPosition()).intValue } diff --git a/app/src/main/scala/app/logorrr/conf/Settings.scala b/app/src/main/scala/app/logorrr/conf/Settings.scala index ec0c81bd..cfd67169 100644 --- a/app/src/main/scala/app/logorrr/conf/Settings.scala +++ b/app/src/main/scala/app/logorrr/conf/Settings.scala @@ -1,8 +1,10 @@ package app.logorrr.conf import app.logorrr.model.LogFileSettings -import pureconfig.{ConfigReader, ConfigWriter} import pureconfig.generic.semiauto.{deriveReader, deriveWriter} +import pureconfig.{ConfigReader, ConfigWriter} + +import java.nio.file.Path /** * Global settings for LogoRRR @@ -15,22 +17,24 @@ object Settings { implicit lazy val reader: ConfigReader[Settings] = deriveReader[Settings] implicit lazy val writer: ConfigWriter[Settings] = deriveWriter[Settings] - val Default = Settings( - StageSettings(0, 0, 500, 500) + val Default: Settings = Settings( + StageSettings(100, 100, 800, 600) , Map() , None + , None ) } case class Settings(stageSettings: StageSettings , logFileSettings: Map[String, LogFileSettings] - , someActive: Option[String]) { + , someActive: Option[String] + , someLastUsedDirectory: Option[Path]) { def remove(pathAsString: String): Settings = { - copy(stageSettings - , logFileSettings - pathAsString - , None) + copy(stageSettings = stageSettings + , logFileSettings = logFileSettings - pathAsString + , someActive = None) } /** updates recent files with given log setting */ diff --git a/app/src/main/scala/app/logorrr/conf/mut/MutSettings.scala b/app/src/main/scala/app/logorrr/conf/mut/MutSettings.scala index 4f481922..3c2dc7ee 100644 --- a/app/src/main/scala/app/logorrr/conf/mut/MutSettings.scala +++ b/app/src/main/scala/app/logorrr/conf/mut/MutSettings.scala @@ -6,6 +6,7 @@ import javafx.beans.property.{SimpleMapProperty, SimpleObjectProperty} import javafx.collections.FXCollections import javafx.stage.Window +import java.nio.file.Path import java.util import scala.jdk.CollectionConverters._ @@ -28,6 +29,17 @@ object MutSettings { class MutSettings { + /** remembers last opened directory for the next execution */ + val lastUsedDirectoryProperty = new SimpleObjectProperty[Option[Path]](None) + + def getSomeLastUsedDirectory: Option[Path] = { + lastUsedDirectoryProperty.get() + } + + def setSomeLastUsedDirectory(someDirectory: Option[Path]): Unit = { + lastUsedDirectoryProperty.set(someDirectory) + } + /** contains mutable information for the application stage */ private val mutStageSettings = new MutStageSettings @@ -49,11 +61,12 @@ class MutSettings { setStageSettings(settings.stageSettings) setLogFileSettings(settings.logFileSettings) setSomeActive(settings.someActive) + setSomeLastUsedDirectory(settings.someLastUsedDirectory) } def setSomeActive(path: Option[String]): Unit = someActiveLogProperty.set(path) - def getSomeActive(): Option[String] = someActiveLogProperty.get() + def getSomeActive: Option[String] = someActiveLogProperty.get() def setLogFileSettings(logFileSettings: Map[String, LogFileSettings]): Unit = { val m = for ((k, v) <- logFileSettings) yield { @@ -66,7 +79,7 @@ class MutSettings { val logFileSettings: Map[String, LogFileSettings] = (for ((k, v) <- mutLogFileSettingsMapProperty.get.asScala) yield { k -> v.petrify() }).toMap - Settings(mutStageSettings.petrify(), logFileSettings, getSomeActive()) + Settings(mutStageSettings.petrify(), logFileSettings, getSomeActive, getSomeLastUsedDirectory) } def setStageSettings(stageSettings: StageSettings): Unit = { @@ -96,15 +109,15 @@ class MutSettings { mutStageSettings.yProperty.unbind() } - def getStageY(): Double = mutStageSettings.yProperty.get() + def getStageY: Double = mutStageSettings.yProperty.get() - def getStageX(): Double = mutStageSettings.xProperty.get() + def getStageX: Double = mutStageSettings.xProperty.get() - def getStageHeight(): Int = mutStageSettings.heightProperty.get() + def getStageHeight: Int = mutStageSettings.heightProperty.get() - def getStageWidth(): Int = mutStageSettings.getWidth() + def getStageWidth: Int = mutStageSettings.getWidth() - def getOrderedLogFileSettings(): Seq[LogFileSettings] = { + def getOrderedLogFileSettings: Seq[LogFileSettings] = { mutLogFileSettingsMapProperty.get().values.asScala.toSeq.sortWith((lt, gt) => lt.getFirstOpened() < gt.getFirstOpened()).map(_.petrify()) } diff --git a/app/src/main/scala/app/logorrr/util/LogoRRRFileChooser.scala b/app/src/main/scala/app/logorrr/util/LogoRRRFileChooser.scala index 30a5d9a8..229ee208 100644 --- a/app/src/main/scala/app/logorrr/util/LogoRRRFileChooser.scala +++ b/app/src/main/scala/app/logorrr/util/LogoRRRFileChooser.scala @@ -1,5 +1,6 @@ package app.logorrr.util +import app.logorrr.conf.LogoRRRGlobals import javafx.stage.{FileChooser, Window} import java.nio.file.Path @@ -9,7 +10,11 @@ class LogoRRRFileChooser(title: String) { def showAndWait(window: Window): Option[Path] = { val fc = new FileChooser fc.setTitle(title) - Option(fc.showOpenDialog(window)).map(_.toPath) + LogoRRRGlobals.getSomeLastUsedDirectory.foreach(d => fc.setInitialDirectory(d.toFile)) + val somePath = Option(fc.showOpenDialog(window)).map(_.toPath) + LogoRRRGlobals.setSomeLastUsedDirectory(somePath.map(_.getParent)) + LogoRRRGlobals.persist() + somePath } } diff --git a/app/src/test/scala/app/logorrr/conf/mut/MutSettingsSpec.scala b/app/src/test/scala/app/logorrr/conf/mut/MutSettingsSpec.scala index d28a37b7..6ce01112 100644 --- a/app/src/test/scala/app/logorrr/conf/mut/MutSettingsSpec.scala +++ b/app/src/test/scala/app/logorrr/conf/mut/MutSettingsSpec.scala @@ -9,7 +9,7 @@ class MutSettingsSpec extends LogoRRRSpec { "MutSettings" should { "deserialize" in { - val s = Settings(StageSettings(0.15142984837327833, 0.5216122226307276, 1, 1), Map(), None) + val s = Settings(StageSettings(0.15142984837327833, 0.5216122226307276, 1, 1), Map(), None, None) assert(s == MutSettings(s).petrify()) } "de/serialize" in { diff --git a/app/src/test/scala/app/logorrr/conf/mut/SettingsSpec.scala b/app/src/test/scala/app/logorrr/conf/mut/SettingsSpec.scala index 518acfc8..d5a42cd6 100644 --- a/app/src/test/scala/app/logorrr/conf/mut/SettingsSpec.scala +++ b/app/src/test/scala/app/logorrr/conf/mut/SettingsSpec.scala @@ -7,5 +7,5 @@ object SettingsSpec { val gen: Gen[Settings] = for { stageSettings <- StageSettingsSpec.gen - } yield Settings(stageSettings, Map(), None) + } yield Settings(stageSettings, Map(), None, None) }