|
| 1 | +using ElectronNET.API.Entities; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using Newtonsoft.Json.Linq; |
| 4 | +using Newtonsoft.Json.Serialization; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace ElectronNET.API |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// A BrowserView can be used to embed additional web content into a BrowserWindow. |
| 14 | + /// It is like a child window, except that it is positioned relative to its owning window. |
| 15 | + /// It is meant to be an alternative to the webview tag. |
| 16 | + /// </summary> |
| 17 | + public class BrowserView |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Gets the identifier. |
| 21 | + /// </summary> |
| 22 | + /// <value> |
| 23 | + /// The identifier. |
| 24 | + /// </value> |
| 25 | + public int Id { get; internal set; } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Render and control web pages. |
| 29 | + /// </summary> |
| 30 | + public WebContents WebContents { get; internal set; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Whether the view is destroyed. |
| 34 | + /// </summary> |
| 35 | + public Task<bool> IsDestroyedAsync |
| 36 | + { |
| 37 | + get |
| 38 | + { |
| 39 | + return Task.Run<bool>(() => |
| 40 | + { |
| 41 | + var taskCompletionSource = new TaskCompletionSource<bool>(); |
| 42 | + |
| 43 | + BridgeConnector.Socket.On("browserView-isDestroyed-reply", (result) => |
| 44 | + { |
| 45 | + BridgeConnector.Socket.Off("browserView-isDestroyed-reply"); |
| 46 | + taskCompletionSource.SetResult((bool)result); |
| 47 | + }); |
| 48 | + |
| 49 | + BridgeConnector.Socket.Emit("browserView-isDestroyed", Id); |
| 50 | + |
| 51 | + return taskCompletionSource.Task; |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Resizes and moves the view to the supplied bounds relative to the window. |
| 58 | + /// |
| 59 | + /// (experimental) |
| 60 | + /// </summary> |
| 61 | + public Rectangle Bounds |
| 62 | + { |
| 63 | + get |
| 64 | + { |
| 65 | + return Task.Run<Rectangle>(() => |
| 66 | + { |
| 67 | + var taskCompletionSource = new TaskCompletionSource<Rectangle>(); |
| 68 | + |
| 69 | + BridgeConnector.Socket.On("browserView-getBounds-reply", (result) => |
| 70 | + { |
| 71 | + BridgeConnector.Socket.Off("browserView-getBounds-reply"); |
| 72 | + taskCompletionSource.SetResult((Rectangle)result); |
| 73 | + }); |
| 74 | + |
| 75 | + BridgeConnector.Socket.Emit("browserView-getBounds", Id); |
| 76 | + |
| 77 | + return taskCompletionSource.Task; |
| 78 | + }).Result; |
| 79 | + } |
| 80 | + set |
| 81 | + { |
| 82 | + BridgeConnector.Socket.Emit("browserView-setBounds", Id, JObject.FromObject(value, _jsonSerializer)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + internal Action<BrowserView> Destroyed; |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// BrowserView |
| 90 | + /// </summary> |
| 91 | + internal BrowserView(int id) |
| 92 | + { |
| 93 | + Id = id; |
| 94 | + |
| 95 | + // Workaround: increase the Id so as not to conflict with BrowserWindow id |
| 96 | + // the backend detect about the value an BrowserView |
| 97 | + WebContents = new WebContents(id + 1000); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Force closing the view, the `unload` and `beforeunload` events won't be emitted |
| 102 | + /// for the web page.After you're done with a view, call this function in order to |
| 103 | + /// free memory and other resources as soon as possible. |
| 104 | + /// </summary> |
| 105 | + public void Destroy() |
| 106 | + { |
| 107 | + BridgeConnector.Socket.Emit("browserView-destroy", Id); |
| 108 | + |
| 109 | + Destroyed?.Invoke(this); |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// (experimental) |
| 114 | + /// </summary> |
| 115 | + /// <param name="options"></param> |
| 116 | + public void SetAutoResize(AutoResizeOptions options) |
| 117 | + { |
| 118 | + BridgeConnector.Socket.Emit("browserView-setAutoResize", Id, JObject.FromObject(options, _jsonSerializer)); |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Color in #aarrggbb or #argb form. The alpha channel is optional. |
| 123 | + /// |
| 124 | + /// (experimental) |
| 125 | + /// </summary> |
| 126 | + /// <param name="color">Color in #aarrggbb or #argb form. The alpha channel is optional.</param> |
| 127 | + public void SetBackgroundColor(string color) |
| 128 | + { |
| 129 | + BridgeConnector.Socket.Emit("browserView-setBackgroundColor", Id, color); |
| 130 | + } |
| 131 | + |
| 132 | + private JsonSerializer _jsonSerializer = new JsonSerializer() |
| 133 | + { |
| 134 | + ContractResolver = new CamelCasePropertyNamesContractResolver(), |
| 135 | + NullValueHandling = NullValueHandling.Ignore |
| 136 | + }; |
| 137 | + } |
| 138 | +} |
0 commit comments