diff --git a/ElectronNET.API/BridgeConnector.cs b/ElectronNET.API/BridgeConnector.cs index bac492d..e479e05 100644 --- a/ElectronNET.API/BridgeConnector.cs +++ b/ElectronNET.API/BridgeConnector.cs @@ -5,15 +5,27 @@ namespace ElectronNET.API { internal static class BridgeConnector { - public static Socket Socket; + private static Socket _socket; - public static void StartConnection() + public static Socket Socket { - Socket = IO.Socket("http://localhost:" + BridgeSettings.SocketPort); - Socket.On(Socket.EVENT_CONNECT, () => + get { - Console.WriteLine("BridgeConnector connected!"); - }); + if(_socket == null && HybridSupport.IsElectronActive) + { + _socket = IO.Socket("http://localhost:" + BridgeSettings.SocketPort); + _socket.On(Socket.EVENT_CONNECT, () => + { + Console.WriteLine("BridgeConnector connected!"); + }); + } + else if(_socket == null && !HybridSupport.IsElectronActive) + { + _socket = IO.Socket(new Uri("http://localhost"), new IO.Options { AutoConnect = false }); + } + + return _socket; + } } } } diff --git a/ElectronNET.API/HybridSupport.cs b/ElectronNET.API/HybridSupport.cs new file mode 100644 index 0000000..26252e4 --- /dev/null +++ b/ElectronNET.API/HybridSupport.cs @@ -0,0 +1,13 @@ +namespace ElectronNET.API +{ + public static class HybridSupport + { + public static bool IsElectronActive + { + get + { + return !string.IsNullOrEmpty(BridgeSettings.SocketPort); + } + } + } +} \ No newline at end of file diff --git a/ElectronNET.API/WebHostBuilderExtensions.cs b/ElectronNET.API/WebHostBuilderExtensions.cs index 0409f6b..226f0f3 100644 --- a/ElectronNET.API/WebHostBuilderExtensions.cs +++ b/ElectronNET.API/WebHostBuilderExtensions.cs @@ -19,20 +19,13 @@ namespace ElectronNET.API } } - if(IsElectronActive()) + if(HybridSupport.IsElectronActive) { builder.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory) .UseUrls("http://0.0.0.0:" + BridgeSettings.WebPort); - - BridgeConnector.StartConnection(); } return builder; } - - private static bool IsElectronActive() - { - return !string.IsNullOrEmpty(BridgeSettings.SocketPort); - } } } diff --git a/ElectronNET.WebApp/Controllers/AppSysInformationController.cs b/ElectronNET.WebApp/Controllers/AppSysInformationController.cs index 51240e1..df4b6c8 100644 --- a/ElectronNET.WebApp/Controllers/AppSysInformationController.cs +++ b/ElectronNET.WebApp/Controllers/AppSysInformationController.cs @@ -9,29 +9,32 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - Electron.IpcMain.On("app-info", async (args) => + if(HybridSupport.IsElectronActive) { - string appPath = await Electron.App.GetAppPathAsync(); + Electron.IpcMain.On("app-info", async (args) => + { + string appPath = await Electron.App.GetAppPathAsync(); - var mainWindow = Electron.WindowManager.BrowserWindows.First(); - Electron.IpcMain.Send(mainWindow, "got-app-path", appPath); - }); + var mainWindow = Electron.WindowManager.BrowserWindows.First(); + Electron.IpcMain.Send(mainWindow, "got-app-path", appPath); + }); - Electron.IpcMain.On("sys-info", async (args) => - { - string homePath = await Electron.App.GetPathAsync(PathName.home); + Electron.IpcMain.On("sys-info", async (args) => + { + string homePath = await Electron.App.GetPathAsync(PathName.home); - var mainWindow = Electron.WindowManager.BrowserWindows.First(); - Electron.IpcMain.Send(mainWindow, "got-sys-info", homePath); - }); + var mainWindow = Electron.WindowManager.BrowserWindows.First(); + Electron.IpcMain.Send(mainWindow, "got-sys-info", homePath); + }); - Electron.IpcMain.On("screen-info", async (args) => - { - var display = await Electron.Screen.GetPrimaryDisplayAsync(); + Electron.IpcMain.On("screen-info", async (args) => + { + var display = await Electron.Screen.GetPrimaryDisplayAsync(); - var mainWindow = Electron.WindowManager.BrowserWindows.First(); - Electron.IpcMain.Send(mainWindow, "got-screen-info", display.Size); - }); + var mainWindow = Electron.WindowManager.BrowserWindows.First(); + Electron.IpcMain.Send(mainWindow, "got-screen-info", display.Size); + }); + } return View(); } diff --git a/ElectronNET.WebApp/Controllers/ClipboardController.cs b/ElectronNET.WebApp/Controllers/ClipboardController.cs index 17ccc3f..27f8d9e 100644 --- a/ElectronNET.WebApp/Controllers/ClipboardController.cs +++ b/ElectronNET.WebApp/Controllers/ClipboardController.cs @@ -8,19 +8,22 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - Electron.IpcMain.On("copy-to", (text) => + if (HybridSupport.IsElectronActive) { - Electron.Clipboard.WriteText(text.ToString()); - }); + Electron.IpcMain.On("copy-to", (text) => + { + Electron.Clipboard.WriteText(text.ToString()); + }); - Electron.IpcMain.On("paste-to", async (text) => - { - Electron.Clipboard.WriteText(text.ToString()); - string pasteText = await Electron.Clipboard.ReadTextAsync(); + Electron.IpcMain.On("paste-to", async (text) => + { + Electron.Clipboard.WriteText(text.ToString()); + string pasteText = await Electron.Clipboard.ReadTextAsync(); - var mainWindow = Electron.WindowManager.BrowserWindows.First(); - Electron.IpcMain.Send(mainWindow, "paste-from", pasteText); - }); + var mainWindow = Electron.WindowManager.BrowserWindows.First(); + Electron.IpcMain.Send(mainWindow, "paste-from", pasteText); + }); + } return View(); } diff --git a/ElectronNET.WebApp/Controllers/CrashHangController.cs b/ElectronNET.WebApp/Controllers/CrashHangController.cs index eae05e2..6df56ea 100644 --- a/ElectronNET.WebApp/Controllers/CrashHangController.cs +++ b/ElectronNET.WebApp/Controllers/CrashHangController.cs @@ -8,57 +8,60 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - Electron.IpcMain.On("process-crash", async (args) => + if (HybridSupport.IsElectronActive) { - string viewPath = $"http://localhost:{BridgeSettings.WebPort}/crashhang/processcrash"; - - var browserWindow = await Electron.WindowManager.CreateWindowAsync(viewPath); - browserWindow.WebContents.OnCrashed += async (killed) => + Electron.IpcMain.On("process-crash", async (args) => { - var options = new MessageBoxOptions("This process has crashed.") + string viewPath = $"http://localhost:{BridgeSettings.WebPort}/crashhang/processcrash"; + + var browserWindow = await Electron.WindowManager.CreateWindowAsync(viewPath); + browserWindow.WebContents.OnCrashed += async (killed) => { - Type = MessageBoxType.info, - Title = "Renderer Process Crashed", - Buttons = new string[] { "Reload", "Close" } + 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(); + } }; - 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 () => + Electron.IpcMain.On("process-hang", async (args) => { - 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); + string viewPath = $"http://localhost:{BridgeSettings.WebPort}/crashhang/processhang"; - if (result.Response == 0) + var browserWindow = await Electron.WindowManager.CreateWindowAsync(viewPath); + browserWindow.OnUnresponsive += async () => { - browserWindow.Reload(); - } - else - { - browserWindow.Close(); - } - }; - }); + 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(); } diff --git a/ElectronNET.WebApp/Controllers/DialogsController.cs b/ElectronNET.WebApp/Controllers/DialogsController.cs index 02a09c1..419e800 100644 --- a/ElectronNET.WebApp/Controllers/DialogsController.cs +++ b/ElectronNET.WebApp/Controllers/DialogsController.cs @@ -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; @@ -12,54 +9,58 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - Electron.IpcMain.On("select-directory", async (args) => { - var mainWindow = Electron.WindowManager.BrowserWindows.First(); - var options = new OpenDialogOptions { - Properties = new OpenDialogProperty[] { + if(HybridSupport.IsElectronActive) + { + 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); - }); + 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?") + Electron.IpcMain.On("error-dialog", (args) => { - Type = MessageBoxType.info, - Title = "Information", - Buttons = new string[] { "Yes", "No" } - }; + Electron.Dialog.ShowErrorBox("An Error Message", "Demonstrating an error message."); + }); - 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 + Electron.IpcMain.On("information-dialog", async (args) => { - Title = "Save an Image", - Filters = new FileFilter[] + var options = new MessageBoxOptions("This is an information dialog. Isn't it nice?") { - new FileFilter { Name = "Images", Extensions = new string[] {"jpg", "png", "gif" } } - } - }; + Type = MessageBoxType.info, + Title = "Information", + Buttons = new string[] { "Yes", "No" } + }; - var result = await Electron.Dialog.ShowSaveDialogAsync(mainWindow, options); - Electron.IpcMain.Send(mainWindow, "save-dialog-reply", result); - }); + 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(); } diff --git a/ElectronNET.WebApp/Controllers/IpcController.cs b/ElectronNET.WebApp/Controllers/IpcController.cs index 62889ee..2f64177 100644 --- a/ElectronNET.WebApp/Controllers/IpcController.cs +++ b/ElectronNET.WebApp/Controllers/IpcController.cs @@ -8,16 +8,19 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - Electron.IpcMain.On("async-msg", (args) => + if(HybridSupport.IsElectronActive) { - var mainWindow = Electron.WindowManager.BrowserWindows.First(); - Electron.IpcMain.Send(mainWindow, "asynchronous-reply", "pong"); - }); + 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"; - }); + Electron.IpcMain.OnSync("sync-msg", (args) => + { + return "pong"; + }); + } return View(); } diff --git a/ElectronNET.WebApp/Controllers/MenusController.cs b/ElectronNET.WebApp/Controllers/MenusController.cs index 7d0fd31..d991e9a 100644 --- a/ElectronNET.WebApp/Controllers/MenusController.cs +++ b/ElectronNET.WebApp/Controllers/MenusController.cs @@ -9,7 +9,9 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - var menu = new MenuItem[] { + if (HybridSupport.IsElectronActive) + { + 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 }, @@ -88,9 +90,10 @@ namespace ElectronNET.WebApp.Controllers } }; - Electron.Menu.SetApplicationMenu(menu); + Electron.Menu.SetApplicationMenu(menu); - CreateContextMenu(); + CreateContextMenu(); + } return View(); } @@ -111,7 +114,8 @@ namespace ElectronNET.WebApp.Controllers var mainWindow = Electron.WindowManager.BrowserWindows.First(); Electron.Menu.SetContextMenu(mainWindow, menu); - Electron.IpcMain.On("show-context-menu", (args) => { + Electron.IpcMain.On("show-context-menu", (args) => + { Electron.Menu.ContextMenuPopup(mainWindow); }); } diff --git a/ElectronNET.WebApp/Controllers/NotificationsController.cs b/ElectronNET.WebApp/Controllers/NotificationsController.cs index 8a112ce..ba41e35 100644 --- a/ElectronNET.WebApp/Controllers/NotificationsController.cs +++ b/ElectronNET.WebApp/Controllers/NotificationsController.cs @@ -12,27 +12,30 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - Electron.IpcMain.On("basic-noti", (args) => { + if(HybridSupport.IsElectronActive) + { + Electron.IpcMain.On("basic-noti", (args) => { - var options = new NotificationOptions("Basic Notification", "Short message part") - { - OnClick = async () => await Electron.Dialog.ShowMessageBoxAsync("Notification clicked") - }; + var options = new NotificationOptions("Basic Notification", "Short message part") + { + OnClick = async () => await Electron.Dialog.ShowMessageBoxAsync("Notification clicked") + }; - Electron.Notification.Show(options); + Electron.Notification.Show(options); - }); + }); - Electron.IpcMain.On("advanced-noti", (args) => { + 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" - }; + 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); - }); + Electron.Notification.Show(options); + }); + } return View(); } diff --git a/ElectronNET.WebApp/Controllers/PdfController.cs b/ElectronNET.WebApp/Controllers/PdfController.cs index 59ca46c..18a4a1a 100644 --- a/ElectronNET.WebApp/Controllers/PdfController.cs +++ b/ElectronNET.WebApp/Controllers/PdfController.cs @@ -9,29 +9,33 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - Electron.IpcMain.On("print-pdf", async (args) => { - BrowserWindow mainWindow = Electron.WindowManager.BrowserWindows.First(); - - var saveOptions = new SaveDialogOptions + if (HybridSupport.IsElectronActive) + { + Electron.IpcMain.On("print-pdf", async (args) => { - Title = "Save an PDF-File", - DefaultPath = await Electron.App.GetPathAsync(PathName.documents), - Filters = new FileFilter[] + BrowserWindow mainWindow = Electron.WindowManager.BrowserWindows.First(); + + var saveOptions = new SaveDialogOptions { + Title = "Save an PDF-File", + DefaultPath = await Electron.App.GetPathAsync(PathName.documents), + Filters = new FileFilter[] + { new FileFilter { Name = "PDF", Extensions = new string[] { "pdf" } } - } - }; - var path = await Electron.Dialog.ShowSaveDialogAsync(mainWindow, saveOptions); + } + }; + var path = await Electron.Dialog.ShowSaveDialogAsync(mainWindow, saveOptions); - if (await mainWindow.WebContents.PrintToPDFAsync(path)) - { - await Electron.Shell.OpenExternalAsync("file://" + path); - } - else - { - Electron.Dialog.ShowErrorBox("Error", "Failed to create pdf file."); - } - }); + if (await mainWindow.WebContents.PrintToPDFAsync(path)) + { + await Electron.Shell.OpenExternalAsync("file://" + path); + } + else + { + Electron.Dialog.ShowErrorBox("Error", "Failed to create pdf file."); + } + }); + } return View(); } diff --git a/ElectronNET.WebApp/Controllers/ShellController.cs b/ElectronNET.WebApp/Controllers/ShellController.cs index bd59553..d2c361d 100644 --- a/ElectronNET.WebApp/Controllers/ShellController.cs +++ b/ElectronNET.WebApp/Controllers/ShellController.cs @@ -8,18 +8,20 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - Electron.IpcMain.On("open-file-manager", async (args) => { + if (HybridSupport.IsElectronActive) + { + Electron.IpcMain.On("open-file-manager", async (args) => + { + string path = await Electron.App.GetPathAsync(PathName.home); + await Electron.Shell.ShowItemInFolderAsync(path); - 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"); - - }); + Electron.IpcMain.On("open-ex-links", async (args) => + { + await Electron.Shell.OpenExternalAsync("https://github.com/ElectronNET"); + }); + } return View(); } diff --git a/ElectronNET.WebApp/Controllers/ShortcutsController.cs b/ElectronNET.WebApp/Controllers/ShortcutsController.cs index 7f0e83a..80a5628 100644 --- a/ElectronNET.WebApp/Controllers/ShortcutsController.cs +++ b/ElectronNET.WebApp/Controllers/ShortcutsController.cs @@ -8,17 +8,21 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - Electron.GlobalShortcut.Register("CommandOrControl+Alt+K", async () => { - var options = new MessageBoxOptions("You pressed the registered global shortcut keybinding.") + if (HybridSupport.IsElectronActive) + { + Electron.GlobalShortcut.Register("CommandOrControl+Alt+K", async () => { - Type = MessageBoxType.info, - Title = "Success!" - }; + var options = new MessageBoxOptions("You pressed the registered global shortcut keybinding.") + { + Type = MessageBoxType.info, + Title = "Success!" + }; - await Electron.Dialog.ShowMessageBoxAsync(options); - }); + await Electron.Dialog.ShowMessageBoxAsync(options); + }); - Electron.App.WillQuit += () => Electron.GlobalShortcut.UnregisterAll(); + Electron.App.WillQuit += () => Electron.GlobalShortcut.UnregisterAll(); + } return View(); } diff --git a/ElectronNET.WebApp/Controllers/TrayController.cs b/ElectronNET.WebApp/Controllers/TrayController.cs index 7166276..a723025 100644 --- a/ElectronNET.WebApp/Controllers/TrayController.cs +++ b/ElectronNET.WebApp/Controllers/TrayController.cs @@ -8,25 +8,29 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - Electron.IpcMain.On("put-in-tray", (args) => { - - if (Electron.Tray.Items.Count == 0) + if (HybridSupport.IsElectronActive) + { + Electron.IpcMain.On("put-in-tray", (args) => { - var menu = new MenuItem + + if (Electron.Tray.Items.Count == 0) { - Label = "Remove", - Click = () => Electron.Tray.Destroy() - }; + 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(); - } + Electron.Tray.Show("/Assets/electron_32x32.png", menu); + Electron.Tray.SetToolTip("Electron Demo in the tray."); + } + else + { + Electron.Tray.Destroy(); + } - }); + }); + } return View(); } diff --git a/ElectronNET.WebApp/Controllers/WindowsController.cs b/ElectronNET.WebApp/Controllers/WindowsController.cs index 8d7fac8..a28888b 100644 --- a/ElectronNET.WebApp/Controllers/WindowsController.cs +++ b/ElectronNET.WebApp/Controllers/WindowsController.cs @@ -9,38 +9,45 @@ namespace ElectronNET.WebApp.Controllers { public IActionResult Index() { - string viewPath = $"http://localhost:{BridgeSettings.WebPort}/windows/demowindow"; + if (HybridSupport.IsElectronActive) + { + 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 + Electron.IpcMain.On("new-window", async (args) => { - Frame = false - }; - await Electron.WindowManager.CreateWindowAsync(options, viewPath); - }); + + 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(); } diff --git a/ElectronNET.WebApp/Properties/launchSettings.json b/ElectronNET.WebApp/Properties/launchSettings.json index 66a2abe..548e6c4 100644 --- a/ElectronNET.WebApp/Properties/launchSettings.json +++ b/ElectronNET.WebApp/Properties/launchSettings.json @@ -17,7 +17,6 @@ }, "ElectronNET.WebApp": { "commandName": "Project", - "commandLineArgs": "/electronPort=3000", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/ElectronNET.WebApp/Startup.cs b/ElectronNET.WebApp/Startup.cs index bbae832..ad9a68c 100644 --- a/ElectronNET.WebApp/Startup.cs +++ b/ElectronNET.WebApp/Startup.cs @@ -35,7 +35,11 @@ namespace ElectronNET.WebApp template: "{controller=Home}/{action=Index}/{id?}"); }); - ElectronBootstrap(); + + if(HybridSupport.IsElectronActive) + { + ElectronBootstrap(); + } } public async void ElectronBootstrap()