• 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 '@ohos/common';
17import { CommonConstants } from '@ohos/common';
18import { PresetStyleConstants } from '@ohos/common';
19import { localEventManager } from '@ohos/common';
20import { EventConstants } from '@ohos/common';
21import AppItem from './AppItem';
22import FormItem from './FormItem';
23import FolderItem from './FolderItem';
24import { PageDesktopGridStyleConfig } from '../PageDesktopGridStyleConfig';
25
26const TAG = "SwiperPage";
27
28@Component
29export default struct SwiperPage {
30  @StorageLink('formRefresh') formRefresh: string = '';
31  @StorageLink('workSpaceWidth') @Watch('updateDeskTopScreen') workSpaceWidth: number = 0;
32  @State ColumnsTemplate: string = '';
33  @State RowsTemplate: string = ''
34  @Prop @Watch('changeColumnAndRow') gridConfig: string;
35  @State mMargin: number = 0;
36  @State mColumnsGap: number = 0;
37  @State mRowsGap: number = 0;
38  @State mPaddingTop: number = 0;
39  @State mNameLines: number = PresetStyleConstants.DEFAULT_APP_NAME_LINES;
40  @State mMarginTop: number = 0;
41  private mAppListInfo;
42  private mPageDesktopViewModel;
43  private mPageDesktopStyleConfig:PageDesktopGridStyleConfig;
44  private mGridWidth : number;
45  private mGridHeight: number;
46  private mIconSize: number;
47  private mGridSpaceWidth : number;
48  private mGridSpaceHeight: number;
49
50  aboutToAppear(): void {
51    Log.showInfo(TAG, 'aboutToAppear');
52    this.updateDeskTopScreen();
53  }
54
55  updateDeskTopScreen(): void {
56    Log.showInfo(TAG, 'updateDeskTopScreen');
57    this.mPageDesktopStyleConfig = this.mPageDesktopViewModel.getPageDesktopStyleConfig();
58    this.mMargin = this.mPageDesktopStyleConfig.mMargin;
59    this.mColumnsGap = this.mPageDesktopStyleConfig.mColumnsGap;
60    this.mRowsGap = this.mPageDesktopStyleConfig.mRowsGap;
61    this.mPaddingTop = this.mPageDesktopStyleConfig.mPaddingTop;
62    this.mNameLines = this.mPageDesktopStyleConfig.mNameLines;
63    this.mMarginTop = this.mPageDesktopStyleConfig.mDesktopMarginTop;
64    this.mGridWidth = this.mPageDesktopStyleConfig.mGridWidth;
65    this.mGridHeight = this.mPageDesktopStyleConfig.mGridHeight;
66    this.mIconSize = this.mPageDesktopStyleConfig.mIconSize;
67    this.mGridSpaceWidth = Number(this.mPageDesktopViewModel.getWorkSpaceWidth()) - this.mMargin;
68    this.mGridSpaceHeight = Number(this.mPageDesktopViewModel.getWorkSpaceHeight());
69    this.changeConfig();
70    localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_PAGEDESK_ITEM_UPDATE, null);
71  }
72
73  private changeColumnAndRow(): void {
74    this.changeConfig();
75  }
76
77  private changeConfig(): void {
78    let mGridConfig = this.mPageDesktopViewModel.getGridConfig();
79    let column = mGridConfig.column;
80    let row = mGridConfig.row;
81    this.ColumnsTemplate = '';
82    this.RowsTemplate = '';
83    for (let i = 0;i < column; i++) {
84      this.ColumnsTemplate += '1fr '
85    }
86    for (let i = 0;i < row; i++) {
87      this.RowsTemplate += '1fr '
88    }
89  }
90
91  private buildLog(item): boolean {
92    return true;
93  }
94
95  build() {
96    Grid() {
97      ForEach(this.mAppListInfo, (item) => {
98        GridItem() {
99          if(this.buildLog(item)){}
100          if (item.typeId == CommonConstants.TYPE_APP) {
101            AppItem({
102              item: item,
103              mPageDesktopViewModel: this.mPageDesktopViewModel,
104              mNameLines: this.mNameLines
105            })
106          } else if (item.typeId == CommonConstants.TYPE_FOLDER) {
107            FolderItem({
108              folderItem: item,
109              mNameLines: this.mNameLines
110            })
111          } else {
112            FormItem({
113              formItem: item
114            })
115          }
116        }
117        .padding({top:this.mPaddingTop})
118        .rowStart(item.row)
119        .columnStart(item.column)
120        .rowEnd(item.row + item.area[1] - 1)
121        .columnEnd(item.column + item.area[0] - 1)
122      }, (item) => {
123        if (item.typeId == CommonConstants.TYPE_FOLDER || item.typeId == CommonConstants.TYPE_CARD) {
124          return JSON.stringify(item) + this.formRefresh;
125        } else {
126          return `${item.typeId}${item.row}${item.column}`;
127        }
128      })
129    }
130    .columnsTemplate(this.ColumnsTemplate)
131    .rowsTemplate(this.RowsTemplate)
132    .columnsGap(this.mColumnsGap)
133    .rowsGap(this.mRowsGap)
134    .width(this.mGridWidth)
135    .height(this.mGridHeight)
136    .margin({
137      right: this.mMargin,
138      left: this.mMargin
139    })
140    .onMouse((event: MouseEvent) => {
141      if (event.button == MouseButton.Right) {
142        AppStorage.SetOrCreate('selectDesktopAppItem', '');
143      }
144    })
145    .onClick(() => {
146      AppStorage.SetOrCreate('selectDesktopAppItem', '')
147    })
148    .onDragEnter((event: DragEvent, extraParams: string) => {
149      Log.showInfo(TAG, `onDragEnter extraParams: ${extraParams}, event: [${event.getX()}, ${event.getY()}]`);
150    })
151    .onDragLeave((event: DragEvent, extraParams: string) => {
152      Log.showInfo(TAG, `onDragLeave event: [${event.getX()}, ${event.getY()}]`);
153    })
154    .onDrop((event: DragEvent, extraParams: string) => {
155      const dragItemType: number = AppStorage.Get('dragItemType');
156      Log.showInfo(TAG, `onDrop event: [${event.getX()}, ${event.getY()}]`);
157      if (dragItemType === CommonConstants.DRAG_FROM_DESKTOP
158      || (dragItemType === CommonConstants.DRAG_FROM_DOCK && AppStorage.Get('deviceType') === CommonConstants.DEFAULT_DEVICE_TYPE)) {
159        const dragResult = globalThis.PageDesktopDragHandler.onDragDrop(event.getX(), event.getY());
160        Log.showInfo(TAG, `onDrop dragResult: ${dragResult}`);
161        AppStorage.SetOrCreate('selectAppIndex', null);
162        AppStorage.SetOrCreate('isDrag', false);
163        if (!dragResult) {
164          AppStorage.SetOrCreate('dragItemInfo', {});
165        } else {
166          // Wait for the UI rendering to end.
167          setTimeout(() => {
168            AppStorage.SetOrCreate('dragItemInfo', {});
169          }, 50);
170        }
171      }
172    })
173  }
174}