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