using System.Collections.Generic; using System.Runtime.Versioning; using System.Threading; using System.Threading.Tasks; using ElectronNET.API.Entities; using ElectronNET.API.Extensions; using ElectronNET.API.Interfaces; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; namespace ElectronNET.API { /// /// Control your app in the macOS dock. /// [SupportedOSPlatform("macos")] public sealed class Dock : IDock { private static Dock _dock; private static readonly object _syncRoot = new(); internal Dock() { } internal static Dock Instance { get { if (_dock == null) { lock (_syncRoot) { if (_dock == null) { _dock = new Dock(); } } } return _dock; } } /// /// When is passed, the dock icon will bounce until either the application becomes /// active or the request is canceled. When 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. /// /// Note: This method can only be used while the app is not focused; when the app is focused it will return -1. /// /// Can be critical or informational. The default is informational. /// The cancellation token. /// Return an ID representing the request. public Task BounceAsync(DockBounceType type, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); return BridgeConnector.OnResult("dock-bounce", "dock-bounce-completed", cancellationToken, type.GetDescription()); } /// /// Cancel the bounce of id. /// /// Id of the request. public void CancelBounce(int id) { BridgeConnector.Emit("dock-cancelBounce", id); } /// /// Bounces the Downloads stack if the filePath is inside the Downloads folder. /// /// public void DownloadFinished(string filePath) { BridgeConnector.Emit("dock-downloadFinished", filePath); } /// /// Sets the string to be displayed in the dock’s badging area. /// /// public void SetBadge(string text) { BridgeConnector.Emit("dock-setBadge", text); } /// /// Gets the string to be displayed in the dock’s badging area. /// /// The cancellation token. /// The badge string of the dock. public Task GetBadgeAsync(CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); return BridgeConnector.OnResult("dock-getBadge", "dock-getBadge-completed", cancellationToken); } /// /// Hides the dock icon. /// public void Hide() { BridgeConnector.Emit("dock-hide"); } /// /// Shows the dock icon. /// public void Show() { BridgeConnector.Emit("dock-show"); } /// /// Whether the dock icon is visible. The app.dock.show() call is asynchronous /// so this method might not return true immediately after that call. /// /// The cancellation token. /// Whether the dock icon is visible. public Task IsVisibleAsync(CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); return BridgeConnector.OnResult("dock-isVisible", "dock-isVisible-completed", cancellationToken); } /// /// Gets the dock menu items. /// /// /// The menu items. /// public IReadOnlyCollection MenuItems { get { return _items.AsReadOnly(); } } private readonly List _items = new(); /// /// Sets the application's dock menu. /// public void SetMenu(MenuItem[] menuItems) { menuItems.AddMenuItemsId(); BridgeConnector.Emit("dock-setMenu", JArray.FromObject(menuItems, _jsonSerializer)); _items.AddRange(menuItems); BridgeConnector.Off("dockMenuItemClicked"); BridgeConnector.On("dockMenuItemClicked", (id) => { MenuItem menuItem = _items.GetMenuItem(id); menuItem?.Click(); }); } /// /// TODO: Menu (macOS) still to be implemented /// Gets the application's dock menu. /// public Task GetMenu(CancellationToken cancellationToken = default) => BridgeConnector.OnResult("dock-getMenu", "dock-getMenu-completed", cancellationToken); /// /// Sets the image associated with this dock icon. /// /// public void SetIcon(string image) { BridgeConnector.Emit("dock-setIcon", image); } private static readonly JsonSerializer _jsonSerializer = new() { ContractResolver = new CamelCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore }; } }