using ElectronNET.API.Entities;
using System;
using System.Linq;
using System.Runtime.Versioning;
using System.Text.Json;
using System.Threading.Tasks;
using ElectronNET.API.Serialization;
namespace ElectronNET.API
{
///
/// Retrieve information about screen size, displays, cursor position, etc.
///
public sealed class Screen : ApiBase
{
protected override SocketTaskEventNameTypes SocketTaskEventNameType => SocketTaskEventNameTypes.DashesLowerFirst;
protected override SocketTaskMessageNameTypes SocketTaskMessageNameType => SocketTaskMessageNameTypes.DashesLowerFirst;
protected override SocketEventNameTypes SocketEventNameType => SocketEventNameTypes.DashedLower;
///
/// Emitted when an new Display has been added.
///
public event Action OnDisplayAdded
{
add => AddEvent(value, GetHashCode());
remove => RemoveEvent(value, GetHashCode());
}
///
/// Emitted when oldDisplay has been removed.
///
public event Action OnDisplayRemoved
{
add => AddEvent(value, GetHashCode());
remove => RemoveEvent(value, GetHashCode());
}
///
/// Emitted when one or more metrics change in a display.
/// The changedMetrics is an array of strings that describe the changes.
/// Possible changes are bounds, workArea, scaleFactor and rotation.
///
public event Action OnDisplayMetricsChanged
{
add
{
if (_onDisplayMetricsChanged == null)
{
BridgeConnector.Socket.On("screen-display-metrics-changed" + GetHashCode(), (args) =>
{
var arr = args.EnumerateArray().ToArray();
var display = arr[0].Deserialize(ElectronJsonContext.Default.Display);
var metrics = arr[1].Deserialize(ElectronJson.Options);
_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" + GetHashCode());
}
}
}
private event Action _onDisplayMetricsChanged;
private static Screen _screen;
private static object _syncRoot = new object();
internal Screen()
{
}
internal static Screen Instance
{
get
{
if (_screen == null)
{
lock (_syncRoot)
{
if (_screen == null)
{
_screen = new Screen();
}
}
}
return _screen;
}
}
///
/// The current absolute position of the mouse pointer.
///
///
public Task GetCursorScreenPointAsync() => this.InvokeAsync();
///
/// macOS: The height of the menu bar in pixels.
///
/// The height of the menu bar in pixels.
[SupportedOSPlatform("macOS")]
public Task GetMenuBarWorkAreaAsync() => this.InvokeAsync();
///
/// The primary display.
///
///
public Task GetPrimaryDisplayAsync() => this.InvokeAsync();
///
/// An array of displays that are currently available.
///
/// An array of displays that are currently available.
public Task GetAllDisplaysAsync() => this.InvokeAsync();
///
/// The display nearest the specified point.
///
/// The display nearest the specified point.
public Task GetDisplayNearestPointAsync(Point point) => this.InvokeAsync(point);
///
/// The display that most closely intersects the provided bounds.
///
///
/// The display that most closely intersects the provided bounds.
public Task GetDisplayMatchingAsync(Rectangle rectangle) => this.InvokeAsync(rectangle);
}
}