-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support for PowerManager.BatteryStatus on WASM
- Loading branch information
1 parent
a45d8ed
commit 238763a
Showing
4 changed files
with
76 additions
and
6 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
declare class BatteryManager { | ||
charging: boolean; | ||
} | ||
|
||
interface Navigator { | ||
getBattery(): Promise<BatteryManager>; | ||
} | ||
|
||
|
||
namespace Windows.System.Power { | ||
|
||
export class PowerManager { | ||
|
||
private static battery: BatteryManager; | ||
|
||
public static async initializeAsync(): Promise<string> { | ||
if (!PowerManager.battery) { | ||
PowerManager.battery = await navigator.getBattery(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static getBatteryStatus(): string { | ||
if (PowerManager.battery) { | ||
return PowerManager.battery.charging ? "Charging" : "Discharging"; | ||
} | ||
|
||
return "Idle"; | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using Uno.Foundation; | ||
|
||
namespace Windows.System.Power; | ||
|
||
public partial class PowerManager | ||
{ | ||
private const string JsType = "Windows.System.Power.PowerManager"; | ||
|
||
private static BatteryStatus GetBatteryStatus() | ||
{ | ||
WebAssemblyRuntime.InvokeAsync($"{JsType}.initializeAsync()"); | ||
var batteryStatusString = WebAssemblyRuntime.InvokeJS($"{JsType}.getBatteryStatus()"); | ||
if (Enum.TryParse<BatteryStatus>(batteryStatusString, out var batteryStatus)) | ||
{ | ||
return batteryStatus; | ||
} | ||
return BatteryStatus.NotPresent; | ||
} | ||
} |