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