• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2021-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
16import { Log } from '../utils/Log';
17import { AppBubble } from './AppBubble';
18import { AppItemInfo } from '../bean/AppItemInfo';
19import { CommonConstants } from '../constants/CommonConstants';
20import { AppGridStyleConfig } from '../layoutconfig/AppGridStyleConfig';
21
22const TAG = 'AppGrid';
23
24@Component
25export struct AppGrid {
26  @Link appGridList: Array<AppItemInfo>;
27  @Link appGridStyleConfig: AppGridStyleConfig;
28  onItemClick: Function = null;
29  buildMenu: Function = null;
30  @State isScroll: boolean = false;
31
32  aboutToDisappear(): void {
33    this.onItemClick = null;
34    this.buildMenu = null;
35  }
36
37  private getColumnsTemplate() {
38    let columnsTemplate = '';
39    for (let i = 0; i < this.appGridStyleConfig.mColumns; i++) {
40      columnsTemplate += ' 1fr';
41    }
42    return columnsTemplate;
43  }
44
45  private getRowsTemplate() {
46    let rowsTemplate = '';
47    if (this.isScroll) {
48      return rowsTemplate;
49    }
50    for (let i = 0; i < this.appGridStyleConfig.mRows; i++) {
51      rowsTemplate += ' 1fr';
52    }
53    return rowsTemplate;
54  }
55
56  build() {
57    Grid() {
58      ForEach(this.appGridList, (item) => {
59        GridItem() {
60          Column() {
61            AppBubble({
62              iconSize: this.appGridStyleConfig.mIconSize,
63              nameSize: this.appGridStyleConfig.mNameSize,
64              nameFontColor: this.appGridStyleConfig.mNameFontColor,
65              nameHeight: this.appGridStyleConfig.mNameHeight,
66              appName: item.appName,
67              bundleName: item.bundleName,
68              moduleName: item.moduleName,
69              abilityName: item.abilityName,
70              appIconId: item.appIconId,
71              appLabelId: item.appLabelId,
72              badgeNumber: item.badgeNumber,
73              menuInfo: this.buildMenu(item),
74              nameLines: this.appGridStyleConfig.mNameLines,
75              mPaddingTop: this.appGridStyleConfig.mIconMarginVertical,
76              dragStart: () => {}
77            })
78          }
79          .onClick((event: ClickEvent) => {
80            this.onItemClick(event, item);
81          })
82          .onMouse((event: MouseEvent) => {
83            Log.showInfo(TAG, `onMouse MouseType: ${event.button}`);
84            if (event.button == MouseButton.Right) {
85              event.stopPropagation();
86            }
87          })
88        }
89        .width(this.appGridStyleConfig.mAppItemSize)
90        .height(this.appGridStyleConfig.mAppItemSize)
91        .transition({ scale: { x: 0.5, y: 0.5 } })
92      }, (item) => JSON.stringify(item))
93    }
94    .columnsTemplate(this.getColumnsTemplate())
95    .rowsTemplate(this.getRowsTemplate())
96    .columnsGap(this.appGridStyleConfig.mColumnsGap)
97    .rowsGap(this.appGridStyleConfig.mRowsGap)
98  }
99}