Skip to content

Commit

Permalink
Fix: Fix duplicate window opening when pressing Ctrl+N (#10648)
Browse files Browse the repository at this point in the history
  • Loading branch information
gave92 authored Dec 7, 2022
1 parent 6bb0724 commit 41556c8
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/Files.App/Helpers/NavigationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public static Task<bool> OpenTabInNewWindowAsync(string tabArgs)
return Launcher.LaunchUriAsync(folderUri).AsTask();
}

public static async void LaunchNewWindow()
public static Task LaunchNewWindowAsync()
{
var filesUWPUri = new Uri("files-uwp:");
await Launcher.LaunchUriAsync(filesUWPUri);
return Launcher.LaunchUriAsync(filesUWPUri).AsTask();
}

public static async Task OpenSelectedItems(IShellPage associatedInstance, bool openViaApplicationPicker = false)
Expand Down
30 changes: 26 additions & 4 deletions src/Files.App/Helpers/UniversalLogWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public async Task InitializeAsync(string name)

if (logsBeforeInit.Count > 0)
{
using var stream = await logFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders);
using var stream = await OpenFileWithRetryAsync(logFile, FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders);
if (stream is null)
return;
using var outputStream = stream.GetOutputStreamAt(stream.Size);
using var dataWriter = new DataWriter(outputStream);
while (logsBeforeInit.TryDequeue(out var text))
Expand All @@ -48,7 +50,9 @@ public async Task WriteLineToLogAsync(string text)
logsBeforeInit.Enqueue(text);
return;
}
using var stream = await logFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders);
using var stream = await OpenFileWithRetryAsync(logFile, FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders);
if (stream is null)
return;
using var outputStream = stream.GetOutputStreamAt(stream.Size);
using var dataWriter = new DataWriter(outputStream);
dataWriter.WriteString("\n" + text);
Expand All @@ -68,9 +72,7 @@ public void WriteLineToLog(string text)
IntPtr hStream = CreateFileFromApp(logFile.Path,
GENERIC_WRITE, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, (uint)File_Attributes.BackupSemantics, IntPtr.Zero);
if (hStream.ToInt64() == -1)
{
return;
}
byte[] buff = Encoding.UTF8.GetBytes("\n" + text);
int dwBytesWritten;
unsafe
Expand All @@ -85,5 +87,25 @@ public void WriteLineToLog(string text)

Debug.WriteLine($"Logged event: {text}");
}

private async Task<IRandomAccessStream?> OpenFileWithRetryAsync(IStorageFile2 file, FileAccessMode mode, StorageOpenOptions share, int maxRetries = 5)
{
for (int numTries = 0; numTries < maxRetries; numTries++)
{
IRandomAccessStream? fs = null;
try
{
fs = await file.OpenAsync(mode, share);
return fs;
}
catch (System.IO.IOException)
{
fs?.Dispose();
await Task.Delay(50);
}
}

return null;
}
}
}
12 changes: 6 additions & 6 deletions src/Files.App/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public bool IsWindowCompactOverlay

public ICommand NavigateToNumberedTabKeyboardAcceleratorCommand { get; private set; }

public ICommand OpenNewWindowAcceleratorCommand { get; private set; }
public IAsyncRelayCommand OpenNewWindowAcceleratorCommand { get; private set; }

public ICommand CloseSelectedTabKeyboardAcceleratorCommand { get; private set; }

public ICommand AddNewInstanceAcceleratorCommand { get; private set; }
public IAsyncRelayCommand AddNewInstanceAcceleratorCommand { get; private set; }

public ICommand ReopenClosedTabAcceleratorCommand { get; private set; }

Expand All @@ -65,9 +65,9 @@ public MainPageViewModel()
{
// Create commands
NavigateToNumberedTabKeyboardAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(NavigateToNumberedTabKeyboardAccelerator);
OpenNewWindowAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(OpenNewWindowAccelerator);
OpenNewWindowAcceleratorCommand = new AsyncRelayCommand<KeyboardAcceleratorInvokedEventArgs>(OpenNewWindowAccelerator);
CloseSelectedTabKeyboardAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(CloseSelectedTabKeyboardAccelerator);
AddNewInstanceAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(AddNewInstanceAccelerator);
AddNewInstanceAcceleratorCommand = new AsyncRelayCommand<KeyboardAcceleratorInvokedEventArgs>(AddNewInstanceAccelerator);
ReopenClosedTabAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(ReopenClosedTabAccelerator);
OpenSettingsCommand = new RelayCommand(OpenSettings);
}
Expand Down Expand Up @@ -141,7 +141,7 @@ private void NavigateToNumberedTabKeyboardAccelerator(KeyboardAcceleratorInvoked
e.Handled = true;
}

private async void OpenNewWindowAccelerator(KeyboardAcceleratorInvokedEventArgs? e)
private async Task OpenNewWindowAccelerator(KeyboardAcceleratorInvokedEventArgs? e)
{
Uri filesUWPUri = new Uri("files-uwp:");
await Launcher.LaunchUriAsync(filesUWPUri);
Expand All @@ -163,7 +163,7 @@ private void CloseSelectedTabKeyboardAccelerator(KeyboardAcceleratorInvokedEvent
e!.Handled = true;
}

private async void AddNewInstanceAccelerator(KeyboardAcceleratorInvokedEventArgs? e)
private async Task AddNewInstanceAccelerator(KeyboardAcceleratorInvokedEventArgs? e)
{
await AddNewTabAsync();
e!.Handled = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/ViewModels/ToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ public void SearchRegion_LostFocus(object sender, RoutedEventArgs e)

public ICommand? PasteItemsFromClipboardCommand { get; set; }

public ICommand? OpenNewWindowCommand { get; set; }
public IAsyncRelayCommand? OpenNewWindowCommand { get; set; }

public ICommand? OpenNewPaneCommand { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Views/ColumnShellPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private void InitToolbarCommands()
ToolbarViewModel.InvertContentPageSelctionCommand = new RelayCommand(() => SlimContentPage?.ItemManipulationModel.InvertSelection());
ToolbarViewModel.ClearContentPageSelectionCommand = new RelayCommand(() => SlimContentPage?.ItemManipulationModel.ClearSelection());
ToolbarViewModel.PasteItemsFromClipboardCommand = new RelayCommand(async () => await UIFilesystemHelpers.PasteItemAsync(FilesystemViewModel.WorkingDirectory, this));
ToolbarViewModel.OpenNewWindowCommand = new RelayCommand(NavigationHelpers.LaunchNewWindow);
ToolbarViewModel.OpenNewWindowCommand = new AsyncRelayCommand(NavigationHelpers.LaunchNewWindowAsync);
ToolbarViewModel.OpenNewPaneCommand = new RelayCommand(() => PaneHolder?.OpenPathInNewPane("Home".GetLocalizedResource()));
ToolbarViewModel.ClosePaneCommand = new RelayCommand(() => PaneHolder?.CloseActivePane());
ToolbarViewModel.CreateNewFileCommand = new RelayCommand<ShellNewEntry>(x => UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.File, x, this));
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Views/ModernShellPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private void InitToolbarCommands()
ToolbarViewModel.InvertContentPageSelctionCommand = new RelayCommand(() => SlimContentPage?.ItemManipulationModel.InvertSelection());
ToolbarViewModel.ClearContentPageSelectionCommand = new RelayCommand(() => SlimContentPage?.ItemManipulationModel.ClearSelection());
ToolbarViewModel.PasteItemsFromClipboardCommand = new RelayCommand(async () => await UIFilesystemHelpers.PasteItemAsync(FilesystemViewModel.WorkingDirectory, this));
ToolbarViewModel.OpenNewWindowCommand = new RelayCommand(NavigationHelpers.LaunchNewWindow);
ToolbarViewModel.OpenNewWindowCommand = new AsyncRelayCommand(NavigationHelpers.LaunchNewWindowAsync);
ToolbarViewModel.OpenNewPaneCommand = new RelayCommand(() => PaneHolder?.OpenPathInNewPane("Home".GetLocalizedResource()));
ToolbarViewModel.ClosePaneCommand = new RelayCommand(() => PaneHolder?.CloseActivePane());
ToolbarViewModel.CreateNewFileCommand = new RelayCommand<ShellNewEntry>(x => UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.File, x, this));
Expand Down
14 changes: 0 additions & 14 deletions src/Files.Shared/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -58,19 +57,6 @@ private async void LogAsync(string type, string caller, string message, int atte
{
await writer.WriteLineToLogAsync($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}|{type}|{caller}|{message}");
}
catch (IOException e) when (e is not FileNotFoundException)
{
if (attemptNumber < 5) // check the attempt count to prevent a stack overflow exception
{
// Log is likely in use by another process instance, so wait then try again
await Task.Delay(50);
LogAsync(type, caller, message, attemptNumber + 1);
}
else
{
Debug.WriteLine($"Writing to log file failed after 5 attempts with the following exception:\n{e}");
}
}
catch (Exception e)
{
Debug.WriteLine($"Writing to log file failed with the following exception:\n{e}");
Expand Down

0 comments on commit 41556c8

Please sign in to comment.