Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ await SafetyExtensions.IgnoreExceptions(async () =>
},
Logger);

// Destroy cached properties windows
FilePropertiesHelpers.DestroyCachedWindows();
AppModel.IsMainWindowClosed = true;

// Wait for ongoing file operations
FileOperationsHelpers.WaitForCompletion();
}
Expand Down
7 changes: 7 additions & 0 deletions src/Files.App/Data/Models/AppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,12 @@ public bool IsPasteEnabled
get => isPasteEnabled;
set => SetProperty(ref isPasteEnabled, value);
}

private bool isMainWindowClosed = false;
public bool IsMainWindowClosed
{
get => isMainWindowClosed;
set => SetProperty(ref isMainWindowClosed, value);
}
}
}
55 changes: 43 additions & 12 deletions src/Files.App/Helpers/FilePropertiesHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.Windows.ApplicationModel.Resources;
using System.Collections.Concurrent;
using System.IO;
using Windows.ApplicationModel;
using Windows.Graphics;
Expand Down Expand Up @@ -38,6 +40,8 @@ public static string LogoPath
public static nint GetWindowHandle(Window w)
=> WinRT.Interop.WindowNative.GetWindowHandle(w);

private static BlockingCollection<WinUIEx.WindowEx> WindowCache = new();

/// <summary>
/// Open properties window
/// </summary>
Expand Down Expand Up @@ -97,17 +101,21 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
RequestedTheme = ThemeHelper.RootTheme
};

var propertiesWindow = new WinUIEx.WindowEx
WinUIEx.WindowEx propertiesWindow;
if (!WindowCache.TryTake(out propertiesWindow!))
{
IsMinimizable = false,
IsMaximizable = false,
MinWidth = 460,
MinHeight = 550,
Width = 800,
Height = 550,
Content = frame,
Backdrop = new WinUIEx.MicaSystemBackdrop(),
};
propertiesWindow = new();
propertiesWindow.Closed += PropertiesWindow_Closed;
}

propertiesWindow.IsMinimizable = false;
propertiesWindow.IsMaximizable = false;
propertiesWindow.MinWidth = 460;
propertiesWindow.MinHeight = 550;
propertiesWindow.Width = 800;
propertiesWindow.Height = 550;
propertiesWindow.Content = frame;
propertiesWindow.SystemBackdrop = new MicaBackdrop();

var appWindow = propertiesWindow.AppWindow;
appWindow.Title = "Properties".GetLocalizedResource();
Expand All @@ -128,8 +136,6 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
},
new SuppressNavigationTransitionInfo());

appWindow.Show();

// WINUI3: Move window to cursor position
InteropHelpers.GetCursorPos(out var pointerPosition);
var displayArea = DisplayArea.GetFromPoint(new PointInt32(pointerPosition.X, pointerPosition.Y), DisplayAreaFallback.Nearest);
Expand All @@ -142,6 +148,31 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
};

appWindow.Move(appWindowPos);

appWindow.Show();
}

// Destruction of Window objects seems to cause access violation. (#12057)
// So instead of destroying the Window object, cache it and reuse it as a workaround.
private static void PropertiesWindow_Closed(object sender, WindowEventArgs args)
{
if (!App.AppModel.IsMainWindowClosed && sender is WinUIEx.WindowEx window)
{
args.Handled = true;

window.AppWindow.Hide();
window.Content = null;
WindowCache.Add(window);
}
}

public static void DestroyCachedWindows()
{
while (WindowCache.TryTake(out var window))
{
window.Closed -= PropertiesWindow_Closed;
window.Close();
}
}
}
}