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 { ResourceManager } from '../manager/ResourceManager';
17import { PresetStyleConstants } from '../constants/PresetStyleConstants';
18
19@Component
20export struct AppName {
21  nameSize: number = 0;
22  nameHeight: number = 0;
23  bundleName: string = '';
24  moduleName: string = '';
25  labelId: number = 0;
26  nameFontColor: string = '#ffffff';
27  @State @Watch('updateName') appName: string = '';
28  useCache: boolean = true;
29  nameLines: number = PresetStyleConstants.DEFAULT_APP_NAME_LINES;
30  marginTop: number = PresetStyleConstants.DEFAULT_ICON_NAME_GAP;
31  marginLeft: number = PresetStyleConstants.DEFAULT_DESKTOP_NAME_MARGIN;
32  private mResourceManager = ResourceManager.getInstance();
33
34  aboutToAppear(): void {
35    this.mResourceManager = ResourceManager.getInstance();
36    this.updateName();
37  }
38
39  public appNameLoadCallback = (name: string) => {
40    this.appName = name;
41  }
42
43  private updateName() {
44    if (this.mResourceManager) {
45      this.mResourceManager.getAppNameWithCache(this.labelId, this.bundleName, this.moduleName,
46        this.appName, this.appNameLoadCallback);
47    }
48  }
49
50  build() {
51    Column() {
52      Text(this.appName)
53        .fontSize(this.nameSize)
54        .fontColor(this.nameFontColor)
55        .textOverflow({overflow: TextOverflow.Ellipsis})
56        .textAlign(TextAlign.Center)
57        .maxLines(this.nameLines)
58        .lineHeight(this.nameSize)
59    }
60    .height(this.nameHeight)
61    .margin({top: this.marginTop,left: this.marginLeft, right: this.marginLeft})
62  }
63}