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 { 17 AppMenu, 18 AppIcon, 19} from '@ohos/common/component' 20import { 21 DockItemInfo, 22 LauncherDragItemInfo, 23 CommonConstants, 24 StyleConstants, 25 ResourceManager, 26 Log, 27 MenuInfo, 28 BadgeManager 29} from '@ohos/common'; 30import { SmartDockStyleConfig } from '../config/SmartDockStyleConfig'; 31import SmartDockDragHandler from '../common/SmartDockDragHandler'; 32 33let mSmartDockStyleConfig: SmartDockStyleConfig | null = null; 34const TAG = 'ResidentLayout'; 35 36interface DockPadding { 37 right: number; 38 left: number; 39 top: number; 40 bottom: number; 41} 42 43@Component 44export default struct ResidentLayout { 45 @StorageLink('dockPadding') dockPadding: DockPadding = { right: 0, left: 0, top: 0, bottom: 0 }; 46 @StorageLink('residentList') @Watch('onDockListChange') appList: Array<DockItemInfo> = []; 47 @Link mSmartDockStyleConfig: SmartDockStyleConfig; 48 onItemClick: (event: ClickEvent, item: DockItemInfo) => void = (event: ClickEvent, item: DockItemInfo) => {}; 49 buildMenu: (item: DockItemInfo) => MenuInfo[] = (item: DockItemInfo): MenuInfo[] => []; 50 onDockListChangeFunc: () => void = () => {}; 51 52 aboutToAppear(): void { 53 mSmartDockStyleConfig = this.mSmartDockStyleConfig; 54 this.onDockListChange(); 55 } 56 57 aboutToDisappear(): void { 58 } 59 60 getListWidth(): number { 61 let residentMaxNum = this.mSmartDockStyleConfig.mMaxDockNum; 62 let width = 0.0; 63 if (AppStorage.get("deviceType") == CommonConstants.DEFAULT_DEVICE_TYPE) { 64 Log.showDebug(TAG, `getListWidth mDockPadding: ${this.mSmartDockStyleConfig.mDockPadding}, mMaxNum: ${residentMaxNum}`); 65 width = this.mSmartDockStyleConfig.mDockPadding * 2 + residentMaxNum * (this.mSmartDockStyleConfig.mListItemWidth) + (residentMaxNum - 1) * (this.mSmartDockStyleConfig.mListItemGap); 66 Log.showDebug(TAG, `getListWidth width: ${width}`); 67 } else { 68 if (this.appList == null || this.appList.length === 0) { 69 } else { 70 let num = this.appList.length; 71 if (num > residentMaxNum) { 72 num = residentMaxNum; 73 } 74 width = this.mSmartDockStyleConfig.mDockPadding * 2 + num * (this.mSmartDockStyleConfig.mListItemWidth) + (num - 1) * (this.mSmartDockStyleConfig.mListItemGap); 75 } 76 } 77 return width; 78 } 79 80 private onDockListChange() { 81 this.onDockListChangeFunc(); 82 } 83 84 build() { 85 if (this.getListWidth && this.getListWidth() !== 0) { 86 Row() { 87 List({ space: this.appList.length == 0 ? 0 : this.mSmartDockStyleConfig.mListItemGap }) { 88 ForEach(this.appList, (item: DockItemInfo) => { 89 ListItem() { 90 AppItem({ 91 appInfo: item, 92 buildMenu: this.buildMenu, 93 onItemClick: this.onItemClick 94 }) 95 } 96 }, (item: DockItemInfo) => JSON.stringify(item)) 97 } 98 .enableScrollInteraction(false) 99 .scrollBar(BarState.Off) 100 .height('100%') 101 .animation({ 102 curve: Curve.Friction 103 }) 104 .listDirection(Axis[this.mSmartDockStyleConfig.mListDirection]) 105 } 106 .backgroundColor(this.mSmartDockStyleConfig.mBackgroundColor) 107 .borderRadius(this.mSmartDockStyleConfig.mDockRadius) 108 .backdropBlur(this.mSmartDockStyleConfig.mBackdropBlur) 109 .padding(this.appList.length == 0 ? this.mSmartDockStyleConfig.mDockPadding : this.dockPadding) 110 .width(this.getListWidth()) 111 .height(this.mSmartDockStyleConfig.mDockHeight) 112 .justifyContent(FlexAlign.Center) 113 .onDragEnter((event: DragEvent, extraParams: string) => { 114 Log.showDebug(TAG, `onDragEnter extraParams: ${extraParams}, event: [${event.getWindowX()}, ${event.getWindowY()}]`); 115 }) 116 .onDragMove((event: DragEvent, extraParams: string) => { 117 Log.showDebug(TAG, `onDragMove event: [${event.getWindowX()}, ${event.getWindowY()}]`); 118 }) 119 .onDragLeave((event: DragEvent, extraParams: string) => { 120 Log.showDebug(TAG, `onDragLeave event: [${event.getWindowX()}, ${event.getWindowY()}]`); 121 }) 122 .onDrop((event: DragEvent, extraParams: string) => { 123 Log.showInfo(TAG, `onDrop event: [${event.getWindowX()}, ${event.getWindowY()}]`); 124 const dragResult = SmartDockDragHandler.getInstance().onDragDrop(event.getWindowX(), event.getWindowY()); 125 AppStorage.setOrCreate('selectAppIndex', null); 126 const dragItemInfo: LauncherDragItemInfo = AppStorage.get('dragItemInfo') as LauncherDragItemInfo; 127 if (dragItemInfo.bundleName) { 128 BadgeManager.getInstance().updateBadgeNumber(dragItemInfo.bundleName, dragItemInfo.badgeNumber); 129 } 130 if (!dragResult) { 131 AppStorage.setOrCreate<LauncherDragItemInfo>('dragItemInfo', new LauncherDragItemInfo()); 132 } else { 133 // Wait for the UI rendering to end. 134 setTimeout(() => { 135 AppStorage.setOrCreate<LauncherDragItemInfo>('dragItemInfo', new LauncherDragItemInfo()); 136 }, 50); 137 } 138 }) 139 } 140 } 141} 142 143@Component 144struct AppItem { 145 @StorageLink('dragItemInfo') smartDragItemInfo: LauncherDragItemInfo = new LauncherDragItemInfo(); 146 @StorageLink('dragItemType') dragItemType: number = CommonConstants.DRAG_FROM_DOCK; 147 @State isShow: boolean = false; 148 onItemClick: (event: ClickEvent, item: DockItemInfo) => void = (event: ClickEvent, item: DockItemInfo) => {}; 149 appInfo: DockItemInfo = new DockItemInfo(); 150 buildMenu: (item: DockItemInfo) => MenuInfo[] = (item: DockItemInfo): MenuInfo[] => []; 151 private menuInfo: MenuInfo[] = []; 152 153 aboutToAppear(): void { 154 this.menuInfo = this.buildMenu(this.appInfo); 155 } 156 157 aboutToDisappear(): void { 158 this.isShow = false; 159 this.menuInfo = []; 160 } 161 162 private getLongPress(): boolean { 163 return AppStorage.get('isLongPress') as boolean; 164 } 165 166 @Builder MenuBuilder() { 167 Column() { 168 AppMenu({ 169 menuInfoList: this.menuInfo, 170 closeMenu: () => { 171 this.isShow = false; 172 } 173 }) 174 } 175 .alignItems(HorizontalAlign.Center) 176 .justifyContent(FlexAlign.Center) 177 .width(StyleConstants.CONTEXT_MENU_WIDTH) 178 .height(StyleConstants.DEFAULT_40 * this.menuInfo.length + StyleConstants.DEFAULT_8) 179 } 180 181 build() { 182 Column() { 183 AppIcon({ 184 iconSize: mSmartDockStyleConfig?.mIconSize as number, 185 iconId: this.appInfo.appIconId, 186 bundleName: this.appInfo.bundleName, 187 moduleName: this.appInfo.moduleName, 188 icon: ResourceManager.getInstance().getCachedAppIcon(this.appInfo.appIconId, 189 this.appInfo.bundleName, this.appInfo.moduleName), 190 badgeNumber: this.appInfo.badgeNumber 191 }) 192 } 193 .visibility(this.dragItemType === CommonConstants.DRAG_FROM_DOCK && this.smartDragItemInfo.keyName === this.appInfo.keyName ? 194 Visibility.Hidden : Visibility.Visible) 195 .width(mSmartDockStyleConfig?.mListItemWidth as number) 196 .height(mSmartDockStyleConfig?.mListItemHeight as number) 197 .backgroundColor(mSmartDockStyleConfig?.mItemBackgroundColor as string) 198 .borderRadius(mSmartDockStyleConfig?.mItemBorderRadius as number) 199 .parallelGesture( 200 LongPressGesture({ repeat: false }) 201 .onAction((event: GestureEvent) => { 202 Log.showInfo(TAG, 'onAction start'); 203 this.isShow = true; 204 AppStorage.setOrCreate('isLongPress', true); 205 }) 206 ) 207 .bindPopup(this.isShow, { 208 builder: this.MenuBuilder, 209 placement: Placement.Top, 210 popupColor: Color.White, 211 arrowOffset: AppStorage.get('deviceType') == CommonConstants.DEFAULT_DEVICE_TYPE 212 ? null 213 : 3 * ((mSmartDockStyleConfig?.mIconSize as number) / 2) + (mSmartDockStyleConfig?.mListItemGap as number), // Avoid the popup offset problem in phone form 214 onStateChange: (e) => { 215 if (!e.isVisible) { 216 this.isShow = false; 217 } 218 AppStorage.setOrCreate('smartDockShowMenu', e.isVisible) 219 }, 220 autoCancel: true 221 }) 222 .onTouch((event: TouchEvent) => { 223 Log.showInfo(TAG, `onTouch event type: ${event.type}`); 224 if (event.type === CommonConstants.TOUCH_TYPE_UP && this.smartDragItemInfo.isDragging) { 225 let mIsDragEffectArea = SmartDockDragHandler.getInstance().isDragEffectArea(event.touches[0].windowX, event.touches[0].windowY); 226 if (!mIsDragEffectArea) { 227 AppStorage.setOrCreate<LauncherDragItemInfo>('dragItemInfo', new LauncherDragItemInfo()); 228 AppStorage.setOrCreate('selectAppIndex', null); 229 } 230 AppStorage.setOrCreate('isLongPress', false); 231 } 232 if (this.smartDragItemInfo.isDragging) { 233 this.isShow = false; 234 } 235 }) 236 .onClick((event) => { 237 this.onItemClick(event, this.appInfo); 238 }) 239 .onMouse((event: MouseEvent) => { 240 Log.showInfo(TAG, `onMouse MouseType: ${event.button}`); 241 if (event.button == MouseButton.Right) { 242 event.stopPropagation(); 243 AppStorage.setOrCreate('selectDesktopAppItem', ''); 244 this.isShow = true; 245 } 246 }) 247 .onDragStart((event: DragEvent, extraParams: string) => { 248 Log.showInfo(TAG, `DragStart`); 249 this.dragItemType = CommonConstants.DRAG_FROM_DOCK; 250 this.appInfo.isDragging = true; 251 this.smartDragItemInfo = this.appInfo as LauncherDragItemInfo; 252 Log.showInfo(TAG, `smartDragItemInfo: ${JSON.stringify(this.smartDragItemInfo)}`); 253 const selectAppIndex = SmartDockDragHandler.getInstance().getDragItemIndexByCoordinates(event.getWindowX(), event.getWindowY()); 254 AppStorage.setOrCreate('selectAppIndex', selectAppIndex); 255 }) 256 .onDragEnd((event: DragEvent, extraParams: string) => { 257 Log.showInfo(TAG, `onDragEnd event: [${event.getWindowX()}, ${event.getWindowY()}]` + event.getResult()); 258 AppStorage.setOrCreate<LauncherDragItemInfo>('dragItemInfo', new LauncherDragItemInfo()); 259 }) 260 } 261}