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