idlize
1/*
2* Copyright (c) 2022-2023 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// Bare empty struct
18
19@Entry
20@Component
21export struct EntryExample {
22build(){ }
23}
24
25@Component
26struct ComponentExample {
27build(){ }
28}
29
30
31// A builder call in the build() function
32
33@Component
34struct BuildExample {
35build(){
36Text("message")
37.fontColor(Color.Red)
38.width(100)
39}
40}
41
42// Simple state management annotations
43
44@Component
45struct StateExample {
46@State x: string = "hello"
47build(){
48Text(this.x)
49}
50}
51
52@Component
53struct LinkExample {
54@Link x: string
55build(){
56Text(this.x)
57}
58}
59
60@Component
61struct PropExample {
62@Prop x: string
63build(){
64Text(this.x)
65}
66}
67
68@Component
69struct PropInitializedExample {
70@Prop x: string = "init"
71build(){
72Text(this.x)
73}
74}
75
76@Component
77struct ProvideExample {
78@Provide("name") x: string = "text"
79build(){
80Text(this.x)
81}
82}
83
84@Component
85struct ConsumeExample {
86@Consume("name") x: string
87build(){
88Text(this.x)
89}
90}
91
92// Builder functions
93
94@Component
95struct BuilderExample {
96@Builder foo() {
97Text("hello")
98}
99build() {
100this.foo()
101}
102}
103
104@Builder
105function bar() {
106Text("hello")
107}
108
109@Component
110struct GlobalBuilderExample {
111build() {
112bar()
113}
114}
115
116@Component
117struct BuilderParamExample {
118@BuilderParam foo: () => {}
119build() {
120this.foo()
121}
122}
123
124// @Styles @Extend @AnimatableExtend
125
126@Styles
127function looks() {
128.height(500)
129.width(400)
130.backgroundColor(Color.Gray)
131}
132
133@Component
134struct StylesExample {
135build() {
136Text()
137.width(17)
138.looks()
139}
140}
141
142@Component
143struct StylesMethodExample {
144@Styles
145nice() {
146.height(500)
147.width(400)
148.backgroundColor(Color.Gray)
149}
150
151build() {
152Text()
153.width(17)
154.nice()
155}
156}
157
158
159@Extend(Column)
160function clown() {
161.height(500)
162.width(400)
163.backgroundColor(Color.Gray)
164}
165
166@Component
167struct ExtendExample {
168build() {
169Column()
170.width(17)
171.clown()
172}
173}
174
175@AnimatableExtend(Text)
176function attributeExtend(n: number, unused: any) {
177.fontSize(n)
178}
179
180@Component
181struct AnimatableExtendExample {
182build() {
183Text()
184.width(17)
185.attributeExtend(50, "unused")
186}
187}
188
189
190// Advanced state management
191
192@Component
193struct WatchExample {
194@State @Watch("watchFunction") x: string = "hello"
195watchFunction() {
196console.log("Watch function")
197}
198build() {
199}
200}
201
202@Component
203struct StorageLinkExample {
204@StorageLink("storage") link: string = "Start"
205build() {
206}
207}
208
209@Component
210struct StoragePropExample {
211@StorageProp("storage") prop: string = "Start"
212build() {
213}
214}
215
216// CustomDialog
217
218@CustomDialog
219struct CustomDialogExample {
220controller: CustomDialogController
221build() {
222}
223}
224
225@Component
226export struct CustomDialogControllerExample {
227dialogController: CustomDialogController = new CustomDialogController({
228builder: CustomDialogExample(),
229autoCancel: true,
230alignment: DialogAlignment.Default,
231offset: { dx: 0, dy: 0 },
232gridCount: 4,
233customStyle: false
234})
235
236aboutToAppear() {
237this.dialogController.open()
238}
239
240aboutToDisappear() {
241this.dialogController.close()
242}
243
244build() {
245}
246}
247
248// ObjectLink and Observed
249
250@Observed
251class ObservedExample {
252public c: number
253constructor(c: number) {
254this.c = c
255}
256}
257
258@Component
259struct ObjectLinkExample {
260@ObjectLink a: ObservedExample
261
262build() {
263Button()
264.onClick(() => {
265this.a.c += 1
266})
267}
268}
269
270@Component
271struct ObjectLinkExampleParent {
272@State a: ObservedExample[] = [new ObservedExample(0), new ObservedExample(0)]
273
274build() {
275ObjectLinkExample()
276}
277}
278
279@Component
280struct PlainPropertyExample {
281field = 17
282}
283
284@Component
285struct CallExample {
286@State
287state: number = 17
288build() {
289Child({counter: this.$state})
290}
291}
292
293@Component
294struct Child {
295@Link
296counter: number
297build() {}
298}
299