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 Constants from '../common/constants' 17import {Log} from '@ohos/common' 18 19const TAG = 'BatteryComponent-batteryPic' 20 21@Component 22export default 23struct BatteryPic { 24 @StorageLink('batterySoc') batterySoc: number = 100 25 @StorageLink('batteryCharging') batteryCharging : boolean = false 26 @State mContentColor: string = "#FFFFFFFF" 27 @State picBodyWidth: Resource = $r('app.float.battery_component_pic_body_width') 28 @State picBodyHeight: Resource = $r('app.float.battery_component_pic_body_height') 29 @State picBodyBorderWidth: Resource = $r('app.float.battery_component_pic_body_border_width') 30 @State picBorderRadius: Resource = $r('app.float.battery_component_pic_border_radius') 31 @State picBodyPadding: Resource = $r('app.float.battery_component_pic_body_padding') 32 @State picGap: Resource = $r('app.float.battery_component_pic_gap') 33 @State picHeadWidth: Resource = $r('app.float.battery_component_pic_head_width') 34 @State picHeadHeight: Resource = $r('app.float.battery_component_pic_head_height') 35 @State picHeadBorderRadius: Resource = $r('app.float.battery_component_pic_head_radius') 36 @State picChargingColor: Resource = $r('app.color.battery_component_pic_charging_color') 37 @State picLevelLowColor: Resource = $r('app.color.battery_component_pic_level_low_color') 38 39 aboutToAppear(){ 40 Log.showInfo(TAG,'aboutToAppear Start'); 41 } 42 43 aboutToDisappear(){ 44 Log.showInfo(TAG,'aboutToDisappear'); 45 } 46 47 build() { 48 49 Row() { 50 Row() { 51 Row() { 52 53 } 54 .height('100%') 55 .width((this.batterySoc < 100 ? this.batterySoc: 100) + '%') 56 .backgroundColor(this.getBatteryColor(this.batterySoc, this.batteryCharging)) 57 } 58 .width(this.picBodyWidth) 59 .height(this.picBodyHeight) 60 .backgroundColor($r('app.color.battery_background')) 61 .border({ width: this.picBodyBorderWidth, 62 color: this.mContentColor, 63 radius: this.picBorderRadius, 64 style: BorderStyle.Solid }) 65 .padding(this.picBodyPadding) 66 67 Row() { 68 69 } 70 .width(this.picGap) 71 .height(1) 72 73 Row() { 74 75 } 76 .width(this.picHeadWidth) 77 .height(this.picHeadHeight) 78 .backgroundColor(this.mContentColor) 79 .borderRadius(this.picHeadBorderRadius) 80 } 81 } 82 83 private getBatteryColor(val: number, charging: boolean) { 84 Log.showDebug(TAG, `getBatteryColor, val: ${ val } charging: ${ charging } `); 85 if (charging) { 86 return this.picChargingColor; 87 } else if (val <= Constants.BATTERY_LEVEL_LOW) { 88 return this.picLevelLowColor; 89 } else { 90 return this.mContentColor; 91 } 92 } 93} 94