16e80583aSopenharmony_ci/**
26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46e80583aSopenharmony_ci * you may not use this file except in compliance with the License.
56e80583aSopenharmony_ci * You may obtain a copy of the License at
66e80583aSopenharmony_ci *
76e80583aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86e80583aSopenharmony_ci *
96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126e80583aSopenharmony_ci * See the License for the specific language governing permissions and
136e80583aSopenharmony_ci * limitations under the License.
146e80583aSopenharmony_ci */
156e80583aSopenharmony_ci
166e80583aSopenharmony_ciimport { Log } from '../utils/Log';
176e80583aSopenharmony_ciimport { BadgeManager } from '../manager/BadgeManager';
186e80583aSopenharmony_ciimport { StyleConstants } from '../constants/StyleConstants';
196e80583aSopenharmony_ciimport { ResourceManager } from '../manager/ResourceManager';
206e80583aSopenharmony_ciimport { LauncherDragItemInfo } from '../bean/LauncherDragItemInfo';
216e80583aSopenharmony_ci
226e80583aSopenharmony_ciconst TAG = 'AppIcon';
236e80583aSopenharmony_ci
246e80583aSopenharmony_ci@Component
256e80583aSopenharmony_ciexport struct AppIcon {
266e80583aSopenharmony_ci  @StorageLink('dragItemInfo') @Watch('updateAppIcon') mDragItemInfo: LauncherDragItemInfo = new LauncherDragItemInfo();
276e80583aSopenharmony_ci  iconSize: number = 0;
286e80583aSopenharmony_ci  iconId: number = 0;
296e80583aSopenharmony_ci  bundleName: string = '';
306e80583aSopenharmony_ci  moduleName: string = '';
316e80583aSopenharmony_ci  @State icon: string = '';
326e80583aSopenharmony_ci  @State badgeNumber: number = 0;
336e80583aSopenharmony_ci  @State iconScale: number = 1;
346e80583aSopenharmony_ci  useCache: boolean = true;
356e80583aSopenharmony_ci  badgeFontSize: number = StyleConstants.DEFAULT_BADGE_FONT_SIZE;
366e80583aSopenharmony_ci  private mResourceManager = ResourceManager.getInstance();
376e80583aSopenharmony_ci  private mBadgeManager = BadgeManager.getInstance();
386e80583aSopenharmony_ci  private mDefaultAppIcon: ResourceStr = '';
396e80583aSopenharmony_ci
406e80583aSopenharmony_ci  updateAppIcon() {
416e80583aSopenharmony_ci    // 拖动图标到无效区域,松手后图标缩放恢复原有尺寸
426e80583aSopenharmony_ci    if (!this.mDragItemInfo.isDragging) {
436e80583aSopenharmony_ci      this.iconScale = 1;
446e80583aSopenharmony_ci    }
456e80583aSopenharmony_ci  }
466e80583aSopenharmony_ci
476e80583aSopenharmony_ci  aboutToAppear(): void {
486e80583aSopenharmony_ci    this.mResourceManager = ResourceManager.getInstance();
496e80583aSopenharmony_ci    this.mBadgeManager = BadgeManager.getInstance();
506e80583aSopenharmony_ci    this.updateIcon();
516e80583aSopenharmony_ci    this.initBadge();
526e80583aSopenharmony_ci  }
536e80583aSopenharmony_ci
546e80583aSopenharmony_ci  public iconLoadCallback = (image: string) => {
556e80583aSopenharmony_ci    this.icon = image;
566e80583aSopenharmony_ci  }
576e80583aSopenharmony_ci
586e80583aSopenharmony_ci  public updateIcon() {
596e80583aSopenharmony_ci    this.mResourceManager.getAppIconWithCache(this.iconId, this.bundleName, this.moduleName,
606e80583aSopenharmony_ci    this.iconLoadCallback, this.mDefaultAppIcon);
616e80583aSopenharmony_ci  }
626e80583aSopenharmony_ci
636e80583aSopenharmony_ci  public badgeInitCallback = (badgeNumber: number) => {
646e80583aSopenharmony_ci    if (this.badgeNumber != badgeNumber) {
656e80583aSopenharmony_ci      this.badgeNumber = badgeNumber;
666e80583aSopenharmony_ci    }
676e80583aSopenharmony_ci  }
686e80583aSopenharmony_ci
696e80583aSopenharmony_ci  public initBadge() {
706e80583aSopenharmony_ci    this.mBadgeManager.getBadgeByBundle(this.bundleName, this.badgeInitCallback);
716e80583aSopenharmony_ci  }
726e80583aSopenharmony_ci
736e80583aSopenharmony_ci  build() {
746e80583aSopenharmony_ci    Column() {
756e80583aSopenharmony_ci      Badge({
766e80583aSopenharmony_ci        count: this.badgeNumber,
776e80583aSopenharmony_ci        maxCount: StyleConstants.MAX_BADGE_COUNT,
786e80583aSopenharmony_ci        style: {
796e80583aSopenharmony_ci          color: StyleConstants.DEFAULT_FONT_COLOR,
806e80583aSopenharmony_ci          fontSize: this.badgeFontSize,
816e80583aSopenharmony_ci          badgeSize: (this.badgeNumber > 0 ? StyleConstants.DEFAULT_BADGE_SIZE : 0),
826e80583aSopenharmony_ci          badgeColor: Color.Red
836e80583aSopenharmony_ci        }
846e80583aSopenharmony_ci      }) {
856e80583aSopenharmony_ci        Image(this.icon)
866e80583aSopenharmony_ci        .width(this.iconSize)
876e80583aSopenharmony_ci        .height(this.iconSize)
886e80583aSopenharmony_ci      }
896e80583aSopenharmony_ci    }
906e80583aSopenharmony_ci    .onHover((isHover: boolean) => {
916e80583aSopenharmony_ci      Log.showInfo(TAG, `onHover isHover ${isHover}`);
926e80583aSopenharmony_ci      this.iconScale = isHover ? 1.05 : 1
936e80583aSopenharmony_ci    })
946e80583aSopenharmony_ci    .onTouch((event: TouchEvent) => {
956e80583aSopenharmony_ci      if (event.type === TouchType.Down) {
966e80583aSopenharmony_ci        this.iconScale = 0.9;
976e80583aSopenharmony_ci      } else if (event.type === TouchType.Up) {
986e80583aSopenharmony_ci        this.iconScale = 1;
996e80583aSopenharmony_ci      }
1006e80583aSopenharmony_ci    })
1016e80583aSopenharmony_ci    .onMouse((event: MouseEvent) => {
1026e80583aSopenharmony_ci      if (event.button === MouseButton.Left && event.action === MouseAction.Press) {
1036e80583aSopenharmony_ci        this.iconScale = 0.9;
1046e80583aSopenharmony_ci      } else if (event.button === MouseButton.Left && event.action === MouseAction.Release) {
1056e80583aSopenharmony_ci        this.iconScale = 1;
1066e80583aSopenharmony_ci      }
1076e80583aSopenharmony_ci    })
1086e80583aSopenharmony_ci    .width(this.iconSize)
1096e80583aSopenharmony_ci    .height(this.iconSize)
1106e80583aSopenharmony_ci    .scale({ x: this.iconScale, y: this.iconScale })
1116e80583aSopenharmony_ci    .animation({ duration: 100 })
1126e80583aSopenharmony_ci  }
1136e80583aSopenharmony_ci}