using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Threading.Tasks;
using ElectronNET.API.Extensions;
using System.Runtime.Versioning;
namespace ElectronNET.API
{
///
/// Manage files and URLs using their default applications.
///
public sealed class Shell
{
private static Shell _shell;
private static readonly object _syncRoot = new();
internal Shell() { }
internal static Shell Instance
{
get
{
if (_shell == null)
{
lock (_syncRoot)
{
if (_shell == null)
{
_shell = new Shell();
}
}
}
return _shell;
}
}
///
/// Show the given file in a file manager. If possible, select the file.
///
/// The full path to the directory / file.
public Task ShowItemInFolderAsync(string fullPath)
{
var taskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
BridgeConnector.On("shell-showItemInFolderCompleted", () =>
{
BridgeConnector.Off("shell-showItemInFolderCompleted");
});
BridgeConnector.Emit("shell-showItemInFolder", fullPath);
return taskCompletionSource.Task;
}
///
/// Open the given file in the desktop's default manner.
///
/// The path to the directory / file.
/// The error message corresponding to the failure if a failure occurred, otherwise .
public Task OpenPathAsync(string path)
{
var taskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
BridgeConnector.On("shell-openPathCompleted", (errorMessage) =>
{
BridgeConnector.Off("shell-openPathCompleted");
taskCompletionSource.SetResult(errorMessage);
});
BridgeConnector.Emit("shell-openPath", path);
return taskCompletionSource.Task;
}
///
/// Open the given external protocol URL in the desktop’s default manner.
/// (For example, mailto: URLs in the user’s default mail agent).
///
/// Max 2081 characters on windows.
/// The error message corresponding to the failure if a failure occurred, otherwise .
public Task OpenExternalAsync(string url)
{
return OpenExternalAsync(url, null);
}
///
/// Open the given external protocol URL in the desktop’s default manner.
/// (For example, mailto: URLs in the user’s default mail agent).
///
/// Max 2081 characters on windows.
/// Controls the behavior of OpenExternal.
/// The error message corresponding to the failure if a failure occurred, otherwise .
public Task OpenExternalAsync(string url, OpenExternalOptions options)
{
var taskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
BridgeConnector.On("shell-openExternalCompleted", (error) =>
{
BridgeConnector.Off("shell-openExternalCompleted");
taskCompletionSource.SetResult(error);
});
if (options == null)
{
BridgeConnector.Emit("shell-openExternal", url);
}
else
{
BridgeConnector.Emit("shell-openExternal", url, options);
}
return taskCompletionSource.Task;
}
///
/// Move the given file to trash and returns a status for the operation.
///
/// The full path to the directory / file.
/// Whether the item was successfully moved to the trash.
public Task TrashItemAsync(string fullPath)
{
return BridgeConnector.OnResult("shell-trashItem", "shell-trashItem-completed", fullPath);
}
///
/// Play the beep sound.
///
public void Beep()
{
BridgeConnector.Emit("shell-beep");
}
///
/// Creates or updates a shortcut link at shortcutPath.
///
/// The path to the shortcut.
/// Default is
/// Structure of a shortcut.
/// Whether the shortcut was created successfully.
[SupportedOSPlatform("windows")]
public Task WriteShortcutLinkAsync(string shortcutPath, ShortcutLinkOperation operation, ShortcutDetails options)
{
return BridgeConnector.OnResult("shell-writeShortcutLink", "shell-writeShortcutLinkCompleted", shortcutPath, operation.GetDescription(), options);
}
///
/// Resolves the shortcut link at shortcutPath.
/// An exception will be thrown when any error happens.
///
/// The path tot the shortcut.
/// of the shortcut.
[SupportedOSPlatform("windows")]
public Task ReadShortcutLinkAsync(string shortcutPath)
{
return BridgeConnector.OnResult("shell-readShortcutLink", "shell-readShortcutLinkCompleted", shortcutPath);
}
}
}