Skip to content

Commit

Permalink
Merge pull request #24 from saalfeldlab/feat/1.0.0
Browse files Browse the repository at this point in the history
Feat/1.0.0
  • Loading branch information
cmhulbert authored Oct 6, 2023
2 parents b0d0fe2 + b53abdb commit a0748ea
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>org.janelia.saalfeldlab</groupId>
<artifactId>saalfx</artifactId>
<version>0.8.1-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>

<name>Saal FX</name>
<description>Saalfeld lab JavaFX tools and extensions</description>
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/SaalFxStyle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import org.janelia.saalfeldlab.fx.ui.NumberField

object SaalFxStyle {
fun registerStylesheets(styleable : Scene) {
NumberField.registStyleSheet(styleable)
NumberField.registerStyleSheet(styleable)
MatchSelection.registStyleSheet(styleable)
}
fun registerStylesheets(styleable : Parent) {
NumberField.registStyleSheet(styleable)
NumberField.registerStyleSheet(styleable)
MatchSelection.registStyleSheet(styleable)
}
}
58 changes: 45 additions & 13 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/extensions/Delegates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ import kotlin.reflect.KProperty
*/
class LazyForeignMap<K, V>(val foreignKeyProvider: () -> K, val valueGenerator: (K) -> V) : MutableMap<K, V> by HashMap() {

private var mapStateHandler: ((MutableMap<K, V>) -> Unit)? = null

/**
* Callback to allow for modification of the current backing map just before the new value is generated and added to it.
* Useful to clear the map if desireable, for example.
*
* @param handleMapState callback to run with the backing map just before a new key/value is added.
* @return this, so you can use this builder-style and still assign as a delegate
*/
fun beforeMapChange(handleMapState : (MutableMap<K, V>) -> Unit) : LazyForeignMap<K, V> {
mapStateHandler = handleMapState
return this
}

operator fun getValue(t: Any, property: KProperty<*>): V {
val foreignKey = foreignKeyProvider()
return getOrPut(foreignKey) { valueGenerator(foreignKey) }
Expand All @@ -42,27 +56,45 @@ class LazyForeignMap<K, V>(val foreignKeyProvider: () -> K, val valueGenerator:
* @constructor This is intended to by constructed via delegation e.g `val test by LazyForeignValue( this::key) { it.getValue }`
*/

class LazyForeignValue<K, V>(val foreignKeyProvider: () -> K, val valueGenerator: (K) -> V) : MutableMap<K, V> by HashMap() {
//TODO Caleb: allow the valueGenerator to operate on (K, V?) -> V where V? is the previous value that was stored.
// Currently we throw it away, but it may require some cleanup, and there is currently no way to trigger that
//
// ALTERNATIVE: an optional middle parameter for [cleanupCallback] which operates on (V?) -> Unit if there is an old value. Not sure which is better.
class LazyForeignValue<K, V>(val foreignKeyProvider: () -> K, val valueGenerator: (K) -> V) {

private var currentKey: K? = null
private var currentValue: V? = null

private var oldValueHandler : ((V?) -> Unit)? = null

/**
* Callback to handle the old value just before the new value is generated.
*
* @param handleOldValue callback to run with the provided soon-to-be old value
* @return this, so you can use this builder-style and still assign as a delegate
*/
fun beforeValueChange(handleOldValue : (V?) -> Unit): LazyForeignValue<K, V> {
oldValueHandler = handleOldValue
return this
}

operator fun getValue(t: Any?, property: KProperty<*>): V {
val foreignKey = foreignKeyProvider()
return getOrPut(foreignKey) {
/* We only want a single value, so clear before we add this new one */
clear()
valueGenerator(foreignKey)
return if (foreignKey == currentKey) currentValue!!
else {
currentKey = foreignKey
val oldValue = currentValue
currentValue = valueGenerator(currentKey!!)
oldValueHandler?.invoke(oldValue)
currentValue!!
}
}

operator fun getValue(t: Nothing?, property: KProperty<*>): V {
val foreignKey = foreignKeyProvider()
return getOrPut(foreignKey) {
/* We only want a single value, so clear before we add this new one */
clear()
valueGenerator(foreignKey)
return if (foreignKey == currentKey) currentValue!!
else {
currentKey = foreignKey
val oldValue = currentValue
currentValue = valueGenerator(currentKey!!)
oldValueHandler?.invoke(oldValue)
currentValue!!
}
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/ui/NumberField.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ class NumberField<P : Property<Number>>(

companion object {

fun registStyleSheet(styleable : Scene) {
fun registerStyleSheet(styleable : Scene) {
NumberField.javaClass.getResource("number_field.css")?.toExternalForm()?.let { css ->
styleable.stylesheets.add(css)
}
}

fun registStyleSheet(styleable : Parent) {
fun registerStyleSheet(styleable : Parent) {
NumberField.javaClass.getResource("number_field.css")?.toExternalForm()?.let { css ->
styleable.stylesheets.add(css)
}
Expand Down

0 comments on commit a0748ea

Please sign in to comment.