11using System ;
22using System . Threading . Tasks ;
3+ using ElectronNET . Common ;
4+ // ReSharper disable InconsistentNaming
35
46namespace ElectronNET . API
57{
@@ -13,26 +15,8 @@ public sealed class PowerMonitor
1315 /// </summary>
1416 public event Action OnLockScreen
1517 {
16- add
17- {
18- if ( _lockScreen == null )
19- {
20- BridgeConnector . Socket . On ( "pm-lock-screen" , ( ) =>
21- {
22- _lockScreen ( ) ;
23- } ) ;
24-
25- BridgeConnector . Socket . Emit ( "register-pm-lock-screen" ) ;
26- }
27- _lockScreen += value ;
28- }
29- remove
30- {
31- _lockScreen -= value ;
32-
33- if ( _lockScreen == null )
34- BridgeConnector . Socket . Off ( "pm-lock-screen" ) ;
35- }
18+ add => ApiEventManager . AddEvent ( "pm-lock-screen" , string . Empty , _lockScreen , value ) ;
19+ remove => ApiEventManager . RemoveEvent ( "pm-lock-screen" , string . Empty , _lockScreen , value ) ;
3620 }
3721
3822 private event Action _lockScreen ;
@@ -42,26 +26,8 @@ public event Action OnLockScreen
4226 /// </summary>
4327 public event Action OnUnLockScreen
4428 {
45- add
46- {
47- if ( _unlockScreen == null )
48- {
49- BridgeConnector . Socket . On ( "pm-unlock-screen" , ( ) =>
50- {
51- _unlockScreen ( ) ;
52- } ) ;
53-
54- BridgeConnector . Socket . Emit ( "register-pm-unlock-screen" ) ;
55- }
56- _unlockScreen += value ;
57- }
58- remove
59- {
60- _unlockScreen -= value ;
61-
62- if ( _unlockScreen == null )
63- BridgeConnector . Socket . Off ( "pm-unlock-screen" ) ;
64- }
29+ add => ApiEventManager . AddEvent ( "pm-unlock-screen" , string . Empty , _unlockScreen , value ) ;
30+ remove => ApiEventManager . RemoveEvent ( "pm-unlock-screen" , string . Empty , _unlockScreen , value ) ;
6531 }
6632
6733 private event Action _unlockScreen ;
@@ -71,26 +37,8 @@ public event Action OnUnLockScreen
7137 /// </summary>
7238 public event Action OnSuspend
7339 {
74- add
75- {
76- if ( _suspend == null )
77- {
78- BridgeConnector . Socket . On ( "pm-suspend" , ( ) =>
79- {
80- _suspend ( ) ;
81- } ) ;
82-
83- BridgeConnector . Socket . Emit ( "register-pm-suspend" ) ;
84- }
85- _suspend += value ;
86- }
87- remove
88- {
89- _suspend -= value ;
90-
91- if ( _suspend == null )
92- BridgeConnector . Socket . Off ( "pm-suspend" ) ;
93- }
40+ add => ApiEventManager . AddEvent ( "pm-suspend" , string . Empty , _suspend , value ) ;
41+ remove => ApiEventManager . RemoveEvent ( "pm-suspend" , string . Empty , _suspend , value ) ;
9442 }
9543
9644 private event Action _suspend ;
@@ -100,26 +48,8 @@ public event Action OnSuspend
10048 /// </summary>
10149 public event Action OnResume
10250 {
103- add
104- {
105- if ( _resume == null )
106- {
107- BridgeConnector . Socket . On ( "pm-resume" , ( ) =>
108- {
109- _resume ( ) ;
110- } ) ;
111-
112- BridgeConnector . Socket . Emit ( "register-pm-resume" ) ;
113- }
114- _resume += value ;
115- }
116- remove
117- {
118- _resume -= value ;
119-
120- if ( _resume == null )
121- BridgeConnector . Socket . Off ( "pm-resume" ) ;
122- }
51+ add => ApiEventManager . AddEvent ( "pm-resume" , string . Empty , _resume , value ) ;
52+ remove => ApiEventManager . RemoveEvent ( "pm-resume" , string . Empty , _resume , value ) ;
12353 }
12454
12555 private event Action _resume ;
@@ -129,26 +59,8 @@ public event Action OnResume
12959 /// </summary>
13060 public event Action OnAC
13161 {
132- add
133- {
134- if ( _onAC == null )
135- {
136- BridgeConnector . Socket . On ( "pm-on-ac" , ( ) =>
137- {
138- _onAC ( ) ;
139- } ) ;
140-
141- BridgeConnector . Socket . Emit ( "register-pm-on-ac" ) ;
142- }
143- _onAC += value ;
144- }
145- remove
146- {
147- _onAC -= value ;
148-
149- if ( _onAC == null )
150- BridgeConnector . Socket . Off ( "pm-on-ac" ) ;
151- }
62+ add => ApiEventManager . AddEvent ( "pm-on-ac" , string . Empty , _onAC , value ) ;
63+ remove => ApiEventManager . RemoveEvent ( "pm-on-ac" , string . Empty , _onAC , value ) ;
15264 }
15365
15466 private event Action _onAC ;
@@ -158,31 +70,12 @@ public event Action OnAC
15870 /// </summary>
15971 public event Action OnBattery
16072 {
161- add
162- {
163- if ( _onBattery == null )
164- {
165- BridgeConnector . Socket . On ( "pm-on-battery" , ( ) =>
166- {
167- _onBattery ( ) ;
168- } ) ;
169-
170- BridgeConnector . Socket . Emit ( "register-pm-on-battery" ) ;
171- }
172- _onBattery += value ;
173- }
174- remove
175- {
176- _onBattery -= value ;
177-
178- if ( _onBattery == null )
179- BridgeConnector . Socket . Off ( "pm-on-battery" ) ;
180- }
73+ add => ApiEventManager . AddEvent ( "pm-on-battery" , string . Empty , _onBattery , value ) ;
74+ remove => ApiEventManager . RemoveEvent ( "pm-on-battery" , string . Empty , _onBattery , value ) ;
18175 }
18276
18377 private event Action _onBattery ;
184-
185-
78+
18679 /// <summary>
18780 /// Emitted when the system is about to reboot or shut down. If the event handler
18881 /// invokes `e.preventDefault()`, Electron will attempt to delay system shutdown in
@@ -191,26 +84,8 @@ public event Action OnBattery
19184 /// </summary>
19285 public event Action OnShutdown
19386 {
194- add
195- {
196- if ( _shutdown == null )
197- {
198- BridgeConnector . Socket . On ( "pm-shutdown" , ( ) =>
199- {
200- _shutdown ( ) ;
201- } ) ;
202-
203- BridgeConnector . Socket . Emit ( "register-pm-shutdown" ) ;
204- }
205- _shutdown += value ;
206- }
207- remove
208- {
209- _shutdown -= value ;
210-
211- if ( _shutdown == null )
212- BridgeConnector . Socket . Off ( "pm-on-shutdown" ) ;
213- }
87+ add => ApiEventManager . AddEvent ( "pm-shutdown" , string . Empty , _shutdown , value ) ;
88+ remove => ApiEventManager . RemoveEvent ( "pm-shutdown" , string . Empty , _shutdown , value ) ;
21489 }
21590
21691 private event Action _shutdown ;
0 commit comments