Commit d36b6844 authored by Ignamarte's avatar Ignamarte

Remove logic from the main class and save window maximized state

parent 898bdd5b
Pipeline #106993806 passed with stage
in 3 minutes and 2 seconds
......@@ -9,33 +9,5 @@ import scalafx.application.{JFXApp, Platform}
import util.config.UserConfig
object Tress extends JFXApp {
stage = MainWindow
Thread.setDefaultUncaughtExceptionHandler(showError)
stage.onCloseRequest = _ => {
UserConfig.setSize(stage.height(), stage.width())
Thread.setDefaultUncaughtExceptionHandler(ignoreError)
WebServer.stop()
}
def ignoreError(thread: Thread, error: Throwable): Unit = {}
def showError(thread: Thread, error: Throwable): Unit = {
error match {
case APIError(status, msg) => Platform.runLater({
new ErrorAlert(
f"The API returned an unexpected status code: $status",
msg
)
})
case e:Throwable => Platform.runLater({
new ErrorAlert(
f"An unexpected error occurred : \n $e",
e.getStackTrace.mkString("\n")
)
})
}
}
}
package gui
import api.webserver.WebServer
import errors.APIError
import gui.panels._
import gui.widgets.alert.ErrorAlert
import javafx.beans.property.ObjectProperty
import javafx.event.EventHandler
import javafx.stage.WindowEvent
import org.controlsfx.control.NotificationPane
import scalafx.Includes._
import scalafx.application.JFXApp.PrimaryStage
......@@ -20,8 +25,10 @@ object MainWindow extends PrimaryStage {
title.value = "Tress"
height = UserConfig.getHeight.getOrElse(DEFAULT_HEIGHT).toDouble
width = UserConfig.getWidth.getOrElse(DEFAULT_WIDTH).toDouble
height = UserConfig.getHeight.getOrElse(DEFAULT_HEIGHT)
width = UserConfig.getWidth.getOrElse(DEFAULT_WIDTH)
maximized = UserConfig.getIsMaximized.getOrElse(false)
minHeight = DEFAULT_HEIGHT
minWidth = DEFAULT_WIDTH
......@@ -64,6 +71,8 @@ object MainWindow extends PrimaryStage {
}.start()
}
/* *** Main Scene *** */
scene = new Scene {
stylesheets += getClass.getResource("/css/window.css").toExternalForm
......@@ -86,6 +95,53 @@ object MainWindow extends PrimaryStage {
}
}
// Display the default panel after loading the scene
Platform.runLater(MainPanel.displayFollowsPanel())
// Register size changes
width.onChange { (source, oldValue, newValue) =>
if (!maximized()) UserConfig.setWidth(newValue.doubleValue())
}
height.onChange { (source, oldValue, newValue) =>
if (!maximized()) UserConfig.setHeight(newValue.doubleValue())
}
maximized.onChange {(source, oldValue, newValue) =>
UserConfig.setMaximized(maximized())
}
// Error handlers
private def ignoreError(thread: Thread, error: Throwable): Unit = {}
private def showError(thread: Thread, error: Throwable): Unit = {
error match {
case APIError(status, msg) => Platform.runLater({
new ErrorAlert(
f"The API returned an unexpected status code: $status",
msg
)
})
case e:Throwable => Platform.runLater({
new ErrorAlert(
f"An unexpected error occurred : \n $e",
e.getStackTrace.mkString("\n")
)
})
}
}
Thread.setDefaultUncaughtExceptionHandler(showError)
// Actions to do when closing the gui
onCloseRequest = _ =>
{
Thread.setDefaultUncaughtExceptionHandler(ignoreError)
WebServer.stop()
}
}
......@@ -15,7 +15,8 @@ object UserConfig {
streamlink: Option[String] = None,
quality: Option[String] = None,
height: Option[Double] = None,
width: Option[Double] = None
width: Option[Double] = None,
maximized: Option[Boolean] = None
)
private val file = "user.conf"
......@@ -54,6 +55,10 @@ object UserConfig {
conf.width
}
def getIsMaximized: Option[Boolean] = {
conf.maximized
}
def setToken(token: String): Unit = {
conf = conf.copy(token = Some(token))
saveConf()
......@@ -74,8 +79,18 @@ object UserConfig {
saveConf()
}
def setSize(height: Double, width: Double): Unit = {
conf = conf.copy(height = Some(height), width= Some(width))
def setHeight(height: Double): Unit = {
conf = conf.copy(height = Some(height))
saveConf()
}
def setWidth(width: Double): Unit = {
conf = conf.copy(width= Some(width))
saveConf()
}
def setMaximized(maximized: Boolean): Unit = {
conf = conf.copy(maximized = Some(maximized))
saveConf()
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment