private async Task OpenNewAppInstance() { if (!await NotepadsProtocolService.LaunchProtocolAsync(NotepadsOperationProtocol.OpenNewInstance)) { Analytics.TrackEvent("FailedToOpenNewAppInstance"); } }
static void Main(string[] args) { var instances = AppInstance.GetInstances(); if (instances.Count == 0) { IsFirstInstance = true; ApplicationSettingsStore.Write(SettingsKey.ActiveInstanceIdStr, null); } IActivatedEventArgs activatedArgs = AppInstance.GetActivatedEventArgs(); if (activatedArgs is FileActivatedEventArgs) { RedirectOrCreateNewInstance(); } else if (activatedArgs is CommandLineActivatedEventArgs cmdActivatedArgs) { RedirectOrCreateNewInstance(); } else if (activatedArgs is ProtocolActivatedEventArgs protocolActivatedEventArgs) { LoggingService.LogInfo($"[Main] [ProtocolActivated] Protocol: {protocolActivatedEventArgs.Uri}"); var protocol = NotepadsProtocolService.GetOperationProtocol(protocolActivatedEventArgs.Uri, out var context); if (protocol == NotepadsOperationProtocol.OpenNewInstance) { OpenNewInstance(); } else { RedirectOrCreateNewInstance(); } } else if (activatedArgs is LaunchActivatedEventArgs launchActivatedEventArgs) { bool handled = false; if (!string.IsNullOrEmpty(launchActivatedEventArgs.Arguments)) { var protocol = NotepadsProtocolService.GetOperationProtocol(new Uri(launchActivatedEventArgs.Arguments), out var context); if (protocol == NotepadsOperationProtocol.OpenNewInstance) { handled = true; OpenNewInstance(); } } if (!handled) { RedirectOrCreateNewInstance(); } } else { RedirectOrCreateNewInstance(); } }
static void Main(string[] args) { #if DEBUG Task.Run(LoggingService.InitializeFileSystemLoggingAsync); #endif switch (AppInstance.GetActivatedEventArgs()) { case FileActivatedEventArgs _: case CommandLineActivatedEventArgs _: RedirectOrCreateNewInstance(); break; case ProtocolActivatedEventArgs protocolActivatedEventArgs: LoggingService.LogInfo($"[{nameof(Main)}] [ProtocolActivated] Protocol: {protocolActivatedEventArgs.Uri}"); var protocol = NotepadsProtocolService.GetOperationProtocol(protocolActivatedEventArgs.Uri, out _); if (protocol == NotepadsOperationProtocol.OpenNewInstance) { OpenNewInstance(); } else { RedirectOrCreateNewInstance(); } break; case LaunchActivatedEventArgs launchActivatedEventArgs: bool handled = false; if (!string.IsNullOrEmpty(launchActivatedEventArgs.Arguments)) { protocol = NotepadsProtocolService.GetOperationProtocol(new Uri(launchActivatedEventArgs.Arguments), out _); if (protocol == NotepadsOperationProtocol.OpenNewInstance) { handled = true; OpenNewInstance(); } } if (!handled) { RedirectOrCreateNewInstance(); } break; //case null: // // No activated event args, so this is not an activation via the multi-instance ID // // Just create a new instance and let App OnActivated resolve the launch // App.IsGameBarWidget = true; // App.IsPrimaryInstance = true; // Windows.UI.Xaml.Application.Start(p => new App()); // break; default: RedirectOrCreateNewInstance(); break; } }
private async void Sets_SetDraggedOutside(object sender, SetDraggedOutsideEventArgs e) { if (Sets.Items?.Count > 1 && e.Set.Content is ITextEditor textEditor) { // Only allow untitled empty document to be dragged outside for now if (!textEditor.IsModified && textEditor.EditingFile == null) { DeleteTextEditor(textEditor); await NotepadsProtocolService.LaunchProtocolAsync(NotepadsOperationProtocol.OpenNewInstance); } } }
static void Main(string[] args) { _instances = AppInstance.GetInstances(); if (_instances.Count == 0) { IsFirstInstance = true; ApplicationSettingsStore.Write("ActiveInstance", null); } IActivatedEventArgs activatedArgs = AppInstance.GetActivatedEventArgs(); if (activatedArgs is FileActivatedEventArgs fileArgs) { foreach (var file in fileArgs.Files) { if (!(file is StorageFile)) { continue; } AssignOrCreateInstanceForFile((file as StorageFile).Path); } } else if (activatedArgs is CommandLineActivatedEventArgs cmdActivatedArgs) { LoggingService.LogInfo($"[Main] [CommandActivated] CurrentDirectoryPath: {cmdActivatedArgs.Operation.CurrentDirectoryPath} Arguments: {cmdActivatedArgs.Operation.Arguments}"); var file = FileSystemUtility.GetAbsolutePathFromCommandLine( cmdActivatedArgs.Operation.CurrentDirectoryPath, cmdActivatedArgs.Operation.Arguments, App.ApplicationName); if (file != null) { AssignOrCreateInstanceForFile(file); } else { OpenNewInstance(); } } else if (activatedArgs is ProtocolActivatedEventArgs protocolActivatedEventArgs) { LoggingService.LogInfo($"[Main] [ProtocolActivated] Protocol: {protocolActivatedEventArgs.Uri}"); var protocol = NotepadsProtocolService.GetOperationProtocol(protocolActivatedEventArgs.Uri, out var context); if (protocol == NotepadsOperationProtocol.OpenNewInstance || protocol == NotepadsOperationProtocol.Unrecognized) { OpenNewInstance(); } } else { OpenNewInstance(); } }
static void Main(string[] args) { #if DEBUG Task.Run(LoggingService.InitializeFileSystemLoggingAsync); #endif IActivatedEventArgs activatedArgs = AppInstance.GetActivatedEventArgs(); //if (activatedArgs == null) //{ // // No activated event args, so this is not an activation via the multi-instance ID // // Just create a new instance and let App OnActivated resolve the launch // App.IsGameBarWidget = true; // App.IsFirstInstance = true; // Windows.UI.Xaml.Application.Start(p => new App()); //} var instances = AppInstance.GetInstances(); if (instances.Count == 0) { IsFirstInstance = true; ApplicationSettingsStore.Write(SettingsKey.ActiveInstanceIdStr, null); } if (activatedArgs is FileActivatedEventArgs) { RedirectOrCreateNewInstance(); } else if (activatedArgs is CommandLineActivatedEventArgs) { RedirectOrCreateNewInstance(); } else if (activatedArgs is ProtocolActivatedEventArgs protocolActivatedEventArgs) { LoggingService.LogInfo($"[{nameof(Main)}] [ProtocolActivated] Protocol: {protocolActivatedEventArgs.Uri}"); var protocol = NotepadsProtocolService.GetOperationProtocol(protocolActivatedEventArgs.Uri, out _); if (protocol == NotepadsOperationProtocol.OpenNewInstance) { OpenNewInstance(); } else { RedirectOrCreateNewInstance(); } } else if (activatedArgs is LaunchActivatedEventArgs launchActivatedEventArgs) { bool handled = false; if (!string.IsNullOrEmpty(launchActivatedEventArgs.Arguments)) { var protocol = NotepadsProtocolService.GetOperationProtocol(new Uri(launchActivatedEventArgs.Arguments), out _); if (protocol == NotepadsOperationProtocol.OpenNewInstance) { handled = true; OpenNewInstance(); } } if (!handled) { RedirectOrCreateNewInstance(); } } else { RedirectOrCreateNewInstance(); } }
// App should wait for Sets fully loaded before opening files requested by user (by click or from cmd) // Open files from external links or cmd args on Sets Loaded private async void Sets_Loaded(object sender, RoutedEventArgs e) { int loadedCount = 0; if (!_loaded && EditorSettingsService.IsSessionSnapshotEnabled) { loadedCount = await SessionManager.LoadLastSessionAsync(); } if (_appLaunchFiles != null && _appLaunchFiles.Count > 0) { loadedCount += await OpenFiles(_appLaunchFiles); _appLaunchFiles = null; } else if (_appLaunchCmdDir != null) { var file = await FileSystemUtility.OpenFileFromCommandLine(_appLaunchCmdDir, _appLaunchCmdArgs); if (file != null && await OpenFile(file)) { loadedCount++; } _appLaunchCmdDir = null; _appLaunchCmdArgs = null; } else if (_appLaunchUri != null) { var operation = NotepadsProtocolService.GetOperationProtocol(_appLaunchUri, out var context); if (operation == NotepadsOperationProtocol.OpenNewInstance || operation == NotepadsOperationProtocol.Unrecognized) { // Do nothing } _appLaunchUri = null; } if (!_loaded) { if (loadedCount == 0) { NotepadsCore.OpenNewTextEditor(); } if (!App.IsFirstInstance) { NotificationCenter.Instance.PostNotification("This is a shadow instance of Notepads. Session snapshot and settings are disabled.", 4000); //(_resourceLoader.GetString("TextEditor_ShadowInstanceIndicator_Description"), 4000); } _loaded = true; } if (EditorSettingsService.IsSessionSnapshotEnabled) { SessionManager.IsBackupEnabled = true; SessionManager.StartSessionBackup(); } Window.Current.CoreWindow.Activated -= CoreWindow_Activated; Window.Current.CoreWindow.Activated += CoreWindow_Activated; Application.Current.EnteredBackground -= App_EnteredBackground; Application.Current.EnteredBackground += App_EnteredBackground; }
// App should wait for Sets fully loaded before opening files requested by user (by click or from cmd) // Open files from external links or cmd args on Sets Loaded private async void Sets_Loaded(object sender, RoutedEventArgs e) { int loadedCount = 0; if (!_loaded && EditorSettingsService.IsSessionSnapshotEnabled) { try { loadedCount = await SessionManager.LoadLastSessionAsync(); } catch (Exception ex) { LoggingService.LogError($"[{nameof(NotepadsMainPage)}] Failed to load last session: {ex}"); Analytics.TrackEvent("FailedToLoadLastSession", new Dictionary <string, string> { { "Exception", ex.ToString() } }); } } if (_appLaunchFiles != null && _appLaunchFiles.Count > 0) { loadedCount += await OpenFiles(_appLaunchFiles); _appLaunchFiles = null; } else if (_appLaunchCmdDir != null) { var file = await FileSystemUtility.OpenFileFromCommandLine(_appLaunchCmdDir, _appLaunchCmdArgs); if (file != null && await OpenFile(file)) { loadedCount++; } _appLaunchCmdDir = null; _appLaunchCmdArgs = null; } else if (_appLaunchUri != null) { var operation = NotepadsProtocolService.GetOperationProtocol(_appLaunchUri, out var context); if (operation == NotepadsOperationProtocol.OpenNewInstance || operation == NotepadsOperationProtocol.Unrecognized) { // Do nothing } _appLaunchUri = null; } if (!_loaded) { if (loadedCount == 0) { NotepadsCore.OpenNewTextEditor(_defaultNewFileName); } if (!App.IsFirstInstance) { NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("App_ShadowWindowIndicator_Description"), 4000); } _loaded = true; } if (EditorSettingsService.IsSessionSnapshotEnabled) { SessionManager.IsBackupEnabled = true; SessionManager.StartSessionBackup(); } await BuildOpenRecentButtonSubItems(); if (!App.IsGameBarWidget) { // An issue with the Game Bar extension model and Windows platform prevents the Notepads process from exiting cleanly // when more than one CoreWindow has been created, and NotepadsMainPage is the last to close. The common case for this // is to open Notepads in Game Bar, then open its settings, then close the settings and finally close Notepads. // This puts the process in a bad state where it will no longer open in Game Bar and the Notepads process is orphaned. // To work around this do not use the EnteredBackground event when running as a widget. // Microsoft is tracking this issue as VSO#25735260 Application.Current.EnteredBackground -= App_EnteredBackground; Application.Current.EnteredBackground += App_EnteredBackground; Window.Current.CoreWindow.Activated -= CoreWindow_Activated; Window.Current.CoreWindow.Activated += CoreWindow_Activated; } }
// App should wait for Sets fully loaded before opening files requested by user (by click or from cmd) // Open files from external links or cmd args on Sets Loaded private async void Sets_Loaded(object sender, RoutedEventArgs e) { int loadedCount = 0; if (!_loaded && EditorSettingsService.IsSessionSnapshotEnabled) { try { loadedCount = await SessionManager.LoadLastSessionAsync(); } catch (Exception ex) { LoggingService.LogError($"[SessionManager] Failed to LoadLastSessionAsync: {ex}"); Analytics.TrackEvent("FailedToLoadLastSession", new Dictionary <string, string> { { "Exception", ex.ToString() } }); } } if (_appLaunchFiles != null && _appLaunchFiles.Count > 0) { loadedCount += await OpenFiles(_appLaunchFiles); _appLaunchFiles = null; } else if (_appLaunchCmdDir != null) { var file = await FileSystemUtility.OpenFileFromCommandLine(_appLaunchCmdDir, _appLaunchCmdArgs); if (file != null && await OpenFile(file)) { loadedCount++; } _appLaunchCmdDir = null; _appLaunchCmdArgs = null; } else if (_appLaunchUri != null) { var operation = NotepadsProtocolService.GetOperationProtocol(_appLaunchUri, out var context); if (operation == NotepadsOperationProtocol.OpenNewInstance || operation == NotepadsOperationProtocol.Unrecognized) { // Do nothing } _appLaunchUri = null; } if (!_loaded) { if (loadedCount == 0) { NotepadsCore.OpenNewTextEditor(_defaultNewFileName); } if (!App.IsFirstInstance) { NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("App_ShadowWindowIndicator_Description"), 4000); } _loaded = true; } if (EditorSettingsService.IsSessionSnapshotEnabled) { SessionManager.IsBackupEnabled = true; SessionManager.StartSessionBackup(); } await BuildOpenRecentButtonSubItems(); Window.Current.CoreWindow.Activated -= CoreWindow_Activated; Window.Current.CoreWindow.Activated += CoreWindow_Activated; Application.Current.EnteredBackground -= App_EnteredBackground; Application.Current.EnteredBackground += App_EnteredBackground; }