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

119 lines
3.8 KiB
C#
Raw Normal View History

using ElectronNET.Common;
using System;
2025-11-09 03:50:24 +01:00
2025-11-05 15:06:41 +00:00
// ReSharper disable InconsistentNaming
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>
public event Action OnLockScreen
{
2025-11-05 15:06:41 +00:00
add => ApiEventManager.AddEvent("pm-lock-screen", string.Empty, _lockScreen, value);
remove => ApiEventManager.RemoveEvent("pm-lock-screen", string.Empty, _lockScreen, value);
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>
public event Action OnUnLockScreen
{
2025-11-05 15:06:41 +00:00
add => ApiEventManager.AddEvent("pm-unlock-screen", string.Empty, _unlockScreen, value);
remove => ApiEventManager.RemoveEvent("pm-unlock-screen", string.Empty, _unlockScreen, value);
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>
public event Action OnSuspend
{
2025-11-05 15:06:41 +00:00
add => ApiEventManager.AddEvent("pm-suspend", string.Empty, _suspend, value);
remove => ApiEventManager.RemoveEvent("pm-suspend", string.Empty, _suspend, value);
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>
public event Action OnResume
{
2025-11-05 15:06:41 +00:00
add => ApiEventManager.AddEvent("pm-resume", string.Empty, _resume, value);
remove => ApiEventManager.RemoveEvent("pm-resume", string.Empty, _resume, value);
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>
public event Action OnAC
{
2025-11-05 15:06:41 +00:00
add => ApiEventManager.AddEvent("pm-on-ac", string.Empty, _onAC, value);
remove => ApiEventManager.RemoveEvent("pm-on-ac", string.Empty, _onAC, value);
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>
public event Action OnBattery
{
2025-11-05 15:06:41 +00:00
add => ApiEventManager.AddEvent("pm-on-battery", string.Empty, _onBattery, value);
remove => ApiEventManager.RemoveEvent("pm-on-battery", string.Empty, _onBattery, value);
2020-05-17 22:16:31 -05:00
}
private event Action _onBattery;
2025-11-09 03:50:24 +01:00
2020-05-17 22:16:31 -05:00
/// <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>
public event Action OnShutdown
{
2025-11-05 15:06:41 +00:00
add => ApiEventManager.AddEvent("pm-shutdown", string.Empty, _shutdown, value);
remove => ApiEventManager.RemoveEvent("pm-shutdown", string.Empty, _shutdown, value);
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();
2025-11-09 03:50:24 +01:00
internal PowerMonitor()
{
}
2020-05-11 12:59:21 -05:00
internal static PowerMonitor Instance
{
get
{
if (_powerMonitor == null)
{
lock (_syncRoot)
{
if (_powerMonitor == null)
{
_powerMonitor = new PowerMonitor();
}
}
}
return _powerMonitor;
}
}
}
2025-11-09 03:50:24 +01:00
}