implement first dialog-, notification-, tray- and menu-api functions

This commit is contained in:
Gregor Biswanger
2017-10-15 17:03:07 +02:00
parent 8f9a84cb0f
commit 08b88e3adf
32 changed files with 511 additions and 45 deletions

View File

@@ -280,12 +280,6 @@ namespace ElectronNET.API
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
// TODO: Auslagern in eigenes Notification-API
public void CreateNotification(NotificationOptions notificationOptions)
{
BridgeConnector.Socket.Emit("createNotification", JObject.FromObject(notificationOptions, _jsonSerializer));
}
/// <summary>
/// Try to close all windows. The before-quit event will be emitted first. If all
/// windows are successfully closed, the will-quit event will be emitted and by

89
ElectronNET.API/Dialog.cs Normal file
View File

@@ -0,0 +1,89 @@
using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Threading.Tasks;
namespace ElectronNET.API
{
public sealed class Dialog
{
private static Dialog _dialog;
internal Dialog() { }
internal static Dialog Instance
{
get
{
if (_dialog == null)
{
_dialog = new Dialog();
}
return _dialog;
}
}
/// <summary>
/// Shows a message box, it will block the process until the message box is closed.
/// It returns the index of the clicked button. The browserWindow argument allows
/// the dialog to attach itself to a parent window, making it modal. If a callback
/// is passed, the dialog will not block the process.The API call will be
/// asynchronous and the result will be passed via callback(response).
/// </summary>
/// <param name="messageBoxOptions"></param>
/// <returns>The API call will be asynchronous and the result will be passed via MessageBoxResult.</returns>
public async Task<MessageBoxResult> ShowMessageBoxAsync(MessageBoxOptions messageBoxOptions)
{
return await ShowMessageBoxAsync(null, messageBoxOptions);
}
/// <summary>
/// Shows a message box, it will block the process until the message box is closed.
/// It returns the index of the clicked button. If a callback
/// is passed, the dialog will not block the process.
/// </summary>
/// <param name="browserWindow">The browserWindow argument allows the dialog to attach itself to a parent window, making it modal.</param>
/// <param name="messageBoxOptions"></param>
/// <returns>The API call will be asynchronous and the result will be passed via MessageBoxResult.</returns>
public Task<MessageBoxResult> ShowMessageBoxAsync(BrowserWindow browserWindow, MessageBoxOptions messageBoxOptions)
{
var taskCompletionSource = new TaskCompletionSource<MessageBoxResult>();
BridgeConnector.Socket.On("showMessageBoxComplete", (args) =>
{
BridgeConnector.Socket.Off("showMessageBoxComplete");
var result = ((JArray)args);
taskCompletionSource.SetResult(new MessageBoxResult
{
Response = (int)result.First,
CheckboxChecked = (bool)result.Last
});
});
if (browserWindow == null)
{
BridgeConnector.Socket.Emit("showMessageBox", JObject.FromObject(messageBoxOptions, _jsonSerializer));
} else
{
BridgeConnector.Socket.Emit("showMessageBox",
JObject.FromObject(browserWindow, _jsonSerializer),
JObject.FromObject(messageBoxOptions, _jsonSerializer));
}
return taskCompletionSource.Task;
}
private JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
}
}

View File

@@ -21,5 +21,20 @@
/// Create native application menus and context menus.
/// </summary>
public static Menu Menu { get { return Menu.Instance; } }
/// <summary>
/// Display native system dialogs for opening and saving files, alerting, etc.
/// </summary>
public static Dialog Dialog { get { return Dialog.Instance; } }
/// <summary>
/// Create OS desktop notifications
/// </summary>
public static Notification Notification { get { return Notification.Instance; } }
/// <summary>
/// Add icons and context menus to the systems notification area.
/// </summary>
public static Tray Tray { get { return Tray.Instance; } }
}
}

View File

@@ -0,0 +1,84 @@
namespace ElectronNET.API.Entities
{
public class MessageBoxOptions
{
/// <summary>
/// Can be "none", "info", "error", "question" or "warning". On Windows, "question"
/// displays the same icon as "info", unless you set an icon using the "icon"
/// option.On macOS, both "warning" and "error" display the same warning icon.
/// </summary>
public string Type { get; set; }
/// <summary>
/// Array of texts for buttons. On Windows, an empty array will result in one button
/// labeled "OK".
/// </summary>
public string[] Buttons { get; set; }
/// <summary>
/// Index of the button in the buttons array which will be selected by default when
/// the message box opens.
/// </summary>
public int DefaultId { get; set; }
/// <summary>
/// Title of the message box, some platforms will not show it.
/// </summary>
public string Title { get; set; }
/// <summary>
/// Content of the message box.
/// </summary>
public string Message { get; set; }
/// <summary>
/// Extra information of the message.
/// </summary>
public string Detail { get; set; }
/// <summary>
/// If provided, the message box will include a checkbox with the given label. The
/// checkbox state can be inspected only when using callback.
/// </summary>
public string CheckboxLabel { get; set; }
/// <summary>
/// Initial checked state of the checkbox. false by default.
/// </summary>
public bool CheckboxChecked { get; set; }
public string Icon { get; set; }
/// <summary>
/// The index of the button to be used to cancel the dialog, via the Esc key. By
/// default this is assigned to the first button with "cancel" or "no" as the label.
/// If no such labeled buttons exist and this option is not set, 0 will be used as
/// the return value or callback response. This option is ignored on Windows.
/// </summary>
public int CancelId { get; set; }
/// <summary>
/// On Windows Electron will try to figure out which one of the buttons are common
/// buttons(like "Cancel" or "Yes"), and show the others as command links in the
/// dialog.This can make the dialog appear in the style of modern Windows apps. If
/// you don't like this behavior, you can set noLink to true.
/// </summary>
public bool NoLink { get; set; }
/// <summary>
/// Normalize the keyboard access keys across platforms. Default is false. Enabling
/// this assumes & is used in the button labels for the placement of the keyboard
/// shortcut access key and labels will be converted so they work correctly on each
/// platform, & characters are removed on macOS, converted to _ on Linux, and left
/// untouched on Windows.For example, a button label of Vie&w will be converted to
/// Vie_w on Linux and View on macOS and can be selected via Alt-W on Windows and
/// Linux.
/// </summary>
public bool NormalizeAccessKeys { get; set; }
public MessageBoxOptions(string message)
{
Message = message;
}
}
}

View File

@@ -0,0 +1,9 @@
namespace ElectronNET.API.Entities
{
public class MessageBoxResult
{
public int Response { get; set; }
public bool CheckboxChecked { get; set; }
}
}

View File

@@ -2,7 +2,52 @@
{
public class NotificationOptions
{
/// <summary>
/// A title for the notification, which will be shown at the top of the notification
/// window when it is shown
/// </summary>
public string Title { get; set; }
/// <summary>
/// The body text of the notification, which will be displayed below the title or
/// subtitle
/// </summary>
public string Body { get; set; }
/// <summary>
/// A subtitle for the notification, which will be displayed below the title.
/// </summary>
public string Subtitle { get; set; }
/// <summary>
/// Whether or not to emit an OS notification noise when showing the notification
/// </summary>
public bool Silent { get; set; }
/// <summary>
/// An icon to use in the notification
/// </summary>
public string Icon { get; set; }
/// <summary>
/// Whether or not to add an inline reply option to the notification.
/// </summary>
public bool HasReply { get; set; }
/// <summary>
/// The placeholder to write in the inline reply input field.
/// </summary>
public string ReplyPlaceholder { get; set; }
/// <summary>
/// The name of the sound file to play when the notification is shown.
/// </summary>
public string Sound { get; set; }
public NotificationOptions(string title, string body)
{
Title = title;
Body = body;
}
}
}

View File

@@ -0,0 +1,39 @@
using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
namespace ElectronNET.API
{
public sealed class Notification
{
private static Notification _notification;
internal Notification() { }
internal static Notification Instance
{
get
{
if (_notification == null)
{
_notification = new Notification();
}
return _notification;
}
}
public void Show(NotificationOptions notificationOptions)
{
BridgeConnector.Socket.Emit("createNotification", JObject.FromObject(notificationOptions, _jsonSerializer));
}
private JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
}
}

39
ElectronNET.API/Tray.cs Normal file
View File

@@ -0,0 +1,39 @@
using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
namespace ElectronNET.API
{
public sealed class Tray
{
private static Tray _tray;
internal Tray() { }
internal static Tray Instance
{
get
{
if (_tray == null)
{
_tray = new Tray();
}
return _tray;
}
}
public void Show(string image, MenuItem[] menuItems)
{
BridgeConnector.Socket.Emit("create-tray", image, JArray.FromObject(menuItems, _jsonSerializer));
}
private JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
}
}