// Make this app a single instance app. // // The main window will be restored and focused instead of a second window // opened when a person attempts to launch a second instance. // // Returns true if the current version of the app should quit instead of // launching. async Task <bool> MakeSingleInstance() { return(await app.MakeSingleInstance(new ScriptObjectCallback <string[], string>( async(cr) => { var state = cr.CallbackState as object[]; if (state != null) { // let's get the arguments that we were called with var args = state[0] as object[]; await console.Log($"we have {args?.Length} args "); foreach (string arg in args) { await console.Log($" arg: {arg}"); } if (state[1] != null) { await console.Log($"working directory: {state[1]}"); } } if (mainWindow != null) { if (await mainWindow.IsMinimized()) { await mainWindow.Restore(); } await mainWindow.Focus(); } } ))); }
public void Show() { if (_started) { if (_mainWindow == null || Electron.WindowManager.BrowserWindows.Count == 0) { Electron.GlobalShortcut.UnregisterAll(); Electron.App.Relaunch(); Electron.App.Exit(); return; } _mainWindow?.Show(); _mainWindow?.Focus(); } }
async Task CreateTray(string __dirname) { if (IsWindows) { tray = await Tray.Create($"{__dirname}/assets/icons/appicon.ico"); } else { tray = await Tray.Create($"{__dirname}/assets/icons/[email protected]"); } // Sets the hover text for this tray icon. await tray.SetToolTip("MinimizeToTray Sample Program"); // Sets the title displayed aside of the tray icon in the status bar. //await tray.SetTitle("MinimizeToTray"); // Create a context menu to be displayed on the Tray var menuItemOptions = new MenuItemOptions[] { new MenuItemOptions() { Label = "Show App", Click = new ScriptObjectCallback <MenuItem, BrowserWindow, Event> ( async(ar) => { //await console.Log("Show App"); if (mainWindow != null) { if (await mainWindow.IsMinimized()) { await mainWindow.Restore(); } if (!await mainWindow.IsVisible()) { await mainWindow.Show(); } await mainWindow.Focus(); } } ) }, new MenuItemOptions() { Label = "Quit App", Click = new ScriptObjectCallback <MenuItem, BrowserWindow, Event> ( async(ar) => { //await console.Log("Quit App"); // Notify the close event that we will be quitting the app IsShouldQuit = true; mainWindow = null; await app.Quit(); } ) }, }; var contextMenu = await Menu.BuildFromTemplate(menuItemOptions); // Set our tray context menu await tray.SetContextMenu(contextMenu); // Allow the user to click on the tray icon to show the context menu. await tray.On("click", new ScriptObjectCallback ( async(sr) => { if (mainWindow != null) { await tray.PopUpContextMenu(); } }) ); }