implement Demo App sections: Dialogs, Menu, Tray, Shell, CrashHang, Notification, Shortcuts etc. Fix some API bugs and implement GlobalShortcut-, Shell- and WebContents-API.

This commit is contained in:
Gregor Biswanger
2017-10-21 04:37:01 +02:00
parent 9046f6ca25
commit 248ddde82b
61 changed files with 2505 additions and 57 deletions

View File

@@ -0,0 +1,76 @@
using Microsoft.AspNetCore.Mvc;
using ElectronNET.API;
using ElectronNET.API.Entities;
namespace ElectronNET.WebApp.Controllers
{
public class CrashHangController : Controller
{
public IActionResult Index()
{
Electron.IpcMain.On("process-crash", async (args) =>
{
string viewPath = $"http://localhost:{BridgeSettings.WebPort}/crashhang/processcrash";
var browserWindow = await Electron.WindowManager.CreateWindowAsync(viewPath);
browserWindow.WebContents.OnCrashed += async (killed) =>
{
var options = new MessageBoxOptions("This process has crashed.")
{
Type = MessageBoxType.info,
Title = "Renderer Process Crashed",
Buttons = new string[] { "Reload", "Close" }
};
var result = await Electron.Dialog.ShowMessageBoxAsync(options);
if (result.Response == 0)
{
browserWindow.Reload();
}
else
{
browserWindow.Close();
}
};
});
Electron.IpcMain.On("process-hang", async (args) =>
{
string viewPath = $"http://localhost:{BridgeSettings.WebPort}/crashhang/processhang";
var browserWindow = await Electron.WindowManager.CreateWindowAsync(viewPath);
browserWindow.OnUnresponsive += async () =>
{
var options = new MessageBoxOptions("This process is hanging.")
{
Type = MessageBoxType.info,
Title = "Renderer Process Hanging",
Buttons = new string[] { "Reload", "Close" }
};
var result = await Electron.Dialog.ShowMessageBoxAsync(options);
if (result.Response == 0)
{
browserWindow.Reload();
}
else
{
browserWindow.Close();
}
};
});
return View();
}
public IActionResult ProcessCrash()
{
return View();
}
public IActionResult ProcessHang()
{
return View();
}
}
}

View File

@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ElectronNET.API;
using ElectronNET.API.Entities;
namespace ElectronNET.WebApp.Controllers
{
public class DialogsController : Controller
{
public IActionResult Index()
{
Electron.IpcMain.On("select-directory", async (args) => {
var mainWindow = Electron.WindowManager.BrowserWindows.First();
var options = new OpenDialogOptions {
Properties = new OpenDialogProperty[] {
OpenDialogProperty.openFile,
OpenDialogProperty.openDirectory
}
};
string[] files = await Electron.Dialog.ShowOpenDialogAsync(mainWindow, options);
Electron.IpcMain.Send(mainWindow, "select-directory-reply", files);
});
Electron.IpcMain.On("error-dialog", (args) =>
{
Electron.Dialog.ShowErrorBox("An Error Message", "Demonstrating an error message.");
});
Electron.IpcMain.On("information-dialog", async (args) =>
{
var options = new MessageBoxOptions("This is an information dialog. Isn't it nice?")
{
Type = MessageBoxType.info,
Title = "Information",
Buttons = new string[] { "Yes", "No" }
};
var result = await Electron.Dialog.ShowMessageBoxAsync(options);
var mainWindow = Electron.WindowManager.BrowserWindows.First();
Electron.IpcMain.Send(mainWindow, "information-dialog-reply", result.Response);
});
Electron.IpcMain.On("save-dialog", async (args) =>
{
var mainWindow = Electron.WindowManager.BrowserWindows.First();
var options = new SaveDialogOptions
{
Title = "Save an Image",
Filters = new FileFilter[]
{
new FileFilter { Name = "Images", Extensions = new string[] {"jpg", "png", "gif" } }
}
};
var result = await Electron.Dialog.ShowSaveDialogAsync(mainWindow, options);
Electron.IpcMain.Send(mainWindow, "save-dialog-reply", result);
});
return View();
}
}
}

View File

@@ -1,12 +1,119 @@
using Microsoft.AspNetCore.Mvc;
using ElectronNET.API;
using ElectronNET.API.Entities;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
namespace ElectronNET.WebApp.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
{
var menu = new MenuItem[] {
new MenuItem { Label = "Edit", Submenu = new MenuItem[] {
new MenuItem { Label = "Undo", Accelerator = "CmdOrCtrl+Z", Role = MenuRole.undo },
new MenuItem { Label = "Redo", Accelerator = "Shift+CmdOrCtrl+Z", Role = MenuRole.redo },
new MenuItem { Type = MenuType.separator },
new MenuItem { Label = "Cut", Accelerator = "CmdOrCtrl+X", Role = MenuRole.cut },
new MenuItem { Label = "Copy", Accelerator = "CmdOrCtrl+C", Role = MenuRole.copy },
new MenuItem { Label = "Paste", Accelerator = "CmdOrCtrl+V", Role = MenuRole.paste },
new MenuItem { Label = "Select All", Accelerator = "CmdOrCtrl+A", Role = MenuRole.selectall }
}
},
new MenuItem { Label = "View", Submenu = new MenuItem[] {
new MenuItem
{
Label = "Reload",
Accelerator = "CmdOrCtrl+R",
Click = () =>
{
// on reload, start fresh and close any old
// open secondary windows
Electron.WindowManager.BrowserWindows.ToList().ForEach(browserWindow => {
if(browserWindow.Id != 1)
{
browserWindow.Close();
}
else
{
browserWindow.Reload();
}
});
}
},
new MenuItem
{
Label = "Toggle Full Screen",
Accelerator = "CmdOrCtrl+F",
Click = async () =>
{
bool isFullScreen = await Electron.WindowManager.BrowserWindows.First().IsFullScreenAsync();
Electron.WindowManager.BrowserWindows.First().SetFullScreen(!isFullScreen);
}
},
new MenuItem
{
Label = "Open Developer Tools",
Accelerator = "CmdOrCtrl+I",
Click = () => Electron.WindowManager.BrowserWindows.First().WebContents.OpenDevTools()
},
new MenuItem
{
Type = MenuType.separator
},
new MenuItem
{
Label = "App Menu Demo",
Click = async () => {
var options = new MessageBoxOptions("This demo is for the Menu section, showing how to create a clickable menu item in the application menu.");
options.Type = MessageBoxType.info;
options.Title = "Application Menu Demo";
await Electron.Dialog.ShowMessageBoxAsync(options);
}
}
}
},
new MenuItem { Label = "Window", Role = MenuRole.window, Submenu = new MenuItem[] {
new MenuItem { Label = "Minimize", Accelerator = "CmdOrCtrl+M", Role = MenuRole.minimize },
new MenuItem { Label = "Close", Accelerator = "CmdOrCtrl+W", Role = MenuRole.close }
}
},
new MenuItem { Label = "Help", Role = MenuRole.help, Submenu = new MenuItem[] {
new MenuItem
{
Label = "Learn More",
Click = async () => await Electron.Shell.OpenExternalAsync("https://github.com/ElectronNET")
}
}
}
};
Electron.Menu.SetApplicationMenu(menu);
CreateContextMenu();
return View();
}
private void CreateContextMenu()
{
var menu = new MenuItem[]
{
new MenuItem
{
Label = "Hello",
Click = async () => await Electron.Dialog.ShowMessageBoxAsync("Electron.NET rocks!")
},
new MenuItem { Type = MenuType.separator },
new MenuItem { Label = "Electron.NET", Type = MenuType.checkbox, Checked = true }
};
var mainWindow = Electron.WindowManager.BrowserWindows.First();
Electron.Menu.SetContextMenu(mainWindow, menu);
Electron.IpcMain.On("show-context-menu", (args) => {
Electron.Menu.ContextMenuPopup(mainWindow);
});
}
}
}

View File

@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Mvc;
using ElectronNET.API;
using System.Linq;
namespace ElectronNET.WebApp.Controllers
{
public class IpcController : Controller
{
public IActionResult Index()
{
Electron.IpcMain.On("async-msg", (args) =>
{
var mainWindow = Electron.WindowManager.BrowserWindows.First();
Electron.IpcMain.Send(mainWindow, "asynchronous-reply", "pong");
});
Electron.IpcMain.OnSync("sync-msg", (args) =>
{
return "pong";
});
return View();
}
}
}

View File

@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ElectronNET.API;
using ElectronNET.API.Entities;
namespace ElectronNET.WebApp.Controllers
{
public class WindowsController : Controller
{
public IActionResult Index()
{
string viewPath = $"http://localhost:{BridgeSettings.WebPort}/windows/demowindow";
Electron.IpcMain.On("new-window", async (args) => {
await Electron.WindowManager.CreateWindowAsync(viewPath);
});
Electron.IpcMain.On("manage-window", async (args) => {
var browserWindow = await Electron.WindowManager.CreateWindowAsync(viewPath);
browserWindow.OnMove += UpdateReply;
browserWindow.OnResize += UpdateReply;
});
Electron.IpcMain.On("listen-to-window", async (args) => {
var mainBrowserWindow = Electron.WindowManager.BrowserWindows.First();
var browserWindow = await Electron.WindowManager.CreateWindowAsync(viewPath);
browserWindow.OnFocus += () => Electron.IpcMain.Send(mainBrowserWindow, "listen-to-window-focus");
browserWindow.OnBlur += () => Electron.IpcMain.Send(mainBrowserWindow, "listen-to-window-blur");
Electron.IpcMain.On("listen-to-window-set-focus", (x) => browserWindow.Focus());
});
Electron.IpcMain.On("frameless-window", async (args) => {
var options = new BrowserWindowOptions
{
Frame = false
};
await Electron.WindowManager.CreateWindowAsync(options, viewPath);
});
return View();
}
private async void UpdateReply()
{
var browserWindow = Electron.WindowManager.BrowserWindows.Last();
var size = await browserWindow.GetSizeAsync();
var position = await browserWindow.GetPositionAsync();
string message = $"Size: {size[0]},{size[1]} Position: {position[0]},{position[1]}";
var mainWindow = Electron.WindowManager.BrowserWindows.First();
Electron.IpcMain.Send(mainWindow, "manage-window-reply", message);
}
public IActionResult DemoWindow()
{
return View();
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace ElectronNET.WebApp.Controllers
{
public class MenusController : Controller
{
public IActionResult Index()
{
return View();
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ElectronNET.API;
using ElectronNET.API.Entities;
namespace ElectronNET.WebApp.Controllers
{
public class NotificationsController : Controller
{
public IActionResult Index()
{
Electron.IpcMain.On("basic-noti", (args) => {
var options = new NotificationOptions("Basic Notification", "Short message part")
{
OnClick = async () => await Electron.Dialog.ShowMessageBoxAsync("Notification clicked")
};
Electron.Notification.Show(options);
});
Electron.IpcMain.On("advanced-noti", (args) => {
var options = new NotificationOptions("Notification with image", "Short message plus a custom image")
{
OnClick = async () => await Electron.Dialog.ShowMessageBoxAsync("Notification clicked"),
Icon = "/assets/img/programming.png"
};
Electron.Notification.Show(options);
});
return View();
}
}
}

View File

@@ -0,0 +1,27 @@
using ElectronNET.API;
using ElectronNET.API.Entities;
using Microsoft.AspNetCore.Mvc;
namespace ElectronNET.WebApp.Controllers
{
public class ShellController : Controller
{
public IActionResult Index()
{
Electron.IpcMain.On("open-file-manager", async (args) => {
string path = await Electron.App.GetPathAsync(PathName.home);
await Electron.Shell.ShowItemInFolderAsync(path);
});
Electron.IpcMain.On("open-ex-links", async (args) => {
await Electron.Shell.OpenExternalAsync("https://github.com/ElectronNET");
});
return View();
}
}
}

View File

@@ -0,0 +1,26 @@
using ElectronNET.API;
using ElectronNET.API.Entities;
using Microsoft.AspNetCore.Mvc;
namespace ElectronNET.WebApp.Controllers
{
public class ShortcutsController : Controller
{
public IActionResult Index()
{
Electron.GlobalShortcut.Register("CommandOrControl+Alt+K", async () => {
var options = new MessageBoxOptions("You pressed the registered global shortcut keybinding.")
{
Type = MessageBoxType.info,
Title = "Success!"
};
await Electron.Dialog.ShowMessageBoxAsync(options);
});
Electron.App.WillQuit += () => Electron.GlobalShortcut.UnregisterAll();
return View();
}
}
}

View File

@@ -0,0 +1,34 @@
using Microsoft.AspNetCore.Mvc;
using ElectronNET.API;
using ElectronNET.API.Entities;
namespace ElectronNET.WebApp.Controllers
{
public class TrayController : Controller
{
public IActionResult Index()
{
Electron.IpcMain.On("put-in-tray", (args) => {
if (Electron.Tray.Items.Count == 0)
{
var menu = new MenuItem
{
Label = "Remove",
Click = () => Electron.Tray.Destroy()
};
Electron.Tray.Show("/Assets/electron_32x32.png", menu);
Electron.Tray.SetToolTip("Electron Demo in the tray.");
}
else
{
Electron.Tray.Destroy();
}
});
return View();
}
}
}

View File

@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using ElectronNET.API;
using ElectronNET.API.Entities;