[Run][Program]Update icons for MSIX packages on package update (#32428)

* ptrun: update msix package icons on update

* extract duplicate code to functions
This commit is contained in:
HydroH
2024-04-23 04:59:00 +08:00
committed by GitHub
parent 88b709bef3
commit e2de521c1e

View File

@@ -29,34 +29,14 @@ namespace Microsoft.Plugin.Program.Storage
_packageCatalog.PackageInstalling += OnPackageInstalling;
_packageCatalog.PackageUninstalling += OnPackageUninstalling;
_packageCatalog.PackageUpdating += OnPackageUpdating;
}
public void OnPackageInstalling(PackageCatalog p, PackageInstallingEventArgs args)
{
if (args.IsComplete)
{
try
{
var packageWrapper = PackageWrapper.GetWrapperFromPackage(args.Package);
if (!string.IsNullOrEmpty(packageWrapper.InstalledLocation))
{
var uwp = new UWP(packageWrapper);
uwp.InitializeAppInfo(packageWrapper.InstalledLocation);
foreach (var app in uwp.Apps)
{
app.UpdateLogoPath(_context.API.GetCurrentTheme());
Add(app);
}
}
}
// InitializeAppInfo will throw if there is no AppxManifest.xml for the package.
// Note there are sometimes multiple packages per product and this doesn't necessarily mean that we haven't found the app.
// eg. "Could not find file 'C:\\Program Files\\WindowsApps\\Microsoft.WindowsTerminalPreview_2020.616.45.0_neutral_~_8wekyb3d8bbwe\\AppxManifest.xml'."
catch (System.IO.FileNotFoundException e)
{
ProgramLogger.Exception(e.Message, e, GetType(), args.Package.InstalledLocation.ToString());
}
AddPackage(args.Package);
}
}
@@ -64,16 +44,62 @@ namespace Microsoft.Plugin.Program.Storage
{
if (args.Progress == 0)
{
// find apps associated with this package.
var packageWrapper = PackageWrapper.GetWrapperFromPackage(args.Package);
var uwp = new UWP(packageWrapper);
var apps = Items.Where(a => a.Package.Equals(uwp)).ToArray();
RemovePackage(args.Package);
}
}
foreach (var app in apps)
public void OnPackageUpdating(PackageCatalog p, PackageUpdatingEventArgs args)
{
if (args.Progress == 0)
{
RemovePackage(args.SourcePackage);
}
if (args.IsComplete)
{
AddPackage(args.TargetPackage);
}
}
private void AddPackage(Package package)
{
var packageWrapper = PackageWrapper.GetWrapperFromPackage(package);
if (string.IsNullOrEmpty(packageWrapper.InstalledLocation))
{
return;
}
try
{
var uwp = new UWP(packageWrapper);
uwp.InitializeAppInfo(packageWrapper.InstalledLocation);
foreach (var app in uwp.Apps)
{
Remove(app);
app.UpdateLogoPath(_context.API.GetCurrentTheme());
Add(app);
}
}
// InitializeAppInfo will throw if there is no AppxManifest.xml for the package.
// Note there are sometimes multiple packages per product and this doesn't necessarily mean that we haven't found the app.
// eg. "Could not find file 'C:\\Program Files\\WindowsApps\\Microsoft.WindowsTerminalPreview_2020.616.45.0_neutral_~_8wekyb3d8bbwe\\AppxManifest.xml'."
catch (System.IO.FileNotFoundException e)
{
ProgramLogger.Exception(e.Message, e, GetType(), package.InstalledLocation.ToString());
}
}
private void RemovePackage(Package package)
{
// find apps associated with this package.
var packageWrapper = PackageWrapper.GetWrapperFromPackage(package);
var uwp = new UWP(packageWrapper);
var apps = Items.Where(a => a.Package.Equals(uwp)).ToArray();
foreach (var app in apps)
{
Remove(app);
}
}
public void IndexPrograms()