Skip to content

Commit

Permalink
Fix issue which makes a prelaunched instance primary instance (#804)
Browse files Browse the repository at this point in the history
Co-authored-by: Jiaqi Liu <[email protected]>
  • Loading branch information
soumyamahunt and 0x7c13 authored Mar 23, 2021
1 parent e520196 commit 037e739
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 107 deletions.
66 changes: 51 additions & 15 deletions src/Notepads/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,27 @@ sealed partial class App : Application

public static Guid Id { get; } = Guid.NewGuid();

public static bool IsPrimaryInstance = false;
public static event EventHandler<bool> OnInstanceTypeChanged;

private static bool _isPrimaryInstance = false;
public static bool IsPrimaryInstance
{
get => _isPrimaryInstance;
set
{
if (value != _isPrimaryInstance)
{
_isPrimaryInstance = value;
OnInstanceTypeChanged?.Invoke(null, value);
}
}
}

public static bool IsGameBarWidget = false;

private const string AppCenterSecret = null;

public static Mutex InstanceHandlerMutex { get; set; }
private static Mutex InstanceHandlerMutex = null;

/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
Expand All @@ -47,19 +62,6 @@ public App()
var services = new Type[] { typeof(Crashes), typeof(Analytics) };
AppCenter.Start(AppCenterSecret, services);

InstanceHandlerMutex = new Mutex(true, App.ApplicationName, out bool isNew);
if (isNew)
{
IsPrimaryInstance = true;
ApplicationSettingsStore.Write(SettingsKey.ActiveInstanceIdStr, null);
}
else
{
InstanceHandlerMutex.Close();
}

LoggingService.LogInfo($"[{nameof(App)}] Started: Instance = {Id} IsPrimaryInstance: {IsPrimaryInstance} IsGameBarWidget: {IsGameBarWidget}.");

ApplicationSettingsStore.Write(SettingsKey.ActiveInstanceIdStr, App.Id.ToString());

InitializeComponent();
Expand All @@ -75,6 +77,7 @@ public App()
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
await ActivateAsync(e);
base.OnLaunched(e);
}

protected override async void OnFileActivated(FileActivatedEventArgs args)
Expand All @@ -91,6 +94,11 @@ protected override async void OnActivated(IActivatedEventArgs args)

private async Task ActivateAsync(IActivatedEventArgs e)
{
if (!(e is LaunchActivatedEventArgs args && args.PrelaunchActivated))
{
InitializeInstance();
}

bool rootFrameCreated = false;

if (!(Window.Current.Content is Frame rootFrame))
Expand Down Expand Up @@ -290,6 +298,34 @@ private static void ExtendViewIntoTitleBar()
}
}

public static void InitializeInstance()
{
if (InstanceHandlerMutex == null)
{
InstanceHandlerMutex = new Mutex(true, App.ApplicationName, out bool createdNew);

if (createdNew)
{
IsPrimaryInstance = true;
ApplicationSettingsStore.Write(SettingsKey.ActiveInstanceIdStr, null);
}
else
{
InstanceHandlerMutex?.Close();
}

LoggingService.LogInfo(
$"[{nameof(App)}] Started: Instance = {Id} " +
$"IsPrimaryInstance: {IsPrimaryInstance} " +
$"IsGameBarWidget: {IsGameBarWidget}.");
}
}

public static void Dispose()
{
InstanceHandlerMutex?.Dispose();
}

//private static void UpdateAppVersion()
//{
// var packageVer = Package.Current.Id.Version;
Expand Down
94 changes: 42 additions & 52 deletions src/Notepads/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,51 @@ static void Main(string[] args)
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.IsPrimaryInstance = true;
// Windows.UI.Xaml.Application.Start(p => new App());
//}

if (activatedArgs is FileActivatedEventArgs)
{
RedirectOrCreateNewInstance();
}
else if (activatedArgs is CommandLineActivatedEventArgs)
switch (AppInstance.GetActivatedEventArgs())
{
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
{
case FileActivatedEventArgs _:
case CommandLineActivatedEventArgs _:
RedirectOrCreateNewInstance();
}
}
else if (activatedArgs is LaunchActivatedEventArgs launchActivatedEventArgs)
{
bool handled = false;

if (!string.IsNullOrEmpty(launchActivatedEventArgs.Arguments))
{
var protocol = NotepadsProtocolService.GetOperationProtocol(new Uri(launchActivatedEventArgs.Arguments), out _);
break;
case ProtocolActivatedEventArgs protocolActivatedEventArgs:
LoggingService.LogInfo($"[{nameof(Main)}] [ProtocolActivated] Protocol: {protocolActivatedEventArgs.Uri}");
var protocol = NotepadsProtocolService.GetOperationProtocol(protocolActivatedEventArgs.Uri, out _);
if (protocol == NotepadsOperationProtocol.OpenNewInstance)
{
handled = true;
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)
{
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();
}
}
else
{
RedirectOrCreateNewInstance();
break;
}
}

Expand Down Expand Up @@ -105,13 +96,12 @@ private static AppInstance GetLastActiveInstance()
{
var instances = AppInstance.GetInstances();

if (instances.Count == 0)
{
return null;
}
else if (instances.Count == 1)
switch (instances.Count)
{
return instances.FirstOrDefault();
case 0:
return null;
case 1:
return instances.FirstOrDefault();
}

if (!(ApplicationSettingsStore.Read(SettingsKey.ActiveInstanceIdStr) is string activeInstance))
Expand Down
40 changes: 19 additions & 21 deletions src/Notepads/Services/ActivationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,26 @@ public static class ActivationService
{
public static async Task ActivateAsync(Frame rootFrame, IActivatedEventArgs e)
{
if (e is ProtocolActivatedEventArgs protocolActivatedEventArgs)
switch (e)
{
ProtocolActivated(rootFrame, protocolActivatedEventArgs);
}
else if (e is FileActivatedEventArgs fileActivatedEventArgs)
{
await FileActivated(rootFrame, fileActivatedEventArgs);
}
else if (e is CommandLineActivatedEventArgs commandLineActivatedEventArgs)
{
await CommandActivated(rootFrame, commandLineActivatedEventArgs);
}
else if (e is LaunchActivatedEventArgs launchActivatedEventArgs)
{
LaunchActivated(rootFrame, launchActivatedEventArgs);
}
else // For other types of activated events
{
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(NotepadsMainPage));
}
case ProtocolActivatedEventArgs protocolActivatedEventArgs:
ProtocolActivated(rootFrame, protocolActivatedEventArgs);
break;
case FileActivatedEventArgs fileActivatedEventArgs:
await FileActivated(rootFrame, fileActivatedEventArgs);
break;
case CommandLineActivatedEventArgs commandLineActivatedEventArgs:
await CommandActivated(rootFrame, commandLineActivatedEventArgs);
break;
case LaunchActivatedEventArgs launchActivatedEventArgs:
LaunchActivated(rootFrame, launchActivatedEventArgs);
break;
default: // For other types of activated events
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(NotepadsMainPage));
}
break;
}
}

Expand Down
33 changes: 29 additions & 4 deletions src/Notepads/Services/AppSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class AppSettingsService
public static event EventHandler<Encoding> OnDefaultEncodingChanged;
public static event EventHandler<int> OnDefaultTabIndentsChanged;
public static event EventHandler<bool> OnStatusBarVisibilityChanged;
public static event EventHandler<bool> OnSessionBackupAndRestoreOptionForInstanceChanged;
public static event EventHandler<bool> OnSessionBackupAndRestoreOptionChanged;
public static event EventHandler<bool> OnHighlightMisspelledWordsChanged;

Expand Down Expand Up @@ -328,23 +329,47 @@ private static void InitializeStatusBarSettings()
private static void InitializeSessionSnapshotSettings()
{
// We should disable session snapshot feature on multi instances
App.OnInstanceTypeChanged += (_, args) =>
{
var wasSessionSnapshotEnabled = _isSessionSnapshotEnabled;

_isSessionSnapshotEnabled = IsSessionSnapshotEnabledInternal();

if (wasSessionSnapshotEnabled != _isSessionSnapshotEnabled)
{
if (_isSessionSnapshotEnabled)
{
OnSessionBackupAndRestoreOptionForInstanceChanged?.Invoke(null, _isSessionSnapshotEnabled);
}
else
{
OnSessionBackupAndRestoreOptionChanged?.Invoke(null, _isSessionSnapshotEnabled);
}
}
};

_isSessionSnapshotEnabled = IsSessionSnapshotEnabledInternal();
}

private static bool IsSessionSnapshotEnabledInternal()
{
if (!App.IsPrimaryInstance)
{
_isSessionSnapshotEnabled = false;
return false;
}
else if (App.IsGameBarWidget)
{
_isSessionSnapshotEnabled = true;
return true;
}
else
{
if (ApplicationSettingsStore.Read(SettingsKey.EditorEnableSessionBackupAndRestoreBool) is bool enableSessionBackupAndRestore)
{
_isSessionSnapshotEnabled = enableSessionBackupAndRestore;
return enableSessionBackupAndRestore;
}
else
{
_isSessionSnapshotEnabled = false;
return false;
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions src/Notepads/Views/MainPage/NotepadsMainPage.MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ private void InitializeMainMenu()
MenuPrintAllButton.Click += async (sender, args) => await PrintAll(NotepadsCore.GetAllTextEditors());
MenuSettingsButton.Click += (sender, args) => RootSplitView.IsPaneOpen = true;

if (!App.IsPrimaryInstance)
{
MainMenuButton.Foreground = new SolidColorBrush(ThemeSettingsService.AppAccentColor);
MenuSettingsButton.IsEnabled = false;
}
App.OnInstanceTypeChanged += (sender, args) => CustomizeBasedOnInstanceType(args);
CustomizeBasedOnInstanceType(App.IsPrimaryInstance);

if (App.IsGameBarWidget)
{
Expand All @@ -61,6 +58,16 @@ private void InitializeMainMenu()
MainMenuButtonFlyout.Opening += MainMenuButtonFlyout_Opening;
}

private void CustomizeBasedOnInstanceType(bool isPrimaryInstance)
{
// Apply UI changes for shadow instance
if (!isPrimaryInstance)
{
MainMenuButton.Foreground = new SolidColorBrush(ThemeSettingsService.AppAccentColor);
MenuSettingsButton.IsEnabled = false;
}
}

private void MainMenuButtonFlyout_Opening(object sender, object e)
{
var selectedTextEditor = NotepadsCore.GetSelectedTextEditor();
Expand Down
1 change: 1 addition & 0 deletions src/Notepads/Views/MainPage/NotepadsMainPage.StatusBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private void InitializeStatusBar()
{
ShowHideStatusBar(AppSettingsService.ShowStatusBar);
AppSettingsService.OnStatusBarVisibilityChanged += OnStatusBarVisibilityChanged;
App.OnInstanceTypeChanged += (_, args) => UpdateShadowWindowIndicator();
}

private void SetupStatusBar(ITextEditor textEditor)
Expand Down
Loading

0 comments on commit 037e739

Please sign in to comment.