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;
using ElectronNET.Common;
// ReSharper disable InconsistentNaming
namespace ElectronNET.API;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using ElectronNET.Common;
///
/// Create and control browser windows.
///
public class BrowserWindow : ApiBase
{
protected override string SocketEventCompleteSuffix => "-completed";
///
/// Gets the identifier.
///
///
/// The identifier.
///
public override int Id { get; protected 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 => ApiEventManager.AddEvent("browserWindow-ready-to-show", Id, _readyToShow, value);
remove => ApiEventManager.RemoveEvent("browserWindow-ready-to-show", Id, _readyToShow, value);
}
private event Action _readyToShow;
///
/// Emitted when the document changed its title.
///
public event Action OnPageTitleUpdated
{
add => ApiEventManager.AddEvent("browserWindow-page-title-updated", Id, _pageTitleUpdated, value, (args) => args.ToString());
remove => ApiEventManager.RemoveEvent("browserWindow-page-title-updated", Id, _pageTitleUpdated, value);
}
private event Action _pageTitleUpdated;
///
/// Emitted when the window is going to be closed.
///
public event Action OnClose
{
add => ApiEventManager.AddEvent("browserWindow-close", Id, _close, value);
remove => ApiEventManager.RemoveEvent("browserWindow-close", Id, _close, value);
}
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 => ApiEventManager.AddEvent("browserWindow-closed", Id, _closed, value);
remove => ApiEventManager.RemoveEvent("browserWindow-closed", Id, _closed, value);
}
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 => ApiEventManager.AddEvent("browserWindow-session-end", Id, _sessionEnd, value);
remove => ApiEventManager.RemoveEvent("browserWindow-session-end", Id, _sessionEnd, value);
}
private event Action _sessionEnd;
///
/// Emitted when the web page becomes unresponsive.
///
public event Action OnUnresponsive
{
add => ApiEventManager.AddEvent("browserWindow-unresponsive", Id, _unresponsive, value);
remove => ApiEventManager.RemoveEvent("browserWindow-unresponsive", Id, _unresponsive, value);
}
private event Action _unresponsive;
///
/// Emitted when the unresponsive web page becomes responsive again.
///
public event Action OnResponsive
{
add => ApiEventManager.AddEvent("browserWindow-responsive", Id, _responsive, value);
remove => ApiEventManager.RemoveEvent("browserWindow-responsive", Id, _responsive, value);
}
private event Action _responsive;
///
/// Emitted when the window loses focus.
///
public event Action OnBlur
{
add => ApiEventManager.AddEvent("browserWindow-blur", Id, _blur, value);
remove => ApiEventManager.RemoveEvent("browserWindow-blur", Id, _blur, value);
}
private event Action _blur;
///
/// Emitted when the window gains focus.
///
public event Action OnFocus
{
add => ApiEventManager.AddEvent("browserWindow-focus", Id, _focus, value);
remove => ApiEventManager.RemoveEvent("browserWindow-focus", Id, _focus, value);
}
private event Action _focus;
///
/// Emitted when the window is shown.
///
public event Action OnShow
{
add => ApiEventManager.AddEvent("browserWindow-show", Id, _show, value);
remove => ApiEventManager.RemoveEvent("browserWindow-show", Id, _show, value);
}
private event Action _show;
///
/// Emitted when the window is hidden.
///
public event Action OnHide
{
add => ApiEventManager.AddEvent("browserWindow-hide", Id, _hide, value);
remove => ApiEventManager.RemoveEvent("browserWindow-hide", Id, _hide, value);
}
private event Action _hide;
///
/// Emitted when window is maximized.
///
public event Action OnMaximize
{
add => ApiEventManager.AddEvent("browserWindow-maximize", Id, _maximize, value);
remove => ApiEventManager.RemoveEvent("browserWindow-maximize", Id, _maximize, value);
}
private event Action _maximize;
///
/// Emitted when the window exits from a maximized state.
///
public event Action OnUnmaximize
{
add => ApiEventManager.AddEvent("browserWindow-unmaximize", Id, _unmaximize, value);
remove => ApiEventManager.RemoveEvent("browserWindow-unmaximize", Id, _unmaximize, value);
}
private event Action _unmaximize;
///
/// Emitted when the window is minimized.
///
public event Action OnMinimize
{
add => ApiEventManager.AddEvent("browserWindow-minimize", Id, _minimize, value);
remove => ApiEventManager.RemoveEvent("browserWindow-minimize", Id, _minimize, value);
}
private event Action _minimize;
///
/// Emitted when the window is restored from a minimized state.
///
public event Action OnRestore
{
add => ApiEventManager.AddEvent("browserWindow-restore", Id, _restore, value);
remove => ApiEventManager.RemoveEvent("browserWindow-restore", Id, _restore, value);
}
private event Action _restore;
///
/// Emitted when the window is being resized.
///
public event Action OnResize
{
add => ApiEventManager.AddEvent("browserWindow-resize", Id, _resize, value);
remove => ApiEventManager.RemoveEvent("browserWindow-resize", Id, _resize, value);
}
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 => ApiEventManager.AddEvent("browserWindow-move", Id, _move, value);
remove => ApiEventManager.RemoveEvent("browserWindow-move", Id, _move, value);
}
private event Action _move;
///
/// macOS: Emitted once when the window is moved to a new position.
///
public event Action OnMoved
{
add => ApiEventManager.AddEvent("browserWindow-moved", Id, _moved, value);
remove => ApiEventManager.RemoveEvent("browserWindow-moved", Id, _moved, value);
}
private event Action _moved;
///
/// Emitted when the window enters a full-screen state.
///
public event Action OnEnterFullScreen
{
add => ApiEventManager.AddEvent("browserWindow-enter-full-screen", Id, _enterFullScreen, value);
remove => ApiEventManager.RemoveEvent("browserWindow-enter-full-screen", Id, _enterFullScreen, value);
}
private event Action _enterFullScreen;
///
/// Emitted when the window leaves a full-screen state.
///
public event Action OnLeaveFullScreen
{
add => ApiEventManager.AddEvent("browserWindow-leave-full-screen", Id, _leaveFullScreen, value);
remove => ApiEventManager.RemoveEvent("browserWindow-leave-full-screen", Id, _leaveFullScreen, value);
}
private event Action _leaveFullScreen;
///
/// Emitted when the window enters a full-screen state triggered by HTML API.
///
public event Action OnEnterHtmlFullScreen
{
add => ApiEventManager.AddEvent("browserWindow-enter-html-full-screen", Id, _enterHtmlFullScreen, value);
remove => ApiEventManager.RemoveEvent("browserWindow-enter-html-full-screen", Id, _enterHtmlFullScreen, value);
}
private event Action _enterHtmlFullScreen;
///
/// Emitted when the window leaves a full-screen state triggered by HTML API.
///
public event Action OnLeaveHtmlFullScreen
{
add => ApiEventManager.AddEvent("browserWindow-leave-html-full-screen", Id, _leaveHtmlFullScreen, value);
remove => ApiEventManager.RemoveEvent("browserWindow-leave-html-full-screen", Id, _leaveHtmlFullScreen, value);
}
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 => ApiEventManager.AddEvent("browserWindow-app-command", Id, _appCommand, value, (args) => args.ToString());
remove => ApiEventManager.RemoveEvent("browserWindow-app-command", Id, _appCommand, value);
}
private event Action _appCommand;
///
/// Emitted on 3-finger swipe. Possible directions are up, right, down, left.
///
public event Action OnSwipe
{
add => ApiEventManager.AddEvent("browserWindow-swipe", Id, _swipe, value, (args) => args.ToString());
remove => ApiEventManager.RemoveEvent("browserWindow-swipe", Id, _swipe, value);
}
private event Action _swipe;
///
/// Emitted when the window opens a sheet.
///
public event Action OnSheetBegin
{
add => ApiEventManager.AddEvent("browserWindow-sheet-begin", Id, _sheetBegin, value);
remove => ApiEventManager.RemoveEvent("browserWindow-sheet-begin", Id, _sheetBegin, value);
}
private event Action _sheetBegin;
///
/// Emitted when the window has closed a sheet.
///
public event Action OnSheetEnd
{
add => ApiEventManager.AddEvent("browserWindow-sheet-end", Id, _sheetEnd, value);
remove => ApiEventManager.RemoveEvent("browserWindow-sheet-end", Id, _sheetEnd, value);
}
private event Action _sheetEnd;
///
/// Emitted when the native new tab button is clicked.
///
public event Action OnNewWindowForTab
{
add => ApiEventManager.AddEvent("browserWindow-new-window-for-tab", Id, _newWindowForTab, value);
remove => ApiEventManager.RemoveEvent("browserWindow-new-window-for-tab", Id, _newWindowForTab, value);
}
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() => this.CallMethod0();
///
/// 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() => this.CallMethod0();
///
/// Focuses on the window.
///
public void Focus() => this.CallMethod0();
///
/// Removes focus from the window.
///
public void Blur() => this.CallMethod0();
///
/// Whether the window is focused.
///
///
public Task IsFocusedAsync() => this.GetPropertyAsync();
///
/// Whether the window is destroyed.
///
///
public Task IsDestroyedAsync() => this.GetPropertyAsync();
///
/// Shows and gives focus to the window.
///
public void Show() => this.CallMethod0();
///
/// Shows the window but doesn’t focus on it.
///
public void ShowInactive() => this.CallMethod0();
///
/// Hides the window.
///
public void Hide() => this.CallMethod0();
///
/// Whether the window is visible to the user.
///
///
public Task IsVisibleAsync() => this.GetPropertyAsync();
///
/// Whether current window is a modal window.
///
///
public Task IsModalAsync() => this.GetPropertyAsync();
///
/// Maximizes the window. This will also show (but not focus) the window if it isn’t being displayed already.
///
public void Maximize() => this.CallMethod0();
///
/// Unmaximizes the window.
///
public void Unmaximize() => this.CallMethod0();
///
/// Whether the window is maximized.
///
///
public Task IsMaximizedAsync() => this.GetPropertyAsync();
///
/// Minimizes the window. On some platforms the minimized window will be shown in the Dock.
///
public void Minimize() => this.CallMethod0();
///
/// Restores the window from minimized state to its previous state.
///
public void Restore() => this.CallMethod0();
///
/// Whether the window is minimized.
///
///
public Task IsMinimizedAsync() => this.GetPropertyAsync();
///
/// Sets whether the window should be in fullscreen mode.
///
///
public void SetFullScreen(bool flag) => this.CallMethod1(flag);
///
/// Whether the window is in fullscreen mode.
///
///
public Task IsFullScreenAsync() => this.GetPropertyAsync();
///
/// 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(double aspectRatio, Size extraSize) =>
this.CallMethod2(aspectRatio, JObject.FromObject(extraSize, _jsonSerializer));
///
/// 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) =>
this.CallMethod2(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) => this.CallMethod1(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) => this.CallMethod2(path, displayname);
///
/// Closes the currently open Quick Look panel.
///
public void CloseFilePreview() => this.CallMethod0();
///
/// Resizes and moves the window to the supplied bounds
///
///
public void SetBounds(Rectangle bounds) => this.CallMethod1(JObject.FromObject(bounds, _jsonSerializer));
///
/// Resizes and moves the window to the supplied bounds
///
///
///
public void SetBounds(Rectangle bounds, bool animate) => this.CallMethod2(JObject.FromObject(bounds, _jsonSerializer), animate);
///
/// Gets the bounds asynchronous.
///
///
public Task GetBoundsAsync() => this.GetPropertyAsync();
///
/// Resizes and moves the window’s client area (e.g. the web page) to the supplied bounds.
///
///
public void SetContentBounds(Rectangle bounds) => this.CallMethod1(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) => this.CallMethod2(JObject.FromObject(bounds, _jsonSerializer), animate);
///
/// Gets the content bounds asynchronous.
///
///
public Task GetContentBoundsAsync() => this.GetPropertyAsync();
///
/// Resizes the window to width and height.
///
///
///
public void SetSize(int width, int height) => this.CallMethod2(width, height);
///
/// Resizes the window to width and height.
///
///
///
///
public void SetSize(int width, int height, bool animate) => this.CallMethod3(width, height, animate);
///
/// Contains the window’s width and height.
///
///
public Task GetSizeAsync() => this.GetPropertyAsync();
///
/// Resizes the window’s client area (e.g. the web page) to width and height.
///
///
///
public void SetContentSize(int width, int height) => this.CallMethod2(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) => this.CallMethod3(width, height, animate);
///
/// Contains the window’s client area’s width and height.
///
///
public Task GetContentSizeAsync() => this.GetPropertyAsync();
///
/// Sets the minimum size of window to width and height.
///
///
///
public void SetMinimumSize(int width, int height) => this.CallMethod2(width, height);
///
/// Contains the window’s minimum width and height.
///
///
public Task GetMinimumSizeAsync() => this.GetPropertyAsync();
///
/// Sets the maximum size of window to width and height.
///
///
///
public void SetMaximumSize(int width, int height) => this.CallMethod2(width, height);
///
/// Contains the window’s maximum width and height.
///
///
public Task GetMaximumSizeAsync() => this.GetPropertyAsync();
///
/// Sets whether the window can be manually resized by user.
///
///
public void SetResizable(bool resizable) => this.CallMethod1(resizable);
///
/// Whether the window can be manually resized by user.
///
///
public Task IsResizableAsync() => this.GetPropertyAsync();
///
/// Sets whether the window can be moved by user. On Linux does nothing.
///
///
public void SetMovable(bool movable) => this.CallMethod1(movable);
///
/// Whether the window can be moved by user.
///
/// On Linux always returns true.
///
/// On Linux always returns true.
public Task IsMovableAsync() => this.GetPropertyAsync();
///
/// Sets whether the window can be manually minimized by user. On Linux does nothing.
///
///
public void SetMinimizable(bool minimizable) => this.CallMethod1(minimizable);
///
/// Whether the window can be manually minimized by user.
///
/// On Linux always returns true.
///
/// On Linux always returns true.
public Task IsMinimizableAsync() => this.GetPropertyAsync();
///
/// Sets whether the window can be manually maximized by user. On Linux does nothing.
///
///
public void SetMaximizable(bool maximizable) => this.CallMethod1(maximizable);
///
/// Whether the window can be manually maximized by user.
///
/// On Linux always returns true.
///
/// On Linux always returns true.
public Task IsMaximizableAsync() => this.GetPropertyAsync();
///
/// Sets whether the maximize/zoom window button toggles fullscreen mode or maximizes the window.
///
///
public void SetFullScreenable(bool fullscreenable) => this.CallMethod1(fullscreenable);
///
/// Whether the maximize/zoom window button toggles fullscreen mode or maximizes the window.
///
///
public Task IsFullScreenableAsync() => this.GetPropertyAsync();
///
/// Sets whether the window can be manually closed by user. On Linux does nothing.
///
///
public void SetClosable(bool closable) => this.CallMethod1(closable);
///
/// Whether the window can be manually closed by user.
///
/// On Linux always returns true.
///
/// On Linux always returns true.
public Task IsClosableAsync() => this.GetPropertyAsync();
///
/// 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) => this.CallMethod1(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) => this.CallMethod2(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) => this.CallMethod3(flag, level.GetDescription(), relativeLevel);
///
/// Whether the window is always on top of other windows.
///
///
public Task IsAlwaysOnTopAsync() => this.GetPropertyAsync();
///
/// Moves window to the center of the screen.
///
public void Center() => this.CallMethod0();
///
/// 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;
}
this.CallMethod2(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;
}
this.CallMethod3(x, y, animate);
}
private bool isWindows10()
{
return RuntimeInformation.OSDescription.Contains("Windows 10");
}
///
/// Contains the window’s current position.
///
///
public Task GetPositionAsync() => this.GetPropertyAsync();
///
/// Changes the title of native window to title.
///
///
public void SetTitle(string title) => this.CallMethod1(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() => this.GetPropertyAsync();
///
/// 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) => this.CallMethod1(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) => this.CallMethod2(offsetY, offsetX);
///
/// Starts or stops flashing the window to attract user’s attention.
///
///
public void FlashFrame(bool flag) => this.CallMethod1(flag);
///
/// Makes the window not show in the taskbar.
///
///
public void SetSkipTaskbar(bool skip) => this.CallMethod1(skip);
///
/// Enters or leaves the kiosk mode.
///
///
public void SetKiosk(bool flag) => this.CallMethod1(flag);
///
/// Whether the window is in kiosk mode.
///
///
public Task IsKioskAsync() => this.GetPropertyAsync();
///
/// 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() => this.GetPropertyAsync();
///
/// 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) => this.CallMethod1(filename);
///
/// The pathname of the file the window represents.
///
///
public Task GetRepresentedFilenameAsync() => this.GetPropertyAsync();
///
/// 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) => this.CallMethod1(edited);
///
/// Whether the window’s document has been edited.
///
///
public Task IsDocumentEditedAsync() => this.GetPropertyAsync();
///
/// Focuses the on web view.
///
public void FocusOnWebView() => this.CallMethod0();
///
/// Blurs the web view.
///
public void BlurWebView() => this.CallMethod0();
///
/// 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) => this.CallMethod1(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) => this.CallMethod2(url, JObject.FromObject(options, _jsonSerializer));
///
/// Same as webContents.reload.
///
public void Reload() => this.CallMethod0();
///
/// Gets the menu items.
///
///
/// The menu items.
///
public IReadOnlyCollection