Browse Source

Centralize the desktop main-window lookup in UiContext

The classic-desktop lifetime cast for the dialog owner, the clipboard and
application shutdown was repeated across the file commands, file pickers,
project export, the PDB selector, the taskbar service and the analyzer
copy entries. Replace those copies with a single UiContext exposing
MainWindow, Clipboard and Shutdown.

Assisted-by: Claude:claude-opus-4-8:Claude Code
pull/3755/head
Siegfried Pammer 4 weeks ago
parent
commit
1c0e8675ee
  1. 5
      ILSpy/Analyzers/CopyAnalysisResultsContextMenuEntry.cs
  2. 5
      ILSpy/Analyzers/CopyAnalyzerErrorContextMenuEntry.cs
  3. 45
      ILSpy/AppEnv/UiContext.cs
  4. 12
      ILSpy/Commands/FileCommands.cs
  5. 8
      ILSpy/Commands/FilePickers.cs
  6. 4
      ILSpy/Commands/ProjectExport.cs
  7. 4
      ILSpy/Commands/SelectPdbContextMenuEntry.cs
  8. 2
      ILSpy/TaskbarProgressService.cs

5
ILSpy/Analyzers/CopyAnalysisResultsContextMenuEntry.cs

@ -24,6 +24,8 @@ using Avalonia; @@ -24,6 +24,8 @@ using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input.Platform;
using ILSpy.AppEnv;
namespace ILSpy.Analyzers
{
/// <summary>
@ -65,8 +67,7 @@ namespace ILSpy.Analyzers @@ -65,8 +67,7 @@ namespace ILSpy.Analyzers
static void TryWriteToClipboard(string payload)
{
if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime lifetime
|| lifetime.MainWindow?.Clipboard is not { } clipboard)
if (UiContext.Clipboard is not { } clipboard)
return;
// SetTextAsync is the extension method on IClipboard — needs Avalonia.Input.Platform.
_ = clipboard.SetTextAsync(payload);

5
ILSpy/Analyzers/CopyAnalyzerErrorContextMenuEntry.cs

@ -26,6 +26,8 @@ using Avalonia.Input.Platform; @@ -26,6 +26,8 @@ using Avalonia.Input.Platform;
using ICSharpCode.ILSpy.Properties;
using ILSpy.AppEnv;
namespace ILSpy.Analyzers
{
/// <summary>
@ -65,8 +67,7 @@ namespace ILSpy.Analyzers @@ -65,8 +67,7 @@ namespace ILSpy.Analyzers
static void TryWriteToClipboard(string payload)
{
if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime lifetime
|| lifetime.MainWindow?.Clipboard is not { } clipboard)
if (UiContext.Clipboard is not { } clipboard)
return;
_ = clipboard.SetTextAsync(payload);
}

45
ILSpy/AppEnv/UiContext.cs

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input.Platform;
namespace ILSpy.AppEnv
{
/// <summary>
/// Accessors for the desktop UI context. The classic-desktop lifetime cast was repeated at every
/// dialog-owner / clipboard / shutdown call site; centralising it here keeps those short and
/// consistent. Each member returns null / no-ops on non-desktop or design-time hosts where there
/// is no main window.
/// </summary>
public static class UiContext
{
/// <summary>The desktop main window, or null when there is no classic-desktop lifetime.</summary>
public static Window? MainWindow
=> (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
/// <summary>The main window's clipboard, or null when unavailable.</summary>
public static IClipboard? Clipboard => MainWindow?.Clipboard;
/// <summary>Requests application shutdown on the desktop lifetime; a no-op elsewhere.</summary>
public static void Shutdown()
=> (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.Shutdown();
}
}

12
ILSpy/Commands/FileCommands.cs

@ -58,8 +58,7 @@ namespace ILSpy.Commands @@ -58,8 +58,7 @@ namespace ILSpy.Commands
return;
}
var owner = (global::Avalonia.Application.Current?.ApplicationLifetime
as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
var owner = UiContext.MainWindow;
if (owner == null)
return;
@ -106,8 +105,7 @@ namespace ILSpy.Commands @@ -106,8 +105,7 @@ namespace ILSpy.Commands
{
if (!CanExecute(parameter))
return;
var owner = (global::Avalonia.Application.Current?.ApplicationLifetime
as global::Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime)?.MainWindow;
var owner = UiContext.MainWindow;
if (owner == null)
return;
ShowAsync(owner).HandleExceptions();
@ -138,8 +136,7 @@ namespace ILSpy.Commands @@ -138,8 +136,7 @@ namespace ILSpy.Commands
public override void Execute(object? parameter)
{
var owner = (global::Avalonia.Application.Current?.ApplicationLifetime
as global::Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime)?.MainWindow;
var owner = UiContext.MainWindow;
if (owner == null)
return;
var dlg = new Views.ManageAssemblyListsDialog(settingsService);
@ -281,8 +278,7 @@ namespace ILSpy.Commands @@ -281,8 +278,7 @@ namespace ILSpy.Commands
{
public override void Execute(object? parameter)
{
(global::Avalonia.Application.Current?.ApplicationLifetime
as IClassicDesktopStyleApplicationLifetime)?.Shutdown();
UiContext.Shutdown();
}
}
}

8
ILSpy/Commands/FilePickers.cs

@ -23,6 +23,8 @@ using System.Threading.Tasks; @@ -23,6 +23,8 @@ using System.Threading.Tasks;
using global::Avalonia.Controls.ApplicationLifetimes;
using global::Avalonia.Platform.Storage;
using ILSpy.AppEnv;
namespace ILSpy.Commands
{
/// <summary>
@ -43,8 +45,7 @@ namespace ILSpy.Commands @@ -43,8 +45,7 @@ namespace ILSpy.Commands
string? defaultFileName = null,
string? title = null)
{
var owner = (global::Avalonia.Application.Current?.ApplicationLifetime
as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
var owner = UiContext.MainWindow;
if (owner == null)
return null;
@ -71,8 +72,7 @@ namespace ILSpy.Commands @@ -71,8 +72,7 @@ namespace ILSpy.Commands
/// </summary>
public static async Task<string?> PickFolderAsync(string? title = null)
{
var owner = (global::Avalonia.Application.Current?.ApplicationLifetime
as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
var owner = UiContext.MainWindow;
if (owner == null)
return null;

4
ILSpy/Commands/ProjectExport.cs

@ -28,6 +28,7 @@ using global::Avalonia.Controls.ApplicationLifetimes; @@ -28,6 +28,7 @@ using global::Avalonia.Controls.ApplicationLifetimes;
using ICSharpCode.ILSpyX;
using ICSharpCode.ILSpyX.TreeView;
using ILSpy.AppEnv;
using ILSpy.Docking;
using ILSpy.Languages;
using ILSpy.TextView;
@ -66,8 +67,7 @@ namespace ILSpy.Commands @@ -66,8 +67,7 @@ namespace ILSpy.Commands
public static async Task PromptAndExportAsync(IReadOnlyList<LoadedAssembly> assemblies,
bool solutionMode, Language language, DockWorkspace dockWorkspace, SettingsService settingsService)
{
var owner = (global::Avalonia.Application.Current?.ApplicationLifetime
as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
var owner = UiContext.MainWindow;
if (owner == null)
return;

4
ILSpy/Commands/SelectPdbContextMenuEntry.cs

@ -27,6 +27,7 @@ using Avalonia.Platform.Storage; @@ -27,6 +27,7 @@ using Avalonia.Platform.Storage;
using ICSharpCode.Decompiler.CSharp.ProjectDecompiler;
using ICSharpCode.ILSpy.Properties;
using ILSpy.AppEnv;
using ILSpy.AssemblyTree;
using ILSpy.TreeNodes;
@ -66,8 +67,7 @@ namespace ILSpy.Commands @@ -66,8 +67,7 @@ namespace ILSpy.Commands
async Task ExecuteAsync(ICSharpCode.ILSpyX.LoadedAssembly assembly)
{
var owner = (global::Avalonia.Application.Current?.ApplicationLifetime
as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
var owner = UiContext.MainWindow;
if (owner == null)
return;
var initialDir = Path.GetDirectoryName(assembly.FileName);

2
ILSpy/TaskbarProgressService.cs

@ -69,7 +69,7 @@ namespace ILSpy @@ -69,7 +69,7 @@ namespace ILSpy
static IntPtr TryGetMainWindowHandle()
{
var window = (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
var window = AppEnv.UiContext.MainWindow;
return window?.TryGetPlatformHandle()?.Handle ?? IntPtr.Zero;
}

Loading…
Cancel
Save