Skip to content

Commit 41556c8

Browse files
authored
Fix: Fix duplicate window opening when pressing Ctrl+N (#10648)
1 parent 6bb0724 commit 41556c8

File tree

7 files changed

+37
-29
lines changed

7 files changed

+37
-29
lines changed

src/Files.App/Helpers/NavigationHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public static Task<bool> OpenTabInNewWindowAsync(string tabArgs)
3636
return Launcher.LaunchUriAsync(folderUri).AsTask();
3737
}
3838

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

4545
public static async Task OpenSelectedItems(IShellPage associatedInstance, bool openViaApplicationPicker = false)

src/Files.App/Helpers/UniversalLogWriter.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public async Task InitializeAsync(string name)
2828

2929
if (logsBeforeInit.Count > 0)
3030
{
31-
using var stream = await logFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders);
31+
using var stream = await OpenFileWithRetryAsync(logFile, FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders);
32+
if (stream is null)
33+
return;
3234
using var outputStream = stream.GetOutputStreamAt(stream.Size);
3335
using var dataWriter = new DataWriter(outputStream);
3436
while (logsBeforeInit.TryDequeue(out var text))
@@ -48,7 +50,9 @@ public async Task WriteLineToLogAsync(string text)
4850
logsBeforeInit.Enqueue(text);
4951
return;
5052
}
51-
using var stream = await logFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders);
53+
using var stream = await OpenFileWithRetryAsync(logFile, FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders);
54+
if (stream is null)
55+
return;
5256
using var outputStream = stream.GetOutputStreamAt(stream.Size);
5357
using var dataWriter = new DataWriter(outputStream);
5458
dataWriter.WriteString("\n" + text);
@@ -68,9 +72,7 @@ public void WriteLineToLog(string text)
6872
IntPtr hStream = CreateFileFromApp(logFile.Path,
6973
GENERIC_WRITE, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, (uint)File_Attributes.BackupSemantics, IntPtr.Zero);
7074
if (hStream.ToInt64() == -1)
71-
{
7275
return;
73-
}
7476
byte[] buff = Encoding.UTF8.GetBytes("\n" + text);
7577
int dwBytesWritten;
7678
unsafe
@@ -85,5 +87,25 @@ public void WriteLineToLog(string text)
8587

8688
Debug.WriteLine($"Logged event: {text}");
8789
}
90+
91+
private async Task<IRandomAccessStream?> OpenFileWithRetryAsync(IStorageFile2 file, FileAccessMode mode, StorageOpenOptions share, int maxRetries = 5)
92+
{
93+
for (int numTries = 0; numTries < maxRetries; numTries++)
94+
{
95+
IRandomAccessStream? fs = null;
96+
try
97+
{
98+
fs = await file.OpenAsync(mode, share);
99+
return fs;
100+
}
101+
catch (System.IO.IOException)
102+
{
103+
fs?.Dispose();
104+
await Task.Delay(50);
105+
}
106+
}
107+
108+
return null;
109+
}
88110
}
89111
}

src/Files.App/ViewModels/MainPageViewModel.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public bool IsWindowCompactOverlay
5151

5252
public ICommand NavigateToNumberedTabKeyboardAcceleratorCommand { get; private set; }
5353

54-
public ICommand OpenNewWindowAcceleratorCommand { get; private set; }
54+
public IAsyncRelayCommand OpenNewWindowAcceleratorCommand { get; private set; }
5555

5656
public ICommand CloseSelectedTabKeyboardAcceleratorCommand { get; private set; }
5757

58-
public ICommand AddNewInstanceAcceleratorCommand { get; private set; }
58+
public IAsyncRelayCommand AddNewInstanceAcceleratorCommand { get; private set; }
5959

6060
public ICommand ReopenClosedTabAcceleratorCommand { get; private set; }
6161

@@ -65,9 +65,9 @@ public MainPageViewModel()
6565
{
6666
// Create commands
6767
NavigateToNumberedTabKeyboardAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(NavigateToNumberedTabKeyboardAccelerator);
68-
OpenNewWindowAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(OpenNewWindowAccelerator);
68+
OpenNewWindowAcceleratorCommand = new AsyncRelayCommand<KeyboardAcceleratorInvokedEventArgs>(OpenNewWindowAccelerator);
6969
CloseSelectedTabKeyboardAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(CloseSelectedTabKeyboardAccelerator);
70-
AddNewInstanceAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(AddNewInstanceAccelerator);
70+
AddNewInstanceAcceleratorCommand = new AsyncRelayCommand<KeyboardAcceleratorInvokedEventArgs>(AddNewInstanceAccelerator);
7171
ReopenClosedTabAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(ReopenClosedTabAccelerator);
7272
OpenSettingsCommand = new RelayCommand(OpenSettings);
7373
}
@@ -141,7 +141,7 @@ private void NavigateToNumberedTabKeyboardAccelerator(KeyboardAcceleratorInvoked
141141
e.Handled = true;
142142
}
143143

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

166-
private async void AddNewInstanceAccelerator(KeyboardAcceleratorInvokedEventArgs? e)
166+
private async Task AddNewInstanceAccelerator(KeyboardAcceleratorInvokedEventArgs? e)
167167
{
168168
await AddNewTabAsync();
169169
e!.Handled = true;

src/Files.App/ViewModels/ToolbarViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ public void SearchRegion_LostFocus(object sender, RoutedEventArgs e)
781781

782782
public ICommand? PasteItemsFromClipboardCommand { get; set; }
783783

784-
public ICommand? OpenNewWindowCommand { get; set; }
784+
public IAsyncRelayCommand? OpenNewWindowCommand { get; set; }
785785

786786
public ICommand? OpenNewPaneCommand { get; set; }
787787

src/Files.App/Views/ColumnShellPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ private void InitToolbarCommands()
270270
ToolbarViewModel.InvertContentPageSelctionCommand = new RelayCommand(() => SlimContentPage?.ItemManipulationModel.InvertSelection());
271271
ToolbarViewModel.ClearContentPageSelectionCommand = new RelayCommand(() => SlimContentPage?.ItemManipulationModel.ClearSelection());
272272
ToolbarViewModel.PasteItemsFromClipboardCommand = new RelayCommand(async () => await UIFilesystemHelpers.PasteItemAsync(FilesystemViewModel.WorkingDirectory, this));
273-
ToolbarViewModel.OpenNewWindowCommand = new RelayCommand(NavigationHelpers.LaunchNewWindow);
273+
ToolbarViewModel.OpenNewWindowCommand = new AsyncRelayCommand(NavigationHelpers.LaunchNewWindowAsync);
274274
ToolbarViewModel.OpenNewPaneCommand = new RelayCommand(() => PaneHolder?.OpenPathInNewPane("Home".GetLocalizedResource()));
275275
ToolbarViewModel.ClosePaneCommand = new RelayCommand(() => PaneHolder?.CloseActivePane());
276276
ToolbarViewModel.CreateNewFileCommand = new RelayCommand<ShellNewEntry>(x => UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.File, x, this));

src/Files.App/Views/ModernShellPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private void InitToolbarCommands()
239239
ToolbarViewModel.InvertContentPageSelctionCommand = new RelayCommand(() => SlimContentPage?.ItemManipulationModel.InvertSelection());
240240
ToolbarViewModel.ClearContentPageSelectionCommand = new RelayCommand(() => SlimContentPage?.ItemManipulationModel.ClearSelection());
241241
ToolbarViewModel.PasteItemsFromClipboardCommand = new RelayCommand(async () => await UIFilesystemHelpers.PasteItemAsync(FilesystemViewModel.WorkingDirectory, this));
242-
ToolbarViewModel.OpenNewWindowCommand = new RelayCommand(NavigationHelpers.LaunchNewWindow);
242+
ToolbarViewModel.OpenNewWindowCommand = new AsyncRelayCommand(NavigationHelpers.LaunchNewWindowAsync);
243243
ToolbarViewModel.OpenNewPaneCommand = new RelayCommand(() => PaneHolder?.OpenPathInNewPane("Home".GetLocalizedResource()));
244244
ToolbarViewModel.ClosePaneCommand = new RelayCommand(() => PaneHolder?.CloseActivePane());
245245
ToolbarViewModel.CreateNewFileCommand = new RelayCommand<ShellNewEntry>(x => UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.File, x, this));

src/Files.Shared/Logger.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Diagnostics;
3-
using System.IO;
43
using System.Runtime.CompilerServices;
54
using System.Threading;
65
using System.Threading.Tasks;
@@ -58,19 +57,6 @@ private async void LogAsync(string type, string caller, string message, int atte
5857
{
5958
await writer.WriteLineToLogAsync($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}|{type}|{caller}|{message}");
6059
}
61-
catch (IOException e) when (e is not FileNotFoundException)
62-
{
63-
if (attemptNumber < 5) // check the attempt count to prevent a stack overflow exception
64-
{
65-
// Log is likely in use by another process instance, so wait then try again
66-
await Task.Delay(50);
67-
LogAsync(type, caller, message, attemptNumber + 1);
68-
}
69-
else
70-
{
71-
Debug.WriteLine($"Writing to log file failed after 5 attempts with the following exception:\n{e}");
72-
}
73-
}
7460
catch (Exception e)
7561
{
7662
Debug.WriteLine($"Writing to log file failed with the following exception:\n{e}");

0 commit comments

Comments
 (0)