Skip to content

Commit

Permalink
Merge pull request #157 from rladstaetter/125-remember-last-opened-di…
Browse files Browse the repository at this point in the history
…rectory-in-open-file-dialog

#125: adds functionality to remember the parent directory of last ope…
  • Loading branch information
rladstaetter authored Oct 3, 2023
2 parents 77002f5 + 8926db8 commit a69d142
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 27 deletions.
24 changes: 14 additions & 10 deletions app/src/main/scala/app/logorrr/conf/LogoRRRGlobals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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
})
Expand All @@ -90,15 +96,13 @@ 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)

def setDividerPosition(pathAsString: String, dividerPosition: Double): Unit = mutSettings.getMutLogFileSetting(pathAsString).setDividerPosition(dividerPosition)

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

}
18 changes: 11 additions & 7 deletions app/src/main/scala/app/logorrr/conf/Settings.scala
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 */
Expand Down
27 changes: 20 additions & 7 deletions app/src/main/scala/app/logorrr/conf/mut/MutSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand All @@ -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

Expand All @@ -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 {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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())
}

Expand Down
7 changes: 6 additions & 1 deletion app/src/main/scala/app/logorrr/util/LogoRRRFileChooser.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.logorrr.util

import app.logorrr.conf.LogoRRRGlobals
import javafx.stage.{FileChooser, Window}

import java.nio.file.Path
Expand All @@ -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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion app/src/test/scala/app/logorrr/conf/mut/SettingsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit a69d142

Please sign in to comment.