1/*
2 * Copyright (c) 2023-2024 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 { LengthMetrics } from '@ohos.arkui.node';
17import { common } from '@kit.AbilityKit';
18import { BusinessError } from '@kit.BasicServicesKit';
19import { hilog } from '@kit.PerformanceAnalysisKit';
20
21const DEFAULT_MARGIN = 16
22const ITEM_SPACE = 4
23const MIN_SIZE = 24
24const MID_SIZE = 40
25const MAX_SIZE = 48
26const MAX_FONT_SIZE = 2
27
28@Component
29export struct SwipeRefresher {
30  @Prop
31  content: string = '';
32  @Prop
33  isLoading: boolean = false;
34  maxAppFontScale: number = 1;
35  isFollowingSystemFontScale: boolean = false;
36  minFontSize: number = 1.75;
37  maxFontSize: number = 2;
38
39  aboutToAppear() {
40    try {
41      let uiContent: UIContext = this.getUIContext();
42      this.isFollowingSystemFontScale = uiContent.isFollowingSystemFontScale();
43      this.maxAppFontScale = uiContent.getMaxFontScale();
44    } catch (err) {
45      let code: number = (err as BusinessError).code;
46      let message: string = (err as BusinessError).message;
47      hilog.error(0x3900, 'SwipeRefresher', `Failed to init fontsizescale info, cause, code: ${code}, message: ${message}`);
48    }
49  }
50
51  updateFontScale(): number {
52    let uiContent: UIContext = this.getUIContext();
53    let systemFontScale = (uiContent.getHostContext() as common.UIAbilityContext)?.config?.fontSizeScale ?? 1;
54    if (!this.isFollowingSystemFontScale) {
55      return 1;
56    }
57    return Math.min(systemFontScale, this.maxAppFontScale);
58  }
59
60  build() {
61    Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
62      if (this.isLoading) {
63        LoadingProgress()
64          .height(Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.maxFontSize ? MAX_SIZE :
65            (Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.minFontSize ? MID_SIZE : MIN_SIZE))
66          .width(Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.maxFontSize ? MAX_SIZE :
67            (Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.minFontSize ? MID_SIZE : MIN_SIZE))
68          .margin({
69            end: LengthMetrics.vp(ITEM_SPACE)
70          })
71      }
72      Text(this.content)
73        .fontColor($r('sys.color.ohos_id_color_text_secondary'))
74        .fontSize($r('sys.float.ohos_id_text_size_body2'))
75        .minFontScale(1)
76        .maxFontScale(Math.min(this.updateFontScale(), 2))
77        .padding({
78          top: DEFAULT_MARGIN,
79          bottom: DEFAULT_MARGIN
80        })
81    }
82  }
83}