Koneko Toujou
09/11/2021, 3:53 PMModifier
implemented such that this
or it
does not need to be passed to it or any of its methods?
As the only way I can think of is as a command list builder or similar
As Modifier
seems to immediately be a very powerful tool to implement parameters and such with due to its simplicity and everything that can be modified is in a single class so it is easy to find a parameter of interest instead of having to search through lots of additional methods and such that might not be related to parameters at allCicero
09/11/2021, 3:57 PMKoneko Toujou
09/11/2021, 3:59 PMCicero
09/11/2021, 4:02 PMCicero
09/11/2021, 4:04 PMAlbert Chang
09/11/2021, 4:27 PMthis
which is the receiver.Koneko Toujou
09/11/2021, 4:58 PMthis
?Koneko Toujou
09/11/2021, 5:01 PMCicero
09/11/2021, 5:01 PMKoneko Toujou
09/11/2021, 5:02 PMI just feel its really fun to navigate trough the API and see how some things were implemented.
🙂Cicero
09/11/2021, 5:14 PM@Stable
fun Modifier.size(size: Dp) = this.then(
SizeModifier(
minWidth = size,
maxWidth = size,
minHeight = size,
maxHeight = size,
enforceIncoming = true,
inspectorInfo = debugInspectorInfo {
name = "size"
value = size
}
)
)
fun Modifier.fixedSize(fixedSize: Dp = 100.dp) = this.then(
other = Modifier.size(fixedSize)
)
Cicero
09/11/2021, 5:14 PMCicero
09/11/2021, 5:15 PMinterface Modifier {
/**
* Accumulates a value starting with [initial] and applying [operation] to the current value
* and each element from outside in.
*
* Elements wrap one another in a chain from left to right; an [Element] that appears to the
* left of another in a `+` expression or in [operation]'s parameter order affects all
* of the elements that appear after it. [foldIn] may be used to accumulate a value starting
* from the parent or head of the modifier chain to the final wrapped child.
*/
fun <R> foldIn(initial: R, operation: (R, Element) -> R): R
/**
* Accumulates a value starting with [initial] and applying [operation] to the current value
* and each element from inside out.
*
* Elements wrap one another in a chain from left to right; an [Element] that appears to the
* left of another in a `+` expression or in [operation]'s parameter order affects all
* of the elements that appear after it. [foldOut] may be used to accumulate a value starting
* from the child or tail of the modifier chain up to the parent or head of the chain.
*/
fun <R> foldOut(initial: R, operation: (Element, R) -> R): R
/**
* Returns `true` if [predicate] returns true for any [Element] in this [Modifier].
*/
fun any(predicate: (Element) -> Boolean): Boolean
/**
* Returns `true` if [predicate] returns true for all [Element]s in this [Modifier] or if
* this [Modifier] contains no [Element]s.
*/
fun all(predicate: (Element) -> Boolean): Boolean
/**
* Concatenates this modifier with another.
*
* Returns a [Modifier] representing this modifier followed by [other] in sequence.
*/
infix fun then(other: Modifier): Modifier =
if (other === Modifier) this else CombinedModifier(this, other)
/**
* A single element contained within a [Modifier] chain.
*/
interface Element : Modifier {
override fun <R> foldIn(initial: R, operation: (R, Element) -> R): R =
operation(initial, this)
override fun <R> foldOut(initial: R, operation: (Element, R) -> R): R =
operation(this, initial)
override fun any(predicate: (Element) -> Boolean): Boolean = predicate(this)
override fun all(predicate: (Element) -> Boolean): Boolean = predicate(this)
}
/**
* The companion object `Modifier` is the empty, default, or starter [Modifier]
* that contains no [elements][Element]. Use it to create a new [Modifier] using
* modifier extension factory functions:
*
* @sample androidx.compose.ui.samples.ModifierUsageSample
*
* or as the default value for [Modifier] parameters:
*
* @sample androidx.compose.ui.samples.ModifierParameterSample
*/
// The companion object implements `Modifier` so that it may be used as the start of a
// modifier extension factory expression.
companion object : Modifier {
override fun <R> foldIn(initial: R, operation: (R, Element) -> R): R = initial
override fun <R> foldOut(initial: R, operation: (Element, R) -> R): R = initial
override fun any(predicate: (Element) -> Boolean): Boolean = false
override fun all(predicate: (Element) -> Boolean): Boolean = true
override infix fun then(other: Modifier): Modifier = other
override fun toString() = "Modifier"
}
}
Cicero
09/11/2021, 5:16 PMKoneko Toujou
09/11/2021, 5:21 PMCicero
09/11/2021, 5:25 PMKoneko Toujou
09/11/2021, 5:27 PMCicero
09/11/2021, 5:27 PMCicero
09/11/2021, 5:29 PMKoneko Toujou
09/11/2021, 5:29 PMCicero
09/11/2021, 5:32 PMKoneko Toujou
09/11/2021, 5:34 PMModifier offset(x : int, y : int) {
return this.andThen(Modifier.drawWithCache {
setOffset(x, y)
}
}
Cicero
09/11/2021, 5:40 PMCicero
09/11/2021, 5:41 PM@Composable
fun Modifier.TestOffset(x : Dp, y : Dp) = this.then(Modifier.offset(x,y))
Koneko Toujou
09/11/2021, 5:42 PMKoneko Toujou
09/11/2021, 5:43 PMCicero
09/11/2021, 5:53 PMZach Klippenstein (he/him) [MOD]
09/11/2021, 8:10 PMAs I don't see any input for variables such as a composable object so I am not sure how it magically worksThere’s no such thing as a composable object. The modifier chain is eventually associated with the LayoutNode that gets emitted by the composition, and then it can be read by the layout and other machinery. https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/layout/Layout.kt;l=84;drc=d1bb6b866401d0c873b942fbfaab53289e88a174
Koneko Toujou
09/12/2021, 2:37 AMKoneko Toujou
09/12/2021, 2:50 AMZach Klippenstein (he/him) [MOD]
09/12/2021, 3:43 AMKoneko Toujou
09/12/2021, 3:48 AMKoneko Toujou
09/12/2021, 3:49 AMtheapache64
10/21/2021, 5:52 PMdebugInspectorInfo
? 🤔Cicero
10/21/2021, 6:03 PMZach Klippenstein (he/him) [MOD]
10/21/2021, 10:27 PM