using ElectronNET.API.Entities;
using ElectronNET.API.Serialization;
using ElectronNET.Common;
using System;
using System.Text.Json;
using System.Threading.Tasks;
namespace ElectronNET.API
{
///
/// Retrieve information about screen size, displays, cursor position, etc.
///
public sealed class Screen
{
///
/// Emitted when an new Display has been added.
///
public event Action OnDisplayAdded
{
add => ApiEventManager.AddEvent("screen-display-added", GetHashCode(), _onDisplayAdded, value, (args) => args.Deserialize(ElectronJsonContext.Default.Display));
remove => ApiEventManager.RemoveEvent("screen-display-added", GetHashCode(), _onDisplayAdded, value);
}
private event Action _onDisplayAdded;
///
/// Emitted when oldDisplay has been removed.
///
public event Action OnDisplayRemoved
{
add => ApiEventManager.AddEvent("screen-display-removed", GetHashCode(), _onDisplayRemoved, value, (args) => args.Deserialize(ElectronJsonContext.Default.Display));
remove => ApiEventManager.RemoveEvent("screen-display-removed", GetHashCode(), _onDisplayRemoved, value);
}
private event Action _onDisplayRemoved;
///
/// 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 => ApiEventManager.AddScreenEvent("screen-display-metrics-changed", GetHashCode(), _onDisplayMetricsChanged, value);
remove => ApiEventManager.RemoveScreenEvent("screen-display-metrics-changed", GetHashCode(), _onDisplayMetricsChanged, value);
}
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()
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("screen-getCursorScreenPointCompleted", tcs.SetResult);
BridgeConnector.Socket.Emit("screen-getCursorScreenPoint");
return tcs.Task;
}
///
/// macOS: The height of the menu bar in pixels.
///
/// The height of the menu bar in pixels.
public Task GetMenuBarHeightAsync()
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("screen-getMenuBarHeightCompleted", tcs.SetResult);
BridgeConnector.Socket.Emit("screen-getMenuBarHeight");
return tcs.Task;
}
///
/// The primary display.
///
///
public Task GetPrimaryDisplayAsync()
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("screen-getPrimaryDisplayCompleted", tcs.SetResult);
BridgeConnector.Socket.Emit("screen-getPrimaryDisplay");
return tcs.Task;
}
///
/// An array of displays that are currently available.
///
/// An array of displays that are currently available.
public Task GetAllDisplaysAsync()
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("screen-getAllDisplaysCompleted", tcs.SetResult);
BridgeConnector.Socket.Emit("screen-getAllDisplays");
return tcs.Task;
}
///
/// The display nearest the specified point.
///
/// The display nearest the specified point.
public Task GetDisplayNearestPointAsync(Point point)
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("screen-getDisplayNearestPointCompleted", tcs.SetResult);
BridgeConnector.Socket.Emit("screen-getDisplayNearestPoint", point);
return tcs.Task;
}
///
/// The display that most closely intersects the provided bounds.
///
///
/// The display that most closely intersects the provided bounds.
public Task GetDisplayMatchingAsync(Rectangle rectangle)
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("screen-getDisplayMatching", tcs.SetResult);
BridgeConnector.Socket.Emit("screen-getDisplayMatching", rectangle);
return tcs.Task;
}
}
}