refactoring dotnet events

This commit is contained in:
agracio
2025-11-05 15:06:41 +00:00
parent 408f83e401
commit b6b9292478
7 changed files with 95 additions and 406 deletions

View File

@@ -1,5 +1,7 @@
using System;
using System.Threading.Tasks;
using ElectronNET.Common;
// ReSharper disable InconsistentNaming
namespace ElectronNET.API
{
@@ -13,26 +15,8 @@ namespace ElectronNET.API
/// </summary>
public event Action OnLockScreen
{
add
{
if (_lockScreen == null)
{
BridgeConnector.Socket.On("pm-lock-screen" , () =>
{
_lockScreen();
});
BridgeConnector.Socket.Emit("register-pm-lock-screen");
}
_lockScreen += value;
}
remove
{
_lockScreen -= value;
if (_lockScreen == null)
BridgeConnector.Socket.Off("pm-lock-screen");
}
add => ApiEventManager.AddEvent("pm-lock-screen", string.Empty, _lockScreen, value);
remove => ApiEventManager.RemoveEvent("pm-lock-screen", string.Empty, _lockScreen, value);
}
private event Action _lockScreen;
@@ -42,26 +26,8 @@ namespace ElectronNET.API
/// </summary>
public event Action OnUnLockScreen
{
add
{
if (_unlockScreen == null)
{
BridgeConnector.Socket.On("pm-unlock-screen", () =>
{
_unlockScreen();
});
BridgeConnector.Socket.Emit("register-pm-unlock-screen");
}
_unlockScreen += value;
}
remove
{
_unlockScreen -= value;
if (_unlockScreen == null)
BridgeConnector.Socket.Off("pm-unlock-screen");
}
add => ApiEventManager.AddEvent("pm-unlock-screen", string.Empty, _unlockScreen, value);
remove => ApiEventManager.RemoveEvent("pm-unlock-screen", string.Empty, _unlockScreen, value);
}
private event Action _unlockScreen;
@@ -71,26 +37,8 @@ namespace ElectronNET.API
/// </summary>
public event Action OnSuspend
{
add
{
if (_suspend == null)
{
BridgeConnector.Socket.On("pm-suspend", () =>
{
_suspend();
});
BridgeConnector.Socket.Emit("register-pm-suspend");
}
_suspend += value;
}
remove
{
_suspend -= value;
if (_suspend == null)
BridgeConnector.Socket.Off("pm-suspend");
}
add => ApiEventManager.AddEvent("pm-suspend", string.Empty, _suspend, value);
remove => ApiEventManager.RemoveEvent("pm-suspend", string.Empty, _suspend, value);
}
private event Action _suspend;
@@ -100,26 +48,8 @@ namespace ElectronNET.API
/// </summary>
public event Action OnResume
{
add
{
if (_resume == null)
{
BridgeConnector.Socket.On("pm-resume", () =>
{
_resume();
});
BridgeConnector.Socket.Emit("register-pm-resume");
}
_resume += value;
}
remove
{
_resume -= value;
if (_resume == null)
BridgeConnector.Socket.Off("pm-resume");
}
add => ApiEventManager.AddEvent("pm-resume", string.Empty, _resume, value);
remove => ApiEventManager.RemoveEvent("pm-resume", string.Empty, _resume, value);
}
private event Action _resume;
@@ -129,26 +59,8 @@ namespace ElectronNET.API
/// </summary>
public event Action OnAC
{
add
{
if (_onAC == null)
{
BridgeConnector.Socket.On("pm-on-ac", () =>
{
_onAC();
});
BridgeConnector.Socket.Emit("register-pm-on-ac");
}
_onAC += value;
}
remove
{
_onAC -= value;
if (_onAC == null)
BridgeConnector.Socket.Off("pm-on-ac");
}
add => ApiEventManager.AddEvent("pm-on-ac", string.Empty, _onAC, value);
remove => ApiEventManager.RemoveEvent("pm-on-ac", string.Empty, _onAC, value);
}
private event Action _onAC;
@@ -158,31 +70,12 @@ namespace ElectronNET.API
/// </summary>
public event Action OnBattery
{
add
{
if (_onBattery == null)
{
BridgeConnector.Socket.On("pm-on-battery", () =>
{
_onBattery();
});
BridgeConnector.Socket.Emit("register-pm-on-battery");
}
_onBattery += value;
}
remove
{
_onBattery -= value;
if (_onBattery == null)
BridgeConnector.Socket.Off("pm-on-battery");
}
add => ApiEventManager.AddEvent("pm-on-battery", string.Empty, _onBattery, value);
remove => ApiEventManager.RemoveEvent("pm-on-battery", string.Empty, _onBattery, value);
}
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
@@ -191,26 +84,8 @@ namespace ElectronNET.API
/// </summary>
public event Action OnShutdown
{
add
{
if (_shutdown == null)
{
BridgeConnector.Socket.On("pm-shutdown", () =>
{
_shutdown();
});
BridgeConnector.Socket.Emit("register-pm-shutdown");
}
_shutdown += value;
}
remove
{
_shutdown -= value;
if (_shutdown == null)
BridgeConnector.Socket.Off("pm-on-shutdown");
}
add => ApiEventManager.AddEvent("pm-shutdown", string.Empty, _shutdown, value);
remove => ApiEventManager.RemoveEvent("pm-shutdown", string.Empty, _shutdown, value);
}
private event Action _shutdown;

View File

@@ -4,6 +4,7 @@ using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Threading.Tasks;
using ElectronNET.Common;
namespace ElectronNET.API
{
@@ -17,26 +18,8 @@ namespace ElectronNET.API
/// </summary>
public event Action<Display> OnDisplayAdded
{
add
{
if (_onDisplayAdded == null)
{
BridgeConnector.Socket.On("screen-display-added-event" + GetHashCode(), (display) =>
{
_onDisplayAdded(((JObject)display).ToObject<Display>());
});
BridgeConnector.Socket.Emit("register-screen-display-added", GetHashCode());
}
_onDisplayAdded += value;
}
remove
{
_onDisplayAdded -= value;
if (_onDisplayAdded == null)
BridgeConnector.Socket.Off("screen-display-added-event" + GetHashCode());
}
add => ApiEventManager.AddEvent("screen-display-added", GetHashCode(), _onDisplayAdded, value, (args) => ((JObject)args).ToObject<Display>());
remove => ApiEventManager.RemoveEvent("screen-display-added", GetHashCode(), _onDisplayAdded, value);
}
private event Action<Display> _onDisplayAdded;
@@ -46,26 +29,8 @@ namespace ElectronNET.API
/// </summary>
public event Action<Display> OnDisplayRemoved
{
add
{
if (_onDisplayRemoved == null)
{
BridgeConnector.Socket.On("screen-display-removed-event" + GetHashCode(), (display) =>
{
_onDisplayRemoved(((JObject)display).ToObject<Display>());
});
BridgeConnector.Socket.Emit("register-screen-display-removed", GetHashCode());
}
_onDisplayRemoved += value;
}
remove
{
_onDisplayRemoved -= value;
if (_onDisplayRemoved == null)
BridgeConnector.Socket.Off("screen-display-removed-event" + GetHashCode());
}
add => ApiEventManager.AddEvent("screen-display-removed", GetHashCode(), _onDisplayRemoved, value, (args) => ((JObject)args).ToObject<Display>());
remove => ApiEventManager.RemoveEvent("screen-display-removed", GetHashCode(), _onDisplayRemoved, value);
}
private event Action<Display> _onDisplayRemoved;
@@ -77,29 +42,8 @@ namespace ElectronNET.API
/// </summary>
public event Action<Display, string[]> OnDisplayMetricsChanged
{
add
{
if (_onDisplayMetricsChanged == null)
{
BridgeConnector.Socket.On("screen-display-metrics-changed-event" + GetHashCode(), (args) =>
{
var display = ((JArray)args).First.ToObject<Display>();
var metrics = ((JArray)args).Last.ToObject<string[]>();
_onDisplayMetricsChanged(display, metrics);
});
BridgeConnector.Socket.Emit("register-screen-display-metrics-changed", GetHashCode());
}
_onDisplayMetricsChanged += value;
}
remove
{
_onDisplayMetricsChanged -= value;
if (_onDisplayMetricsChanged == null)
BridgeConnector.Socket.Off("screen-display-metrics-changed-event" + GetHashCode());
}
add => ApiEventManager.AddScreenEvent("screen-display-metrics-changed", GetHashCode(), _onDisplayMetricsChanged, value);
remove => ApiEventManager.RemoveScreenEvent("screen-display-metrics-changed", GetHashCode(), _onDisplayMetricsChanged, value);
}
private event Action<Display, string[]> _onDisplayMetricsChanged;

View File

@@ -4,6 +4,8 @@ using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Threading.Tasks;
using ElectronNET.Common;
// ReSharper disable InconsistentNaming
namespace ElectronNET.API;
@@ -30,26 +32,8 @@ public class WebContents
/// </summary>
public event Action<bool> OnCrashed
{
add
{
if (_crashed == null)
{
BridgeConnector.Socket.On("webContents-crashed" + Id, (killed) =>
{
_crashed((bool)killed);
});
BridgeConnector.Socket.Emit("register-webContents-crashed", Id);
}
_crashed += value;
}
remove
{
_crashed -= value;
if (_crashed == null)
BridgeConnector.Socket.Off("webContents-crashed" + Id);
}
add => ApiEventManager.AddEvent("webContents-crashed", Id, _crashed, value, (args) => (bool)args);
remove => ApiEventManager.RemoveEvent("webContents-crashed", Id, _crashed, value);
}
private event Action<bool> _crashed;
@@ -60,26 +44,8 @@ public class WebContents
/// </summary>
public event Action OnDidFinishLoad
{
add
{
if (_didFinishLoad == null)
{
BridgeConnector.Socket.On("webContents-didFinishLoad" + Id, () =>
{
_didFinishLoad();
});
BridgeConnector.Socket.Emit("register-webContents-didFinishLoad", Id);
}
_didFinishLoad += value;
}
remove
{
_didFinishLoad -= value;
if (_didFinishLoad == null)
BridgeConnector.Socket.Off("webContents-didFinishLoad" + Id);
}
add => ApiEventManager.AddEvent("webContents-didFinishLoad", Id, _didFinishLoad, value);
remove => ApiEventManager.RemoveEvent("webContents-didFinishLoad", Id, _didFinishLoad, value);
}
private event Action _didFinishLoad;
@@ -89,26 +55,8 @@ public class WebContents
/// </summary>
public event Action<string> OnDidStartNavigation
{
add
{
if (_didStartNavigation == null)
{
BridgeConnector.Socket.On<string>("webContents-didStartNavigation" + Id, (url) =>
{
_didStartNavigation(url);
});
BridgeConnector.Socket.Emit("register-webContents-didStartNavigation", Id);
}
_didStartNavigation += value;
}
remove
{
_didStartNavigation -= value;
if (_didStartNavigation == null)
BridgeConnector.Socket.Off("webContents-didStartNavigation" + Id);
}
add => ApiEventManager.AddEvent("webContents-didStartNavigation", Id, _didStartNavigation, value);
remove => ApiEventManager.RemoveEvent("webContents-didStartNavigation", Id, _didStartNavigation, value);
}
private event Action<string> _didStartNavigation;
@@ -119,26 +67,8 @@ public class WebContents
/// </summary>
public event Action<OnDidNavigateInfo> OnDidNavigate
{
add
{
if (_didNavigate == null)
{
BridgeConnector.Socket.On<OnDidNavigateInfo>("webContents-didNavigate" + Id, (data) =>
{
_didNavigate(data);
});
BridgeConnector.Socket.Emit("register-webContents-didNavigate", Id);
}
_didNavigate += value;
}
remove
{
_didNavigate -= value;
if (_didNavigate == null)
BridgeConnector.Socket.Off("webContents-didNavigate" + Id);
}
add => ApiEventManager.AddEvent("webContents-didNavigate", Id, _didNavigate, value);
remove => ApiEventManager.RemoveEvent("webContents-didNavigate", Id, _didNavigate, value);
}
private event Action<OnDidNavigateInfo> _didNavigate;
@@ -149,26 +79,8 @@ public class WebContents
/// </summary>
public event Action<string> OnWillRedirect
{
add
{
if (_willRedirect == null)
{
BridgeConnector.Socket.On<string>("webContents-willRedirect" + Id, (url) =>
{
_willRedirect(url);
});
BridgeConnector.Socket.Emit("register-webContents-willRedirect", Id);
}
_willRedirect += value;
}
remove
{
_willRedirect -= value;
if (_willRedirect == null)
BridgeConnector.Socket.Off("webContents-willRedirect" + Id);
}
add => ApiEventManager.AddEvent("webContents-willRedirect", Id, _willRedirect, value);
remove => ApiEventManager.RemoveEvent("webContents-willRedirect", Id, _willRedirect, value);
}
private event Action<string> _willRedirect;
@@ -178,56 +90,19 @@ public class WebContents
/// </summary>
public event Action<string> OnDidRedirectNavigation
{
add
{
if (_didRedirectNavigation == null)
{
BridgeConnector.Socket.On("webContents-didRedirectNavigation" + Id, (url) =>
{
_didRedirectNavigation(url?.ToString());
});
BridgeConnector.Socket.Emit("register-webContents-didRedirectNavigation", Id);
}
_didRedirectNavigation += value;
}
remove
{
_didRedirectNavigation -= value;
if (_didRedirectNavigation == null)
BridgeConnector.Socket.Off("webContents-didRedirectNavigation" + Id);
}
add => ApiEventManager.AddEvent("webContents-didRedirectNavigation", Id, _didRedirectNavigation, value);
remove => ApiEventManager.RemoveEvent("webContents-didRedirectNavigation", Id, _didRedirectNavigation, value);
}
private event Action<string> _didRedirectNavigation;
/// <summary>
/// This event is like OnDidFinishLoad but emitted when the load failed.
/// </summary>
public event Action<OnDidFailLoadInfo> OnDidFailLoad
{
add
{
if (_didFailLoad == null)
{
BridgeConnector.Socket.On("webContents-willRedirect" + Id, (data) =>
{
_didFailLoad(((JObject) data).ToObject<OnDidFailLoadInfo>());
});
BridgeConnector.Socket.Emit("register-webContents-willRedirect", Id);
}
_didFailLoad += value;
}
remove
{
_didFailLoad -= value;
if (_didFailLoad == null)
BridgeConnector.Socket.Off("webContents-willRedirect" + Id);
}
add => ApiEventManager.AddEvent("webContents-didFailLoad", Id, _didFailLoad, value, (args) => ((JObject)args).ToObject<OnDidFailLoadInfo>());
remove => ApiEventManager.RemoveEvent("webContents-didFailLoad", Id, _didFailLoad, value);
}
private event Action<OnDidFailLoadInfo> _didFailLoad;
@@ -237,27 +112,8 @@ public class WebContents
/// </summary>
public event Action<InputEvent> InputEvent
{
add
{
if (_inputEvent == null)
{
BridgeConnector.Socket.On("webContents-input-event" + Id, (eventArgs) =>
{
var inputEvent = ((JObject)eventArgs).ToObject<InputEvent>();
_inputEvent(inputEvent);
});
BridgeConnector.Socket.Emit("register-webContents-input-event", Id);
}
_inputEvent += value;
}
remove
{
_inputEvent -= value;
if (_inputEvent == null)
BridgeConnector.Socket.Off("webContents-input-event" + Id);
}
add => ApiEventManager.AddEvent("webContents-input-event", Id, _inputEvent, value, (args) => ((JObject)args).ToObject<InputEvent>());
remove => ApiEventManager.RemoveEvent("webContents-input-event", Id, _inputEvent, value);
}
private event Action<InputEvent> _inputEvent;
@@ -267,26 +123,8 @@ public class WebContents
/// </summary>
public event Action OnDomReady
{
add
{
if (_domReady == null)
{
BridgeConnector.Socket.On("webContents-domReady" + Id, () =>
{
_domReady();
});
BridgeConnector.Socket.Emit("register-webContents-domReady", Id);
}
_domReady += value;
}
remove
{
_domReady -= value;
if (_domReady == null)
BridgeConnector.Socket.Off("webContents-domReady" + Id);
}
add => ApiEventManager.AddEvent("webContents-domReady", Id, _domReady, value);
remove => ApiEventManager.RemoveEvent("webContents-domReady", Id, _domReady, value);
}
private event Action _domReady;