Files
Electron.NET/ElectronNET.API/BrowserWindow.cs

2256 lines
70 KiB
C#
Raw Permalink Normal View History

2017-10-15 21:39:52 +02:00
using ElectronNET.API.Entities;
2017-10-16 16:53:35 +02:00
using ElectronNET.API.Extensions;
2017-10-15 21:39:52 +02:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
2017-10-16 16:53:35 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
2017-10-15 21:39:52 +02:00
using System.Threading.Tasks;
2023-03-24 02:17:09 +01:00
namespace ElectronNET.API;
/// <summary>
/// Create and control browser windows.
/// </summary>
public class BrowserWindow
{
/// <summary>
2023-03-24 02:17:09 +01:00
/// Gets the identifier.
/// </summary>
2023-03-24 02:17:09 +01:00
/// <value>
/// The identifier.
/// </value>
public int Id { get; private set; }
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the web page has been rendered (while not being shown) and
/// window can be displayed without a visual flash.
/// </summary>
public event Action OnReadyToShow
{
add
{
2023-03-24 02:17:09 +01:00
if (_readyToShow == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-ready-to-show" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_readyToShow();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-ready-to-show", Id);
}
2023-03-24 02:17:09 +01:00
_readyToShow += value;
}
2023-03-24 02:17:09 +01:00
remove
{
2023-03-24 02:17:09 +01:00
_readyToShow -= value;
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
if (_readyToShow == null)
BridgeConnector.Socket.Off("browserWindow-ready-to-show" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _readyToShow;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the document changed its title.
/// </summary>
public event Action<string> OnPageTitleUpdated
{
add
{
2023-03-24 02:17:09 +01:00
if (_pageTitleUpdated == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-page-title-updated" + Id, (title) =>
{
2023-03-24 02:17:09 +01:00
_pageTitleUpdated(title.ToString());
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-page-title-updated", Id);
}
2023-03-24 02:17:09 +01:00
_pageTitleUpdated += value;
}
remove
{
_pageTitleUpdated -= value;
if (_pageTitleUpdated == null)
BridgeConnector.Socket.Off("browserWindow-page-title-updated" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action<string> _pageTitleUpdated;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window is going to be closed.
/// </summary>
public event Action OnClose
{
add
{
2023-03-24 02:17:09 +01:00
if (_close == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-close" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_close();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-close", Id);
}
2023-03-24 02:17:09 +01:00
_close += value;
}
2023-03-24 02:17:09 +01:00
remove
{
_close -= value;
if (_close == null)
BridgeConnector.Socket.Off("browserWindow-close" + Id);
}
}
2023-03-24 02:17:09 +01:00
private event Action _close;
2023-03-24 02:17:09 +01:00
/// <summary>
/// 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.
/// </summary>
public event Action OnClosed
{
add
{
2023-03-24 02:17:09 +01:00
if (_closed == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-closed" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_closed();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-closed", Id);
}
2023-03-24 02:17:09 +01:00
_closed += value;
}
remove
{
_closed -= value;
if (_closed == null)
BridgeConnector.Socket.Off("browserWindow-closed" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _closed;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when window session is going to end due to force shutdown or machine restart or session log off.
/// </summary>
public event Action OnSessionEnd
{
add
{
2023-03-24 02:17:09 +01:00
if (_sessionEnd == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-session-end" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_sessionEnd();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-session-end", Id);
}
2023-03-24 02:17:09 +01:00
_sessionEnd += value;
}
2023-03-24 02:17:09 +01:00
remove
{
_sessionEnd -= value;
if (_sessionEnd == null)
BridgeConnector.Socket.Off("browserWindow-session-end" + Id);
}
}
2023-03-24 02:17:09 +01:00
private event Action _sessionEnd;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the web page becomes unresponsive.
/// </summary>
public event Action OnUnresponsive
{
add
{
2023-03-24 02:17:09 +01:00
if (_unresponsive == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-unresponsive" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_unresponsive();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-unresponsive", Id);
}
2023-03-24 02:17:09 +01:00
_unresponsive += value;
}
remove
{
_unresponsive -= value;
if (_unresponsive == null)
BridgeConnector.Socket.Off("browserWindow-unresponsive" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _unresponsive;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the unresponsive web page becomes responsive again.
/// </summary>
public event Action OnResponsive
{
add
{
2023-03-24 02:17:09 +01:00
if (_responsive == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-responsive" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_responsive();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-responsive", Id);
}
2023-03-24 02:17:09 +01:00
_responsive += value;
}
2023-03-24 02:17:09 +01:00
remove
{
_responsive -= value;
if (_responsive == null)
BridgeConnector.Socket.Off("browserWindow-responsive" + Id);
}
}
2023-03-24 02:17:09 +01:00
private event Action _responsive;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window loses focus.
/// </summary>
public event Action OnBlur
{
add
{
2023-03-24 02:17:09 +01:00
if (_blur == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-blur" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_blur();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-blur", Id);
}
2023-03-24 02:17:09 +01:00
_blur += value;
}
remove
{
_blur -= value;
if (_blur == null)
BridgeConnector.Socket.Off("browserWindow-blur" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _blur;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window gains focus.
/// </summary>
public event Action OnFocus
{
add
{
2023-03-24 02:17:09 +01:00
if (_focus == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-focus" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_focus();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-focus", Id);
}
2023-03-24 02:17:09 +01:00
_focus += value;
}
2023-03-24 02:17:09 +01:00
remove
{
_focus -= value;
if (_focus == null)
BridgeConnector.Socket.Off("browserWindow-focus" + Id);
}
}
2023-03-24 02:17:09 +01:00
private event Action _focus;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window is shown.
/// </summary>
public event Action OnShow
{
add
{
2023-03-24 02:17:09 +01:00
if (_show == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-show" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_show();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-show", Id);
}
2023-03-24 02:17:09 +01:00
_show += value;
}
remove
{
_show -= value;
if (_show == null)
BridgeConnector.Socket.Off("browserWindow-show" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _show;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window is hidden.
/// </summary>
public event Action OnHide
{
add
{
2023-03-24 02:17:09 +01:00
if (_hide == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-hide" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_hide();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-hide", Id);
}
2023-03-24 02:17:09 +01:00
_hide += value;
}
2023-03-24 02:17:09 +01:00
remove
{
_hide -= value;
if (_hide == null)
BridgeConnector.Socket.Off("browserWindow-hide" + Id);
}
}
2023-03-24 02:17:09 +01:00
private event Action _hide;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when window is maximized.
/// </summary>
public event Action OnMaximize
{
add
{
2023-03-24 02:17:09 +01:00
if (_maximize == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-maximize" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_maximize();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-maximize", Id);
}
2023-03-24 02:17:09 +01:00
_maximize += value;
}
remove
{
_maximize -= value;
if (_maximize == null)
BridgeConnector.Socket.Off("browserWindow-maximize" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _maximize;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window exits from a maximized state.
/// </summary>
public event Action OnUnmaximize
{
add
{
2023-03-24 02:17:09 +01:00
if (_unmaximize == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-unmaximize" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_unmaximize();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-unmaximize", Id);
}
2023-03-24 02:17:09 +01:00
_unmaximize += value;
}
2023-03-24 02:17:09 +01:00
remove
{
_unmaximize -= value;
if (_unmaximize == null)
BridgeConnector.Socket.Off("browserWindow-unmaximize" + Id);
}
}
2023-03-24 02:17:09 +01:00
private event Action _unmaximize;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window is minimized.
/// </summary>
public event Action OnMinimize
{
add
{
2023-03-24 02:17:09 +01:00
if (_minimize == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-minimize" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_minimize();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-minimize", Id);
}
2023-03-24 02:17:09 +01:00
_minimize += value;
}
remove
{
_minimize -= value;
if (_minimize == null)
BridgeConnector.Socket.Off("browserWindow-minimize" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _minimize;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window is restored from a minimized state.
/// </summary>
public event Action OnRestore
{
add
{
2023-03-24 02:17:09 +01:00
if (_restore == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-restore" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_restore();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-restore", Id);
}
2023-03-24 02:17:09 +01:00
_restore += value;
}
2023-03-24 02:17:09 +01:00
remove
{
_restore -= value;
if (_restore == null)
BridgeConnector.Socket.Off("browserWindow-restore" + Id);
}
}
2023-03-24 02:17:09 +01:00
private event Action _restore;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window is being resized.
/// </summary>
public event Action OnResize
{
add
2017-10-16 16:53:35 +02:00
{
2023-03-24 02:17:09 +01:00
if (_resize == null)
2017-10-16 16:53:35 +02:00
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-resize" + Id, () =>
2017-10-16 16:53:35 +02:00
{
2023-03-24 02:17:09 +01:00
_resize();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-resize", Id);
2017-10-16 16:53:35 +02:00
}
2023-03-24 02:17:09 +01:00
_resize += value;
}
remove
{
_resize -= value;
if (_resize == null)
BridgeConnector.Socket.Off("browserWindow-resize" + Id);
2017-10-16 16:53:35 +02:00
}
2023-03-24 02:17:09 +01:00
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
private event Action _resize;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window is being moved to a new position.
///
/// Note: On macOS this event is just an alias of moved.
/// </summary>
public event Action OnMove
{
add
{
2023-03-24 02:17:09 +01:00
if (_move == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-move" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_move();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-move", Id);
}
2023-03-24 02:17:09 +01:00
_move += value;
}
2023-03-24 02:17:09 +01:00
remove
{
_move -= value;
if (_move == null)
BridgeConnector.Socket.Off("browserWindow-move" + Id);
}
}
2023-03-24 02:17:09 +01:00
private event Action _move;
2023-03-24 02:17:09 +01:00
/// <summary>
/// macOS: Emitted once when the window is moved to a new position.
/// </summary>
public event Action OnMoved
{
add
{
2023-03-24 02:17:09 +01:00
if (_moved == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-moved" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_moved();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-moved", Id);
}
2023-03-24 02:17:09 +01:00
_moved += value;
}
remove
{
_moved -= value;
if (_moved == null)
BridgeConnector.Socket.Off("browserWindow-moved" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _moved;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window enters a full-screen state.
/// </summary>
public event Action OnEnterFullScreen
{
add
{
2023-03-24 02:17:09 +01:00
if (_enterFullScreen == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-enter-full-screen" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_enterFullScreen();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-enter-full-screen", Id);
}
2023-03-24 02:17:09 +01:00
_enterFullScreen += value;
}
2023-03-24 02:17:09 +01:00
remove
{
_enterFullScreen -= value;
if (_enterFullScreen == null)
BridgeConnector.Socket.Off("browserWindow-enter-full-screen" + Id);
}
}
2023-03-24 02:17:09 +01:00
private event Action _enterFullScreen;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window leaves a full-screen state.
/// </summary>
public event Action OnLeaveFullScreen
{
add
{
2023-03-24 02:17:09 +01:00
if (_leaveFullScreen == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-leave-full-screen" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_leaveFullScreen();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-leave-full-screen", Id);
}
2023-03-24 02:17:09 +01:00
_leaveFullScreen += value;
}
remove
{
_leaveFullScreen -= value;
if (_leaveFullScreen == null)
BridgeConnector.Socket.Off("browserWindow-leave-full-screen" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _leaveFullScreen;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window enters a full-screen state triggered by HTML API.
/// </summary>
public event Action OnEnterHtmlFullScreen
{
add
{
2023-03-24 02:17:09 +01:00
if (_enterHtmlFullScreen == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-enter-html-full-screen" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_enterHtmlFullScreen();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-enter-html-full-screen", Id);
}
2023-03-24 02:17:09 +01:00
_enterHtmlFullScreen += value;
}
2023-03-24 02:17:09 +01:00
remove
{
2023-03-24 02:17:09 +01:00
_enterHtmlFullScreen -= value;
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
if (_enterHtmlFullScreen == null)
BridgeConnector.Socket.Off("browserWindow-enter-html-full-screen" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _enterHtmlFullScreen;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window leaves a full-screen state triggered by HTML API.
/// </summary>
public event Action OnLeaveHtmlFullScreen
{
add
{
2023-03-24 02:17:09 +01:00
if (_leaveHtmlFullScreen == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-leave-html-full-screen" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_leaveHtmlFullScreen();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-leave-html-full-screen", Id);
}
2023-03-24 02:17:09 +01:00
_leaveHtmlFullScreen += value;
}
remove
{
_leaveHtmlFullScreen -= value;
if (_leaveHtmlFullScreen == null)
BridgeConnector.Socket.Off("browserWindow-leave-html-full-screen" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _leaveHtmlFullScreen;
2023-03-24 02:17:09 +01:00
/// <summary>
/// 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.
/// </summary>
public event Action<string> OnAppCommand
{
add
{
2023-03-24 02:17:09 +01:00
if (_appCommand == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-app-command" + Id, (command) =>
{
2023-03-24 02:17:09 +01:00
_appCommand(command.ToString());
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-app-command", Id);
}
2023-03-24 02:17:09 +01:00
_appCommand += value;
}
remove
{
_appCommand -= value;
if (_appCommand == null)
BridgeConnector.Socket.Off("browserWindow-app-command" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action<string> _appCommand;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted on 3-finger swipe. Possible directions are up, right, down, left.
/// </summary>
public event Action<string> OnSwipe
{
add
{
2023-03-24 02:17:09 +01:00
if (_swipe == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-swipe" + Id, (direction) =>
{
2023-03-24 02:17:09 +01:00
_swipe(direction.ToString());
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-swipe", Id);
}
2023-03-24 02:17:09 +01:00
_swipe += value;
}
remove
{
_swipe -= value;
if (_swipe == null)
BridgeConnector.Socket.Off("browserWindow-swipe" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action<string> _swipe;
2023-03-24 02:17:09 +01:00
/// <summary>
/// Emitted when the window opens a sheet.
/// </summary>
public event Action OnSheetBegin
{
add
{
2023-03-24 02:17:09 +01:00
if (_sheetBegin == null)
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-sheet-begin" + Id, () =>
{
2023-03-24 02:17:09 +01:00
_sheetBegin();
});
2017-10-25 21:28:43 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-sheet-begin", Id);
}
2023-03-24 02:17:09 +01:00
_sheetBegin += value;
}
2023-03-24 02:17:09 +01:00
remove
{
_sheetBegin -= value;
2023-03-24 02:17:09 +01:00
if (_sheetBegin == null)
BridgeConnector.Socket.Off("browserWindow-sheet-begin" + Id);
}
2023-03-24 02:17:09 +01:00
}
2023-03-24 02:17:09 +01:00
private event Action _sheetBegin;
/// <summary>
/// Emitted when the window has closed a sheet.
/// </summary>
public event Action OnSheetEnd
{
add
2017-10-15 21:39:52 +02:00
{
2023-03-24 02:17:09 +01:00
if (_sheetEnd == null)
{
BridgeConnector.Socket.On("browserWindow-sheet-end" + Id, () =>
{
_sheetEnd();
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-sheet-end", Id);
}
_sheetEnd += value;
}
remove
2017-10-15 21:39:52 +02:00
{
2023-03-24 02:17:09 +01:00
_sheetEnd -= value;
if (_sheetEnd == null)
BridgeConnector.Socket.Off("browserWindow-sheet-end" + Id);
2017-10-15 21:39:52 +02:00
}
2023-03-24 02:17:09 +01:00
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
private event Action _sheetEnd;
/// <summary>
/// Emitted when the native new tab button is clicked.
/// </summary>
public event Action OnNewWindowForTab
{
add
2017-10-15 21:39:52 +02:00
{
2023-03-24 02:17:09 +01:00
if (_newWindowForTab == null)
{
BridgeConnector.Socket.On("browserWindow-new-window-for-tab" + Id, () =>
{
_newWindowForTab();
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("register-browserWindow-new-window-for-tab", Id);
}
_newWindowForTab += value;
}
remove
2017-10-15 21:39:52 +02:00
{
2023-03-24 02:17:09 +01:00
_newWindowForTab -= value;
if (_newWindowForTab == null)
BridgeConnector.Socket.Off("browserWindow-new-window-for-tab" + Id);
2017-10-15 21:39:52 +02:00
}
2023-03-24 02:17:09 +01:00
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
private event Action _newWindowForTab;
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
internal BrowserWindow(int id) {
Id = id;
WebContents = new WebContents(id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Force closing the window, the unload and beforeunload event wont 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.
/// </summary>
public void Destroy()
{
BridgeConnector.Socket.Emit("browserWindowDestroy", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// 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.
/// </summary>
public void Close()
{
BridgeConnector.Socket.Emit("browserWindowClose", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Focuses on the window.
/// </summary>
public void Focus()
{
BridgeConnector.Socket.Emit("browserWindowFocus", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Removes focus from the window.
/// </summary>
public void Blur()
{
BridgeConnector.Socket.Emit("browserWindowBlur", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window is focused.
/// </summary>
/// <returns></returns>
public Task<bool> IsFocusedAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isFocused-completed", (isFocused) => {
BridgeConnector.Socket.Off("browserWindow-isFocused-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isFocused);
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsFocused", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window is destroyed.
/// </summary>
/// <returns></returns>
public Task<bool> IsDestroyedAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isDestroyed-completed", (isDestroyed) => {
BridgeConnector.Socket.Off("browserWindow-isDestroyed-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isDestroyed);
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsDestroyed", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Shows and gives focus to the window.
/// </summary>
public void Show()
{
BridgeConnector.Socket.Emit("browserWindowShow", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Shows the window but doesnt focus on it.
/// </summary>
public void ShowInactive()
{
BridgeConnector.Socket.Emit("browserWindowShowInactive", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Hides the window.
/// </summary>
public void Hide()
{
BridgeConnector.Socket.Emit("browserWindowHide", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window is visible to the user.
/// </summary>
/// <returns></returns>
public Task<bool> IsVisibleAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isVisible-completed", (isVisible) => {
BridgeConnector.Socket.Off("browserWindow-isVisible-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isVisible);
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsVisible", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether current window is a modal window.
/// </summary>
/// <returns></returns>
public Task<bool> IsModalAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isModal-completed", (isModal) => {
BridgeConnector.Socket.Off("browserWindow-isModal-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isModal);
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsModal", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Maximizes the window. This will also show (but not focus) the window if it isnt being displayed already.
/// </summary>
public void Maximize()
{
BridgeConnector.Socket.Emit("browserWindowMaximize", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Unmaximizes the window.
/// </summary>
public void Unmaximize()
{
BridgeConnector.Socket.Emit("browserWindowUnmaximize", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window is maximized.
/// </summary>
/// <returns></returns>
public Task<bool> IsMaximizedAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isMaximized-completed", (isMaximized) => {
BridgeConnector.Socket.Off("browserWindow-isMaximized-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isMaximized);
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsMaximized", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Minimizes the window. On some platforms the minimized window will be shown in the Dock.
/// </summary>
public void Minimize()
{
BridgeConnector.Socket.Emit("browserWindowMinimize", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Restores the window from minimized state to its previous state.
/// </summary>
public void Restore()
{
BridgeConnector.Socket.Emit("browserWindowRestore", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window is minimized.
/// </summary>
/// <returns></returns>
public Task<bool> IsMinimizedAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isMinimized-completed", (isMinimized) => {
BridgeConnector.Socket.Off("browserWindow-isMinimized-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isMinimized);
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsMinimized", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the window should be in fullscreen mode.
/// </summary>
public void SetFullScreen(bool flag)
{
BridgeConnector.Socket.Emit("browserWindowSetFullScreen", Id, flag);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window is in fullscreen mode.
/// </summary>
/// <returns></returns>
public Task<bool> IsFullScreenAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isFullScreen-completed", (isFullScreen) => {
BridgeConnector.Socket.Off("browserWindow-isFullScreen-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isFullScreen);
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsFullScreen", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// 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 windows 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
/// doesnt care where the extra width and height are within the content viewonly that they exist. Just
/// sum any extra width and height areas you have within the overall content view.
/// </summary>
/// <param name="aspectRatio">The aspect ratio to maintain for some portion of the content view.</param>
/// <param name="extraSize">The extra size not to be included while maintaining the aspect ratio.</param>
public void SetAspectRatio(int aspectRatio, Size extraSize)
{
BridgeConnector.Socket.Emit("browserWindowSetAspectRatio", Id, aspectRatio, JObject.FromObject(extraSize, _jsonSerializer));
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Uses Quick Look to preview a file at a given path.
/// </summary>
/// <param name="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.</param>
public void PreviewFile(string path)
{
BridgeConnector.Socket.Emit("browserWindowPreviewFile", Id, path);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Uses Quick Look to preview a file at a given path.
/// </summary>
/// <param name="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.</param>
/// <param name="displayname">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.</param>
public void PreviewFile(string path, string displayname)
{
BridgeConnector.Socket.Emit("browserWindowPreviewFile", Id, path, displayname);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Closes the currently open Quick Look panel.
/// </summary>
public void CloseFilePreview()
{
BridgeConnector.Socket.Emit("browserWindowCloseFilePreview", Id);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Resizes and moves the window to the supplied bounds
/// </summary>
/// <param name="bounds"></param>
public void SetBounds(Rectangle bounds)
{
BridgeConnector.Socket.Emit("browserWindowSetBounds", Id, JObject.FromObject(bounds, _jsonSerializer));
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Resizes and moves the window to the supplied bounds
/// </summary>
/// <param name="bounds"></param>
/// <param name="animate"></param>
public void SetBounds(Rectangle bounds, bool animate)
{
BridgeConnector.Socket.Emit("browserWindowSetBounds", Id, JObject.FromObject(bounds, _jsonSerializer), animate);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Gets the bounds asynchronous.
/// </summary>
/// <returns></returns>
public Task<Rectangle> GetBoundsAsync()
{
var taskCompletionSource = new TaskCompletionSource<Rectangle>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getBounds-completed", (getBounds) => {
BridgeConnector.Socket.Off("browserWindow-getBounds-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(((JObject)getBounds).ToObject<Rectangle>());
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetBounds", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Resizes and moves the windows client area (e.g. the web page) to the supplied bounds.
/// </summary>
/// <param name="bounds"></param>
public void SetContentBounds(Rectangle bounds)
{
BridgeConnector.Socket.Emit("browserWindowSetContentBounds", Id, JObject.FromObject(bounds, _jsonSerializer));
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Resizes and moves the windows client area (e.g. the web page) to the supplied bounds.
/// </summary>
/// <param name="bounds"></param>
/// <param name="animate"></param>
public void SetContentBounds(Rectangle bounds, bool animate)
{
BridgeConnector.Socket.Emit("browserWindowSetContentBounds", Id, JObject.FromObject(bounds, _jsonSerializer), animate);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Gets the content bounds asynchronous.
/// </summary>
/// <returns></returns>
public Task<Rectangle> GetContentBoundsAsync()
{
var taskCompletionSource = new TaskCompletionSource<Rectangle>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getContentBounds-completed", (getContentBounds) => {
BridgeConnector.Socket.Off("browserWindow-getContentBounds-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(((JObject)getContentBounds).ToObject<Rectangle>());
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetContentBounds", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Resizes the window to width and height.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public void SetSize(int width, int height)
{
BridgeConnector.Socket.Emit("browserWindowSetSize", Id, width, height);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Resizes the window to width and height.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="animate"></param>
public void SetSize(int width, int height, bool animate)
{
BridgeConnector.Socket.Emit("browserWindowSetSize", Id, width, height, animate);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Contains the windows width and height.
/// </summary>
/// <returns></returns>
public Task<int[]> GetSizeAsync()
{
var taskCompletionSource = new TaskCompletionSource<int[]>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getSize-completed", (size) => {
BridgeConnector.Socket.Off("browserWindow-getSize-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(((JArray)size).ToObject<int[]>());
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetSize", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Resizes the windows client area (e.g. the web page) to width and height.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public void SetContentSize(int width, int height)
{
BridgeConnector.Socket.Emit("browserWindowSetContentSize", Id, width, height);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Resizes the windows client area (e.g. the web page) to width and height.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="animate"></param>
public void SetContentSize(int width, int height, bool animate)
{
BridgeConnector.Socket.Emit("browserWindowSetContentSize", Id, width, height, animate);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Contains the windows client areas width and height.
/// </summary>
/// <returns></returns>
public Task<int[]> GetContentSizeAsync()
{
var taskCompletionSource = new TaskCompletionSource<int[]>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getContentSize-completed", (size) => {
BridgeConnector.Socket.Off("browserWindow-getContentSize-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(((JArray)size).ToObject<int[]>());
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetContentSize", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets the minimum size of window to width and height.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public void SetMinimumSize(int width, int height)
{
BridgeConnector.Socket.Emit("browserWindowSetMinimumSize", Id, width, height);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Contains the windows minimum width and height.
/// </summary>
/// <returns></returns>
public Task<int[]> GetMinimumSizeAsync()
{
var taskCompletionSource = new TaskCompletionSource<int[]>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getMinimumSize-completed", (size) => {
BridgeConnector.Socket.Off("browserWindow-getMinimumSize-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(((JArray)size).ToObject<int[]>());
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetMinimumSize", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets the maximum size of window to width and height.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public void SetMaximumSize(int width, int height)
{
BridgeConnector.Socket.Emit("browserWindowSetMaximumSize", Id, width, height);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Contains the windows maximum width and height.
/// </summary>
/// <returns></returns>
public Task<int[]> GetMaximumSizeAsync()
{
var taskCompletionSource = new TaskCompletionSource<int[]>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getMaximumSize-completed", (size) => {
BridgeConnector.Socket.Off("browserWindow-getMaximumSize-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(((JArray)size).ToObject<int[]>());
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetMaximumSize", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the window can be manually resized by user.
/// </summary>
/// <param name="resizable"></param>
public void SetResizable(bool resizable)
{
BridgeConnector.Socket.Emit("browserWindowSetResizable", Id, resizable);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window can be manually resized by user.
/// </summary>
/// <returns></returns>
public Task<bool> IsResizableAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isResizable-completed", (resizable) => {
BridgeConnector.Socket.Off("browserWindow-isResizable-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)resizable);
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsResizable", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the window can be moved by user. On Linux does nothing.
/// </summary>
/// <param name="movable"></param>
public void SetMovable(bool movable)
{
BridgeConnector.Socket.Emit("browserWindowSetMovable", Id, movable);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window can be moved by user.
///
/// On Linux always returns true.
/// </summary>
/// <returns>On Linux always returns true.</returns>
public Task<bool> IsMovableAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isMovable-completed", (movable) => {
BridgeConnector.Socket.Off("browserWindow-isMovable-completed");
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)movable);
});
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsMovable", Id);
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the window can be manually minimized by user. On Linux does nothing.
/// </summary>
/// <param name="minimizable"></param>
public void SetMinimizable(bool minimizable)
{
BridgeConnector.Socket.Emit("browserWindowSetMinimizable", Id, minimizable);
}
2017-10-15 21:39:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window can be manually minimized by user.
///
/// On Linux always returns true.
/// </summary>
/// <returns>On Linux always returns true.</returns>
public Task<bool> IsMinimizableAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isMinimizable-completed", (minimizable) => {
BridgeConnector.Socket.Off("browserWindow-isMinimizable-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)minimizable);
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsMinimizable", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the window can be manually maximized by user. On Linux does nothing.
/// </summary>
/// <param name="maximizable"></param>
public void SetMaximizable(bool maximizable)
{
BridgeConnector.Socket.Emit("browserWindowSetMaximizable", Id, maximizable);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window can be manually maximized by user.
///
/// On Linux always returns true.
/// </summary>
/// <returns>On Linux always returns true.</returns>
public Task<bool> IsMaximizableAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isMaximizable-completed", (maximizable) => {
BridgeConnector.Socket.Off("browserWindow-isMaximizable-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)maximizable);
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsMaximizable", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the maximize/zoom window button toggles fullscreen mode or maximizes the window.
/// </summary>
/// <param name="fullscreenable"></param>
public void SetFullScreenable(bool fullscreenable)
{
BridgeConnector.Socket.Emit("browserWindowSetFullScreenable", Id, fullscreenable);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the maximize/zoom window button toggles fullscreen mode or maximizes the window.
/// </summary>
/// <returns></returns>
public Task<bool> IsFullScreenableAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isFullScreenable-completed", (fullscreenable) => {
BridgeConnector.Socket.Off("browserWindow-isFullScreenable-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)fullscreenable);
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsFullScreenable", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the window can be manually closed by user. On Linux does nothing.
/// </summary>
/// <param name="closable"></param>
public void SetClosable(bool closable)
{
BridgeConnector.Socket.Emit("browserWindowSetClosable", Id, closable);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window can be manually closed by user.
///
/// On Linux always returns true.
/// </summary>
/// <returns>On Linux always returns true.</returns>
public Task<bool> IsClosableAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isClosable-completed", (closable) => {
BridgeConnector.Socket.Off("browserWindow-isClosable-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)closable);
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsClosable", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// 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.
/// </summary>
/// <param name="flag"></param>
public void SetAlwaysOnTop(bool flag)
{
BridgeConnector.Socket.Emit("browserWindowSetAlwaysOnTop", Id, flag);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// 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.
/// </summary>
/// <param name="flag"></param>
/// <param name="level">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</param>
public void SetAlwaysOnTop(bool flag, OnTopLevel level)
{
BridgeConnector.Socket.Emit("browserWindowSetAlwaysOnTop", Id, flag, level.GetDescription());
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// 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.
/// </summary>
/// <param name="flag"></param>
/// <param name="level">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</param>
/// <param name="relativeLevel">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.</param>
public void SetAlwaysOnTop(bool flag, OnTopLevel level, int relativeLevel)
{
BridgeConnector.Socket.Emit("browserWindowSetAlwaysOnTop", Id, flag, level.GetDescription(), relativeLevel);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window is always on top of other windows.
/// </summary>
/// <returns></returns>
public Task<bool> IsAlwaysOnTopAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isAlwaysOnTop-completed", (isAlwaysOnTop) => {
BridgeConnector.Socket.Off("browserWindow-isAlwaysOnTop-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isAlwaysOnTop);
});
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsAlwaysOnTop", Id);
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Moves window to the center of the screen.
/// </summary>
public void Center()
{
BridgeConnector.Socket.Emit("browserWindowCenter", Id);
}
/// <summary>
/// Moves window to x and y.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void SetPosition(int x, int y)
{
// Workaround Windows 10 / Electron Bug
// https://github.com/electron/electron/issues/4045
if (isWindows10())
{
2023-03-24 02:17:09 +01:00
x = x - 7;
}
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowSetPosition", Id, x, y);
}
/// <summary>
/// Moves window to x and y.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="animate"></param>
public void SetPosition(int x, int y, bool animate)
{
// Workaround Windows 10 / Electron Bug
// https://github.com/electron/electron/issues/4045
if (isWindows10())
2017-10-16 16:53:35 +02:00
{
2023-03-24 02:17:09 +01:00
x = x - 7;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowSetPosition", Id, x, y, animate);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
private bool isWindows10()
{
return RuntimeInformation.OSDescription.Contains("Windows 10");
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Contains the windows current position.
/// </summary>
/// <returns></returns>
public Task<int[]> GetPositionAsync()
{
var taskCompletionSource = new TaskCompletionSource<int[]>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getPosition-completed", (position) => {
BridgeConnector.Socket.Off("browserWindow-getPosition-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(((JArray)position).ToObject<int[]>());
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetPosition", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Changes the title of native window to title.
/// </summary>
/// <param name="title"></param>
public void SetTitle(string title)
{
BridgeConnector.Socket.Emit("browserWindowSetTitle", Id, title);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// The title of the native window.
///
/// Note: The title of web page can be different from the title of the native window.
/// </summary>
/// <returns></returns>
public Task<string> GetTitleAsync()
{
var taskCompletionSource = new TaskCompletionSource<string>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getTitle-completed", (title) => {
BridgeConnector.Socket.Off("browserWindow-getTitle-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(title.ToString());
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetTitle", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// 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.
/// </summary>
/// <param name="offsetY"></param>
public void SetSheetOffset(float offsetY)
{
BridgeConnector.Socket.Emit("browserWindowSetSheetOffset", Id, offsetY);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// 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.
/// </summary>
/// <param name="offsetY"></param>
/// <param name="offsetX"></param>
public void SetSheetOffset(float offsetY, float offsetX)
{
BridgeConnector.Socket.Emit("browserWindowSetSheetOffset", Id, offsetY, offsetX);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Starts or stops flashing the window to attract users attention.
/// </summary>
/// <param name="flag"></param>
public void FlashFrame(bool flag)
{
BridgeConnector.Socket.Emit("browserWindowFlashFrame", Id, flag);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Makes the window not show in the taskbar.
/// </summary>
/// <param name="skip"></param>
public void SetSkipTaskbar(bool skip)
{
BridgeConnector.Socket.Emit("browserWindowSetSkipTaskbar", Id, skip);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Enters or leaves the kiosk mode.
/// </summary>
/// <param name="flag"></param>
public void SetKiosk(bool flag)
{
BridgeConnector.Socket.Emit("browserWindowSetKiosk", Id, flag);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window is in kiosk mode.
/// </summary>
/// <returns></returns>
public Task<bool> IsKioskAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isKiosk-completed", (isKiosk) => {
BridgeConnector.Socket.Off("browserWindow-isKiosk-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isKiosk);
});
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsKiosk", Id);
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2023-03-24 02:17:09 +01:00
/// <summary>
/// Returns the native type of the handle is HWND on Windows, NSView* on macOS, and Window (unsigned long) on Linux.
/// </summary>
/// <returns>string of the native handle obtained, HWND on Windows, NSView* on macOS, and Window (unsigned long) on Linux.</returns>
public Task<string> GetNativeWindowHandle()
{
var taskCompletionSource = new TaskCompletionSource<string>();
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getNativeWindowHandle-completed", (nativeWindowHandle) =>
2017-10-16 16:53:35 +02:00
{
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Off("browserWindow-getNativeWindowHandle-completed");
taskCompletionSource.SetResult(nativeWindowHandle.ToString());
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetNativeWindowHandle", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets the pathname of the file the window represents,
/// and the icon of the file will show in windows title bar.
/// </summary>
/// <param name="filename"></param>
public void SetRepresentedFilename(string filename)
{
BridgeConnector.Socket.Emit("browserWindowSetRepresentedFilename", Id, filename);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// The pathname of the file the window represents.
/// </summary>
/// <returns></returns>
public Task<string> GetRepresentedFilenameAsync()
{
var taskCompletionSource = new TaskCompletionSource<string>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getRepresentedFilename-completed", (pathname) => {
BridgeConnector.Socket.Off("browserWindow-getRepresentedFilename-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(pathname.ToString());
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetRepresentedFilename", Id);
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Specifies whether the windows document has been edited,
/// and the icon in title bar will become gray when set to true.
/// </summary>
/// <param name="edited"></param>
public void SetDocumentEdited(bool edited)
{
BridgeConnector.Socket.Emit("browserWindowSetDocumentEdited", Id, edited);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the windows document has been edited.
/// </summary>
/// <returns></returns>
public Task<bool> IsDocumentEditedAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isDocumentEdited-completed", (edited) => {
BridgeConnector.Socket.Off("browserWindow-isDocumentEdited-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)edited);
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsDocumentEdited", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Focuses the on web view.
/// </summary>
public void FocusOnWebView()
{
BridgeConnector.Socket.Emit("browserWindowFocusOnWebView", Id);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Blurs the web view.
/// </summary>
public void BlurWebView()
{
BridgeConnector.Socket.Emit("browserWindowBlurWebView", Id);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// The url can be a remote address (e.g. http://) or
/// a path to a local HTML file using the file:// protocol.
/// </summary>
/// <param name="url"></param>
public void LoadURL(string url)
{
BridgeConnector.Socket.Emit("browserWindowLoadURL", Id, url);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// The url can be a remote address (e.g. http://) or
/// a path to a local HTML file using the file:// protocol.
/// </summary>
/// <param name="url"></param>
/// <param name="options"></param>
public void LoadURL(string url, LoadURLOptions options)
{
BridgeConnector.Socket.Emit("browserWindowLoadURL", Id, url, JObject.FromObject(options, _jsonSerializer));
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Same as webContents.reload.
/// </summary>
public void Reload()
{
BridgeConnector.Socket.Emit("browserWindowReload", Id);
}
2019-08-21 06:43:56 +03:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Gets the menu items.
/// </summary>
/// <value>
/// The menu items.
/// </value>
public IReadOnlyCollection<MenuItem> MenuItems { get { return _items.AsReadOnly(); } }
private List<MenuItem> _items = new List<MenuItem>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets the menu as the windows menu bar,
/// setting it to null will remove the menu bar.
/// </summary>
/// <param name="menuItems"></param>
public void SetMenu(MenuItem[] menuItems)
{
menuItems.AddMenuItemsId();
BridgeConnector.Socket.Emit("browserWindowSetMenu", Id, JArray.FromObject(menuItems, _jsonSerializer));
_items.AddRange(menuItems);
BridgeConnector.Socket.Off("windowMenuItemClicked");
BridgeConnector.Socket.On("windowMenuItemClicked", (id) => {
MenuItem menuItem = _items.GetMenuItem(id.ToString());
menuItem?.Click();
});
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Remove the window's menu bar.
/// </summary>
public void RemoveMenu()
{
BridgeConnector.Socket.Emit("browserWindowRemoveMenu", Id);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets progress value in progress bar. Valid range is [0, 1.0]. Remove progress
/// bar when progress smaler as 0; Change to indeterminate mode when progress bigger as 1. On Linux
/// platform, only supports Unity desktop environment, you need to specify the
/// .desktop file name to desktopName field in package.json.By default, it will
/// assume app.getName().desktop.On Windows, a mode can be passed.Accepted values
/// are none, normal, indeterminate, error, and paused. If you call setProgressBar
/// without a mode set (but with a value within the valid range), normal will be
/// assumed.
/// </summary>
/// <param name="progress"></param>
public void SetProgressBar(double progress)
{
BridgeConnector.Socket.Emit("browserWindowSetProgressBar", Id, progress);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets progress value in progress bar. Valid range is [0, 1.0]. Remove progress
/// bar when progress smaler as 0; Change to indeterminate mode when progress bigger as 1. On Linux
/// platform, only supports Unity desktop environment, you need to specify the
/// .desktop file name to desktopName field in package.json.By default, it will
/// assume app.getName().desktop.On Windows, a mode can be passed.Accepted values
/// are none, normal, indeterminate, error, and paused. If you call setProgressBar
/// without a mode set (but with a value within the valid range), normal will be
/// assumed.
/// </summary>
/// <param name="progress"></param>
/// <param name="progressBarOptions"></param>
public void SetProgressBar(double progress, ProgressBarOptions progressBarOptions)
{
BridgeConnector.Socket.Emit("browserWindowSetProgressBar", Id, progress, JObject.FromObject(progressBarOptions, _jsonSerializer));
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the window should have a shadow. On Windows and Linux does nothing.
/// </summary>
/// <param name="hasShadow"></param>
public void SetHasShadow(bool hasShadow)
{
BridgeConnector.Socket.Emit("browserWindowSetHasShadow", Id, hasShadow);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window has a shadow.
///
/// On Windows and Linux always returns true.
/// </summary>
/// <returns></returns>
public Task<bool> HasShadowAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-hasShadow-completed", (hasShadow) => {
BridgeConnector.Socket.Off("browserWindow-hasShadow-completed");
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)hasShadow);
});
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowHasShadow", Id);
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2023-03-24 02:17:09 +01:00
/// <summary>
/// Gets the thumbar buttons.
/// </summary>
/// <value>
/// The thumbar buttons.
/// </value>
public IReadOnlyCollection<ThumbarButton> ThumbarButtons { get { return _thumbarButtons.AsReadOnly(); } }
private List<ThumbarButton> _thumbarButtons = new List<ThumbarButton>();
2023-03-24 02:17:09 +01:00
/// <summary>
/// Add a thumbnail toolbar with a specified set of buttons to the thumbnail
/// image of a window in a taskbar button layout. Returns a Boolean object
/// indicates whether the thumbnail has been added successfully.
///
/// The number of buttons in thumbnail toolbar should be no greater than 7 due
/// to the limited room.Once you setup the thumbnail toolbar, the toolbar cannot
/// be removed due to the platforms limitation.But you can call the API with an
/// empty array to clean the buttons.
/// </summary>
/// <param name="thumbarButtons"></param>
/// <returns>Whether the buttons were added successfully.</returns>
public Task<bool> SetThumbarButtonsAsync(ThumbarButton[] thumbarButtons)
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindowSetThumbarButtons-completed", (success) => {
BridgeConnector.Socket.Off("browserWindowSetThumbarButtons-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)success);
});
2023-03-24 02:17:09 +01:00
thumbarButtons.AddThumbarButtonsId();
BridgeConnector.Socket.Emit("browserWindowSetThumbarButtons", Id, JArray.FromObject(thumbarButtons, _jsonSerializer));
_thumbarButtons.Clear();
_thumbarButtons.AddRange(thumbarButtons);
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Off("thumbarButtonClicked");
BridgeConnector.Socket.On("thumbarButtonClicked", (id) => {
ThumbarButton thumbarButton = _thumbarButtons.GetThumbarButton(id.ToString());
thumbarButton?.Click();
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets the region of the window to show as the thumbnail image displayed when hovering over
/// the window in the taskbar. You can reset the thumbnail to be the entire window by specifying
/// an empty region: {x: 0, y: 0, width: 0, height: 0}.
/// </summary>
/// <param name="rectangle"></param>
public void SetThumbnailClip(Rectangle rectangle)
{
BridgeConnector.Socket.Emit("browserWindowSetThumbnailClip", Id, rectangle);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets the toolTip that is displayed when hovering over the window thumbnail in the taskbar.
/// </summary>
/// <param name="tooltip"></param>
public void SetThumbnailToolTip(string tooltip)
{
BridgeConnector.Socket.Emit("browserWindowSetThumbnailToolTip", Id, tooltip);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets the properties for the windows taskbar button.
///
/// Note: relaunchCommand and relaunchDisplayName must always be set together.
/// If one of those properties is not set, then neither will be used.
/// </summary>
/// <param name="options"></param>
public void SetAppDetails(AppDetailsOptions options)
{
BridgeConnector.Socket.Emit("browserWindowSetAppDetails", Id, JObject.FromObject(options, _jsonSerializer));
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Same as webContents.showDefinitionForSelection().
/// </summary>
public void ShowDefinitionForSelection()
{
BridgeConnector.Socket.Emit("browserWindowShowDefinitionForSelection", Id);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the window menu bar should hide itself automatically.
/// Once set the menu bar will only show when users press the single Alt key.
///
/// If the menu bar is already visible, calling setAutoHideMenuBar(true) wont hide it immediately.
/// </summary>
/// <param name="hide"></param>
public void SetAutoHideMenuBar(bool hide)
{
BridgeConnector.Socket.Emit("browserWindowSetAutoHideMenuBar", Id, hide);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether menu bar automatically hides itself.
/// </summary>
/// <returns></returns>
public Task<bool> IsMenuBarAutoHideAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isMenuBarAutoHide-completed", (isMenuBarAutoHide) => {
BridgeConnector.Socket.Off("browserWindow-isMenuBarAutoHide-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isMenuBarAutoHide);
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsMenuBarAutoHide", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the menu bar should be visible. If the menu bar is auto-hide,
/// users can still bring up the menu bar by pressing the single Alt key.
/// </summary>
/// <param name="visible"></param>
public void SetMenuBarVisibility(bool visible)
{
BridgeConnector.Socket.Emit("browserWindowSetMenuBarVisibility", Id, visible);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the menu bar is visible.
/// </summary>
/// <returns></returns>
public Task<bool> IsMenuBarVisibleAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isMenuBarVisible-completed", (isMenuBarVisible) => {
BridgeConnector.Socket.Off("browserWindow-isMenuBarVisible-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isMenuBarVisible);
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsMenuBarVisible", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Sets whether the window should be visible on all workspaces.
///
/// Note: This API does nothing on Windows.
/// </summary>
/// <param name="visible"></param>
public void SetVisibleOnAllWorkspaces(bool visible)
{
BridgeConnector.Socket.Emit("browserWindowSetVisibleOnAllWorkspaces", Id, visible);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Whether the window is visible on all workspaces.
///
/// Note: This API always returns false on Windows.
/// </summary>
/// <returns></returns>
public Task<bool> IsVisibleOnAllWorkspacesAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-isVisibleOnAllWorkspaces-completed", (isVisibleOnAllWorkspaces) => {
BridgeConnector.Socket.Off("browserWindow-isVisibleOnAllWorkspaces-completed");
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult((bool)isVisibleOnAllWorkspaces);
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowIsVisibleOnAllWorkspaces", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Makes the window ignore all mouse events.
///
/// All mouse events happened in this window will be passed to the window
/// below this window, but if this window has focus, it will still receive keyboard events.
/// </summary>
/// <param name="ignore"></param>
public void SetIgnoreMouseEvents(bool ignore)
{
BridgeConnector.Socket.Emit("browserWindowSetIgnoreMouseEvents", Id, ignore);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Prevents the window contents from being captured by other apps.
///
/// On macOS it sets the NSWindows sharingType to NSWindowSharingNone.
/// On Windows it calls SetWindowDisplayAffinity with WDA_MONITOR.
/// </summary>
/// <param name="enable"></param>
public void SetContentProtection(bool enable)
{
BridgeConnector.Socket.Emit("browserWindowSetContentProtection", Id, enable);
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Changes whether the window can be focused.
/// </summary>
/// <param name="focusable"></param>
public void SetFocusable(bool focusable)
{
BridgeConnector.Socket.Emit("browserWindowSetFocusable", Id, focusable);
}
/// <summary>
/// Sets parent as current windows parent window,
/// passing null will turn current window into a top-level window.
/// </summary>
/// <param name="parent"></param>
public void SetParentWindow(BrowserWindow parent)
{
BridgeConnector.Socket.Emit("browserWindowSetParentWindow", Id, JObject.FromObject(parent, _jsonSerializer));
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// The parent window.
/// </summary>
/// <returns></returns>
public Task<BrowserWindow> GetParentWindowAsync()
{
var taskCompletionSource = new TaskCompletionSource<BrowserWindow>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getParentWindow-completed", (id) => {
BridgeConnector.Socket.Off("browserWindow-getParentWindow-completed");
var browserWindowId = int.Parse(id.ToString());
var browserWindow = Electron.WindowManager.BrowserWindows.ToList().Single(x => x.Id == browserWindowId);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(browserWindow);
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetParentWindow", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
/// <summary>
/// All child windows.
/// </summary>
/// <returns></returns>
public Task<List<BrowserWindow>> GetChildWindowsAsync()
{
var taskCompletionSource = new TaskCompletionSource<List<BrowserWindow>>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.On("browserWindow-getChildWindows-completed", (ids) => {
BridgeConnector.Socket.Off("browserWindow-getChildWindows-completed");
var browserWindowIds = ((JArray)ids).ToObject<int[]>();
var browserWindows = new List<BrowserWindow>();
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
browserWindowIds.ToList().ForEach(id =>
{
var browserWindow = Electron.WindowManager.BrowserWindows.ToList().Single(x => x.Id == id);
browserWindows.Add(browserWindow);
2017-10-16 16:53:35 +02:00
});
2023-03-24 02:17:09 +01:00
taskCompletionSource.SetResult(browserWindows);
});
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
BridgeConnector.Socket.Emit("browserWindowGetChildWindows", Id);
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
return taskCompletionSource.Task;
}
2017-10-16 16:53:35 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Controls whether to hide cursor when typing.
/// </summary>
/// <param name="autoHide"></param>
public void SetAutoHideCursor(bool autoHide)
{
BridgeConnector.Socket.Emit("browserWindowSetAutoHideCursor", Id, autoHide);
}
2020-04-23 03:29:52 +02:00
2023-03-24 02:17:09 +01:00
/// <summary>
/// Adds a vibrancy effect to the browser window.
/// Passing null or an empty string will remove the vibrancy effect on the window.
/// </summary>
/// <param name="type">Can be appearance-based, light, dark, titlebar, selection,
/// menu, popover, sidebar, medium-light or ultra-dark.
/// See the macOS documentation for more details.</param>
public void SetVibrancy(Vibrancy type)
{
BridgeConnector.Socket.Emit("browserWindowSetVibrancy", Id, type.GetDescription());
}
2023-03-24 02:17:09 +01:00
/// <summary>
/// Render and control web pages.
/// </summary>
public WebContents WebContents { get; internal set; }
/// <summary>
/// A BrowserView can be used to embed additional web content into a BrowserWindow.
/// It is like a child window, except that it is positioned relative to its owning window.
/// It is meant to be an alternative to the webview tag.
/// </summary>
/// <param name="browserView"></param>
public void SetBrowserView(BrowserView browserView)
{
BridgeConnector.Socket.Emit("browserWindow-setBrowserView", Id, browserView.Id);
}
private readonly JsonSerializer _jsonSerializer = new()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
};
}