using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Threading.Tasks;
namespace ElectronNET.API
{
///
/// 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.
///
public class BrowserView
{
///
/// Gets the identifier.
///
///
/// The identifier.
///
public int Id { get; internal set; }
///
/// Render and control web pages.
///
public WebContents WebContents { get; internal set; }
///
/// Resizes and moves the view to the supplied bounds relative to the window.
///
/// (experimental)
///
public Rectangle Bounds
{
get
{
return Task.Run(() =>
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("browserView-getBounds-reply", (result) =>
{
BridgeConnector.Socket.Off("browserView-getBounds-reply");
taskCompletionSource.SetResult((Rectangle)result);
});
BridgeConnector.Socket.Emit("browserView-getBounds", Id);
return taskCompletionSource.Task;
}).Result;
}
set
{
BridgeConnector.Socket.Emit("browserView-setBounds", Id, JObject.FromObject(value, _jsonSerializer));
}
}
///
/// BrowserView
///
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);
}
///
/// (experimental)
///
///
public void SetAutoResize(AutoResizeOptions options)
{
BridgeConnector.Socket.Emit("browserView-setAutoResize", Id, JObject.FromObject(options, _jsonSerializer));
}
///
/// Color in #aarrggbb or #argb form. The alpha channel is optional.
///
/// (experimental)
///
/// Color in #aarrggbb or #argb form. The alpha channel is optional.
public void SetBackgroundColor(string color)
{
BridgeConnector.Socket.Emit("browserView-setBackgroundColor", Id, color);
}
private JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
};
}
}