Skip to content

Commit

Permalink
Enhanced User Experience with Progress Dialog for NuGet Installations
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed May 5, 2024
1 parent 345e79c commit 268b45b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
16 changes: 8 additions & 8 deletions dev/WinUICommunity_VS_Templates/Common/PreDefinedLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static List<Library> InitUseful()
List<Library> list = new()
{
new Library("TenMica", "1.0.0-beta", false, true),
new Library("WinUI.TableView", "1.0.0-preview4", false, true),
new Library("WinUI.TableView", "1.0.0"),
new Library("Microsoft.Windows.CsWinRT", "2.0.7"),
new Library("Microsoft.Windows.CsWin32", "0.3.49-beta", false, true),
new Library("WinUIEx", "2.3.4"),
Expand All @@ -110,7 +110,7 @@ public static List<Library> InitUseful()
new Library("YamlDotNet", "15.1.2"),
new Library("System.Drawing.Common", "8.0.4"),
new Library("System.Management", "8.0.0"),
new Library("SharpCompress", "0.36.0"),
new Library("SharpCompress", "0.37.2"),
new Library("RestSharp", "110.2.0"),
new Library("Vanara.Windows.Shell", "4.0.0"),
new Library("protobuf-net", "3.2.30"),
Expand All @@ -125,11 +125,11 @@ public static List<Library> InitWinUICommunity()
{
List<Library> list = new()
{
new Library(Constants.WinUICommunity_Win2D, "6.7.0"),
new Library(Constants.WinUICommunity_Core, "6.7.0"),
new Library(Constants.WinUICommunity_Components, "6.7.0"),
new Library(Constants.WinUICommunity_LandingPages, "6.7.0"),
new Library(Constants.WinUICommunity_ContextMenuExtensions, "6.7.0")
new Library(Constants.WinUICommunity_Win2D, "6.8.0"),
new Library(Constants.WinUICommunity_Core, "6.8.0"),
new Library(Constants.WinUICommunity_Components, "6.8.0"),
new Library(Constants.WinUICommunity_LandingPages, "6.8.0"),
new Library(Constants.WinUICommunity_ContextMenuExtensions, "6.8.0")
};
return list;
}
Expand All @@ -143,7 +143,7 @@ public static List<Library> InitLog()
new Library("Serilog.Sinks.Debug", "2.0.0"),
new Library("Serilog.Sinks.Console", "5.0.1"),
new Library("log4net", "2.0.17"),
new Library("NLog", "5.2.8")
new Library("NLog", "5.3.2")
};
return list;
}
Expand Down
54 changes: 38 additions & 16 deletions dev/WinUICommunity_VS_Templates/TemplateWizard/SharedWizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class SharedWizard
private IComponentModel _componentModel;
private IEnumerable<string> _nuGetPackages;
private IVsNuGetProjectUpdateEvents _nugetProjectUpdateEvents;

private IVsThreadedWaitDialog2 _waitDialog;
public void ProjectFinishedGenerating(Project project)
{
_project = project;
Expand Down Expand Up @@ -97,6 +97,7 @@ public async void RunStarted(object automationObject, Dictionary<string, string>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
_componentModel = (IComponentModel)ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel));
_waitDialog = ServiceProvider.GlobalProvider.GetService(typeof(SVsThreadedWaitDialog)) as IVsThreadedWaitDialog2;
if (_componentModel != null)
{
_nugetProjectUpdateEvents = _componentModel.GetService<IVsNuGetProjectUpdateEvents>();
Expand Down Expand Up @@ -580,29 +581,40 @@ public async void CopyFileToDestination(string inputfile, string outputfile)
return (folderPath, projectTemplatesFolder, vstemplateFileName);
}

private async Task InstallNuGetPackageAsync(IVsPackageInstaller2 installer, string packageId, string source)
private async Task InstallNuGetPackagesAsync()
{
await Task.Run(() =>
await ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
try
{
installer.InstallLatestPackage(source, _project, packageId, false, false);
// If there's any CPU-bound work, it will be done on the background thread.
}
catch (Exception ex)
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
int canceled; // This variable will store the status after the dialog is closed
// Start the package installation task but do not await it here
var installationTask = StartInstallationAsync();
// Start the threaded wait dialog
_waitDialog.StartWaitDialog("Installing NuGet packages", "Please wait while NuGet packages are being installed to your project...", null, null, "Operation in progress...", 0, false, true);
// Now await the installation task to complete
await installationTask;
// Once the installation is complete, end the wait dialog
_waitDialog.EndWaitDialog(out canceled);
// Check if the process was canceled before proceeding
if (canceled == 0) // If not canceled, finalize the process
{
LogError($"Error installing package {packageId}: {ex.Message}");
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
SaveAllProjects();
}
});
}
// InstallNuGetPackagesAsync iterates over the package list and installs each
private async Task InstallNuGetPackagesAsync()

private async Task StartInstallationAsync()
{
IVsPackageInstaller2 installer = _componentModel.GetService<IVsPackageInstaller2>();
if (installer == null)
{
LogError("Could not obtain IVsPackageInstaller service.");

return;
}

foreach (var packageId in _nuGetPackages)
Expand Down Expand Up @@ -633,10 +645,20 @@ private async Task InstallNuGetPackagesAsync()
LogError($"Failed to install NuGet package: {packageId}. Error: {ex.Message}");
}
}

await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
SaveAllProjects();
}
private async Task InstallNuGetPackageAsync(IVsPackageInstaller2 installer, string packageId, string source)
{
try
{
// Simulate or perform package installation, which might be CPU-bound
await Task.Run(() => installer.InstallLatestPackage(source, _project, packageId, false, false));
}
catch (Exception ex)
{
LogError($"Error installing package {packageId}: {ex.Message}");
}
}

private void SaveAllProjects()
{
ThreadHelper.ThrowIfNotOnUIThread("SaveAllProjects must be called on the UI thread.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
{
// When initialized asynchronously, the current thread may be a background thread at this point.
// Do any initialization that requires the UI thread after switching to the UI thread.
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="WinUICommunity_VS_Templates.8944ffa3-95dc-4d19-8ec7-2b21dad0eaf5" Version="8.2.0" Language="en-US" Publisher="Mahdi Hosseini" />
<Identity Id="WinUICommunity_VS_Templates.8944ffa3-95dc-4d19-8ec7-2b21dad0eaf5" Version="8.3.0" Language="en-US" Publisher="Mahdi Hosseini" />
<DisplayName>WinUICommunity Templates for WinUI</DisplayName>
<Description xml:space="preserve">WinUICommunity Project Template, help you quickly create a new WinUI 3 App with WinUICommunity and MVVM Packages. We prepare your project with the following features: NavigationView, Custom TitleBar, HomeLandingPage and Settings Page (with Theme settings).
We also always use the latest version of WindowsAppSDK.
Expand Down

0 comments on commit 268b45b

Please sign in to comment.