• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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
16
17@Component
18struct CompB02 {
19  @State CompValue: string = ''
20
21  aboutToAppear() {
22  }
23
24  aboutToDisappear() {
25  }
26
27  build() {
28    Column() {
29      Button(this.CompValue)
30        .margin(5)
31    }
32  }
33}
34
35@Entry
36@Component
37struct CompA04 {
38  size1: number = 100
39  @State CompValue1: string = "Hello,CompValue1"
40  @State CompValue2: string = "Hello,CompValue2"
41  @State CompValue3: string = "Hello,CompValue3"
42
43  @Builder CompC(value: string) {
44    CompB02({ CompValue: value })
45  }
46
47  @Builder SquareText(label: string) {
48    Text(label)
49      .fontSize(18)
50      .width(1 * this.size1)
51      .height(1 * this.size1)
52  }
53
54  @Builder RowOfSquareTexts(label1: string, label2: string) {
55    Row() {
56      this.SquareText(label1)
57      this.SquareText(label2)
58    }
59    .width(2 * this.size1)
60    .height(1 * this.size1)
61  }
62
63  build() {
64    Column() {
65      Row() {
66        this.SquareText("A")
67        this.SquareText("B")
68      }
69      .width(2 * this.size1)
70      .height(1 * this.size1)
71
72      this.RowOfSquareTexts("C", "D")
73      Column() {
74        this.CompC(this.CompValue1)
75        this.CompC(this.CompValue2)
76        this.CompC(this.CompValue3)
77      }
78      .width(2 * this.size1)
79      .height(2 * this.size1)
80    }
81    .width(2 * this.size1)
82    .height(2 * this.size1)
83  }
84}
85
86@Component
87struct CustomContainer {
88  header: string = ''
89  @BuilderParam noParam: () => void
90  @BuilderParam withParam: any
91  footer: string = ''
92
93  build() {
94    Column() {
95      Text(this.header)
96        .fontSize(30)
97      this.noParam()
98      this.withParam()
99      Text(this.footer)
100        .fontSize(30)
101    }
102  }
103}
104
105@Entry
106@Component
107struct CustomContainerUser {
108  @Builder specificNoParam() {
109    Column() {
110      Text('noParam').fontSize(30)
111    }
112  }
113
114  @Builder SpecificWithParam(label: string) {
115    Column() {
116      Text(label).fontSize(30)
117    }
118  }
119
120  build() {
121    Column() {
122      CustomContainer({
123        header: 'HeaderA',
124        noParam: this.specificNoParam,
125        withParam: this.SpecificWithParam('WithParamA'),
126        footer: 'FooterA'
127      })
128      Divider()
129        .strokeWidth(3)
130        .margin(10)
131      CustomContainer({
132        header: 'HeaderB',
133        noParam: this.specificNoParam,
134        withParam: this.SpecificWithParam('WithParamB'),
135        footer: 'FooterB'
136      })
137    }
138  }
139}
140
141@Component
142struct CustomContainer02 {
143  header: string = ''
144  @BuilderParam closer: () => void
145
146  build() {
147    Column() {
148      Text(this.header)
149        .fontSize(30)
150      this.closer()
151    }
152  }
153}
154
155@Builder function specificParam(label1: string, label2: string) {
156  Column() {
157    Text(label1)
158      .fontSize(30)
159    Text(label2)
160      .fontSize(30)
161  }
162}
163
164@Entry
165@Component
166struct CustomContainerUser02 {
167  @State text: string = 'header'
168
169  build() {
170    Column() {
171      CustomContainer02({
172        header: this.text,
173      }) {
174        Column() {
175          specificParam('testA', 'testB')
176        }.backgroundColor(Color.Yellow)
177        .onClick(() => {
178          this.text = 'changeHeader'
179        })
180      }
181    }
182  }
183}
184
185@Styles function globalFancy () {
186  .width(150)
187  .height(100)
188  .backgroundColor(Color.Pink)
189}
190
191@Entry
192@Component
193struct FancyUse {
194  @Styles componentFancy() {
195    .width(100)
196    .height(200)
197    .backgroundColor(Color.Yellow)
198  }
199
200  build() {
201    Column({ space: 10 }) {
202      Text('FancyA')
203        .globalFancy()
204        .fontSize(30)
205      Text('FancyB')
206        .globalFancy()
207        .fontSize(20)
208      Text('FancyC')
209        .componentFancy()
210        .fontSize(30)
211      Text('FancyD')
212        .componentFancy()
213        .fontSize(20)
214    }
215  }
216}
217
218@Styles function globalFancy02 () {
219  .width(120)
220  .height(120)
221  .backgroundColor(Color.Green)
222}
223
224@Entry
225@Component
226struct FancyUse02 {
227  @Styles componentFancy() {
228    .width(80)
229    .height(80)
230    .backgroundColor(Color.Red)
231  }
232
233  build() {
234    Row({ space: 10 }) {
235      Button('Fancy')
236        .stateStyles({
237          normal: {
238            .width(100)
239            .height(100)
240            .backgroundColor(Color.Blue)
241          },
242          disabled: this.componentFancy,
243          pressed: globalFancy02
244        })
245    }
246  }
247}
248
249// xxx.ets
250@Extend(Text) function fancy (fontSize: number) {
251  .fontColor(Color.Red)
252  .fontSize(fontSize)
253  .fontStyle(FontStyle.Italic)
254  .fontWeight(600)
255}
256
257@Entry
258@Component
259struct FancyUse03 {
260  build() {
261    Row({ space: 10 }) {
262      Text("Fancy")
263        .fancy(16)
264      Text("Fancy")
265        .fancy(24)
266      Text("Fancy")
267        .fancy(32)
268    }
269  }
270}
271
272@CustomDialog
273struct DialogExample02 {
274  controller: CustomDialogController
275  action: () => void
276
277  build() {
278    Row() {
279      Button('Close CustomDialog')
280        .onClick(() => {
281          this.controller.close()
282          this.action()
283        })
284    }.padding(20)
285  }
286}
287
288@Entry
289@Component
290struct CustomDialogUser {
291  dialogController: CustomDialogController = new CustomDialogController({
292    builder: DialogExample02({ action: this.onAccept }),
293    cancel: this.existApp,
294    autoCancel: true
295  });
296
297  onAccept() {
298  }
299
300  existApp() {
301  }
302
303  build() {
304    Column() {
305      Button('Click to open Dialog')
306        .onClick(() => {
307          this.dialogController.open()
308        })
309    }
310  }
311}