• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-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 router from '@ohos.router';
17import worker from '@ohos.worker';
18import { IconItem } from './IconView';
19import { IconItemSource } from './IconItemSource';
20
21
22class PicData {
23  public iconItemSourceList: IconItemSource[] = [];
24
25  public setUp(): string {
26    for (let index = 0; index < 20; index++) {
27      const numStart: number = index * 6;
28      // 此处循环使用6张图片资源
29      this.iconItemSourceList.push(new IconItemSource($r('app.media.nearby'), `item${numStart + 1}`));
30      this.iconItemSourceList.push(new IconItemSource($r('app.media.scan'), `item${numStart + 2}`));
31      this.iconItemSourceList.push(new IconItemSource($r('app.media.shop'), `item${numStart + 3}`));
32      this.iconItemSourceList.push(new IconItemSource($r('app.media.cards'), `item${numStart + 4}`));
33      this.iconItemSourceList.push(new IconItemSource($r('app.media.album'), `item${numStart + 5}`));
34      this.iconItemSourceList.push(new IconItemSource($r('app.media.applet'), `item${numStart + 6}`));
35    }
36    return "setUpIconItemSourceList success!";
37  }
38}
39
40@Component
41export struct WorkerCallGlobalUsage {
42  @State iconItemSourceList: IconItemSource[] = [];
43  private workerInstance: worker.ThreadWorker | undefined = undefined;
44  private message: string = "run setUp in picData";
45
46  aboutToAppear() {
47    this.initWorker();
48  }
49
50  build() {
51    Column() {
52      // 顶部导航栏
53      Row() {
54        Image($r("app.media.back"))
55          .height(24)
56          .width(24)
57          .onClick(() => {
58            router.back();
59          })
60
61        Text($r('app.string.scenario_5'))
62          .fontSize(24)
63          .lineHeight(30)
64          .fontColor(Color.Black)
65          .fontWeight(FontWeight.Bold)
66          .textAlign(TextAlign.Start)
67          .textOverflow({ overflow: TextOverflow.MARQUEE })
68          .margin({
69            left: 16
70          })
71      }
72      .width($r('app.string.percent_100'))
73      .height(50)
74      .justifyContent(FlexAlign.Start)
75      .padding({
76        left: 24
77      })
78
79      Button($r('app.string.load_image'), { type: ButtonType.Normal, stateEffect: true })
80        .fontSize(14)
81        .borderRadius(8)
82        .backgroundColor(Color.Blue)
83        .width(250)
84        .height(60)
85        .margin({
86          top: 30
87        })
88        .onClick(() => {
89          if (this.workerInstance !== undefined) {
90            this.workerInstance.postMessage(this.message);
91          }
92        })
93
94      if (this.iconItemSourceList.length > 0) {
95        List({ space: 20 }) {
96          ForEach(this.iconItemSourceList, (item: IconItemSource) => {
97            ListItem() {
98              IconItem({ image: item.image, text: item.text });
99            }
100          }, (item: IconItemSource, index) => index.toString())
101        }
102        .divider({ strokeWidth: 2, startMargin: 20, endMargin: 20 }) // 每行之间的分界线
103        .width($r('app.string.percent_100'))
104        .height($r('app.string.percent_100'))
105        .layoutWeight(1)
106      }
107    }
108    .width($r('app.string.percent_100'))
109    .height($r('app.string.percent_100'))
110    .alignItems(HorizontalAlign.Center)
111  }
112
113  initWorker() {
114    if (this.workerInstance !== undefined) {
115      let picData: PicData = new PicData();
116      picData.iconItemSourceList = this.iconItemSourceList;
117      // 在ThreadWorker实例上注册registerObj
118      this.workerInstance.registerGlobalCallObject("picData", picData);
119    }
120  }
121}