using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace ElectronNET.API
{
///
///
///
public sealed class WindowManager
{
private static WindowManager _windowManager;
private static readonly object SyncRoot = new();
internal WindowManager() { }
internal static WindowManager Instance
{
get
{
if (_windowManager == null)
{
lock (SyncRoot)
{
if (_windowManager == null)
{
_windowManager = new WindowManager();
}
}
}
return _windowManager;
}
}
///
/// Quit when all windows are closed. (Default is true)
///
///
/// true if [quit window all closed]; otherwise, false.
///
public bool IsQuitOnWindowAllClosed
{
get => _isQuitOnWindowAllClosed;
set
{
BridgeConnector.Socket.Emit("quit-app-window-all-closed-event", value);
_isQuitOnWindowAllClosed = value;
}
}
private bool _isQuitOnWindowAllClosed = true;
///
/// Gets the browser windows.
///
///
/// The browser windows.
///
public IReadOnlyCollection BrowserWindows => _browserWindows.AsReadOnly();
private readonly List _browserWindows = new();
///
/// Gets the browser views.
///
///
/// The browser view.
///
public IReadOnlyCollection BrowserViews => _browserViews.AsReadOnly();
private readonly List _browserViews = new();
///
/// Creates the window asynchronous.
///
/// The load URL.
///
public async Task CreateWindowAsync(string loadUrl = "http://localhost")
{
return await CreateWindowAsync(new BrowserWindowOptions(), loadUrl);
}
///
/// Creates the window asynchronous.
///
/// The options.
/// The load URL.
///
public async Task CreateWindowAsync(BrowserWindowOptions options, string loadUrl = "http://localhost")
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("BrowserWindowCreated", (id) =>
{
BridgeConnector.Socket.Off("BrowserWindowCreated");
var browserWindowId = int.Parse(id.ToString()!);
var browserWindow = new BrowserWindow(browserWindowId);
_browserWindows.Add(browserWindow);
taskCompletionSource.SetResult(browserWindow);
});
BridgeConnector.Socket.On