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.
This commit is contained in:
parent
ccc644f9d4
commit
fd040af74f
35
src/main/scala/org/gerweck/scalafx/util/SingletonStage.scala
Normal file
35
src/main/scala/org/gerweck/scalafx/util/SingletonStage.scala
Normal file
@ -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…
Reference in New Issue
Block a user