using ElectronNET.API.Entities;
using ElectronNET.API.Extensions;
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;
///
/// Create and control browser windows.
///
public class BrowserWindow
{
///
/// Gets the identifier.
///
///
/// The identifier.
///
public int Id { get; private set; }
///
/// Emitted when the web page has been rendered (while not being shown) and
/// window can be displayed without a visual flash.
///
public event Action OnReadyToShow
{
add
{
if (_readyToShow == null)
{
BridgeConnector.Socket.On("browserWindow-ready-to-show" + Id, () =>
{
_readyToShow();
});
BridgeConnector.Socket.Emit("register-browserWindow-ready-to-show", Id);
}
_readyToShow += value;
}
remove
{
_readyToShow -= value;
if (_readyToShow == null)
BridgeConnector.Socket.Off("browserWindow-ready-to-show" + Id);
}
}
private event Action _readyToShow;
///
/// Emitted when the document changed its title.
///
public event Action OnPageTitleUpdated
{
add
{
if (_pageTitleUpdated == null)
{
BridgeConnector.Socket.On("browserWindow-page-title-updated" + Id, (title) =>
{
_pageTitleUpdated(title.ToString());
});
BridgeConnector.Socket.Emit("register-browserWindow-page-title-updated", Id);
}
_pageTitleUpdated += value;
}
remove
{
_pageTitleUpdated -= value;
if (_pageTitleUpdated == null)
BridgeConnector.Socket.Off("browserWindow-page-title-updated" + Id);
}
}
private event Action _pageTitleUpdated;
///
/// Emitted when the window is going to be closed.
///
public event Action OnClose
{
add
{
if (_close == null)
{
BridgeConnector.Socket.On("browserWindow-close" + Id, () =>
{
_close();
});
BridgeConnector.Socket.Emit("register-browserWindow-close", Id);
}
_close += value;
}
remove
{
_close -= value;
if (_close == null)
BridgeConnector.Socket.Off("browserWindow-close" + Id);
}
}
private event Action _close;
///
/// Emitted when the window is closed.
/// After you have received this event you should remove the
/// reference to the window and avoid using it any more.
///
public event Action OnClosed
{
add
{
if (_closed == null)
{
BridgeConnector.Socket.On("browserWindow-closed" + Id, () =>
{
_closed();
});
BridgeConnector.Socket.Emit("register-browserWindow-closed", Id);
}
_closed += value;
}
remove
{
_closed -= value;
if (_closed == null)
BridgeConnector.Socket.Off("browserWindow-closed" + Id);
}
}
private event Action _closed;
///
/// Emitted when window session is going to end due to force shutdown or machine restart or session log off.
///
public event Action OnSessionEnd
{
add
{
if (_sessionEnd == null)
{
BridgeConnector.Socket.On("browserWindow-session-end" + Id, () =>
{
_sessionEnd();
});
BridgeConnector.Socket.Emit("register-browserWindow-session-end", Id);
}
_sessionEnd += value;
}
remove
{
_sessionEnd -= value;
if (_sessionEnd == null)
BridgeConnector.Socket.Off("browserWindow-session-end" + Id);
}
}
private event Action _sessionEnd;
///
/// Emitted when the web page becomes unresponsive.
///
public event Action OnUnresponsive
{
add
{
if (_unresponsive == null)
{
BridgeConnector.Socket.On("browserWindow-unresponsive" + Id, () =>
{
_unresponsive();
});
BridgeConnector.Socket.Emit("register-browserWindow-unresponsive", Id);
}
_unresponsive += value;
}
remove
{
_unresponsive -= value;
if (_unresponsive == null)
BridgeConnector.Socket.Off("browserWindow-unresponsive" + Id);
}
}
private event Action _unresponsive;
///
/// Emitted when the unresponsive web page becomes responsive again.
///
public event Action OnResponsive
{
add
{
if (_responsive == null)
{
BridgeConnector.Socket.On("browserWindow-responsive" + Id, () =>
{
_responsive();
});
BridgeConnector.Socket.Emit("register-browserWindow-responsive", Id);
}
_responsive += value;
}
remove
{
_responsive -= value;
if (_responsive == null)
BridgeConnector.Socket.Off("browserWindow-responsive" + Id);
}
}
private event Action _responsive;
///
/// Emitted when the window loses focus.
///
public event Action OnBlur
{
add
{
if (_blur == null)
{
BridgeConnector.Socket.On("browserWindow-blur" + Id, () =>
{
_blur();
});
BridgeConnector.Socket.Emit("register-browserWindow-blur", Id);
}
_blur += value;
}
remove
{
_blur -= value;
if (_blur == null)
BridgeConnector.Socket.Off("browserWindow-blur" + Id);
}
}
private event Action _blur;
///
/// Emitted when the window gains focus.
///
public event Action OnFocus
{
add
{
if (_focus == null)
{
BridgeConnector.Socket.On("browserWindow-focus" + Id, () =>
{
_focus();
});
BridgeConnector.Socket.Emit("register-browserWindow-focus", Id);
}
_focus += value;
}
remove
{
_focus -= value;
if (_focus == null)
BridgeConnector.Socket.Off("browserWindow-focus" + Id);
}
}
private event Action _focus;
///
/// Emitted when the window is shown.
///
public event Action OnShow
{
add
{
if (_show == null)
{
BridgeConnector.Socket.On("browserWindow-show" + Id, () =>
{
_show();
});
BridgeConnector.Socket.Emit("register-browserWindow-show", Id);
}
_show += value;
}
remove
{
_show -= value;
if (_show == null)
BridgeConnector.Socket.Off("browserWindow-show" + Id);
}
}
private event Action _show;
///
/// Emitted when the window is hidden.
///
public event Action OnHide
{
add
{
if (_hide == null)
{
BridgeConnector.Socket.On("browserWindow-hide" + Id, () =>
{
_hide();
});
BridgeConnector.Socket.Emit("register-browserWindow-hide", Id);
}
_hide += value;
}
remove
{
_hide -= value;
if (_hide == null)
BridgeConnector.Socket.Off("browserWindow-hide" + Id);
}
}
private event Action _hide;
///
/// Emitted when window is maximized.
///
public event Action OnMaximize
{
add
{
if (_maximize == null)
{
BridgeConnector.Socket.On("browserWindow-maximize" + Id, () =>
{
_maximize();
});
BridgeConnector.Socket.Emit("register-browserWindow-maximize", Id);
}
_maximize += value;
}
remove
{
_maximize -= value;
if (_maximize == null)
BridgeConnector.Socket.Off("browserWindow-maximize" + Id);
}
}
private event Action _maximize;
///
/// Emitted when the window exits from a maximized state.
///
public event Action OnUnmaximize
{
add
{
if (_unmaximize == null)
{
BridgeConnector.Socket.On("browserWindow-unmaximize" + Id, () =>
{
_unmaximize();
});
BridgeConnector.Socket.Emit("register-browserWindow-unmaximize", Id);
}
_unmaximize += value;
}
remove
{
_unmaximize -= value;
if (_unmaximize == null)
BridgeConnector.Socket.Off("browserWindow-unmaximize" + Id);
}
}
private event Action _unmaximize;
///
/// Emitted when the window is minimized.
///
public event Action OnMinimize
{
add
{
if (_minimize == null)
{
BridgeConnector.Socket.On("browserWindow-minimize" + Id, () =>
{
_minimize();
});
BridgeConnector.Socket.Emit("register-browserWindow-minimize", Id);
}
_minimize += value;
}
remove
{
_minimize -= value;
if (_minimize == null)
BridgeConnector.Socket.Off("browserWindow-minimize" + Id);
}
}
private event Action _minimize;
///
/// Emitted when the window is restored from a minimized state.
///
public event Action OnRestore
{
add
{
if (_restore == null)
{
BridgeConnector.Socket.On("browserWindow-restore" + Id, () =>
{
_restore();
});
BridgeConnector.Socket.Emit("register-browserWindow-restore", Id);
}
_restore += value;
}
remove
{
_restore -= value;
if (_restore == null)
BridgeConnector.Socket.Off("browserWindow-restore" + Id);
}
}
private event Action _restore;
///
/// Emitted when the window is being resized.
///
public event Action OnResize
{
add
{
if (_resize == null)
{
BridgeConnector.Socket.On("browserWindow-resize" + Id, () =>
{
_resize();
});
BridgeConnector.Socket.Emit("register-browserWindow-resize", Id);
}
_resize += value;
}
remove
{
_resize -= value;
if (_resize == null)
BridgeConnector.Socket.Off("browserWindow-resize" + Id);
}
}
private event Action _resize;
///
/// Emitted when the window is being moved to a new position.
///
/// Note: On macOS this event is just an alias of moved.
///
public event Action OnMove
{
add
{
if (_move == null)
{
BridgeConnector.Socket.On("browserWindow-move" + Id, () =>
{
_move();
});
BridgeConnector.Socket.Emit("register-browserWindow-move", Id);
}
_move += value;
}
remove
{
_move -= value;
if (_move == null)
BridgeConnector.Socket.Off("browserWindow-move" + Id);
}
}
private event Action _move;
///
/// macOS: Emitted once when the window is moved to a new position.
///
public event Action OnMoved
{
add
{
if (_moved == null)
{
BridgeConnector.Socket.On("browserWindow-moved" + Id, () =>
{
_moved();
});
BridgeConnector.Socket.Emit("register-browserWindow-moved", Id);
}
_moved += value;
}
remove
{
_moved -= value;
if (_moved == null)
BridgeConnector.Socket.Off("browserWindow-moved" + Id);
}
}
private event Action _moved;
///
/// Emitted when the window enters a full-screen state.
///
public event Action OnEnterFullScreen
{
add
{
if (_enterFullScreen == null)
{
BridgeConnector.Socket.On("browserWindow-enter-full-screen" + Id, () =>
{
_enterFullScreen();
});
BridgeConnector.Socket.Emit("register-browserWindow-enter-full-screen", Id);
}
_enterFullScreen += value;
}
remove
{
_enterFullScreen -= value;
if (_enterFullScreen == null)
BridgeConnector.Socket.Off("browserWindow-enter-full-screen" + Id);
}
}
private event Action _enterFullScreen;
///
/// Emitted when the window leaves a full-screen state.
///
public event Action OnLeaveFullScreen
{
add
{
if (_leaveFullScreen == null)
{
BridgeConnector.Socket.On("browserWindow-leave-full-screen" + Id, () =>
{
_leaveFullScreen();
});
BridgeConnector.Socket.Emit("register-browserWindow-leave-full-screen", Id);
}
_leaveFullScreen += value;
}
remove
{
_leaveFullScreen -= value;
if (_leaveFullScreen == null)
BridgeConnector.Socket.Off("browserWindow-leave-full-screen" + Id);
}
}
private event Action _leaveFullScreen;
///
/// Emitted when the window enters a full-screen state triggered by HTML API.
///
public event Action OnEnterHtmlFullScreen
{
add
{
if (_enterHtmlFullScreen == null)
{
BridgeConnector.Socket.On("browserWindow-enter-html-full-screen" + Id, () =>
{
_enterHtmlFullScreen();
});
BridgeConnector.Socket.Emit("register-browserWindow-enter-html-full-screen", Id);
}
_enterHtmlFullScreen += value;
}
remove
{
_enterHtmlFullScreen -= value;
if (_enterHtmlFullScreen == null)
BridgeConnector.Socket.Off("browserWindow-enter-html-full-screen" + Id);
}
}
private event Action _enterHtmlFullScreen;
///
/// Emitted when the window leaves a full-screen state triggered by HTML API.
///
public event Action OnLeaveHtmlFullScreen
{
add
{
if (_leaveHtmlFullScreen == null)
{
BridgeConnector.Socket.On("browserWindow-leave-html-full-screen" + Id, () =>
{
_leaveHtmlFullScreen();
});
BridgeConnector.Socket.Emit("register-browserWindow-leave-html-full-screen", Id);
}
_leaveHtmlFullScreen += value;
}
remove
{
_leaveHtmlFullScreen -= value;
if (_leaveHtmlFullScreen == null)
BridgeConnector.Socket.Off("browserWindow-leave-html-full-screen" + Id);
}
}
private event Action _leaveHtmlFullScreen;
///
/// Emitted when an App Command is invoked. These are typically related to
/// keyboard media keys or browser commands, as well as the “Back” button
/// built into some mice on Windows.
///
/// Commands are lowercased, underscores are replaced with hyphens,
/// and the APPCOMMAND_ prefix is stripped off.e.g.APPCOMMAND_BROWSER_BACKWARD
/// is emitted as browser-backward.
///
public event Action OnAppCommand
{
add
{
if (_appCommand == null)
{
BridgeConnector.Socket.On("browserWindow-app-command" + Id, (command) =>
{
_appCommand(command.ToString());
});
BridgeConnector.Socket.Emit("register-browserWindow-app-command", Id);
}
_appCommand += value;
}
remove
{
_appCommand -= value;
if (_appCommand == null)
BridgeConnector.Socket.Off("browserWindow-app-command" + Id);
}
}
private event Action _appCommand;
///
/// Emitted on 3-finger swipe. Possible directions are up, right, down, left.
///
public event Action OnSwipe
{
add
{
if (_swipe == null)
{
BridgeConnector.Socket.On("browserWindow-swipe" + Id, (direction) =>
{
_swipe(direction.ToString());
});
BridgeConnector.Socket.Emit("register-browserWindow-swipe", Id);
}
_swipe += value;
}
remove
{
_swipe -= value;
if (_swipe == null)
BridgeConnector.Socket.Off("browserWindow-swipe" + Id);
}
}
private event Action _swipe;
///
/// Emitted when the window opens a sheet.
///
public event Action OnSheetBegin
{
add
{
if (_sheetBegin == null)
{
BridgeConnector.Socket.On("browserWindow-sheet-begin" + Id, () =>
{
_sheetBegin();
});
BridgeConnector.Socket.Emit("register-browserWindow-sheet-begin", Id);
}
_sheetBegin += value;
}
remove
{
_sheetBegin -= value;
if (_sheetBegin == null)
BridgeConnector.Socket.Off("browserWindow-sheet-begin" + Id);
}
}
private event Action _sheetBegin;
///
/// Emitted when the window has closed a sheet.
///
public event Action OnSheetEnd
{
add
{
if (_sheetEnd == null)
{
BridgeConnector.Socket.On("browserWindow-sheet-end" + Id, () =>
{
_sheetEnd();
});
BridgeConnector.Socket.Emit("register-browserWindow-sheet-end", Id);
}
_sheetEnd += value;
}
remove
{
_sheetEnd -= value;
if (_sheetEnd == null)
BridgeConnector.Socket.Off("browserWindow-sheet-end" + Id);
}
}
private event Action _sheetEnd;
///
/// Emitted when the native new tab button is clicked.
///
public event Action OnNewWindowForTab
{
add
{
if (_newWindowForTab == null)
{
BridgeConnector.Socket.On("browserWindow-new-window-for-tab" + Id, () =>
{
_newWindowForTab();
});
BridgeConnector.Socket.Emit("register-browserWindow-new-window-for-tab", Id);
}
_newWindowForTab += value;
}
remove
{
_newWindowForTab -= value;
if (_newWindowForTab == null)
BridgeConnector.Socket.Off("browserWindow-new-window-for-tab" + Id);
}
}
private event Action _newWindowForTab;
internal BrowserWindow(int id) {
Id = id;
WebContents = new WebContents(id);
}
///
/// Force closing the window, the unload and beforeunload event won’t be
/// emitted for the web page, and close event will also not be emitted
/// for this window, but it guarantees the closed event will be emitted.
///
public void Destroy()
{
BridgeConnector.Socket.Emit("browserWindowDestroy", Id);
}
///
/// Try to close the window. This has the same effect as a user manually
/// clicking the close button of the window. The web page may cancel the close though.
///
public void Close()
{
BridgeConnector.Socket.Emit("browserWindowClose", Id);
}
///
/// Focuses on the window.
///
public void Focus()
{
BridgeConnector.Socket.Emit("browserWindowFocus", Id);
}
///
/// Removes focus from the window.
///
public void Blur()
{
BridgeConnector.Socket.Emit("browserWindowBlur", Id);
}
///
/// Whether the window is focused.
///
///
public Task IsFocusedAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isFocused-completed", (isFocused) => {
BridgeConnector.Socket.Off("browserWindow-isFocused-completed");
taskCompletionSource.SetResult((bool)isFocused);
});
BridgeConnector.Socket.Emit("browserWindowIsFocused", Id);
return taskCompletionSource.Task;
}
///
/// Whether the window is destroyed.
///
///
public Task IsDestroyedAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isDestroyed-completed", (isDestroyed) => {
BridgeConnector.Socket.Off("browserWindow-isDestroyed-completed");
taskCompletionSource.SetResult((bool)isDestroyed);
});
BridgeConnector.Socket.Emit("browserWindowIsDestroyed", Id);
return taskCompletionSource.Task;
}
///
/// Shows and gives focus to the window.
///
public void Show()
{
BridgeConnector.Socket.Emit("browserWindowShow", Id);
}
///
/// Shows the window but doesn’t focus on it.
///
public void ShowInactive()
{
BridgeConnector.Socket.Emit("browserWindowShowInactive", Id);
}
///
/// Hides the window.
///
public void Hide()
{
BridgeConnector.Socket.Emit("browserWindowHide", Id);
}
///
/// Whether the window is visible to the user.
///
///
public Task IsVisibleAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isVisible-completed", (isVisible) => {
BridgeConnector.Socket.Off("browserWindow-isVisible-completed");
taskCompletionSource.SetResult((bool)isVisible);
});
BridgeConnector.Socket.Emit("browserWindowIsVisible", Id);
return taskCompletionSource.Task;
}
///
/// Whether current window is a modal window.
///
///
public Task IsModalAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isModal-completed", (isModal) => {
BridgeConnector.Socket.Off("browserWindow-isModal-completed");
taskCompletionSource.SetResult((bool)isModal);
});
BridgeConnector.Socket.Emit("browserWindowIsModal", Id);
return taskCompletionSource.Task;
}
///
/// Maximizes the window. This will also show (but not focus) the window if it isn’t being displayed already.
///
public void Maximize()
{
BridgeConnector.Socket.Emit("browserWindowMaximize", Id);
}
///
/// Unmaximizes the window.
///
public void Unmaximize()
{
BridgeConnector.Socket.Emit("browserWindowUnmaximize", Id);
}
///
/// Whether the window is maximized.
///
///
public Task IsMaximizedAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isMaximized-completed", (isMaximized) => {
BridgeConnector.Socket.Off("browserWindow-isMaximized-completed");
taskCompletionSource.SetResult((bool)isMaximized);
});
BridgeConnector.Socket.Emit("browserWindowIsMaximized", Id);
return taskCompletionSource.Task;
}
///
/// Minimizes the window. On some platforms the minimized window will be shown in the Dock.
///
public void Minimize()
{
BridgeConnector.Socket.Emit("browserWindowMinimize", Id);
}
///
/// Restores the window from minimized state to its previous state.
///
public void Restore()
{
BridgeConnector.Socket.Emit("browserWindowRestore", Id);
}
///
/// Whether the window is minimized.
///
///
public Task IsMinimizedAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isMinimized-completed", (isMinimized) => {
BridgeConnector.Socket.Off("browserWindow-isMinimized-completed");
taskCompletionSource.SetResult((bool)isMinimized);
});
BridgeConnector.Socket.Emit("browserWindowIsMinimized", Id);
return taskCompletionSource.Task;
}
///
/// Sets whether the window should be in fullscreen mode.
///
public void SetFullScreen(bool flag)
{
BridgeConnector.Socket.Emit("browserWindowSetFullScreen", Id, flag);
}
///
/// Whether the window is in fullscreen mode.
///
///
public Task IsFullScreenAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isFullScreen-completed", (isFullScreen) => {
BridgeConnector.Socket.Off("browserWindow-isFullScreen-completed");
taskCompletionSource.SetResult((bool)isFullScreen);
});
BridgeConnector.Socket.Emit("browserWindowIsFullScreen", Id);
return taskCompletionSource.Task;
}
///
/// This will make a window maintain an aspect ratio. The extra size allows a developer to have space,
/// specified in pixels, not included within the aspect ratio calculations. This API already takes into
/// account the difference between a window’s size and its content size.
///
/// Consider a normal window with an HD video player and associated controls.Perhaps there are 15 pixels
/// of controls on the left edge, 25 pixels of controls on the right edge and 50 pixels of controls below
/// the player. In order to maintain a 16:9 aspect ratio (standard aspect ratio for HD @1920x1080) within
/// the player itself we would call this function with arguments of 16/9 and[40, 50]. The second argument
/// doesn’t care where the extra width and height are within the content view–only that they exist. Just
/// sum any extra width and height areas you have within the overall content view.
///
/// The aspect ratio to maintain for some portion of the content view.
/// The extra size not to be included while maintaining the aspect ratio.
public void SetAspectRatio(int aspectRatio, Size extraSize)
{
BridgeConnector.Socket.Emit("browserWindowSetAspectRatio", Id, aspectRatio, JObject.FromObject(extraSize, _jsonSerializer));
}
///
/// Uses Quick Look to preview a file at a given path.
///
/// The absolute path to the file to preview with QuickLook. This is important as
/// Quick Look uses the file name and file extension on the path to determine the content type of the
/// file to open.
public void PreviewFile(string path)
{
BridgeConnector.Socket.Emit("browserWindowPreviewFile", Id, path);
}
///
/// Uses Quick Look to preview a file at a given path.
///
/// The absolute path to the file to preview with QuickLook. This is important as
/// Quick Look uses the file name and file extension on the path to determine the content type of the
/// file to open.
/// The name of the file to display on the Quick Look modal view. This is
/// purely visual and does not affect the content type of the file. Defaults to path.
public void PreviewFile(string path, string displayname)
{
BridgeConnector.Socket.Emit("browserWindowPreviewFile", Id, path, displayname);
}
///
/// Closes the currently open Quick Look panel.
///
public void CloseFilePreview()
{
BridgeConnector.Socket.Emit("browserWindowCloseFilePreview", Id);
}
///
/// Resizes and moves the window to the supplied bounds
///
///
public void SetBounds(Rectangle bounds)
{
BridgeConnector.Socket.Emit("browserWindowSetBounds", Id, JObject.FromObject(bounds, _jsonSerializer));
}
///
/// Resizes and moves the window to the supplied bounds
///
///
///
public void SetBounds(Rectangle bounds, bool animate)
{
BridgeConnector.Socket.Emit("browserWindowSetBounds", Id, JObject.FromObject(bounds, _jsonSerializer), animate);
}
///
/// Gets the bounds asynchronous.
///
///
public Task GetBoundsAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-getBounds-completed", (getBounds) => {
BridgeConnector.Socket.Off("browserWindow-getBounds-completed");
taskCompletionSource.SetResult(((JObject)getBounds).ToObject());
});
BridgeConnector.Socket.Emit("browserWindowGetBounds", Id);
return taskCompletionSource.Task;
}
///
/// Resizes and moves the window’s client area (e.g. the web page) to the supplied bounds.
///
///
public void SetContentBounds(Rectangle bounds)
{
BridgeConnector.Socket.Emit("browserWindowSetContentBounds", Id, JObject.FromObject(bounds, _jsonSerializer));
}
///
/// Resizes and moves the window’s client area (e.g. the web page) to the supplied bounds.
///
///
///
public void SetContentBounds(Rectangle bounds, bool animate)
{
BridgeConnector.Socket.Emit("browserWindowSetContentBounds", Id, JObject.FromObject(bounds, _jsonSerializer), animate);
}
///
/// Gets the content bounds asynchronous.
///
///
public Task GetContentBoundsAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-getContentBounds-completed", (getContentBounds) => {
BridgeConnector.Socket.Off("browserWindow-getContentBounds-completed");
taskCompletionSource.SetResult(((JObject)getContentBounds).ToObject());
});
BridgeConnector.Socket.Emit("browserWindowGetContentBounds", Id);
return taskCompletionSource.Task;
}
///
/// Resizes the window to width and height.
///
///
///
public void SetSize(int width, int height)
{
BridgeConnector.Socket.Emit("browserWindowSetSize", Id, width, height);
}
///
/// Resizes the window to width and height.
///
///
///
///
public void SetSize(int width, int height, bool animate)
{
BridgeConnector.Socket.Emit("browserWindowSetSize", Id, width, height, animate);
}
///
/// Contains the window’s width and height.
///
///
public Task GetSizeAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-getSize-completed", (size) => {
BridgeConnector.Socket.Off("browserWindow-getSize-completed");
taskCompletionSource.SetResult(((JArray)size).ToObject());
});
BridgeConnector.Socket.Emit("browserWindowGetSize", Id);
return taskCompletionSource.Task;
}
///
/// Resizes the window’s client area (e.g. the web page) to width and height.
///
///
///
public void SetContentSize(int width, int height)
{
BridgeConnector.Socket.Emit("browserWindowSetContentSize", Id, width, height);
}
///
/// Resizes the window’s client area (e.g. the web page) to width and height.
///
///
///
///
public void SetContentSize(int width, int height, bool animate)
{
BridgeConnector.Socket.Emit("browserWindowSetContentSize", Id, width, height, animate);
}
///
/// Contains the window’s client area’s width and height.
///
///
public Task GetContentSizeAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-getContentSize-completed", (size) => {
BridgeConnector.Socket.Off("browserWindow-getContentSize-completed");
taskCompletionSource.SetResult(((JArray)size).ToObject());
});
BridgeConnector.Socket.Emit("browserWindowGetContentSize", Id);
return taskCompletionSource.Task;
}
///
/// Sets the minimum size of window to width and height.
///
///
///
public void SetMinimumSize(int width, int height)
{
BridgeConnector.Socket.Emit("browserWindowSetMinimumSize", Id, width, height);
}
///
/// Contains the window’s minimum width and height.
///
///
public Task GetMinimumSizeAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-getMinimumSize-completed", (size) => {
BridgeConnector.Socket.Off("browserWindow-getMinimumSize-completed");
taskCompletionSource.SetResult(((JArray)size).ToObject());
});
BridgeConnector.Socket.Emit("browserWindowGetMinimumSize", Id);
return taskCompletionSource.Task;
}
///
/// Sets the maximum size of window to width and height.
///
///
///
public void SetMaximumSize(int width, int height)
{
BridgeConnector.Socket.Emit("browserWindowSetMaximumSize", Id, width, height);
}
///
/// Contains the window’s maximum width and height.
///
///
public Task GetMaximumSizeAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-getMaximumSize-completed", (size) => {
BridgeConnector.Socket.Off("browserWindow-getMaximumSize-completed");
taskCompletionSource.SetResult(((JArray)size).ToObject());
});
BridgeConnector.Socket.Emit("browserWindowGetMaximumSize", Id);
return taskCompletionSource.Task;
}
///
/// Sets whether the window can be manually resized by user.
///
///
public void SetResizable(bool resizable)
{
BridgeConnector.Socket.Emit("browserWindowSetResizable", Id, resizable);
}
///
/// Whether the window can be manually resized by user.
///
///
public Task IsResizableAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isResizable-completed", (resizable) => {
BridgeConnector.Socket.Off("browserWindow-isResizable-completed");
taskCompletionSource.SetResult((bool)resizable);
});
BridgeConnector.Socket.Emit("browserWindowIsResizable", Id);
return taskCompletionSource.Task;
}
///
/// Sets whether the window can be moved by user. On Linux does nothing.
///
///
public void SetMovable(bool movable)
{
BridgeConnector.Socket.Emit("browserWindowSetMovable", Id, movable);
}
///
/// Whether the window can be moved by user.
///
/// On Linux always returns true.
///
/// On Linux always returns true.
public Task IsMovableAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isMovable-completed", (movable) => {
BridgeConnector.Socket.Off("browserWindow-isMovable-completed");
taskCompletionSource.SetResult((bool)movable);
});
BridgeConnector.Socket.Emit("browserWindowIsMovable", Id);
return taskCompletionSource.Task;
}
///
/// Sets whether the window can be manually minimized by user. On Linux does nothing.
///
///
public void SetMinimizable(bool minimizable)
{
BridgeConnector.Socket.Emit("browserWindowSetMinimizable", Id, minimizable);
}
///
/// Whether the window can be manually minimized by user.
///
/// On Linux always returns true.
///
/// On Linux always returns true.
public Task IsMinimizableAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isMinimizable-completed", (minimizable) => {
BridgeConnector.Socket.Off("browserWindow-isMinimizable-completed");
taskCompletionSource.SetResult((bool)minimizable);
});
BridgeConnector.Socket.Emit("browserWindowIsMinimizable", Id);
return taskCompletionSource.Task;
}
///
/// Sets whether the window can be manually maximized by user. On Linux does nothing.
///
///
public void SetMaximizable(bool maximizable)
{
BridgeConnector.Socket.Emit("browserWindowSetMaximizable", Id, maximizable);
}
///
/// Whether the window can be manually maximized by user.
///
/// On Linux always returns true.
///
/// On Linux always returns true.
public Task IsMaximizableAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isMaximizable-completed", (maximizable) => {
BridgeConnector.Socket.Off("browserWindow-isMaximizable-completed");
taskCompletionSource.SetResult((bool)maximizable);
});
BridgeConnector.Socket.Emit("browserWindowIsMaximizable", Id);
return taskCompletionSource.Task;
}
///
/// Sets whether the maximize/zoom window button toggles fullscreen mode or maximizes the window.
///
///
public void SetFullScreenable(bool fullscreenable)
{
BridgeConnector.Socket.Emit("browserWindowSetFullScreenable", Id, fullscreenable);
}
///
/// Whether the maximize/zoom window button toggles fullscreen mode or maximizes the window.
///
///
public Task IsFullScreenableAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isFullScreenable-completed", (fullscreenable) => {
BridgeConnector.Socket.Off("browserWindow-isFullScreenable-completed");
taskCompletionSource.SetResult((bool)fullscreenable);
});
BridgeConnector.Socket.Emit("browserWindowIsFullScreenable", Id);
return taskCompletionSource.Task;
}
///
/// Sets whether the window can be manually closed by user. On Linux does nothing.
///
///
public void SetClosable(bool closable)
{
BridgeConnector.Socket.Emit("browserWindowSetClosable", Id, closable);
}
///
/// Whether the window can be manually closed by user.
///
/// On Linux always returns true.
///
/// On Linux always returns true.
public Task IsClosableAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isClosable-completed", (closable) => {
BridgeConnector.Socket.Off("browserWindow-isClosable-completed");
taskCompletionSource.SetResult((bool)closable);
});
BridgeConnector.Socket.Emit("browserWindowIsClosable", Id);
return taskCompletionSource.Task;
}
///
/// Sets whether the window should show always on top of other windows.
/// After setting this, the window is still a normal window, not a toolbox
/// window which can not be focused on.
///
///
public void SetAlwaysOnTop(bool flag)
{
BridgeConnector.Socket.Emit("browserWindowSetAlwaysOnTop", Id, flag);
}
///
/// Sets whether the window should show always on top of other windows.
/// After setting this, the window is still a normal window, not a toolbox
/// window which can not be focused on.
///
///
/// Values include normal, floating, torn-off-menu, modal-panel, main-menu,
/// status, pop-up-menu and screen-saver. The default is floating.
/// See the macOS docs
public void SetAlwaysOnTop(bool flag, OnTopLevel level)
{
BridgeConnector.Socket.Emit("browserWindowSetAlwaysOnTop", Id, flag, level.GetDescription());
}
///
/// Sets whether the window should show always on top of other windows.
/// After setting this, the window is still a normal window, not a toolbox
/// window which can not be focused on.
///
///
/// Values include normal, floating, torn-off-menu, modal-panel, main-menu,
/// status, pop-up-menu and screen-saver. The default is floating.
/// See the macOS docs
/// The number of layers higher to set this window relative to the given level.
/// The default is 0. Note that Apple discourages setting levels higher than 1 above screen-saver.
public void SetAlwaysOnTop(bool flag, OnTopLevel level, int relativeLevel)
{
BridgeConnector.Socket.Emit("browserWindowSetAlwaysOnTop", Id, flag, level.GetDescription(), relativeLevel);
}
///
/// Whether the window is always on top of other windows.
///
///
public Task IsAlwaysOnTopAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isAlwaysOnTop-completed", (isAlwaysOnTop) => {
BridgeConnector.Socket.Off("browserWindow-isAlwaysOnTop-completed");
taskCompletionSource.SetResult((bool)isAlwaysOnTop);
});
BridgeConnector.Socket.Emit("browserWindowIsAlwaysOnTop", Id);
return taskCompletionSource.Task;
}
///
/// Moves window to the center of the screen.
///
public void Center()
{
BridgeConnector.Socket.Emit("browserWindowCenter", Id);
}
///
/// Moves window to x and y.
///
///
///
public void SetPosition(int x, int y)
{
// Workaround Windows 10 / Electron Bug
// https://github.com/electron/electron/issues/4045
if (isWindows10())
{
x = x - 7;
}
BridgeConnector.Socket.Emit("browserWindowSetPosition", Id, x, y);
}
///
/// Moves window to x and y.
///
///
///
///
public void SetPosition(int x, int y, bool animate)
{
// Workaround Windows 10 / Electron Bug
// https://github.com/electron/electron/issues/4045
if (isWindows10())
{
x = x - 7;
}
BridgeConnector.Socket.Emit("browserWindowSetPosition", Id, x, y, animate);
}
private bool isWindows10()
{
return RuntimeInformation.OSDescription.Contains("Windows 10");
}
///
/// Contains the window’s current position.
///
///
public Task GetPositionAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-getPosition-completed", (position) => {
BridgeConnector.Socket.Off("browserWindow-getPosition-completed");
taskCompletionSource.SetResult(((JArray)position).ToObject());
});
BridgeConnector.Socket.Emit("browserWindowGetPosition", Id);
return taskCompletionSource.Task;
}
///
/// Changes the title of native window to title.
///
///
public void SetTitle(string title)
{
BridgeConnector.Socket.Emit("browserWindowSetTitle", Id, title);
}
///
/// The title of the native window.
///
/// Note: The title of web page can be different from the title of the native window.
///
///
public Task GetTitleAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-getTitle-completed", (title) => {
BridgeConnector.Socket.Off("browserWindow-getTitle-completed");
taskCompletionSource.SetResult(title.ToString());
});
BridgeConnector.Socket.Emit("browserWindowGetTitle", Id);
return taskCompletionSource.Task;
}
///
/// Changes the attachment point for sheets on macOS.
/// By default, sheets are attached just below the window frame,
/// but you may want to display them beneath a HTML-rendered toolbar.
///
///
public void SetSheetOffset(float offsetY)
{
BridgeConnector.Socket.Emit("browserWindowSetSheetOffset", Id, offsetY);
}
///
/// Changes the attachment point for sheets on macOS.
/// By default, sheets are attached just below the window frame,
/// but you may want to display them beneath a HTML-rendered toolbar.
///
///
///
public void SetSheetOffset(float offsetY, float offsetX)
{
BridgeConnector.Socket.Emit("browserWindowSetSheetOffset", Id, offsetY, offsetX);
}
///
/// Starts or stops flashing the window to attract user’s attention.
///
///
public void FlashFrame(bool flag)
{
BridgeConnector.Socket.Emit("browserWindowFlashFrame", Id, flag);
}
///
/// Makes the window not show in the taskbar.
///
///
public void SetSkipTaskbar(bool skip)
{
BridgeConnector.Socket.Emit("browserWindowSetSkipTaskbar", Id, skip);
}
///
/// Enters or leaves the kiosk mode.
///
///
public void SetKiosk(bool flag)
{
BridgeConnector.Socket.Emit("browserWindowSetKiosk", Id, flag);
}
///
/// Whether the window is in kiosk mode.
///
///
public Task IsKioskAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isKiosk-completed", (isKiosk) => {
BridgeConnector.Socket.Off("browserWindow-isKiosk-completed");
taskCompletionSource.SetResult((bool)isKiosk);
});
BridgeConnector.Socket.Emit("browserWindowIsKiosk", Id);
return taskCompletionSource.Task;
}
///
/// Returns the native type of the handle is HWND on Windows, NSView* on macOS, and Window (unsigned long) on Linux.
///
/// string of the native handle obtained, HWND on Windows, NSView* on macOS, and Window (unsigned long) on Linux.
public Task GetNativeWindowHandle()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-getNativeWindowHandle-completed", (nativeWindowHandle) =>
{
BridgeConnector.Socket.Off("browserWindow-getNativeWindowHandle-completed");
taskCompletionSource.SetResult(nativeWindowHandle.ToString());
});
BridgeConnector.Socket.Emit("browserWindowGetNativeWindowHandle", Id);
return taskCompletionSource.Task;
}
///
/// Sets the pathname of the file the window represents,
/// and the icon of the file will show in window’s title bar.
///
///
public void SetRepresentedFilename(string filename)
{
BridgeConnector.Socket.Emit("browserWindowSetRepresentedFilename", Id, filename);
}
///
/// The pathname of the file the window represents.
///
///
public Task GetRepresentedFilenameAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-getRepresentedFilename-completed", (pathname) => {
BridgeConnector.Socket.Off("browserWindow-getRepresentedFilename-completed");
taskCompletionSource.SetResult(pathname.ToString());
});
BridgeConnector.Socket.Emit("browserWindowGetRepresentedFilename", Id);
return taskCompletionSource.Task;
}
///
/// Specifies whether the window’s document has been edited,
/// and the icon in title bar will become gray when set to true.
///
///
public void SetDocumentEdited(bool edited)
{
BridgeConnector.Socket.Emit("browserWindowSetDocumentEdited", Id, edited);
}
///
/// Whether the window’s document has been edited.
///
///
public Task IsDocumentEditedAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserWindow-isDocumentEdited-completed", (edited) => {
BridgeConnector.Socket.Off("browserWindow-isDocumentEdited-completed");
taskCompletionSource.SetResult((bool)edited);
});
BridgeConnector.Socket.Emit("browserWindowIsDocumentEdited", Id);
return taskCompletionSource.Task;
}
///
/// Focuses the on web view.
///
public void FocusOnWebView()
{
BridgeConnector.Socket.Emit("browserWindowFocusOnWebView", Id);
}
///
/// Blurs the web view.
///
public void BlurWebView()
{
BridgeConnector.Socket.Emit("browserWindowBlurWebView", Id);
}
///
/// The url can be a remote address (e.g. http://) or
/// a path to a local HTML file using the file:// protocol.
///
///
public void LoadURL(string url)
{
BridgeConnector.Socket.Emit("browserWindowLoadURL", Id, url);
}
///
/// The url can be a remote address (e.g. http://) or
/// a path to a local HTML file using the file:// protocol.
///
///
///
public void LoadURL(string url, LoadURLOptions options)
{
BridgeConnector.Socket.Emit("browserWindowLoadURL", Id, url, JObject.FromObject(options, _jsonSerializer));
}
///
/// Same as webContents.reload.
///
public void Reload()
{
BridgeConnector.Socket.Emit("browserWindowReload", Id);
}
///
/// Gets the menu items.
///
///
/// The menu items.
///
public IReadOnlyCollection