Skip to content

Commit dae5211

Browse files
committed
adding WebContents APIs for Zoom, Audio and DevTools
1 parent c81ed54 commit dae5211

File tree

6 files changed

+487
-9
lines changed

6 files changed

+487
-9
lines changed

src/ElectronNET.API/API/WebContents.cs

Lines changed: 183 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ internal WebContents(int id)
123123
/// </summary>
124124
public void OpenDevTools()
125125
{
126-
BridgeConnector.Socket.Emit("webContentsOpenDevTools", Id);
126+
BridgeConnector.Socket.Emit("webContents-openDevTools", Id);
127127
}
128128

129129
/// <summary>
@@ -132,7 +132,41 @@ public void OpenDevTools()
132132
/// <param name="openDevToolsOptions"></param>
133133
public void OpenDevTools(OpenDevToolsOptions openDevToolsOptions)
134134
{
135-
BridgeConnector.Socket.Emit("webContentsOpenDevTools", Id, openDevToolsOptions);
135+
BridgeConnector.Socket.Emit("webContents-openDevTools", Id, openDevToolsOptions);
136+
}
137+
138+
/// <summary>
139+
/// Toggles the devtools.
140+
/// </summary>
141+
public void ToggleDevTools()
142+
{
143+
BridgeConnector.Socket.Emit("webContents-toggleDevTools", Id);
144+
}
145+
146+
/// <summary>
147+
/// Closes the devtools.
148+
/// </summary>
149+
public void CloseDevTools()
150+
{
151+
BridgeConnector.Socket.Emit("webContents-closeDevTools", Id);
152+
}
153+
154+
/// <summary>
155+
/// Returns boolean - Whether the devtools is opened.
156+
/// </summary>
157+
/// <returns></returns>
158+
public bool IsDevToolsOpened()
159+
{
160+
return Task.Run(() => InvokeAsync<bool>()).Result;
161+
}
162+
163+
/// <summary>
164+
/// Returns boolean - Whether the devtools view is focused.
165+
/// </summary>
166+
/// <returns></returns>
167+
public bool IsDevToolsFocused()
168+
{
169+
return Task.Run(() => InvokeAsync<bool>()).Result;
136170
}
137171

138172
/// <summary>
@@ -280,4 +314,151 @@ public void InsertCSS(bool isBrowserWindow, string path)
280314
{
281315
BridgeConnector.Socket.Emit("webContents-insertCSS", Id, isBrowserWindow, path);
282316
}
317+
318+
/// <summary>
319+
/// A number property that determines the zoom level for this web contents.
320+
///The original size is 0 and each increment above or below represents zooming 20% larger or smaller to default limits of 300% and 50% of original size, respectively.
321+
///The formula for this is scale := 1.2 ^ level.
322+
/// </summary>
323+
public int ZoomLevel
324+
{
325+
get
326+
{
327+
return Task.Run(() => this.InvokeAsync<int>()).Result;
328+
}
329+
set
330+
{
331+
BridgeConnector.Socket.Emit("webContents-zoomLevel-set", Id, value);
332+
}
333+
}
334+
335+
/// <summary>
336+
/// A number property that determines the zoom factor for this web contents.
337+
///The zoom factor is the zoom percent divided by 100, so 300% = 3.0.
338+
/// </summary>
339+
public double ZoomFactor
340+
{
341+
get
342+
{
343+
return Task.Run(() => this.InvokeAsync<double>()).Result;
344+
}
345+
set
346+
{
347+
BridgeConnector.Socket.Emit("webContents-zoomFactor-set", Id, value);
348+
}
349+
}
350+
351+
/// <summary>
352+
/// Returns number - The current zoom factor.
353+
/// </summary>
354+
/// <returns></returns>
355+
public Task<double> GetZoomFactorAsync() => InvokeAsync<double>();
356+
357+
/// <summary>
358+
/// Changes the zoom factor to the specified factor.
359+
/// Zoom factor is zoom percent divided by 100, so 300% = 3.0.
360+
/// The factor must be greater than 0.0.
361+
/// </summary>
362+
/// <param name="factor"></param>
363+
public void SetZoomFactor(double factor)
364+
{
365+
BridgeConnector.Socket.Emit("webContents-setZoomFactor", Id, factor);
366+
}
367+
368+
/// <summary>
369+
/// Returns number - The current zoom level.
370+
/// </summary>
371+
/// <returns></returns>
372+
public Task<int> GetZoomLevelAsync() => InvokeAsync<int>();
373+
374+
/// <summary>
375+
/// Changes the zoom level to the specified level.
376+
/// The original size is 0 and each increment above or below represents zooming 20% larger or smaller to default limits of 300% and 50% of original size, respectively.
377+
/// </summary>
378+
/// <param name="level"></param>
379+
public void SetZoomLevel(int level)
380+
{
381+
BridgeConnector.Socket.Emit("webContents-setZoomLevel", Id, level);
382+
}
383+
384+
/// <summary>
385+
/// Sets the maximum and minimum pinch-to-zoom level.
386+
/// </summary>
387+
/// <param name="minimumLevel"></param>
388+
/// <param name="maximumLevel"></param>
389+
public Task SetVisualZoomLevelLimitsAsync(int minimumLevel, int maximumLevel)
390+
{
391+
var tcs = new TaskCompletionSource();
392+
393+
BridgeConnector.Socket.Once("webContents-setVisualZoomLevelLimits-completed", tcs.SetResult);
394+
BridgeConnector.Socket.Emit("webContents-setVisualZoomLevelLimits", Id, minimumLevel, maximumLevel);
395+
396+
return tcs.Task;
397+
}
398+
399+
/// <summary>
400+
/// A boolean property that determines whether this page is muted.
401+
/// </summary>
402+
public bool AudioMuted
403+
{
404+
get
405+
{
406+
return Task.Run(() => this.InvokeAsync<bool>()).Result;
407+
}
408+
set
409+
{
410+
BridgeConnector.Socket.Emit("webContents-audioMuted-set", Id, value);
411+
}
412+
}
413+
414+
/// <summary>
415+
/// Returns boolean - Whether this page has been muted.
416+
/// </summary>
417+
/// <returns></returns>
418+
public Task<bool> IsAudioMutedAsync() => InvokeAsync<bool>();
419+
420+
/// <summary>
421+
/// Returns boolean - Whether audio is currently playing.
422+
/// </summary>
423+
/// <returns></returns>
424+
public Task<bool> IsCurrentlyAudibleAsync() => InvokeAsync<bool>();
425+
426+
/// <summary>
427+
/// Mute the audio on the current web page.
428+
/// </summary>
429+
/// <param name="muted"></param>
430+
public void SetAudioMuted(bool muted)
431+
{
432+
BridgeConnector.Socket.Emit("webContents-setAudioMuted", Id, muted);
433+
}
434+
435+
/// <summary>
436+
/// A string property that determines the user agent for this web page.
437+
/// </summary>
438+
public string UserAgent
439+
{
440+
get
441+
{
442+
return Task.Run(() => this.InvokeAsync<string>()).Result;
443+
}
444+
set
445+
{
446+
BridgeConnector.Socket.Emit("webContents-userAgent-set", Id, value);
447+
}
448+
}
449+
450+
/// <summary>
451+
/// Returns string - The user agent for this web page.
452+
/// </summary>
453+
/// <returns></returns>
454+
public Task<string> GetUserAgentAsync() => InvokeAsync<string>();
455+
456+
/// <summary>
457+
/// Overrides the user agent for this web page.
458+
/// </summary>
459+
/// <param name="userAgent"></param>
460+
public void SetUserAgent(string userAgent)
461+
{
462+
BridgeConnector.Socket.Emit("webContents-setUserAgent", Id, userAgent);
463+
}
283464
}

src/ElectronNET.Host/api/webContents.js

Lines changed: 88 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)