Skip to content

App API enhancements #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
974 changes: 440 additions & 534 deletions ElectronNET.API/App.cs

Large diffs are not rendered by default.

206 changes: 206 additions & 0 deletions ElectronNET.API/Dock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
using System.Threading;
using System.Threading.Tasks;
using ElectronNET.API.Entities;
using ElectronNET.API.Extensions;
using Newtonsoft.Json.Linq;

namespace ElectronNET.API
{
/// <summary>
/// Control your app in the macOS dock.
/// </summary>
public sealed class Dock
{
private static Dock _dock;
private static object _syncRoot = new object();

internal Dock()
{
}

internal static Dock Instance
{
get
{
if (_dock == null)
{
lock (_syncRoot)
{
if (_dock == null)
{
_dock = new Dock();
}
}
}

return _dock;
}
}

/// <summary>
/// When <see cref="DockBounceType.Critical"/> is passed, the dock icon will bounce until either the application becomes
/// active or the request is canceled. When <see cref="DockBounceType.Informational"/> is passed, the dock icon will bounce
/// for one second. However, the request remains active until either the application becomes active or the request is canceled.
/// <para/>
/// Note: This method can only be used while the app is not focused; when the app is focused it will return -1.
/// </summary>
/// <param name="type">Can be critical or informational. The default is informational.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Return an ID representing the request.</returns>
public async Task<int> BounceAsync(DockBounceType type, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

var taskCompletionSource = new TaskCompletionSource<int>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("dock-bounce-completed", (id) =>
{
BridgeConnector.Socket.Off("dock-bounce-completed");
taskCompletionSource.SetResult((int) id);
});

BridgeConnector.Socket.Emit("dock-bounce", type.GetDescription());

return await taskCompletionSource.Task
.ConfigureAwait(false);
}
}

/// <summary>
/// Cancel the bounce of id.
/// </summary>
/// <param name="id">Id of the request.</param>
public void CancelBounce(int id)
{
BridgeConnector.Socket.Emit("dock-cancelBounce", id);
}

/// <summary>
/// Bounces the Downloads stack if the filePath is inside the Downloads folder.
/// </summary>
/// <param name="filePath"></param>
public void DownloadFinished(string filePath)
{
BridgeConnector.Socket.Emit("dock-downloadFinished", filePath);
}

/// <summary>
/// Sets the string to be displayed in the dock’s badging area.
/// </summary>
/// <param name="text"></param>
public void SetBadge(string text)
{
BridgeConnector.Socket.Emit("dock-setBadge", text);
}

/// <summary>
/// Gets the string to be displayed in the dock’s badging area.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The badge string of the dock.</returns>
public async Task<string> GetBadgeAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

var taskCompletionSource = new TaskCompletionSource<string>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("dock-getBadge-completed", (text) =>
{
BridgeConnector.Socket.Off("dock-getBadge-completed");
taskCompletionSource.SetResult((string) text);
});

BridgeConnector.Socket.Emit("dock-getBadge");

return await taskCompletionSource.Task
.ConfigureAwait(false);
}
}

/// <summary>
/// Hides the dock icon.
/// </summary>
public void Hide()
{
BridgeConnector.Socket.Emit("dock-hide");
}

/// <summary>
/// Shows the dock icon.
/// </summary>
public void Show()
{
BridgeConnector.Socket.Emit("dock-show");
}

/// <summary>
/// Whether the dock icon is visible. The app.dock.show() call is asynchronous
/// so this method might not return true immediately after that call.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Whether the dock icon is visible.</returns>
public async Task<bool> IsVisibleAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

var taskCompletionSource = new TaskCompletionSource<bool>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("dock-isVisible-completed", (isVisible) =>
{
BridgeConnector.Socket.Off("dock-isVisible-completed");
taskCompletionSource.SetResult((bool) isVisible);
});

BridgeConnector.Socket.Emit("dock-isVisible");

return await taskCompletionSource.Task
.ConfigureAwait(false);
}
}

/// <summary>
/// TODO: Menu (macOS) still to be implemented
/// Sets the application's dock menu.
/// </summary>
public void SetMenu()
{
BridgeConnector.Socket.Emit("dock-setMenu");
}

/// <summary>
/// TODO: Menu (macOS) still to be implemented
/// Gets the application's dock menu.
/// </summary>
public async Task<Menu> GetMenu(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

var taskCompletionSource = new TaskCompletionSource<Menu>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On("dock-getMenu-completed", (menu) =>
{
BridgeConnector.Socket.Off("dock-getMenu-completed");
taskCompletionSource.SetResult(((JObject)menu).ToObject<Menu>());
});

BridgeConnector.Socket.Emit("dock-getMenu");

return await taskCompletionSource.Task
.ConfigureAwait(false);
}
}

/// <summary>
/// Sets the image associated with this dock icon.
/// </summary>
/// <param name="image"></param>
public void SetIcon(string image)
{
BridgeConnector.Socket.Emit("dock-setIcon", image);
}
}
}
7 changes: 6 additions & 1 deletion ElectronNET.API/Electron.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,10 @@ public static class Electron
/// Read and respond to changes in Chromium's native color theme.
/// </summary>
public static NativeTheme NativeTheme { get { return NativeTheme.Instance; } }

/// <summary>
/// Control your app in the macOS dock.
/// </summary>
public static Dock Dock { get { return Dock.Instance; } }
}
}
}
21 changes: 18 additions & 3 deletions ElectronNET.API/Entities/AboutPanelOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace ElectronNET.API.Entities
{
/// <summary>
///
/// About panel options.
/// </summary>
public class AboutPanelOptions
{
Expand All @@ -20,14 +20,29 @@ public class AboutPanelOptions
/// </summary>
public string Copyright { get; set; }

/// <summary>
/// The app's build version number.
/// </summary>
public string Version { get; set; }

/// <summary>
/// Credit information.
/// </summary>
public string Credits { get; set; }

/// <summary>
/// The app's build version number.
/// List of app authors.
/// </summary>
public string Version { get; set; }
public string[] Authors { get; set; }

/// <summary>
/// The app's website.
/// </summary>
public string Website { get; set; }

/// <summary>
/// Path to the app's icon. On Linux, will be shown as 64x64 pixels while retaining aspect ratio.
/// </summary>
public string IconPath { get; set; }
}
}
10 changes: 5 additions & 5 deletions ElectronNET.API/Entities/CPUUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
public class CPUUsage
{
/// <summary>
/// The number of average idle cpu wakeups per second since the last call to
/// getCPUUsage.First call returns 0.
/// Percentage of CPU used since the last call to getCPUUsage. First call returns 0.
/// </summary>
public int IdleWakeupsPerSecond { get; set; }
public int PercentCPUUsage { get; set; }

/// <summary>
/// Percentage of CPU used since the last call to getCPUUsage. First call returns 0.
/// The number of average idle cpu wakeups per second since the last call to
/// getCPUUsage.First call returns 0.
/// </summary>
public int PercentCPUUsage { get; set; }
public int IdleWakeupsPerSecond { get; set; }
}
}
16 changes: 10 additions & 6 deletions ElectronNET.API/Entities/DockBounceType.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
namespace ElectronNET.API
using System.ComponentModel;

namespace ElectronNET.API.Entities
{
/// <summary>
///
/// Defines the DockBounceType enumeration.
/// </summary>
public enum DockBounceType
{
/// <summary>
/// The critical
/// Dock icon will bounce until either the application becomes active or the request is canceled.
/// </summary>
critical,
[Description("critical")]
Critical,

/// <summary>
/// The informational
/// The dock icon will bounce for one second.
/// </summary>
informational
[Description("informational")]
Informational
}
}
15 changes: 15 additions & 0 deletions ElectronNET.API/Entities/FocusOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// Controls the behavior of <see cref="App.Focus(FocusOptions)"/>.
/// </summary>
public class FocusOptions
{
/// <summary>
/// Make the receiver the active app even if another app is currently active.
/// <para/>
/// You should seek to use the <see cref="Steal"/> option as sparingly as possible.
/// </summary>
public bool Steal { get; set; }
}
}
Loading