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 { BadgeManager } from '../manager/BadgeManager'; 18import { StyleConstants } from '../constants/StyleConstants'; 19import { ResourceManager } from '../manager/ResourceManager'; 20import { LauncherDragItemInfo } from '../bean/LauncherDragItemInfo'; 21 22const TAG = 'AppIcon'; 23 24@Component 25export struct AppIcon { 26 @StorageLink('dragItemInfo') @Watch('updateAppIcon') mDragItemInfo: LauncherDragItemInfo = new LauncherDragItemInfo(); 27 iconSize: number = 0; 28 iconId: number = 0; 29 bundleName: string = ''; 30 moduleName: string = ''; 31 @State icon: string = ''; 32 @State badgeNumber: number = 0; 33 @State iconScale: number = 1; 34 useCache: boolean = true; 35 badgeFontSize: number = StyleConstants.DEFAULT_BADGE_FONT_SIZE; 36 private mResourceManager = ResourceManager.getInstance(); 37 private mBadgeManager = BadgeManager.getInstance(); 38 private mDefaultAppIcon: ResourceStr = ''; 39 40 updateAppIcon() { 41 // 拖动图标到无效区域,松手后图标缩放恢复原有尺寸 42 if (!this.mDragItemInfo.isDragging) { 43 this.iconScale = 1; 44 } 45 } 46 47 aboutToAppear(): void { 48 this.mResourceManager = ResourceManager.getInstance(); 49 this.mBadgeManager = BadgeManager.getInstance(); 50 this.updateIcon(); 51 this.initBadge(); 52 } 53 54 public iconLoadCallback = (image: string) => { 55 this.icon = image; 56 } 57 58 public updateIcon() { 59 this.mResourceManager.getAppIconWithCache(this.iconId, this.bundleName, this.moduleName, 60 this.iconLoadCallback, this.mDefaultAppIcon); 61 } 62 63 public badgeInitCallback = (badgeNumber: number) => { 64 if (this.badgeNumber != badgeNumber) { 65 this.badgeNumber = badgeNumber; 66 } 67 } 68 69 public initBadge() { 70 this.mBadgeManager.getBadgeByBundle(this.bundleName, this.badgeInitCallback); 71 } 72 73 build() { 74 Column() { 75 Badge({ 76 count: this.badgeNumber, 77 maxCount: StyleConstants.MAX_BADGE_COUNT, 78 style: { 79 color: StyleConstants.DEFAULT_FONT_COLOR, 80 fontSize: this.badgeFontSize, 81 badgeSize: (this.badgeNumber > 0 ? StyleConstants.DEFAULT_BADGE_SIZE : 0), 82 badgeColor: Color.Red 83 } 84 }) { 85 Image(this.icon) 86 .width(this.iconSize) 87 .height(this.iconSize) 88 } 89 } 90 .onHover((isHover: boolean) => { 91 Log.showInfo(TAG, `onHover isHover ${isHover}`); 92 this.iconScale = isHover ? 1.05 : 1 93 }) 94 .onTouch((event: TouchEvent) => { 95 if (event.type === TouchType.Down) { 96 this.iconScale = 0.9; 97 } else if (event.type === TouchType.Up) { 98 this.iconScale = 1; 99 } 100 }) 101 .onMouse((event: MouseEvent) => { 102 if (event.button === MouseButton.Left && event.action === MouseAction.Press) { 103 this.iconScale = 0.9; 104 } else if (event.button === MouseButton.Left && event.action === MouseAction.Release) { 105 this.iconScale = 1; 106 } 107 }) 108 .width(this.iconSize) 109 .height(this.iconSize) 110 .scale({ x: this.iconScale, y: this.iconScale }) 111 .animation({ duration: 100 }) 112 } 113}