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 { DragArea } from '../interface/DragArea'; 18import { CommonConstants } from '../constants/CommonConstants'; 19import type { LauncherDragItemInfo } from '../bean/LauncherDragItemInfo'; 20 21const TAG = 'BaseDragHandler'; 22 23/** 24 * Drag processing base class, drag processing is mainly responsible for the processing of the following tasks: 25 * 1.Efficient event distribution based on drag area. 26 * 2.Initialize drag function related parameters. 27 * 3.Adjust and refresh the desktop layout according to the drag results. 28 */ 29export abstract class BaseDragHandler { 30 protected mIsInEffectArea = false; 31 protected mDragEffectArea: DragArea | undefined; 32 private mDragStateListener = null; 33 private mSelectItemIndex: number = CommonConstants.INVALID_VALUE; 34 35 constructor() { 36 this.setIsLongPress(false); 37 } 38 39 /** 40 * Get the data object corresponding to the drag operation. 41 */ 42 protected abstract getDragRelativeData(): any; 43 44 /** 45 * Get the position of the drag target. 46 */ 47 protected abstract getItemIndex(x: number, y: number): number; 48 49 /** 50 * Get the object at the target location. 51 */ 52 protected abstract getItemByIndex(index: number): any; 53 54 /** 55 * Set the drag effective area. 56 */ 57 setDragEffectArea(effectArea: DragArea): void { 58 this.mDragEffectArea = effectArea; 59 } 60 61 /** 62 * Get valid area. 63 */ 64 protected getDragEffectArea(): DragArea | undefined { 65 return this.mDragEffectArea; 66 } 67 68 /** 69 * Set up drag listeners. 70 */ 71 setDragStateListener(dragStateListener): void { 72 this.mDragStateListener = dragStateListener; 73 } 74 75 /** 76 * Set drag and drop item information. 77 * 78 * @param dragItemInfo 79 */ 80 protected setDragItemInfo(dragItemInfo: LauncherDragItemInfo): void { 81 Log.showDebug(TAG, `setDragItemInfo dragItemInfo: ${JSON.stringify(dragItemInfo)}`); 82 AppStorage.setOrCreate<LauncherDragItemInfo>('dragItemInfo', dragItemInfo); 83 } 84 85 /** 86 * Get drag item information. 87 * 88 * @return dragItemInfo 89 */ 90 protected getDragItemInfo() { 91 const dragItemInfo: LauncherDragItemInfo = AppStorage.get<LauncherDragItemInfo>('dragItemInfo'); 92 // avoid dragItemInfo from AppStorage is undefined 93 return dragItemInfo; 94 } 95 96 /** 97 * Get IsLongPress parameter. 98 * 99 * @return isLongPress 100 */ 101 protected getIsLongPress(): boolean { 102 const isLongPress: boolean = AppStorage.get('isLongPress'); 103 return isLongPress; 104 } 105 106 /** 107 * Set the IsLongPress parameter. 108 */ 109 protected setIsLongPress(isLongPress): void { 110 Log.showDebug(TAG, `setIsLongPress isLongPress: ${isLongPress}`); 111 AppStorage.setOrCreate('isLongPress', isLongPress); 112 } 113 114 protected isDragEffectArea(x: number, y: number): boolean { 115 if (this.mDragEffectArea) { 116 if (x >= this.mDragEffectArea.left && x <= this.mDragEffectArea.right 117 && y >= this.mDragEffectArea.top && y <= this.mDragEffectArea.bottom) { 118 return true; 119 } 120 } 121 return false; 122 } 123} 124