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