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

93 lines
3.0 KiB
C#
Raw Normal View History

2020-04-23 03:29:52 +02:00
using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Threading.Tasks;
namespace ElectronNET.API
{
/// <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>
public class BrowserView
{
/// <summary>
/// Gets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
public int Id { get; internal set; }
/// <summary>
/// Render and control web pages.
/// </summary>
public WebContents WebContents { get; internal set; }
/// <summary>
/// Resizes and moves the view to the supplied bounds relative to the window.
///
/// (experimental)
/// </summary>
2021-07-12 18:16:07 +02:00
public Task<Rectangle> GetBoundsAsync()
2020-04-23 03:29:52 +02:00
{
2021-07-12 18:16:07 +02:00
var taskCompletionSource = new TaskCompletionSource<Rectangle>();
2021-07-12 19:50:39 +02:00
BridgeConnector.On<Rectangle>("browserView-getBounds-reply", (result) =>
2020-04-23 03:29:52 +02:00
{
2021-07-12 19:50:39 +02:00
BridgeConnector.Off("browserView-getBounds-reply");
taskCompletionSource.SetResult(result);
2021-07-12 18:16:07 +02:00
});
2020-04-23 03:29:52 +02:00
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("browserView-getBounds", Id);
2020-04-23 03:29:52 +02:00
2021-07-12 18:16:07 +02:00
return taskCompletionSource.Task;
}
2020-04-23 03:29:52 +02:00
2021-07-12 18:16:07 +02:00
public void SetBounds(Rectangle value)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("browserView-setBounds", Id, JObject.FromObject(value, _jsonSerializer));
2020-04-23 03:29:52 +02:00
}
/// <summary>
/// BrowserView
/// </summary>
internal BrowserView(int id)
{
Id = id;
// Workaround: increase the Id so as not to conflict with BrowserWindow id
// the backend detect about the value an BrowserView
WebContents = new WebContents(id + 1000);
}
/// <summary>
/// (experimental)
/// </summary>
/// <param name="options"></param>
public void SetAutoResize(AutoResizeOptions options)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("browserView-setAutoResize", Id, JObject.FromObject(options, _jsonSerializer));
2020-04-23 03:29:52 +02:00
}
/// <summary>
/// Color in #aarrggbb or #argb form. The alpha channel is optional.
///
/// (experimental)
/// </summary>
/// <param name="color">Color in #aarrggbb or #argb form. The alpha channel is optional.</param>
public void SetBackgroundColor(string color)
{
2021-07-12 19:50:39 +02:00
BridgeConnector.Emit("browserView-setBackgroundColor", Id, color);
2020-04-23 03:29:52 +02:00
}
private JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
};
}
}