Files
Electron.NET/ElectronNET.API/WindowManager.cs
Maher Jendoubi ed6ebe90b1 Fixed some spelling issues
* Cleanup of unnecessary usings
* Used language keywords instead of BCL types : Object --> object
* Fixed some spelling issues
2019-05-18 02:00:56 +02:00

166 lines
5.4 KiB
C#

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.Runtime.InteropServices;
using System.Threading.Tasks;
namespace ElectronNET.API
{
/// <summary>
///
/// </summary>
public sealed class WindowManager
{
private static WindowManager _windowManager;
private static object _syncRoot = new object();
internal WindowManager() { }
internal static WindowManager Instance
{
get
{
if (_windowManager == null)
{
lock (_syncRoot)
{
if (_windowManager == null)
{
_windowManager = new WindowManager();
}
}
}
return _windowManager;
}
}
/// <summary>
/// Quit when all windows are closed. (Default is true)
/// </summary>
/// <value>
/// <c>true</c> if [quit window all closed]; otherwise, <c>false</c>.
/// </value>
public bool IsQuitOnWindowAllClosed
{
get { return _isQuitOnWindowAllClosed; }
set
{
BridgeConnector.Socket.Emit("quit-app-window-all-closed-event", value);
_isQuitOnWindowAllClosed = value;
}
}
private bool _isQuitOnWindowAllClosed = true;
/// <summary>
/// Gets the browser windows.
/// </summary>
/// <value>
/// The browser windows.
/// </value>
public IReadOnlyCollection<BrowserWindow> BrowserWindows { get { return _browserWindows.AsReadOnly(); } }
private List<BrowserWindow> _browserWindows = new List<BrowserWindow>();
/// <summary>
/// Creates the window asynchronous.
/// </summary>
/// <param name="loadUrl">The load URL.</param>
/// <returns></returns>
public async Task<BrowserWindow> CreateWindowAsync(string loadUrl = "http://localhost")
{
return await CreateWindowAsync(new BrowserWindowOptions(), loadUrl);
}
/// <summary>
/// Creates the window asynchronous.
/// </summary>
/// <param name="options">The options.</param>
/// <param name="loadUrl">The load URL.</param>
/// <returns></returns>
public Task<BrowserWindow> CreateWindowAsync(BrowserWindowOptions options, string loadUrl = "http://localhost")
{
var taskCompletionSource = new TaskCompletionSource<BrowserWindow>();
BridgeConnector.Socket.On("BrowserWindowCreated", (id) =>
{
BridgeConnector.Socket.Off("BrowserWindowCreated");
string windowId = id.ToString();
BrowserWindow browserWindow = new BrowserWindow(int.Parse(windowId));
_browserWindows.Add(browserWindow);
taskCompletionSource.SetResult(browserWindow);
});
BridgeConnector.Socket.Off("BrowserWindowClosed");
BridgeConnector.Socket.On("BrowserWindowClosed", (ids) =>
{
var browserWindowIds = ((JArray)ids).ToObject<int[]>();
for (int index = 0; index < _browserWindows.Count; index++)
{
if (!browserWindowIds.Contains(_browserWindows[index].Id))
{
_browserWindows.RemoveAt(index);
}
}
});
if (loadUrl.ToUpper() == "HTTP://LOCALHOST")
{
loadUrl = $"{loadUrl}:{BridgeSettings.WebPort}";
}
// Workaround Windows 10 / Electron Bug
// https://github.com/electron/electron/issues/4045
if (isWindows10())
{
options.Width = options.Width + 14;
options.Height = options.Height + 7;
}
if (options.X == -1 && options.Y == -1)
{
options.X = 0;
options.Y = 0;
BridgeConnector.Socket.Emit("createBrowserWindow", JObject.FromObject(options, _jsonSerializer), loadUrl);
}
else
{
// Workaround Windows 10 / Electron Bug
// https://github.com/electron/electron/issues/4045
if (isWindows10())
{
options.X = options.X - 7;
}
var ownjsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
};
BridgeConnector.Socket.Emit("createBrowserWindow", JObject.FromObject(options, ownjsonSerializer), loadUrl);
}
return taskCompletionSource.Task;
}
private bool isWindows10()
{
return RuntimeInformation.OSDescription.Contains("Windows 10");
}
private JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
}
}