-
-
Save anga83/ffbdf6d0f544a01203aa1f093167460e to your computer and use it in GitHub Desktop.
Add battery element to weather-cal by @mzeryck
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// battery indicator element for use with weather-cal Scriptable widget | |
// https://github.com/mzeryck/Weather-Cal/blob/main/weather-cal.js | |
function provideBatteryIcon() { | |
const batteryLevel = Device.batteryLevel() | |
if ( Device.isCharging() ) { | |
return SFSymbol.named("battery.100.bolt").image | |
} else | |
if ( Math.round(batteryLevel * 100) > 75 ) { | |
return SFSymbol.named("battery.100").image | |
} else if ( Math.round(batteryLevel * 100) > 25 ) { | |
return SFSymbol.named("battery.25").image | |
} else { | |
return SFSymbol.named("battery.0").image | |
} | |
} | |
function battery(column, alignment) { | |
function getBatteryLevel() { | |
const batteryLevel = Device.batteryLevel() | |
const batteryPercentage = `${Math.round(batteryLevel * 100)}%` | |
return batteryPercentage | |
} | |
// Set up the battery level. | |
let batteryStack = align(column, alignment) | |
batteryStack.layoutHorizontally() | |
batteryStack.centerAlignContent() | |
let batteryIcon = batteryStack.addImage(provideBatteryIcon()) | |
batteryIcon.imageSize = new Size(30,30) | |
batteryIcon.tintColor = Color.white() | |
batteryStack.addSpacer(8) | |
let battery = batteryStack.addText(getBatteryLevel()) | |
formatText(battery, textFormat.battery) | |
batteryStack.setPadding(10, 10, 10, 10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment