@@ -29,7 +29,9 @@ import androidx.ui.geometry.Offset
2929import androidx.ui.graphics.Color
3030import androidx.ui.graphics.Paint
3131import androidx.ui.graphics.Shadow
32- import androidx.ui.graphics.withSave
32+ import androidx.ui.graphics.painter.Stroke
33+ import androidx.ui.graphics.painter.drawCanvas
34+ import androidx.ui.graphics.painter.rotate
3335import androidx.ui.graphics.withSaveLayer
3436import androidx.ui.layout.fillMaxSize
3537import androidx.ui.text.TextStyle
@@ -49,17 +51,19 @@ import com.squareup.workflow.ui.compose.bindCompose
4951internal fun placeholderViewFactory (modifier : Modifier ): ViewFactory <Any > =
5052 bindCompose { rendering, _ ->
5153 Text (
52- modifier = modifier/* .fillMaxSize() */
54+ modifier = modifier
5355 .clipToBounds()
5456 .drawBehind {
55- withSaveLayer(size.toRect(), Paint ().apply { alpha = .2f }) {
56- drawRect(size.toRect(), Paint ().apply { color = Color .Gray })
57- drawCrossHatch(
58- color = Color .Red ,
59- strokeWidth = 2 .dp,
60- spaceWidth = 5 .dp,
61- angle = 45f
62- )
57+ drawCanvas { canvas, size ->
58+ canvas.withSaveLayer(size.toRect(), Paint ().apply { alpha = .2f }) {
59+ canvas.drawRect(size.toRect(), Paint ().apply { color = Color .Gray })
60+ drawCrossHatch(
61+ color = Color .Red ,
62+ strokeWidth = 2 .dp,
63+ spaceWidth = 5 .dp,
64+ angle = 45f
65+ )
66+ }
6367 }
6468 },
6569 text = rendering.toString(),
@@ -74,25 +78,25 @@ internal fun placeholderViewFactory(modifier: Modifier): ViewFactory<Any> =
7478@Preview(widthDp = 200 , heightDp = 200 )
7579@Composable private fun PreviewStubViewBindingOnWhite () {
7680 Box (backgroundColor = Color .White ) {
77- placeholderViewFactory(Modifier ).preview(
78- rendering = " preview" ,
79- modifier = Modifier .fillMaxSize()
80- .drawBorder(size = 1 .dp, color = Color .Red )
81- )
81+ PreviewStubBindingPreviewTemplate ()
8282 }
8383}
8484
8585@Preview(widthDp = 200 , heightDp = 200 )
8686@Composable private fun PreviewStubViewBindingOnBlack () {
8787 Box (backgroundColor = Color .Black ) {
88- placeholderViewFactory(Modifier ).preview(
89- rendering = " preview" ,
90- modifier = Modifier .fillMaxSize()
91- .drawBorder(size = 1 .dp, color = Color .Red )
92- )
88+ PreviewStubBindingPreviewTemplate ()
9389 }
9490}
9591
92+ @Composable private fun PreviewStubBindingPreviewTemplate () {
93+ placeholderViewFactory(Modifier ).preview(
94+ rendering = " preview" ,
95+ placeholderModifier = Modifier .fillMaxSize()
96+ .drawBorder(size = 1 .dp, color = Color .Red )
97+ )
98+ }
99+
96100private fun DrawScope.drawCrossHatch (
97101 color : Color ,
98102 strokeWidth : Dp ,
@@ -109,34 +113,27 @@ private fun DrawScope.drawHatch(
109113 spaceWidth : Dp ,
110114 angle : Float
111115) {
112- val strokeWidthPx = strokeWidth.toPx()
113- .value
114- val paint = Paint ().also {
115- it.color = color.scaleColors(.5f )
116- it.strokeWidth = strokeWidthPx
117- }
118-
119- withSave {
120- val halfWidth = size.width.value / 2
121- val halfHeight = size.height.value / 2
122- translate(halfWidth, halfHeight)
123- rotate(angle)
124- translate(- halfWidth, - halfHeight)
116+ val strokeWidthPx = strokeWidth.toPx().value
117+ val spaceWidthPx = spaceWidth.toPx().value
118+ val strokeColor = color.scaleColors(.5f )
119+ val stroke = Stroke (width = strokeWidthPx)
125120
121+ rotate(angle) {
126122 // Draw outside our bounds to fill the space even when rotated.
127- val left = - size.width.value
128- val right = size.width.value * 2
129- val top = - size.height.value
130- val bottom = size.height.value * 2
123+ val left = - size.width
124+ val right = size.width * 2
125+ val top = - size.height
126+ val bottom = size.height * 2
131127
132128 var y = top + strokeWidthPx * 2f
133129 while (y < bottom) {
134130 drawLine(
131+ strokeColor,
135132 Offset (left, y),
136133 Offset (right, y),
137- paint
134+ stroke = stroke
138135 )
139- y + = spaceWidth.toPx().value * 2
136+ y + = spaceWidthPx * 2
140137 }
141138 }
142139}
0 commit comments