File tree Expand file tree Collapse file tree 9 files changed +155
-1
lines changed
recompose-ast/src/main/kotlin/recompose
main/kotlin/recompose/composer
test/kotlin/recompose/composer
main/kotlin/recompose/parser/xml
test/kotlin/recompose/parser
recompose-test/src/main/resources Expand file tree Collapse file tree 9 files changed +155
-1
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright 2020 Sebastian Kaspari
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package recompose.ast.viewgroup
18+
19+ import recompose.ast.ViewGroupNode
20+ import recompose.ast.attributes.ViewAttributes
21+ import recompose.ast.attributes.ViewGroupAttributes
22+ import recompose.visitor.Visitor
23+
24+ /* *
25+ * Data classs holding value of a parsed `FrameLayout`
26+ *
27+ * https://developer.android.com/reference/android/widget/FrameLayout
28+ */
29+ data class FrameLayoutNode (
30+ override val viewGroup : ViewGroupAttributes ,
31+ override val view : ViewAttributes ,
32+ ) : ViewGroupNode {
33+ override fun accept (visitor : Visitor ) = visitor.visitFrameLayout(this )
34+ }
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ import recompose.ast.view.TextViewNode
2525import recompose.ast.view.ViewNode
2626import recompose.ast.viewgroup.CardViewNode
2727import recompose.ast.viewgroup.ConstraintLayoutNode
28+ import recompose.ast.viewgroup.FrameLayoutNode
2829import recompose.ast.viewgroup.LinearLayoutNode
2930import recompose.ast.viewgroup.UnknownNode
3031
@@ -41,6 +42,7 @@ interface Visitor {
4142 fun visitCardView (node : CardViewNode )
4243 fun visitImageView (node : ImageViewNode )
4344 fun visitLinearLayout (node : LinearLayoutNode )
45+ fun visitFrameLayout (node : FrameLayoutNode )
4446 fun visitCheckBox (node : CheckBoxNode )
4547 fun visitConstraintLayout (node : ConstraintLayoutNode )
4648 fun visitUnknown (node : UnknownNode )
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ import recompose.ast.view.TextViewNode
2626import recompose.ast.view.ViewNode
2727import recompose.ast.viewgroup.CardViewNode
2828import recompose.ast.viewgroup.ConstraintLayoutNode
29+ import recompose.ast.viewgroup.FrameLayoutNode
2930import recompose.ast.viewgroup.LinearLayoutNode
3031import recompose.ast.viewgroup.UnknownNode
3132import recompose.composer.ext.findChains
@@ -172,6 +173,13 @@ internal class ComposingVisitor : Visitor {
172173 }
173174 }
174175
176+ override fun visitFrameLayout (node : FrameLayoutNode ) {
177+ val rowModifier = ModifierBuilder (node)
178+ writer.writeCall(name = " Box" , parameters = listOf (rowModifier.toCallParameter())) {
179+ node.viewGroup.children.forEach { it.accept(this @ComposingVisitor) }
180+ }
181+ }
182+
175183 override fun visitConstraintLayout (node : ConstraintLayoutNode ) {
176184 val modifier = ModifierBuilder (node)
177185
Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ import recompose.ast.values.InputType
2222import recompose.ast.values.Size
2323
2424/* *
25- * Sealed class for types of parameter values that a passed to a `Composable`.
25+ * Sealed class for types of parameter values that are passed to a `Composable`.
2626 */
2727internal sealed class ParameterValue {
2828 class RawValue (
Original file line number Diff line number Diff line change @@ -300,4 +300,19 @@ class ComposerTest {
300300 """ .trimIndent()
301301 )
302302 }
303+
304+ @Test
305+ fun `FrameLayout with TextView and Button` () {
306+ assertComposing(
307+ fileName = " framelayout-textview-button.xml" ,
308+ """
309+ Box(modifier = Modifier.fillMaxWidth().fillMaxHeight().padding(16.dp)) {
310+ Text(text = "Center", fontSize = 20.sp, modifier = Modifier.fillMaxWidth().fillMaxHeight())
311+ Button(onClick = {}, modifier = Modifier.fillMaxWidth()) {
312+ Text(text = "Button", textAlign = TextAlign.Center)
313+ }
314+ }
315+ """ .trimIndent()
316+ )
317+ }
303318}
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ import recompose.parser.xml.view.imageView
3434import recompose.parser.xml.view.textView
3535import recompose.parser.xml.view.view
3636import recompose.parser.xml.viewgroup.constraintLayout
37+ import recompose.parser.xml.viewgroup.frameLayout
3738import recompose.parser.xml.viewgroup.linearLayout
3839import recompose.parser.xml.viewgroup.unknown
3940
@@ -46,6 +47,7 @@ internal fun XmlPullParser.node(): Node {
4647 return when (name) {
4748 // ViewGroupNode
4849 " LinearLayout" -> linearLayout()
50+ " FrameLayout" -> frameLayout()
4951
5052 // ViewNode
5153 " View" -> view()
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright 2020 Sebastian Kaspari
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package recompose.parser.xml.viewgroup
18+
19+ import org.xmlpull.v1.XmlPullParser
20+ import recompose.ast.viewgroup.FrameLayoutNode
21+ import recompose.parser.xml.viewAttributes
22+ import recompose.parser.xml.viewGroupAttributes
23+
24+ /* *
25+ * Parses a `<FrameLayout> element.
26+ *
27+ * https://developer.android.com/reference/android/widget/FrameLayout
28+ */
29+ fun XmlPullParser.frameLayout (): FrameLayoutNode {
30+ return FrameLayoutNode (
31+ view = viewAttributes(),
32+ viewGroup = viewGroupAttributes()
33+ )
34+ }
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ import recompose.ast.view.TextViewNode
3636import recompose.ast.view.ViewNode
3737import recompose.ast.viewgroup.CardViewNode
3838import recompose.ast.viewgroup.ConstraintLayoutNode
39+ import recompose.ast.viewgroup.FrameLayoutNode
3940import recompose.ast.viewgroup.LinearLayoutNode
4041import recompose.test.utils.assertAST
4142
@@ -418,4 +419,42 @@ class ParserTest {
418419 )
419420 )
420421 }
422+
423+ @Test
424+ fun `FrameLayout with TextView and Button` () {
425+ assertAST(
426+ fileName = " framelayout-textview-button.xml" ,
427+ Layout (
428+ listOf (
429+ FrameLayoutNode (
430+ view = ViewAttributes (
431+ width = LayoutSize .MatchParent ,
432+ height = LayoutSize .MatchParent ,
433+ padding = Padding (all = Size .Dp (16 ))
434+ ),
435+ viewGroup = ViewGroupAttributes (
436+ children = listOf (
437+ TextViewNode (
438+ view = ViewAttributes (
439+ width = LayoutSize .MatchParent ,
440+ height = LayoutSize .MatchParent
441+ ),
442+ text = " Center" ,
443+ textSize = Size .Sp (20 )
444+ ),
445+ ButtonNode (
446+ view = ViewAttributes (
447+ width = LayoutSize .MatchParent ,
448+ height = LayoutSize .WrapContent
449+ ),
450+ text = " Button"
451+ )
452+ )
453+
454+ )
455+ )
456+ )
457+ )
458+ )
459+ }
421460}
Original file line number Diff line number Diff line change 1+ <FrameLayout xmlns : android =" http://schemas.android.com/apk/res/android"
2+ android : layout_width =" match_parent"
3+ android : layout_height =" match_parent"
4+ android : padding =" 16dp" >
5+
6+ <TextView
7+ android : layout_width =" match_parent"
8+ android : layout_height =" match_parent"
9+ android : gravity =" center"
10+ android : text =" Center"
11+ android : textSize =" 20sp" />
12+
13+ <Button
14+ android : layout_width =" match_parent"
15+ android : layout_height =" wrap_content"
16+ android : layout_gravity =" bottom|center"
17+ android : gravity =" bottom|center"
18+ android : text =" Button"
19+ android : textSize =" 20sp" />
20+ </FrameLayout >
You can’t perform that action at this time.
0 commit comments