-
Notifications
You must be signed in to change notification settings - Fork 730
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 on WASM
- Loading branch information
1 parent
eb00147
commit 95a5a2b
Showing
6 changed files
with
161 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
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
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 |
---|---|---|
@@ -1,22 +1,98 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Uno; | ||
using Uno.Foundation; | ||
using Uno.Foundation.Logging; | ||
|
||
namespace Windows.System.Power; | ||
|
||
public partial class PowerManager | ||
{ | ||
private const string JsType = "Windows.System.Power.PowerManager"; | ||
|
||
[Preserve] | ||
public static int DispatchChargingChanged() | ||
{ | ||
RaiseBatteryStatusChanged(); | ||
RaisePowerSupplyStatusChanged(); | ||
return 0; | ||
} | ||
|
||
[Preserve] | ||
public static int DispatchRemainingChargePercentChanged() | ||
{ | ||
RaiseRemainingChargePercentChanged(); | ||
return 0; | ||
} | ||
|
||
[Preserve] | ||
public static int DispatchRemainingDischargeTimeChanged() | ||
{ | ||
RaiseRemainingDischargeTimeChanged(); | ||
return 0; | ||
} | ||
|
||
private static BatteryStatus GetBatteryStatus() | ||
{ | ||
WebAssemblyRuntime.InvokeAsync($"{JsType}.initializeAsync()"); | ||
InitializeAsync(); // TODO | ||
|
||
var batteryStatusString = WebAssemblyRuntime.InvokeJS($"{JsType}.getBatteryStatus()"); | ||
if (Enum.TryParse<BatteryStatus>(batteryStatusString, out var batteryStatus)) | ||
return Enum.TryParse<BatteryStatus>(batteryStatusString, out var batteryStatus) ? | ||
batteryStatus : BatteryStatus.NotPresent; | ||
} | ||
|
||
private static PowerSupplyStatus GetPowerSupplyStatus() | ||
{ | ||
InitializeAsync(); // TODO | ||
|
||
var powerSupplyStatusString = WebAssemblyRuntime.InvokeJS($"{JsType}.getPowerSupplyStatus()"); | ||
return Enum.TryParse<PowerSupplyStatus>(powerSupplyStatusString, out var powerSupplyStatus) ? | ||
powerSupplyStatus : PowerSupplyStatus.NotPresent; | ||
} | ||
|
||
private static int GetRemainingChargePercent() | ||
{ | ||
InitializeAsync(); // TODO | ||
|
||
var remainingChargeString = WebAssemblyRuntime.InvokeJS($"{JsType}.getRemainingChargePercent()"); | ||
if (double.TryParse(remainingChargeString, out var remainingCharge) && | ||
!double.IsNaN(remainingCharge)) | ||
{ | ||
return (int)(remainingCharge * 100); | ||
} | ||
|
||
return 100; | ||
} | ||
|
||
private static TimeSpan GetRemainingDischargeTime() | ||
{ | ||
InitializeAsync(); // TODO | ||
|
||
var remainingDischargeTimeString = WebAssemblyRuntime.InvokeJS($"{JsType}.getRemainingDischargeTime()"); | ||
if (double.TryParse(remainingDischargeTimeString, out var remainingDischargeTimeInSeconds) && | ||
!double.IsNaN(remainingDischargeTimeInSeconds) && | ||
remainingDischargeTimeInSeconds >= 0) | ||
{ | ||
return TimeSpan.FromSeconds(remainingDischargeTimeInSeconds); | ||
} | ||
|
||
return TimeSpan.MaxValue; | ||
} | ||
|
||
private static async Task InitializeAsync() | ||
{ | ||
try | ||
{ | ||
await WebAssemblyRuntime.InvokeAsync($"{JsType}.initializeAsync()"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return batteryStatus; | ||
if (typeof(PowerManager).Log().IsEnabled(LogLevel.Error)) | ||
{ | ||
typeof(PowerManager).Log().LogError("Could not initialize PowerManager", ex); | ||
} | ||
} | ||
return BatteryStatus.NotPresent; | ||
} | ||
} |