Skip to content

Commit

Permalink
Skip adding program to pause rule if failed to retrieve information..
Browse files Browse the repository at this point in the history
Resolved: #2104
  • Loading branch information
rocksdanister committed Apr 29, 2024
1 parent 9af17f3 commit da24d0e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 35 deletions.
29 changes: 19 additions & 10 deletions src/Lively/Lively.Common.Factories/ApplicationsFactory.cs
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -63,7 +72,7 @@ public ApplicationModel CreateApp(string path)
}
model.AppIcon = iconPath;
}
catch { }
catch { /* Model is still useful without Icon */ }

return model;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Lively/Lively.UI.WinUI/Services/DialogService.cs
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +52,7 @@ public async Task<DisplayMonitor> ShowDisplayChooseDialogAsync()
public async Task<ApplicationModel> ShowApplicationPickerDialogAsync()
{
var vm = App.Services.GetRequiredService<FindMoreAppsViewModel>();
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"));
Expand Down
23 changes: 15 additions & 8 deletions src/Lively/Lively.UI.WinUI/ViewModels/FindMoreAppsViewModel.cs
Expand Up @@ -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<ApplicationModel> applications = new();
private ObservableCollection<ApplicationModel> applications = [];

[ObservableProperty]
private AdvancedCollectionView applicationsFiltered;

[ObservableProperty]
private ApplicationModel selectedItem;

Expand All @@ -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();
Expand All @@ -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;
}
}
}

Expand Down
28 changes: 12 additions & 16 deletions src/Lively/Lively.UI.WinUI/ViewModels/SettingsViewModel.cs
Expand Up @@ -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()
Expand Down

0 comments on commit da24d0e

Please sign in to comment.