Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package helper.javafx
- import javafx.stage.Stage
- import java.awt.Point
- import java.util.concurrent.ConcurrentHashMap
- import java.util.prefs.Preferences
- import kotlin.reflect.KClass
- /**
- * ## WindowPosition
- * - based on: [Broadly Applicable: JavaFX Restore Window Size & Position](http://broadlyapplicable.blogspot.hk/2015/02/javafx-restore-window-size-position.html)
- *
- * ### usage
- * - call [Stage.restore] on the [Stage] object after it has been created
- *
- * ### example
- * primaryStage.restore(this) // save/restore WindowPosition
- * primaryStage.show()
- */
- private object WindowPosition {
- private const val WINDOW_POSITION_X = "Window_Position_X"
- private const val WINDOW_POSITION_Y = "Window_Position_Y"
- private const val WINDOW_WIDTH = "Window_Width"
- private const val WINDOW_HEIGHT = "Window_Height"
- private val offsets: MutableMap<KClass<out Any>, Point> by lazy { ConcurrentHashMap<KClass<out Any>, Point>() }
- /**
- * ## loads the position and size settings previously saved by [save]
- * - loads from [Preferences] under the node specified by [node]
- * - if there aren't any, uses the defaults specified by [stage]
- * @param caller the _calling object_
- * @param stage the [Stage]
- * @param key the key to save under `e.g. "MainWindow"`
- * @param offset the [Point] offset used for each subsequent call for the [caller] class
- * @see save
- */
- internal fun restore(caller: Any, stage: Stage, key: String, offset: Point?=null) {
- var newOffset: Point?=null
- offset?.let {
- val off = offsets[caller::class]
- newOffset = if (off == null) Point(0,0) else Point(off.x + it.x, off.y + it.y)
- offsets[caller::class] = newOffset!!
- }
- val pref = Preferences.userRoot().node(node(caller, stage, key))
- val x = pref.getDouble(WINDOW_POSITION_X, stage.x) + (newOffset?.x ?: 0)
- val y = pref.getDouble(WINDOW_POSITION_Y, stage.y) + (newOffset?.y ?: 0)
- if (x >= 0 && y >= 0) { // might be minimized or something
- val width = pref.getDouble(WINDOW_WIDTH, stage.width)
- val height = pref.getDouble(WINDOW_HEIGHT, stage.height)
- stage.x = x
- stage.y = y
- stage.width = width
- stage.height = height
- }
- // add listeners
- stage.widthProperty() .addListener { obs, old, selected -> save(caller, stage, key) }
- stage.heightProperty().addListener { obs, old, selected -> save(caller, stage, key) }
- stage.xProperty() .addListener { obs, old, selected -> save(caller, stage, key) }
- stage.yProperty() .addListener { obs, old, selected -> save(caller, stage, key) }
- }
- /**
- * ## saves the position and size settings of the [Stage] to [Preferences]
- * - saves to [Preferences] to the node specified by [node]
- * @param caller the _calling object_
- * @param stage the [Stage]
- * @param key the key to save under `e.g. "MainWindow"`
- * @see restore
- */
- private fun save(caller: Any, stage: Stage, key: String) {
- if (stage.x < 0 || stage.y < 0) return // might be minimized or something
- val preferences = Preferences.userRoot().node(node(caller, stage, key))
- preferences.putDouble(WINDOW_POSITION_X, stage.x)
- preferences.putDouble(WINDOW_POSITION_Y, stage.y)
- preferences.putDouble(WINDOW_WIDTH, stage.width)
- preferences.putDouble(WINDOW_HEIGHT, stage.height)
- }
- private fun node(caller: Any, stage: Stage, key: String) = "${caller.javaClass.name}/WindowPosition/$key}"
- }
- fun Stage.restore(caller: Any, key: String, offset: Point?=null) = WindowPosition.restore(caller, this, key, offset)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement