diff --git a/src/Lively/Lively.Common.Factories/ApplicationsFactory.cs b/src/Lively/Lively.Common.Factories/ApplicationsFactory.cs index 94a2b5c3..05916716 100644 --- a/src/Lively/Lively.Common.Factories/ApplicationsFactory.cs +++ b/src/Lively/Lively.Common.Factories/ApplicationsFactory.cs @@ -3,6 +3,7 @@ using Lively.Models; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.IO; @@ -18,29 +19,37 @@ public class ApplicationsFactory : IApplicationsFactory public ApplicationModel CreateApp(Process process) { - var model = new ApplicationModel - { - AppName = process.ProcessName - }; - + var model = new ApplicationModel(); try { + //Can throw exception + model.AppName = process.ProcessName; + //Workaround: x86 apps cannot access Process.MainModule of x64 apps int capacity = 1024; var sb = new StringBuilder(capacity); - //Workaround: x86 apps cannot access Process.MainModule of x64 apps - NativeMethods.QueryFullProcessImageName(process.Handle, 0, sb, ref capacity); + if (!NativeMethods.QueryFullProcessImageName(process.Handle, 0, sb, ref capacity)) + throw new Win32Exception(); + model.AppPath = sb.ToString(0, capacity); + } + catch + { + //Failed to retrieve process information. + return null; + } + try + { Directory.CreateDirectory(cacheDir); var iconPath = Path.Combine(cacheDir, model.AppName); if (!File.Exists(iconPath)) { - //temp cache + //Temp cache Icon.ExtractAssociatedIcon(model.AppPath).ToBitmap().Save(iconPath); } model.AppIcon = iconPath; } - catch { } + catch { /* Model is still useful without Icon */ } return model; } @@ -63,7 +72,7 @@ public ApplicationModel CreateApp(string path) } model.AppIcon = iconPath; } - catch { } + catch { /* Model is still useful without Icon */ } return model; } diff --git a/src/Lively/Lively.UI.WinUI/Services/DialogService.cs b/src/Lively/Lively.UI.WinUI/Services/DialogService.cs index c9e750f8..fe40f36a 100644 --- a/src/Lively/Lively.UI.WinUI/Services/DialogService.cs +++ b/src/Lively/Lively.UI.WinUI/Services/DialogService.cs @@ -7,6 +7,7 @@ using Lively.UI.WinUI.Views.Pages; using Lively.UI.WinUI.Views.Pages.ControlPanel; using Lively.UI.WinUI.Views.Pages.Gallery; +using Lively.UI.WinUI.Views.Pages.Settings; using Microsoft.Extensions.DependencyInjection; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -51,7 +52,7 @@ public async Task ShowDisplayChooseDialogAsync() public async Task ShowApplicationPickerDialogAsync() { var vm = App.Services.GetRequiredService(); - var result = await ShowDialogAsync(new Views.Pages.Settings.FindMoreAppsView() { DataContext = vm }, + var result = await ShowDialogAsync(new FindMoreAppsView() { DataContext = vm }, i18n.GetString("TitleChooseApplication/Text"), i18n.GetString("TextAdd"), i18n.GetString("Cancel/Content")); diff --git a/src/Lively/Lively.UI.WinUI/ViewModels/FindMoreAppsViewModel.cs b/src/Lively/Lively.UI.WinUI/ViewModels/FindMoreAppsViewModel.cs index 955d8c31..5d7f74a4 100644 --- a/src/Lively/Lively.UI.WinUI/ViewModels/FindMoreAppsViewModel.cs +++ b/src/Lively/Lively.UI.WinUI/ViewModels/FindMoreAppsViewModel.cs @@ -22,18 +22,20 @@ namespace Lively.UI.WinUI.ViewModels public partial class FindMoreAppsViewModel : ObservableObject { private readonly IApplicationsFactory appFactory; - private readonly string[] excludedClasses = new string[] - { + private readonly string[] excludedClasses = + [ //uwp apps "ApplicationFrameWindow", //startmeu, taskview (win10), action center etc "Windows.UI.Core.CoreWindow", - }; + ]; [ObservableProperty] - private ObservableCollection applications = new(); + private ObservableCollection applications = []; + [ObservableProperty] private AdvancedCollectionView applicationsFiltered; + [ObservableProperty] private ApplicationModel selectedItem; @@ -48,7 +50,9 @@ public FindMoreAppsViewModel(IApplicationsFactory appFactory) foreach (var item in Process.GetProcesses() .Where(x => x.MainWindowHandle != IntPtr.Zero && !string.IsNullOrEmpty(x.MainWindowTitle) && !IsExcluded(x.MainWindowHandle))) { - Applications.Add(appFactory.CreateApp(item)); + var app = appFactory.CreateApp(item); + if (app is not null) + Applications.Add(app); } } SelectedItem = Applications.FirstOrDefault(); @@ -65,9 +69,12 @@ private async Task BrowseApp() var file = await filePicker.PickSingleFileAsync(); if (file != null) { - var item = appFactory.CreateApp(file.Path); - Applications.Add(item); - SelectedItem = item; + var app = appFactory.CreateApp(file.Path); + if (app is not null) + { + Applications.Add(app); + SelectedItem = app; + } } } diff --git a/src/Lively/Lively.UI.WinUI/ViewModels/SettingsViewModel.cs b/src/Lively/Lively.UI.WinUI/ViewModels/SettingsViewModel.cs index b7515f7d..50564869 100644 --- a/src/Lively/Lively.UI.WinUI/ViewModels/SettingsViewModel.cs +++ b/src/Lively/Lively.UI.WinUI/ViewModels/SettingsViewModel.cs @@ -357,24 +357,20 @@ public ApplicationRulesModel SelectedAppRuleItem private async Task AppRuleAddProgram() { var result = await dialogService.ShowApplicationPickerDialogAsync(); - if (result != null) + if (result is null) + return; + + try { - try - { - var rule = appRuleFactory.CreateAppRule(result.AppPath, AppRulesEnum.pause); - if (AppRules.Any(x => x.AppName.Equals(rule.AppName, StringComparison.Ordinal))) - { - return; - } - userSettings.AppRules.Add(rule); - AppRules.Add(rule); - UpdateAppRulesConfigFile(); - } - catch (Exception) - { - //TODO - } + var rule = appRuleFactory.CreateAppRule(result.AppPath, AppRulesEnum.pause); + if (AppRules.Any(x => x.AppName.Equals(rule.AppName, StringComparison.Ordinal))) + return; + + userSettings.AppRules.Add(rule); + AppRules.Add(rule); + UpdateAppRulesConfigFile(); } + catch { /* Failed to parse program information, ignore. */ } } private void AppRuleRemoveProgram()