• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2022-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 { BaseStartAppHandler } from '@ohos/common';
17import { CommonConstants } from '@ohos/common';
18import { AppItemInfo } from '@ohos/common';
19import { layoutConfigManager } from '@ohos/common';
20import { LayoutViewModel } from '@ohos/common';
21import { Log } from '@ohos/common';
22import { CheckEmptyUtils } from '@ohos/common';
23import SmartDockConstants from '../common/constants/SmartDockConstants';
24import { SmartDockStyleConfig } from '../config/SmartDockStyleConfig';
25
26const TAG = 'SmartDockStartAppHandler';
27
28/**
29 * smartDock start app processing class
30 */
31export default class SmartDockStartAppHandler extends BaseStartAppHandler {
32  private mSmartDockStyleConfig;
33
34  private constructor() {
35    super();
36    this.mSmartDockStyleConfig = layoutConfigManager.getStyleConfig(SmartDockStyleConfig.APP_LIST_STYLE_CONFIG,
37      SmartDockConstants.FEATURE_NAME);
38  }
39
40  static getInstance(): SmartDockStartAppHandler {
41    if (globalThis.SmartDockStartAppHandler == null) {
42      globalThis.SmartDockStartAppHandler = new SmartDockStartAppHandler();
43    }
44    return globalThis.SmartDockStartAppHandler;
45  }
46
47  protected calculateAppIconPosition(): void {
48    if (CheckEmptyUtils.isEmpty(this.mSmartDockStyleConfig)) {
49      Log.showError(TAG, `calculateAppIconPosition with invalid config`)
50      return;
51    }
52    const appItemInfo = AppStorage.Get('startAppItemInfo');
53    const residentList: AppItemInfo[] = AppStorage.Get('residentList');
54    const recentList: AppItemInfo[] = AppStorage.Get('recentList');
55    const screenWidth: number = AppStorage.Get('screenWidth');
56    const workSpaceHeight: number = LayoutViewModel.getInstance().getWorkSpaceHeight();
57    this.mAppIconPositionY = workSpaceHeight + this.mSmartDockStyleConfig.mListItemGap;
58    const smartDockWidth: number = this.getListWidth(residentList) +
59    (recentList.length > 0 ? this.mSmartDockStyleConfig.mDockGap + this.getListWidth(recentList) : 0);
60    const smartDockStartPositionX: number = (screenWidth - smartDockWidth) / 2;
61    const startAppTypeFromPageDesktop: number = AppStorage.Get('startAppTypeFromPageDesktop');
62    if (startAppTypeFromPageDesktop === CommonConstants.OVERLAY_TYPE_APP_RECENT) {
63      const indexInRecentList: number = this.getIndexInList(appItemInfo, recentList);
64      this.mAppIconPositionX = smartDockStartPositionX + this.getListWidth(residentList) + this.mSmartDockStyleConfig.mDockGap
65      + this.mSmartDockStyleConfig.mDockPadding + indexInRecentList
66      * (this.mSmartDockStyleConfig.mListItemWidth + this.mSmartDockStyleConfig.mListItemGap);
67      return;
68    }else {
69      const indexInResidentList: number = this.getIndexInList(appItemInfo, residentList);
70      this.mAppIconPositionX = smartDockStartPositionX + this.mSmartDockStyleConfig.mDockPadding + indexInResidentList
71      * (this.mSmartDockStyleConfig.mListItemWidth + this.mSmartDockStyleConfig.mListItemGap)
72      - (recentList.length > 0 ? 0 : this.mSmartDockStyleConfig.mListItemGap / 2);
73      return;
74    }
75  }
76
77  private getIndexInList(appItemInfo, list) : number {
78    let index: number = CommonConstants.INVALID_VALUE;
79    if (CheckEmptyUtils.isEmptyArr(list)) {
80      Log.showError(TAG, `getIndexInRecentList with invalid list`)
81      return index;
82    }
83
84    for (var i = 0; i < list.length; i++) {
85      if (appItemInfo.bundleName === list[i].bundleName) {
86        index = i;
87        break;
88      }
89    }
90
91    return index;
92  }
93
94  private getListWidth(itemList): number {
95    let width = 0;
96    if (CheckEmptyUtils.isEmptyArr(itemList)) {
97      return width;
98    } else {
99      let num = itemList.length;
100      if (num > this.mSmartDockStyleConfig.mMaxDockNum) {
101        num = this.mSmartDockStyleConfig.mMaxDockNum
102      }
103      width = this.mSmartDockStyleConfig.mDockPadding * 2 + num * (this.mSmartDockStyleConfig.mListItemWidth) + (num - 1)
104      * (this.mSmartDockStyleConfig.mListItemGap);
105    }
106    return width;
107  }
108}
109