• 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 */
15import { Log } from '@ohos/common';
16import { DragArea } from '@ohos/common';
17import { EventConstants } from '@ohos/common';
18import { localEventManager } from '@ohos/common';
19import { BaseDragHandler } from '@ohos/common';
20import { CommonConstants } from '@ohos/common';
21import { layoutConfigManager } from '@ohos/common';
22import { CheckEmptyUtils } from '@ohos/common';
23import { SmartDockStyleConfig } from '../config/SmartDockStyleConfig';
24import SmartDockModel from '../model/SmartDockModel';
25import SmartDockConstants from '../common/constants/SmartDockConstants';
26import type { LauncherDragItemInfo } from '@ohos/common';
27
28const TAG = 'SmartDockDragHandler';
29
30/**
31 * SmartDock DragHandler
32 */
33export default class SmartDockDragHandler extends BaseDragHandler {
34  private mDockCoordinateData = [];
35  private readonly mSmartDockModel: SmartDockModel;
36  private readonly mSmartDockStyleConfig: SmartDockStyleConfig;
37  private mDevice = CommonConstants.DEFAULT_DEVICE_TYPE;
38
39  constructor() {
40    super();
41    this.mSmartDockModel = SmartDockModel.getInstance();
42    this.mSmartDockStyleConfig = layoutConfigManager.getStyleConfig(SmartDockStyleConfig.APP_LIST_STYLE_CONFIG, SmartDockConstants.FEATURE_NAME);
43    Log.showInfo(TAG, 'constructor!');
44  }
45
46  static getInstance(): SmartDockDragHandler {
47    if (globalThis.SmartDockDragHandler == null) {
48      globalThis.SmartDockDragHandler = new SmartDockDragHandler();
49    }
50    Log.showDebug(TAG, 'getInstance!');
51    return globalThis.SmartDockDragHandler;
52  }
53
54  setDragEffectArea(effectArea): void {
55    Log.showDebug(TAG, `setDragEffectArea: ${JSON.stringify(effectArea)}`);
56    AppStorage.setOrCreate('smartDockDragEffectArea', effectArea);
57    super.setDragEffectArea(effectArea);
58    this.updateDockParam(effectArea);
59  }
60
61  isDragEffectArea(x: number, y: number): boolean {
62    const isInEffectArea = super.isDragEffectArea(x, y);
63    Log.showDebug(TAG, `isDragEffectArea x: ${x}, y: ${y}, isInEffectArea: ${isInEffectArea}`);
64    const deviceType = AppStorage.get('deviceType');
65    const pageDesktopDragEffectArea: DragArea = AppStorage.get('pageDesktopDragEffectArea');
66    Log.showDebug(TAG, `isDragEffectArea pageDesktopDragEffectArea: ${JSON.stringify(pageDesktopDragEffectArea)}`);
67    if (pageDesktopDragEffectArea) {
68      if (deviceType == CommonConstants.DEFAULT_DEVICE_TYPE) {
69        if (isInEffectArea || (y < pageDesktopDragEffectArea.bottom && y > pageDesktopDragEffectArea.top)
70        && x < pageDesktopDragEffectArea.right && x > pageDesktopDragEffectArea.left) {
71          return true;
72        }
73        return false;
74      }
75      return isInEffectArea;
76    }
77    return false;
78  }
79
80  private updateDockParam(effectArea: DragArea): void {
81    this.mDockCoordinateData = [];
82    const dockWidth = effectArea.right - effectArea.left;
83    const dockData: [] = this.getDragRelativeData();
84    const dataCount = dockData.length;
85    const dockPadding: {right: number, left: number, top: number, bottom: number} = AppStorage.get('dockPadding');
86    const itemSize = this.mSmartDockStyleConfig.mListItemWidth;
87    const itemGap = this.mSmartDockStyleConfig.mListItemGap;
88    if (dataCount > 0) {
89      for (let index = 0; index < dataCount; index++) {
90        if (index == dataCount - 1) {
91          this.mDockCoordinateData.push(effectArea.right - dockPadding.left - (itemSize / 2));
92          return;
93        }
94        this.mDockCoordinateData.push(effectArea.left + dockPadding.left + (itemSize / 2) + ((itemSize + itemGap) * index));
95      }
96    } else {
97      this.mDockCoordinateData.push(dockWidth);
98    }
99    Log.showDebug(TAG, `updateDockParam DockCoordinateData: ${JSON.stringify(this.mDockCoordinateData)}`);
100  }
101
102  protected getDragRelativeData(): any {
103    const dockData: [] = AppStorage.get('residentList');
104    return dockData;
105  }
106
107  protected getItemIndex(x: number, y: number): number {
108    if (super.isDragEffectArea(x, y)) {
109      for (let index = 0; index < this.mDockCoordinateData.length; index++) {
110        if (this.mDockCoordinateData[index] > x) {
111          return index;
112        }
113      }
114      return this.mDockCoordinateData.length;
115    }
116    return CommonConstants.INVALID_VALUE;
117  }
118
119  /**
120   * Get dragItem location by coordinates.
121   *
122   * @param x - x position
123   * @param y - y position
124   */
125  getDragItemIndexByCoordinates(x: number, y: number): number {
126    if (super.isDragEffectArea(x, y)) {
127      for (let index = 0; index < this.mDockCoordinateData.length; index++) {
128        if (this.mDockCoordinateData[index] + this.mSmartDockStyleConfig.mListItemWidth / 2 >= x) {
129          return index;
130        }
131      }
132    }
133    return CommonConstants.INVALID_VALUE;
134  }
135
136  protected getItemByIndex(index: number): any {
137    const dockData: [] = this.getDragRelativeData();
138    if (index >= 0 && index < dockData.length) {
139      return dockData[index];
140    }
141    return null;
142  }
143
144  layoutAdjustment(insertIndex: number, itemIndex: number): void {
145    if (insertIndex != CommonConstants.INVALID_VALUE || itemIndex != CommonConstants.INVALID_VALUE) {
146      this.mSmartDockModel.insertItemToIndex(insertIndex, itemIndex);
147    }
148  }
149
150  onDragDrop(x: number, y: number): boolean {
151    const dragItemInfo: LauncherDragItemInfo = AppStorage.get<LauncherDragItemInfo>('dragItemInfo');
152    if (!dragItemInfo.isDragging) {
153      return false;
154    }
155    const dragItemType: number = AppStorage.get('dragItemType');
156    const insertIndex = this.getItemIndex(x, y);
157    if (dragItemType === CommonConstants.DRAG_FROM_DOCK) {
158      const selectAppIndex: number = AppStorage.get('selectAppIndex');
159      globalThis.SmartDockDragHandler.layoutAdjustment(insertIndex, selectAppIndex);
160      return true;
161    }
162    if (dragItemType === CommonConstants.DRAG_FROM_DESKTOP
163    && AppStorage.get('deviceType') == CommonConstants.DEFAULT_DEVICE_TYPE) {
164      Log.showInfo(TAG, `onDrop insertIndex: ${insertIndex}`);
165      this.addItemToSmartDock(dragItemInfo, insertIndex);
166      return true;
167    }
168    return false;
169  }
170
171  addItemToSmartDock(dragItemInfo: LauncherDragItemInfo, insertIndex: number): boolean {
172    let addToDockRes = this.mSmartDockModel.addToSmartdock(dragItemInfo, insertIndex);
173    if (addToDockRes) {
174      localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_PAGEDESK_ITEM_DELETE, {
175        bundleName: undefined,
176        keyName: dragItemInfo.keyName
177      });
178    }
179    return true;
180  }
181}