using System.Runtime.Versioning;
using System.Threading.Tasks;
using ElectronNET.API.Entities;
using ElectronNET.API.Extensions;
namespace ElectronNET.API
{
///
/// Manage files and URLs using their default applications.
///
public sealed class Shell
{
private static Shell _shell;
private static object _syncRoot = new object();
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)
{
BridgeConnector.Socket.Emit("shell-showItemInFolder", fullPath);
return Task.CompletedTask;
}
///
/// 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 tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("shell-openPathCompleted", tcs.SetResult);
BridgeConnector.Socket.Emit("shell-openPath", path);
return tcs.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 tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("shell-openExternalCompleted", tcs.SetResult);
if (options == null)
{
BridgeConnector.Socket.Emit("shell-openExternal", url);
}
else
{
BridgeConnector.Socket.Emit("shell-openExternal", url, options);
}
return tcs.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)
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("shell-trashItem-completed", tcs.SetResult);
BridgeConnector.Socket.Emit("shell-trashItem", fullPath);
return tcs.Task;
}
///
/// Play the beep sound.
///
public void Beep()
{
BridgeConnector.Socket.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)
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("shell-writeShortcutLinkCompleted", tcs.SetResult);
BridgeConnector.Socket.Emit("shell-writeShortcutLink", shortcutPath, operation, options);
return tcs.Task;
}
///
/// 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)
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("shell-readShortcutLinkCompleted", tcs.SetResult);
BridgeConnector.Socket.Emit("shell-readShortcutLink", shortcutPath);
return tcs.Task;
}
}
}