Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions changelog/release-1.3.9.3.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ package gg.essential.gui.common
import gg.essential.elementa.state.State
import gg.essential.elementa.state.v2.ReferenceHolder

@Deprecated("Use StateV2 instead")
fun <T> State<T>.onSetValueAndNow(listener: (T) -> Unit) = onSetValue(listener).also { listener(get()) }

@Deprecated("See `State.onSetValue`. Use `stateBy`/`effect` instead.")
fun <T> gg.essential.gui.elementa.state.v2.State<T>.onSetValueAndNow(owner: ReferenceHolder, listener: (T) -> Unit) =
onSetValue(owner, listener).also { listener(get()) }

@Deprecated("Use StateV2 instead")
operator fun State<Boolean>.not() = map { !it }
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,12 @@ inline fun <reified T : Effect> UIComponent.get() =
inline fun <reified T : Effect> UIComponent.getOrPut(init: () -> T) =
get<T>() ?: init().also { enableEffect(it) }

@Deprecated("Use StateV2 instead", ReplaceWith("pollingStateV2(initialValue, getter)"))
fun <T> UIComponent.pollingState(initialValue: T? = null, getter: () -> T): State<T> {
val state = BasicState(initialValue ?: getter())
addUpdateFunc { _, _ -> state.set(getter()) }
return state
}

/**
* Turns some impure computation into a pure [StateV2] by evaluating it before every frame.
*
* This should be avoided in favor of pure State-based computations where possible, but may be necessary when
* interfacing with third-party code.
* If the impure part of your computation is the current time, [Observer.systemTime] can likely replace this method.
*/
fun <T> UIComponent.pollingStateV2(initialValue: T? = null, getter: () -> T): StateV2<T> {
val state = mutableStateOf(initialValue ?: getter())
addUpdateFunc { _, _ -> state.set(getter()) }
Expand Down Expand Up @@ -232,7 +224,6 @@ fun UIComponent.hoverScopeV2(parentOnly: Boolean = false): StateV2<Boolean> {
return consumer.state
}

@Deprecated("Use StateV2 instead", ReplaceWith("hoverScopeV2(parentOnly)"))
fun UIComponent.hoverScope(parentOnly: Boolean = false): State<Boolean> =
hoverScopeV2(parentOnly).toV1(this)

Expand Down Expand Up @@ -369,7 +360,6 @@ fun UIComponent.isComponentInParentChain(target: UIComponent): Boolean {
fun UIComponent.isInComponentTree(): Boolean =
this is Window || hasParent && this in parent.children && parent.isInComponentTree()

@Deprecated("ObservableList should be replaced with StateV2's `ListState`")
fun <E> ObservableList<E>.onItemRemoved(callback: (E) -> Unit) {
addObserver { _, arg ->
if (arg is ObservableRemoveEvent<*>) {
Expand All @@ -378,7 +368,6 @@ fun <E> ObservableList<E>.onItemRemoved(callback: (E) -> Unit) {
}
}

@Deprecated("ObservableList should be replaced with StateV2's `ListState`")
fun <E> ObservableList<E>.onItemAdded(callback: (E) -> Unit) {
addObserver { _, arg ->
if (arg is ObservableAddEvent<*>) {
Expand All @@ -387,7 +376,6 @@ fun <E> ObservableList<E>.onItemAdded(callback: (E) -> Unit) {
}
}

@Deprecated("ObservableList should be replaced with StateV2's `ListState`")
@Suppress("UNCHECKED_CAST")
fun <E> ObservableList<E>.toStateV2List(): ListStateV2<E> {
val stateList = mutableStateOf(MutableTrackedList(this.toMutableList()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private fun UIComponent.setupKeyboardNavigation(): UIComponent.(Char, Int) -> Un
}

when (keyCode) {
UKeyboard.KEY_ENTER, UKeyboard.KEY_NUMPADENTER -> simulateLeftClick()
UKeyboard.KEY_ENTER -> simulateLeftClick()
UKeyboard.KEY_TAB -> passFocusToNextComponent(backwards = UKeyboard.isShiftKeyDown())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,12 @@ package gg.essential.gui.elementa.state.v2.combinators
import gg.essential.gui.elementa.state.v2.MutableState
import gg.essential.gui.elementa.state.v2.State

@Deprecated("Exists primarily for easier migration from State v1. Prefer using [State] lambda (with `memo` where necessary) instead.",
replaceWith = ReplaceWith("State { this() && other() }"))
@Suppress("DEPRECATION")
infix fun State<Boolean>.and(other: State<Boolean>) =
zip(other) { a, b -> a && b }

@Deprecated("Exists primarily for easier migration from State v1. Prefer using [State] lambda (with `memo` where necessary) instead.",
replaceWith = ReplaceWith("State { this() || other() }"))
@Suppress("DEPRECATION")
infix fun State<Boolean>.or(other: State<Boolean>) =
zip(other) { a, b -> a || b }

/**
* Creates a new [State] which has the inverse value of this [State].
*
* This is mostly a convenience method so one can write `if_(!myState) {` instead of the more verbose
* `if_({ !myState() }) {`. Both are equivalent.
* This method shouldn't be overused though. If the expression is more complex, the verbose version is usually
* preferred because it generalizes much better.
*/
operator fun State<Boolean>.not() = letState { !it }
operator fun State<Boolean>.not() = map { !it }

operator fun MutableState<Boolean>.not() = bimapState({ !it }, { !it })
operator fun MutableState<Boolean>.not() = bimap({ !it }, { !it })
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,27 @@
package gg.essential.gui.elementa.state.v2.combinators

import gg.essential.gui.elementa.state.v2.MutableState
import gg.essential.gui.elementa.state.v2.Observer
import gg.essential.gui.elementa.state.v2.State
import gg.essential.gui.elementa.state.v2.memo

/** Maps this state into a new state */
@Deprecated("This method always applies `memo` even though it is often unnecessary. " +
"Use `letState` instead, and explicitly call `.memo()` on the result only where required.")
fun <T, U> State<T>.map(mapper: (T) -> U): State<U> {
return memo { mapper(get()) }
}

/**
* Derives a new [State] from this [State].
*
* This method is equivalent to `.let { state -> State { block(state()) } }`.
*
* Note: For repeated or more complex derivations, an explicit [State] or [memo] lambda is likely easier to use.
*/
inline fun <T, U> State<T>.letState(crossinline block: Observer.(T) -> U): State<U> {
val sourceState = this
return State { block(sourceState()) }
}

/** Maps this mutable state into a new mutable state. */
@Deprecated("This method always applies `memo` even though it is often unnecessary. " +
"Use `bimapState` or `bimapMemo` instead.")
fun <T, U> MutableState<T>.bimap(map: (T) -> U, unmap: (U) -> T): MutableState<U> {
return bimapMemo(map, unmap)
}

/**
* Derives a new [MutableState] from this [MutableState].
*
* This variant uses [memo] internally. If this is not required, use [bimapState] instead.
*/
fun <T, U> MutableState<T>.bimapMemo(map: (T) -> U, unmap: (U) -> T): MutableState<U> {
val sourceState = this
return object : MutableState<U>, State<U> by (memo { map(sourceState()) }) {
return object : MutableState<U>, State<U> by this.map(map) {
override fun set(mapper: (U) -> U) {
sourceState.set { unmap(mapper(map(it))) }
this@bimap.set { unmap(mapper(map(it))) }
}
}
}

/**
* Derives a new [MutableState] from this [MutableState].
*
* @see [bimapMemo]
*/
fun <T, U> MutableState<T>.bimapState(map: (T) -> U, unmap: (U) -> T): MutableState<U> {
val sourceState = this
return object : MutableState<U> {
override fun Observer.get(): U = map(sourceState())
override fun set(mapper: (U) -> U) = sourceState.set { unmap(mapper(map(it))) }
}
}

/** Zips this state with another state */
@Deprecated("Exists primarily for easier migration from State v1. Prefer using [State] lambda (with `memo` where necessary) instead.",
replaceWith = ReplaceWith("State { Pair(this(), other()) }"))
@Suppress("DEPRECATION")
fun <T, U> State<T>.zip(other: State<U>): State<Pair<T, U>> = zip(other, ::Pair)

/** Zips this state with another state using [mapper] */
@Deprecated("Exists primarily for easier migration from State v1. Prefer using [State] lambda (with `memo` where necessary) instead.",
replaceWith = ReplaceWith("State { mapper(this(), other()) }"))
fun <T, U, V> State<T>.zip(other: State<U>, mapper: (T, U) -> V): State<V> {
return memo { mapper(this@zip(), other()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@ package gg.essential.gui.elementa.state.v2.combinators

import gg.essential.gui.elementa.state.v2.State

@Deprecated("Exists primarily for easier migration from State v1. Prefer using [State] lambda (with `memo` where necessary) instead.",
replaceWith = ReplaceWith("State { this().contains(other(), ignoreCase = ignoreCase) }"))
@Suppress("DEPRECATION")
fun State<String>.contains(other: State<String>, ignoreCase: Boolean = false) =
zip(other) { a, b -> a.contains(b, ignoreCase) }

@Deprecated("Exists primarily for easier migration from State v1. Prefer using [State] lambda (with `memo` where necessary) instead.",
replaceWith = ReplaceWith("State { this().isEmpty() }"))
@Suppress("DEPRECATION")
fun State<String>.isEmpty() = map { it.isEmpty() }

@Deprecated("Exists primarily for easier migration from State v1. Prefer using [State] lambda (with `memo` where necessary) instead.",
replaceWith = ReplaceWith("State { this().isNotEmpty() }"))
@Suppress("DEPRECATION")
fun State<String>.isNotEmpty() = map { it.isNotEmpty() }
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ package gg.essential.gui.elementa.state.v2.combinators
import gg.essential.gui.elementa.state.v2.MutableState

fun MutableState<Int>.reorder(vararg mapping: Int) =
bimapState({ mapping[it] }, { mapping.indexOf(it) })
bimap({ mapping[it] }, { mapping.indexOf(it) })
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ internal interface Impl {
fun <T> memo(func: Observer.() -> T): State<T>
fun effect(referenceHolder: ReferenceHolder, func: Observer.() -> Unit): () -> Unit

@Suppress("DEPRECATION")
fun <T> stateDelegatingTo(state: State<T>): DelegatingState<T> =
object : DelegatingState<T> {
private val target = mutableStateOf(state)
override fun rebind(newState: State<T>) = target.set(newState)
override fun Observer.get(): T = target()()
}

@Suppress("DEPRECATION")
fun <T> mutableStateDelegatingTo(state: MutableState<T>): DelegatingMutableState<T> =
object : DelegatingMutableState<T> {
private val target = mutableStateOf(state)
Expand Down
Loading