1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { CustomLayout } from '@kit.ArkUI'; 17 18import { 19 Entry, 20 Component, 21 Column, 22 Builder, 23 ForEach, 24 Text, 25 BuilderParam, 26 State, 27 SizeResult, 28 GeometryInfo, 29 Layoutable, 30 ConstraintSizeOptions, 31 Measurable, 32 MeasureResult, 33} from '@kit.ArkUI'; 34 35@Entry 36@Component 37struct Index { 38 build() { 39 Column() { 40 CustomLayout1({ builder: ColumnChildren }) 41 CustomLayout2({ builder: ColumnChildren }) 42 CustomLayout3({ builder: ColumnChildren }) 43 } 44 } 45} 46 47@Builder 48function ColumnChildren() { 49 ForEach([1.0, 2.0, 3.0], (index: number) => { //暂不支持lazyForEach的写法 50 Text('S' + index) 51 .fontSize(30.0) 52 .width(100.0) 53 .height(100.0) 54 .borderWidth(2.0) 55 .offset({ x: 10.0, y: 20.0 }) 56 }) 57} 58 59@Component 60@CustomLayout 61struct CustomLayout1 { 62 @Builder 63 doNothingBuilder() { 64 }; 65 66 @BuilderParam builder: () => void = this.doNothingBuilder; 67 @State startSize: number = 100.0; 68 result: SizeResult = { 69 width: 0.0, 70 height: 0.0 71 }; 72 73 onPlaceChildren(selfLayoutInfo: GeometryInfo, children: Array<Layoutable>, constraint: ConstraintSizeOptions) { 74 let startPos: number = 300.0; 75 children.forEach((child) => { 76 let pos: number = startPos - child.measureResult.height; 77 child.layout({ x: pos, y: pos }) 78 }) 79 } 80 81 build() { 82 this.builder() 83 } 84} 85 86@Component 87@CustomLayout 88struct CustomLayout2 { 89 @Builder 90 doNothingBuilder() { 91 }; 92 93 @BuilderParam builder: () => void = this.doNothingBuilder; 94 @State startSize: number = 100.0; 95 result: SizeResult = { 96 width: 0.0, 97 height: 0.0 98 }; 99 100 onMeasureSize(selfLayoutInfo: GeometryInfo, children: Array<Measurable>, constraint: ConstraintSizeOptions) { 101 let size: number = 100.0; 102 children.forEach((child) => { 103 let result: MeasureResult = child.measure({ minHeight: size, minWidth: size, maxWidth: size, maxHeight: size }) 104 size += result.width / 2.0 105 ; 106 }) 107 this.result.width = 100.0; 108 this.result.height = 400.0; 109 return this.result; 110 } 111 112 build() { 113 this.builder() 114 } 115} 116 117@Component 118@CustomLayout 119struct CustomLayout3 { 120 @Builder 121 doNothingBuilder() { 122 }; 123 124 @BuilderParam builder: () => void = this.doNothingBuilder; 125 @State startSize: number = 100.0; 126 result: SizeResult = { 127 width: 0.0, 128 height: 0.0 129 }; 130 131 onMeasureSize(selfLayoutInfo: GeometryInfo, children: Array<Measurable>, constraint: ConstraintSizeOptions) { 132 let size: number = 100.0; 133 children.forEach((child) => { 134 let result: MeasureResult = child.measure({ minHeight: size, minWidth: size, maxWidth: size, maxHeight: size }) 135 size += result.width / 2.0; 136 }) 137 this.result.width = 100.0; 138 this.result.height = 400.0; 139 return this.result; 140 } 141 142 onPlaceChildren(selfLayoutInfo: GeometryInfo, children: Array<Layoutable>, constraint: ConstraintSizeOptions) { 143 let startPos: number = 300.0; 144 children.forEach((child) => { 145 let pos: number = startPos - child.measureResult.height; 146 child.layout({ x: pos, y: pos }) 147 }) 148 } 149 150 build() { 151 this.builder() 152 } 153} 154 155@Component 156struct CustomLayout4 { 157 build() { 158 159 } 160}