Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix session snapshot not being properly loaded #866

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 51 additions & 15 deletions src/Notepads/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,28 @@ 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;

// Notepads GitHub CD workflow will swap null with production value getting from Github Secrets
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 @@ -48,19 +63,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 @@ -76,6 +78,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 @@ -92,6 +95,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 @@ -291,6 +299,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
14 changes: 13 additions & 1 deletion src/Notepads/Core/NotepadsCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,20 @@ public void OpenTextEditors(ITextEditor[] editors, Guid? selectedEditorId = null
{
bool selectedEditorFound = false;

// Notepads should replace current "Untitled.txt" with open file if it is empty and it is the only tab that has been created.
if (GetNumberOfOpenedTextEditors() == 1 && editors.Length > 0)
{
var selectedEditor = GetAllTextEditors().First();
if (selectedEditor.EditingFile == null && !selectedEditor.IsModified)
{
Sets.Items?.Clear();
}
}

foreach (var textEditor in editors)
{
Sets.Items?.Remove(GetTextEditorSetsViewItem(textEditor.EditingFile));

var editorSetsViewItem = CreateTextEditorSetsViewItem(textEditor);
Sets.Items?.Add(editorSetsViewItem);
if (selectedEditorId.HasValue && textEditor.Id == selectedEditorId.Value)
Expand Down Expand Up @@ -429,7 +441,7 @@ private SetsViewItem CreateTextEditorSetsViewItem(ITextEditor textEditor)

private SetsViewItem GetTextEditorSetsViewItem(StorageFile file)
{
if (Sets.Items == null) return null;
if (Sets.Items == null || file == null) return null;
foreach (SetsViewItem setsItem in Sets.Items)
{
if (!(setsItem.Content is ITextEditor textEditor)) continue;
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
Loading