2021-01-18 16:32:58 -06:00
|
|
|
|
using System.Collections.Generic;
|
2021-08-26 14:22:54 +02:00
|
|
|
|
using System.Runtime.Versioning;
|
2021-01-18 16:32:58 -06:00
|
|
|
|
using System.Threading;
|
2020-06-01 01:59:40 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using ElectronNET.API.Entities;
|
|
|
|
|
|
using ElectronNET.API.Extensions;
|
2021-01-18 16:32:58 -06:00
|
|
|
|
using Newtonsoft.Json;
|
2020-06-01 01:59:40 +02:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2021-01-18 16:32:58 -06:00
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2020-06-01 01:59:40 +02:00
|
|
|
|
|
|
|
|
|
|
namespace ElectronNET.API
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Control your app in the macOS dock.
|
|
|
|
|
|
/// </summary>
|
2021-08-26 14:22:54 +02:00
|
|
|
|
[SupportedOSPlatform("macos")]
|
2020-06-01 01:59:40 +02:00
|
|
|
|
public sealed class Dock
|
|
|
|
|
|
{
|
|
|
|
|
|
private static Dock _dock;
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private static readonly object _syncRoot = new();
|
2020-06-01 01:59:40 +02:00
|
|
|
|
|
|
|
|
|
|
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>
|
2021-08-18 10:24:12 +02:00
|
|
|
|
public Task<int> BounceAsync(DockBounceType type, CancellationToken cancellationToken = default)
|
2020-06-01 01:59:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
2021-08-18 10:24:12 +02:00
|
|
|
|
return BridgeConnector.OnResult<int>("dock-bounce", "dock-bounce-completed", cancellationToken, type.GetDescription());
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Cancel the bounce of id.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">Id of the request.</param>
|
|
|
|
|
|
public void CancelBounce(int id)
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("dock-cancelBounce", id);
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Bounces the Downloads stack if the filePath is inside the Downloads folder.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filePath"></param>
|
|
|
|
|
|
public void DownloadFinished(string filePath)
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("dock-downloadFinished", filePath);
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets the string to be displayed in the dock’s badging area.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="text"></param>
|
|
|
|
|
|
public void SetBadge(string text)
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("dock-setBadge", text);
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2021-08-18 10:24:12 +02:00
|
|
|
|
public Task<string> GetBadgeAsync(CancellationToken cancellationToken = default)
|
2020-06-01 01:59:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2021-08-18 10:24:12 +02:00
|
|
|
|
return BridgeConnector.OnResult<string>("dock-getBadge", "dock-getBadge-completed", cancellationToken);
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Hides the dock icon.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Hide()
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("dock-hide");
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Shows the dock icon.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Show()
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("dock-show");
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2021-08-18 10:24:12 +02:00
|
|
|
|
public Task<bool> IsVisibleAsync(CancellationToken cancellationToken = default)
|
2020-06-01 01:59:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2021-08-18 10:24:12 +02:00
|
|
|
|
return BridgeConnector.OnResult<bool>("dock-isVisible", "dock-isVisible-completed", cancellationToken);
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-01-18 16:32:58 -06:00
|
|
|
|
/// Gets the dock menu items.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <value>
|
|
|
|
|
|
/// The menu items.
|
|
|
|
|
|
/// </value>
|
|
|
|
|
|
public IReadOnlyCollection<MenuItem> MenuItems { get { return _items.AsReadOnly(); } }
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private readonly List<MenuItem> _items = new();
|
2021-01-18 16:32:58 -06:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-01 01:59:40 +02:00
|
|
|
|
/// Sets the application's dock menu.
|
|
|
|
|
|
/// </summary>
|
2021-01-18 16:32:58 -06:00
|
|
|
|
public void SetMenu(MenuItem[] menuItems)
|
2020-06-01 01:59:40 +02:00
|
|
|
|
{
|
2021-01-18 16:32:58 -06:00
|
|
|
|
menuItems.AddMenuItemsId();
|
2021-08-25 10:34:36 +02:00
|
|
|
|
BridgeConnector.Emit("dock-setMenu", JArray.FromObject(menuItems, _jsonSerializer));
|
2021-01-18 16:32:58 -06:00
|
|
|
|
_items.AddRange(menuItems);
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("dockMenuItemClicked");
|
|
|
|
|
|
BridgeConnector.On<string>("dockMenuItemClicked", (id) => {
|
|
|
|
|
|
MenuItem menuItem = _items.GetMenuItem(id);
|
2021-01-18 16:32:58 -06:00
|
|
|
|
menuItem?.Click();
|
|
|
|
|
|
});
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// TODO: Menu (macOS) still to be implemented
|
|
|
|
|
|
/// Gets the application's dock menu.
|
|
|
|
|
|
/// </summary>
|
2021-08-23 11:22:37 +02:00
|
|
|
|
public Task<Menu> GetMenu(CancellationToken cancellationToken = default) => BridgeConnector.OnResult<Menu>("dock-getMenu", "dock-getMenu-completed", cancellationToken);
|
2020-06-01 01:59:40 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets the image associated with this dock icon.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="image"></param>
|
|
|
|
|
|
public void SetIcon(string image)
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("dock-setIcon", image);
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
2021-08-25 10:34:36 +02:00
|
|
|
|
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private static readonly JsonSerializer _jsonSerializer = new()
|
2021-08-25 10:34:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore
|
|
|
|
|
|
};
|
2020-06-01 01:59:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|