Merge pull request #84 in YR/star-web-components from battery-widget to master

* commit '9c6d0aeb7a447f0009a7470abbbe7e5379ab914c':
  TASK: #103649 - add battery widget
This commit is contained in:
汪昌棋 2022-10-13 16:12:05 +08:00
commit befbd471d9
1 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,98 @@
import GaiaWidget from '../gaia-widget'
import '../../components/battery/battery'
import {StarBattery} from '../../components/battery/battery'
import {customElement} from 'lit/decorators.js'
@customElement('gaia-battery')
class BatteryWidget extends GaiaWidget {
constructor({
url,
appName,
origin,
size,
manifestWidgetName,
}: {
url: string
size: [number, number]
origin: string
appName: string
manifestWidgetName: string
}) {
super({
url: url || 'js/widgets/battery.js',
appName: appName || 'homescreen',
origin: origin || 'http://homescreen.localhost/manifest.webmanifest',
size: size || [2, 2],
manifestWidgetName: manifestWidgetName || 'battery',
})
}
_battery: any
async init() {
// @ts-ignore
this._battery = await navigator.getBattery()
this.percent = this._battery.level
this.charge = this._battery.charging
this._battery.addEventListener('levelchange', this)
this._battery.addEventListener('chargingchange', this)
}
handleEvent(event: Event): void {
switch (event.type) {
case 'levelchange':
this.percent = this._battery.level
break
case 'chargingchange':
this.charge = this._battery.charging
break
}
}
battery!: StarBattery
connectedCallback() {
this.battery = this.shadowRoot!.querySelector('star-battery')!
}
get percent() {
return this.battery.percent
}
set percent(value) {
this.battery.percent = value
}
get charge() {
return this.battery.charge
}
set charge(value) {
// this.battery.charge = !!value
if (value) {
setTimeout(() => {
this.battery.charge = false
})
}
this.battery.charge = value
}
get template() {
return `
<star-battery></star-battery>
<style>
:host {
display: flex;
height: 100%;
width: 100%;
align-item: center;
justify-content: center;
}
star-battery {
flex: 1;
}
</style>
`
}
}
export default BatteryWidget