Skip to content

Commit 6690b4a

Browse files
Merge pull request #399 from gustavo-lara-molina/powerMonitor
Power monitor
2 parents 6ddce85 + c8e51ef commit 6690b4a

File tree

7 files changed

+165
-1
lines changed

7 files changed

+165
-1
lines changed

ElectronNET.API/Electron.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,10 @@ public static class Electron
7373
/// <c>electronize add HostHook</c>
7474
/// </summary>
7575
public static HostHook HostHook { get { return HostHook.Instance; } }
76+
77+
/// <summary>
78+
/// Allows you to execute native Lock and Unlock process.
79+
/// </summary>
80+
public static PowerMonitor PowerMonitor { get { return PowerMonitor.Instance; } }
7681
}
7782
}

ElectronNET.API/PowerMonitor.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using ElectronNET.API.Entities;
2+
using ElectronNET.API.Extensions;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
5+
using Newtonsoft.Json.Serialization;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Threading.Tasks;
9+
10+
namespace ElectronNET.API
11+
{
12+
/// <summary>
13+
/// Monitor power state changes..
14+
/// </summary>
15+
public sealed class PowerMonitor
16+
{
17+
18+
19+
20+
/// <summary>
21+
/// Emitted when the system is about to lock the screen.
22+
/// </summary>
23+
public event Action OnLockScreen
24+
{
25+
add
26+
{
27+
if (_lockScreen == null)
28+
{
29+
BridgeConnector.Socket.On("pm-lock-screen" , () =>
30+
{
31+
_lockScreen();
32+
});
33+
34+
BridgeConnector.Socket.Emit("register-pm-lock-screen");
35+
}
36+
_lockScreen += value;
37+
}
38+
remove
39+
{
40+
_lockScreen -= value;
41+
42+
if (_lockScreen == null)
43+
BridgeConnector.Socket.Off("pm-lock-screen");
44+
}
45+
}
46+
47+
private event Action _lockScreen;
48+
49+
/// <summary>
50+
/// Emitted when the system is about to unlock the screen.
51+
/// </summary>
52+
public event Action OnUnLockScreen
53+
{
54+
add
55+
{
56+
if (_unlockScreen == null)
57+
{
58+
BridgeConnector.Socket.On("pm-unlock-screen", () =>
59+
{
60+
_unlockScreen();
61+
});
62+
63+
BridgeConnector.Socket.Emit("register-pm-unlock-screen");
64+
}
65+
_unlockScreen += value;
66+
}
67+
remove
68+
{
69+
_unlockScreen -= value;
70+
71+
if (_unlockScreen == null)
72+
BridgeConnector.Socket.Off("pm-unlock-screen");
73+
}
74+
}
75+
76+
private event Action _unlockScreen;
77+
78+
79+
80+
private static PowerMonitor _powerMonitor;
81+
private static object _syncRoot = new object();
82+
83+
internal PowerMonitor() { }
84+
85+
internal static PowerMonitor Instance
86+
{
87+
get
88+
{
89+
if (_powerMonitor == null)
90+
{
91+
lock (_syncRoot)
92+
{
93+
if (_powerMonitor == null)
94+
{
95+
_powerMonitor = new PowerMonitor();
96+
}
97+
}
98+
}
99+
100+
return _powerMonitor;
101+
}
102+
}
103+
104+
105+
106+
}
107+
}

ElectronNET.Host/api/powerMonitor.js

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/powerMonitor.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { powerMonitor } from 'electron';
2+
let electronSocket;
3+
4+
export = (socket: SocketIO.Socket) => {
5+
electronSocket = socket;
6+
socket.on('register-pm-lock-screen', () => {
7+
powerMonitor.on('lock-screen', () => {
8+
electronSocket.emit('pm-lock-screen');
9+
});
10+
});
11+
socket.on('register-pm-unlock-screen', () => {
12+
powerMonitor.on('unlock-screen', () => {
13+
electronSocket.emit('pm-unlock-screen');
14+
});
15+
});
16+
};

ElectronNET.Host/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let io, server, browserWindows, ipc, apiProcess, loadURL;
88
let appApi, menu, dialogApi, notification, tray, webContents;
99
let globalShortcut, shellApi, screen, clipboard, autoUpdater;
1010
let commandLine, browserView;
11+
let powerMonitor;
1112
let splashScreen, hostHook;
1213
let mainWindowId;
1314

@@ -152,6 +153,7 @@ function startSocketApiBridge(port) {
152153
delete require.cache[require.resolve('./api/screen')];
153154
delete require.cache[require.resolve('./api/clipboard')];
154155
delete require.cache[require.resolve('./api/browserView')];
156+
delete require.cache[require.resolve('./api/powerMonitor')];
155157
});
156158

157159
global['electronsocket'] = socket;
@@ -173,6 +175,7 @@ function startSocketApiBridge(port) {
173175
screen = require('./api/screen')(socket);
174176
clipboard = require('./api/clipboard')(socket);
175177
browserView = require('./api/browserView')(socket);
178+
powerMonitor = require('./api/powerMonitor')(socket);
176179

177180

178181

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
using Microsoft.AspNetCore.Mvc;
2+
using ElectronNET.API;
3+
using ElectronNET.API.Entities;
4+
using System;
25

36
namespace ElectronNET.WebApp.Controllers
47
{
58
public class HomeController : Controller
69
{
710
public IActionResult Index()
811
{
9-
return View();
12+
if (HybridSupport.IsElectronActive)
13+
{
14+
Electron.PowerMonitor.OnLockScreen += () =>
15+
{
16+
Console.WriteLine("Screen Locked detected from C #");
17+
};
18+
19+
Electron.PowerMonitor.OnUnLockScreen += () =>
20+
{
21+
Console.WriteLine("Screen unlocked detected from C # ");
22+
};
23+
}
24+
return View();
1025
}
1126
}
1227
}

0 commit comments

Comments
 (0)