Skip to content

Commit

Permalink
feat: Support for PowerManager.BatteryStatus on WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund authored and ahmed605 committed Sep 13, 2023
1 parent d1674f8 commit 26d7a75
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,23 @@ public PowerManager()
UwpPowerManager.EnergySaverStatusChanged += UwpPowerManager_EnergySaverStatusChanged;
UwpPowerManager.RemainingChargePercentChanged += UwpPowerManager_RemainingChargePercentChanged;
UwpPowerManager.PowerSupplyStatusChanged += UwpPowerManager_PowerSupplyStatusChanged;
BatteryStatusOutput.Text = UwpPowerManager.BatteryStatus.ToString();
EnergySaverStatusOuptut.Text = UwpPowerManager.EnergySaverStatus.ToString();
RemainingChargePercentOutput.Text = UwpPowerManager.RemainingChargePercent.ToString();
PowerSupplyStatusOuptut.Text = UwpPowerManager.PowerSupplyStatus.ToString();
BatteryStatusOutput.Text = SafeGetter(() => UwpPowerManager.BatteryStatus.ToString());
EnergySaverStatusOuptut.Text = SafeGetter(() => UwpPowerManager.EnergySaverStatus.ToString());
RemainingChargePercentOutput.Text = SafeGetter(() => UwpPowerManager.RemainingChargePercent.ToString());
PowerSupplyStatusOuptut.Text = SafeGetter(() => UwpPowerManager.PowerSupplyStatus.ToString());
}

private string SafeGetter(Func<string> action)
{
try
{
return action();
}
catch (Exception)
{
// Ignore
return "Not implemented";
}
}

private async void UwpPowerManager_PowerSupplyStatusChanged(object sender, object e)
Expand Down
32 changes: 32 additions & 0 deletions src/Uno.UI/ts/Windows/System/Power/PowerManager.ts
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";
}
}
}
7 changes: 5 additions & 2 deletions src/Uno.UWP/System/Power/PowerManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if __ANDROID__ || __IOS__
#if __ANDROID__ || __IOS__ || __WASM__
using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -7,6 +7,7 @@ namespace Windows.System.Power
{
public partial class PowerManager
{
#if !__WASM__
private static object _syncLock = new object();

private static PowerSupplyStatus? _lastPowerSupplyStatus;
Expand All @@ -18,9 +19,10 @@ public partial class PowerManager
private static EventHandler<object> _energySaverStatusChanged;
private static EventHandler<object> _remainingChargePercentChanged;
private static EventHandler<object> _batteryStatusChanged;

#endif
public static BatteryStatus BatteryStatus => GetBatteryStatus();

#if !__WASM__
public static EnergySaverStatus EnergySaverStatus => GetEnergySaverStatus();

public static PowerSupplyStatus PowerSupplyStatus => GetPowerSupplyStatus();
Expand Down Expand Up @@ -173,6 +175,7 @@ internal static void RaiseEnergySaverStatusChanged()
_energySaverStatusChanged?.Invoke(null, null);
}
}
#endif
}
}
#endif
22 changes: 22 additions & 0 deletions src/Uno.UWP/System/Power/PowerManager.wasm.cs
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;
}
}

0 comments on commit 26d7a75

Please sign in to comment.