Browse Source

New class for making singleton windows

This ensures there will be at most one copy of the stage, which will be
created and destroyed as needed. Trying to create a second just brings
the first into focus. This is good for things like preferences windows.
master
Sarah Gerweck 8 years ago
parent
commit
fd040af74f
  1. 35
      src/main/scala/org/gerweck/scalafx/util/SingletonStage.scala

35
src/main/scala/org/gerweck/scalafx/util/SingletonStage.scala

@ -0,0 +1,35 @@
package org.gerweck.scalafx.util
import scalafx.stage.Stage
import org.log4s._
/** A stage that should only be open at most once per application. */
abstract class SingletonStage {
private[this] val logger = getLogger
type InstanceStage <: ParentStage
protected[this] var singletonStage: Option[InstanceStage] = None
def name = getClass.getSimpleName
protected[this] def makeStage(): InstanceStage
def showStage(): Unit = {
singletonStage match {
case Some(stg) =>
logger.debug("Singleton ${name} stage is already open")
stg.requestFocus()
case None =>
val ns = makeStage()
singletonStage = Some(ns)
ns.show()
}
}
protected trait ParentStage extends Stage {
require(singletonStage.isEmpty, s"Cannot have two ${name} stages")
logger.debug(s"Creating singleton ${name} stage")
}
}
Loading…
Cancel
Save