Files
Electron.NET/ElectronNET.API/PowerMonitor.cs

259 lines
6.8 KiB
C#
Raw Normal View History

using System;
2021-08-26 14:22:54 +02:00
using System.Runtime.Versioning;
using System.Threading.Tasks;
2020-05-11 12:59:21 -05:00
namespace ElectronNET.API
{
/// <summary>
2020-05-12 10:50:08 -05:00
/// Monitor power state changes..
2020-05-11 12:59:21 -05:00
/// </summary>
public sealed class PowerMonitor
{
/// <summary>
2020-05-12 10:50:08 -05:00
/// Emitted when the system is about to lock the screen.
2020-05-11 12:59:21 -05:00
/// </summary>
2021-08-26 14:22:54 +02:00
[SupportedOSPlatform("windows")]
[SupportedOSPlatform("macos")]
2020-05-11 12:59:21 -05:00
public event Action OnLockScreen
{
add
{
if (_lockScreen == null)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.On("pm-lock-screen" , () =>
2020-05-11 12:59:21 -05:00
{
_lockScreen();
});
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("register-pm-lock-screen");
2020-05-11 12:59:21 -05:00
}
_lockScreen += value;
}
remove
{
_lockScreen -= value;
if (_lockScreen == null)
2021-07-12 19:50:39 +02:00
BridgeConnector.Off("pm-lock-screen");
2020-05-11 12:59:21 -05:00
}
}
private event Action _lockScreen;
/// <summary>
2020-05-12 10:50:08 -05:00
/// Emitted when the system is about to unlock the screen.
2020-05-11 12:59:21 -05:00
/// </summary>
2021-08-26 14:22:54 +02:00
[SupportedOSPlatform("windows")]
[SupportedOSPlatform("macos")]
2020-05-11 12:59:21 -05:00
public event Action OnUnLockScreen
{
add
{
if (_unlockScreen == null)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.On("pm-unlock-screen", () =>
2020-05-11 12:59:21 -05:00
{
_unlockScreen();
});
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("register-pm-unlock-screen");
2020-05-11 12:59:21 -05:00
}
_unlockScreen += value;
}
remove
{
_unlockScreen -= value;
if (_unlockScreen == null)
2021-07-12 19:50:39 +02:00
BridgeConnector.Off("pm-unlock-screen");
2020-05-11 12:59:21 -05:00
}
}
private event Action _unlockScreen;
2020-05-17 22:16:31 -05:00
/// <summary>
/// Emitted when the system is suspending.
2020-05-17 22:16:31 -05:00
/// </summary>
2021-08-26 14:22:54 +02:00
[SupportedOSPlatform("windows")]
[SupportedOSPlatform("macos")]
2020-05-17 22:16:31 -05:00
public event Action OnSuspend
{
add
{
if (_suspend == null)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.On("pm-suspend", () =>
2020-05-17 22:16:31 -05:00
{
_suspend();
});
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("register-pm-suspend");
2020-05-17 22:16:31 -05:00
}
_suspend += value;
}
remove
{
_suspend -= value;
if (_suspend == null)
2021-07-12 19:50:39 +02:00
BridgeConnector.Off("pm-suspend");
2020-05-17 22:16:31 -05:00
}
}
private event Action _suspend;
/// <summary>
/// Emitted when system is resuming.
2020-05-17 22:16:31 -05:00
/// </summary>
2021-08-26 14:22:54 +02:00
[SupportedOSPlatform("windows")]
[SupportedOSPlatform("macos")]
2020-05-17 22:16:31 -05:00
public event Action OnResume
{
add
{
if (_resume == null)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.On("pm-resume", () =>
2020-05-17 22:16:31 -05:00
{
_resume();
});
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("register-pm-resume");
2020-05-17 22:16:31 -05:00
}
_resume += value;
}
remove
{
_resume -= value;
if (_resume == null)
2021-07-12 19:50:39 +02:00
BridgeConnector.Off("pm-resume");
2020-05-17 22:16:31 -05:00
}
}
private event Action _resume;
/// <summary>
/// Emitted when the system changes to AC power.
2020-05-17 22:16:31 -05:00
/// </summary>
2021-08-26 14:22:54 +02:00
[SupportedOSPlatform("windows")]
[SupportedOSPlatform("macos")]
2020-05-17 22:16:31 -05:00
public event Action OnAC
{
add
{
if (_onAC == null)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.On("pm-on-ac", () =>
2020-05-17 22:16:31 -05:00
{
_onAC();
});
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("register-pm-on-ac");
2020-05-17 22:16:31 -05:00
}
_onAC += value;
}
remove
{
_onAC -= value;
if (_onAC == null)
2021-07-12 19:50:39 +02:00
BridgeConnector.Off("pm-on-ac");
2020-05-17 22:16:31 -05:00
}
}
private event Action _onAC;
/// <summary>
/// Emitted when system changes to battery power.
2020-05-17 22:16:31 -05:00
/// </summary>
2021-08-26 14:22:54 +02:00
[SupportedOSPlatform("windows")]
[SupportedOSPlatform("macos")]
2020-05-17 22:16:31 -05:00
public event Action OnBattery
{
add
{
if (_onBattery == null)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.On("pm-on-battery", () =>
2020-05-17 22:16:31 -05:00
{
_onBattery();
});
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("register-pm-on-battery");
2020-05-17 22:16:31 -05:00
}
_onBattery += value;
}
remove
{
_onBattery -= value;
if (_onBattery == null)
2021-07-12 19:50:39 +02:00
BridgeConnector.Off("pm-on-battery");
2020-05-17 22:16:31 -05:00
}
}
private event Action _onBattery;
/// <summary>
/// Emitted when the system is about to reboot or shut down. If the event handler
/// invokes `e.preventDefault()`, Electron will attempt to delay system shutdown in
/// order for the app to exit cleanly.If `e.preventDefault()` is called, the app
/// should exit as soon as possible by calling something like `app.quit()`.
2020-05-17 22:16:31 -05:00
/// </summary>
2021-08-26 14:22:54 +02:00
[SupportedOSPlatform("linux")]
[SupportedOSPlatform("macos")]
2020-05-17 22:16:31 -05:00
public event Action OnShutdown
{
add
{
if (_shutdown == null)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.On("pm-shutdown", () =>
2020-05-17 22:16:31 -05:00
{
_shutdown();
});
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("register-pm-shutdown");
2020-05-17 22:16:31 -05:00
}
_shutdown += value;
}
remove
{
_shutdown -= value;
if (_shutdown == null)
2021-07-12 19:50:39 +02:00
BridgeConnector.Off("pm-on-shutdown");
2020-05-17 22:16:31 -05:00
}
}
private event Action _shutdown;
2020-05-11 12:59:21 -05:00
private static PowerMonitor _powerMonitor;
private static object _syncRoot = new object();
internal PowerMonitor() { }
internal static PowerMonitor Instance
{
get
{
if (_powerMonitor == null)
{
lock (_syncRoot)
{
if (_powerMonitor == null)
{
_powerMonitor = new PowerMonitor();
}
}
}
return _powerMonitor;
}
}
}
}