-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from mori-atsushi/border
Add BorderStyle
- Loading branch information
Showing
11 changed files
with
299 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
...-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/StyleBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...heet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/border/BorderSetter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.moriatsushi.compose.stylesheet.border | ||
|
||
import androidx.compose.foundation.BorderStroke | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.Dp | ||
import com.moriatsushi.compose.stylesheet.component.StyleSheetComponentApi | ||
import com.moriatsushi.compose.stylesheet.graphics.BrushSetterCallback | ||
import com.moriatsushi.compose.stylesheet.graphics.BrushStyle | ||
import com.moriatsushi.compose.stylesheet.token.TokenSetterCallback | ||
|
||
class BorderSetter @StyleSheetComponentApi constructor() { | ||
@StyleSheetComponentApi | ||
var value: BorderStyle? = null | ||
private set | ||
|
||
/** | ||
* A width of the border. | ||
*/ | ||
val width: TokenSetterCallback<Dp> = TokenSetterCallback { | ||
value = value?.copy(width = it) ?: BorderStyle(width = it) | ||
} | ||
|
||
/** | ||
* A brush of the border. | ||
*/ | ||
val brush: BrushSetterCallback = BrushSetterCallback { | ||
value = value?.copy(brush = it) ?: BorderStyle(brush = it) | ||
} | ||
|
||
/** | ||
* A color of the border. | ||
*/ | ||
val color: TokenSetterCallback<Color> = TokenSetterCallback { | ||
brush += BrushStyle(it) | ||
} | ||
|
||
operator fun plusAssign(border: BorderStyle?) { | ||
if (border != null) { | ||
value = border | ||
} | ||
} | ||
|
||
operator fun plusAssign(border: BorderStroke) { | ||
this.value = BorderStyle(border) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...sheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/border/BorderStyle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.moriatsushi.compose.stylesheet.border | ||
|
||
import androidx.compose.foundation.BorderStroke | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Immutable | ||
import androidx.compose.ui.unit.Dp | ||
import com.moriatsushi.compose.stylesheet.component.StyleSheetComponentApi | ||
import com.moriatsushi.compose.stylesheet.graphics.BrushStyle | ||
import com.moriatsushi.compose.stylesheet.token.Token | ||
import com.moriatsushi.compose.stylesheet.token.value | ||
|
||
@Immutable | ||
sealed interface BorderStyle { | ||
val width: Token<Dp> | ||
val brush: Token<BrushStyle> | ||
|
||
companion object { | ||
val Unspecified: BorderStyle = BorderStyle() | ||
} | ||
} | ||
|
||
@Composable | ||
@StyleSheetComponentApi | ||
fun BorderStyle.asBorderStroke(): BorderStroke? = | ||
if (this != BorderStyle.Unspecified) { | ||
BorderStroke(width = width.value, brush = brush.value.asBrush()) | ||
} else { | ||
null | ||
} | ||
|
||
internal fun BorderStyle.copy( | ||
width: Token<Dp> = this.width, | ||
brush: Token<BrushStyle> = this.brush, | ||
): BorderStyle = BorderStyle(width = width, brush = brush) | ||
|
||
internal fun BorderStyle( | ||
width: Token<Dp> = Token(Dp.Unspecified), | ||
brush: Token<BrushStyle> = Token(BrushStyle.Unspecified), | ||
): BorderStyle = BorderStyleImpl(width = width, brush = brush) | ||
|
||
internal fun BorderStyle(borderStroke: BorderStroke): BorderStyle = | ||
BorderStyle(width = Token(borderStroke.width), brush = Token(BrushStyle(borderStroke.brush))) | ||
|
||
@Immutable | ||
private data class BorderStyleImpl( | ||
override val width: Token<Dp>, | ||
override val brush: Token<BrushStyle>, | ||
) : BorderStyle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
.../src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/graphics/BrushSetterCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.moriatsushi.compose.stylesheet.graphics | ||
|
||
import androidx.compose.ui.graphics.Brush | ||
import com.moriatsushi.compose.stylesheet.component.StyleSheetComponentApi | ||
import com.moriatsushi.compose.stylesheet.token.Token | ||
|
||
class BrushSetterCallback @StyleSheetComponentApi constructor( | ||
private val onSet: (Token<BrushStyle>) -> Unit, | ||
) { | ||
/** | ||
* Sets the given [token]. | ||
*/ | ||
operator fun plusAssign(token: Token<BrushStyle>?) { | ||
if (token != null) { | ||
onSet(token) | ||
} | ||
} | ||
|
||
/** | ||
* Sets the given [brush]. | ||
*/ | ||
operator fun plusAssign(brush: BrushStyle) { | ||
onSet(Token(brush)) | ||
} | ||
|
||
/** | ||
* Sets the given [brush]. | ||
*/ | ||
operator fun plusAssign(brush: Brush) { | ||
onSet(Token(BrushStyle(brush))) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...heet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/graphics/BrushStyle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.moriatsushi.compose.stylesheet.graphics | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.graphics.Brush | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.SolidColor | ||
import com.moriatsushi.compose.stylesheet.component.StyleSheetComponentApi | ||
import com.moriatsushi.compose.stylesheet.token.Token | ||
import com.moriatsushi.compose.stylesheet.token.value | ||
|
||
sealed interface BrushStyle { | ||
@StyleSheetComponentApi | ||
@Composable | ||
fun asBrush(): Brush | ||
|
||
companion object { | ||
val Unspecified: BrushStyle = BrushStyle(Color.Unspecified) | ||
} | ||
} | ||
|
||
fun BrushStyle(color: Token<Color>): BrushStyle = SolidColorBrush(color = color) | ||
|
||
fun BrushStyle(color: Color): BrushStyle = BrushStyle(color = Token(color)) | ||
|
||
fun BrushStyle(brush: Brush): BrushStyle = BrushHolder(brush = brush) | ||
|
||
private data class SolidColorBrush(val color: Token<Color>) : BrushStyle { | ||
@Composable | ||
override fun asBrush(): Brush = SolidColor(color.value) | ||
} | ||
|
||
private data class BrushHolder(private val brush: Brush) : BrushStyle { | ||
@Composable | ||
override fun asBrush(): Brush = brush | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ore/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/token/TokenSetterCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.moriatsushi.compose.stylesheet.token | ||
|
||
import com.moriatsushi.compose.stylesheet.component.StyleSheetComponentApi | ||
|
||
class TokenSetterCallback<T> @StyleSheetComponentApi constructor( | ||
private val onSet: (Token<T>) -> Unit, | ||
) { | ||
/** | ||
* Sets the given [token]. | ||
*/ | ||
operator fun plusAssign(token: Token<T>?) { | ||
if (token != null) { | ||
onSet(token) | ||
} | ||
} | ||
|
||
/** | ||
* Sets the given [value]. | ||
*/ | ||
operator fun plusAssign(value: T) { | ||
onSet(Token(value)) | ||
} | ||
} |
Oops, something went wrong.