mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-23 19:49:43 +01:00
Refactoring start menu source
1. refactoriong start menu source 2. fix depth problems involved in last commit
This commit is contained in:
@@ -152,24 +152,28 @@ namespace Wox.Plugin.Program
|
||||
|
||||
private static List<ProgramSource> ProgramSources()
|
||||
{
|
||||
var fileSystemSource = new List<FileSystemProgramSource>();
|
||||
var sources = new List<ProgramSource>();
|
||||
var source1 = _settings.ProgramSources;
|
||||
sources.AddRange(source1);
|
||||
if (_settings.EnableStartMenuSource)
|
||||
{
|
||||
fileSystemSource.Add(new CommonStartMenuProgramSource());
|
||||
fileSystemSource.Add(new UserStartMenuProgramSource());
|
||||
var source2 = new StartMenu();
|
||||
sources.Add(source2);
|
||||
}
|
||||
fileSystemSource.AddRange(_settings.ProgramSources);
|
||||
FileChangeWatcher.AddAll(fileSystemSource, _settings.ProgramSuffixes);
|
||||
foreach (var s in fileSystemSource)
|
||||
{
|
||||
s.Suffixes = _settings.ProgramSuffixes;
|
||||
}
|
||||
|
||||
var sources = new List<ProgramSource>();
|
||||
sources.AddRange(fileSystemSource);
|
||||
if (_settings.EnableRegistrySource)
|
||||
{
|
||||
sources.Add(new AppPathsProgramSource());
|
||||
var source3 = new AppPathsProgramSource();
|
||||
sources.Add(source3);
|
||||
}
|
||||
|
||||
FileChangeWatcher.AddAll(source1, _settings.ProgramSuffixes);
|
||||
foreach (var source in sources)
|
||||
{
|
||||
var win32 = source as Win32;
|
||||
if (win32 != null)
|
||||
{
|
||||
win32.Suffixes = _settings.ProgramSuffixes;
|
||||
}
|
||||
}
|
||||
|
||||
return sources;
|
||||
|
||||
@@ -15,6 +15,5 @@ namespace Wox.Plugin.Program
|
||||
public string Directory { get; set; }
|
||||
public string ExecutableName { get; set; }
|
||||
public int Score { get; set; }
|
||||
public ProgramSource Source { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -25,14 +25,14 @@
|
||||
Drop="programSourceView_Drop" >
|
||||
<ListView.View>
|
||||
<GridView>
|
||||
<GridViewColumn Header="{DynamicResource wox_plugin_program_location}" Width="250">
|
||||
<GridViewColumn Header="{DynamicResource wox_plugin_program_location}" Width="450">
|
||||
<GridViewColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Location, ConverterParameter=(null), Converter={program:LocationConverter}}"/>
|
||||
</DataTemplate>
|
||||
</GridViewColumn.CellTemplate>
|
||||
</GridViewColumn>
|
||||
<GridViewColumn Header="{DynamicResource wox_plugin_program_max_depth_header}" Width="75">
|
||||
<GridViewColumn Header="{DynamicResource wox_plugin_program_max_depth_header}" Width="130">
|
||||
<GridViewColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding MaxDepth, ConverterParameter=(null)}"/>
|
||||
|
||||
@@ -7,7 +7,7 @@ using Microsoft.Win32;
|
||||
namespace Wox.Plugin.Program.ProgramSources
|
||||
{
|
||||
[Serializable]
|
||||
public class AppPathsProgramSource : ProgramSource
|
||||
public class AppPathsProgramSource : Win32
|
||||
{
|
||||
public override List<Program> LoadPrograms()
|
||||
{
|
||||
@@ -57,7 +57,6 @@ namespace Wox.Plugin.Program.ProgramSources
|
||||
{
|
||||
var entry = CreateEntry(path);
|
||||
entry.ExecutableName = subkey;
|
||||
entry.Source = this;
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Plugin.Program.ProgramSources
|
||||
{
|
||||
[Serializable]
|
||||
public sealed class CommonStartMenuProgramSource : FileSystemProgramSource
|
||||
{
|
||||
private const int CSIDL_COMMON_PROGRAMS = 0x17;
|
||||
|
||||
// todo happlebao how to pass location before loadPrograms
|
||||
public CommonStartMenuProgramSource()
|
||||
{
|
||||
Location = getPath();
|
||||
}
|
||||
|
||||
[DllImport("shell32.dll")]
|
||||
private static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder,
|
||||
bool fCreate);
|
||||
|
||||
private static string getPath()
|
||||
{
|
||||
var commonStartMenuPath = new StringBuilder(560);
|
||||
SHGetSpecialFolderPath(IntPtr.Zero, commonStartMenuPath, CSIDL_COMMON_PROGRAMS, false);
|
||||
|
||||
return commonStartMenuPath.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,18 +8,16 @@ using Wox.Infrastructure.Logger;
|
||||
namespace Wox.Plugin.Program.ProgramSources
|
||||
{
|
||||
[Serializable]
|
||||
public class FileSystemProgramSource : ProgramSource
|
||||
public class FileSystemProgramSource : Win32
|
||||
{
|
||||
public string Location { get; set; } = "";
|
||||
public int MaxDepth { get; set; } = -1;
|
||||
internal string[] Suffixes { get; set; } = { "" };
|
||||
|
||||
public override List<Program> LoadPrograms()
|
||||
{
|
||||
if (Directory.Exists(Location) && MaxDepth >= -1)
|
||||
{
|
||||
var apps = new List<Program>();
|
||||
GetAppFromDirectory(apps, Location, 0);
|
||||
GetAppFromDirectory(apps, Location, MaxDepth);
|
||||
return apps;
|
||||
}
|
||||
else
|
||||
@@ -27,36 +25,5 @@ namespace Wox.Plugin.Program.ProgramSources
|
||||
return new List<Program>();
|
||||
}
|
||||
}
|
||||
|
||||
private void GetAppFromDirectory(List<Program> apps, string path, int depth)
|
||||
{
|
||||
if (MaxDepth != depth)
|
||||
{
|
||||
foreach (var file in Directory.GetFiles(path))
|
||||
{
|
||||
if (Suffixes.Any(o => file.EndsWith("." + o)))
|
||||
{
|
||||
Program p;
|
||||
try
|
||||
{
|
||||
p = CreateEntry(file);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var woxPluginException = new WoxPluginException("Program",
|
||||
$"GetAppFromDirectory failed: {path}", e);
|
||||
Log.Exception(woxPluginException);
|
||||
continue;
|
||||
}
|
||||
p.Source = this;
|
||||
apps.Add(p);
|
||||
}
|
||||
}
|
||||
foreach (var d in Directory.GetDirectories(path))
|
||||
{
|
||||
GetAppFromDirectory(apps, d, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,37 +8,6 @@ namespace Wox.Plugin.Program.ProgramSources
|
||||
[Serializable]
|
||||
public abstract class ProgramSource
|
||||
{
|
||||
|
||||
public abstract List<Program> LoadPrograms();
|
||||
|
||||
protected Program CreateEntry(string file)
|
||||
{
|
||||
var p = new Program
|
||||
{
|
||||
Title = Path.GetFileNameWithoutExtension(file),
|
||||
IcoPath = file,
|
||||
Path = file,
|
||||
Directory = Directory.GetParent(file).FullName
|
||||
};
|
||||
|
||||
switch (Path.GetExtension(file).ToLower())
|
||||
{
|
||||
case ".exe":
|
||||
p.ExecutableName = Path.GetFileName(file);
|
||||
try
|
||||
{
|
||||
var versionInfo = FileVersionInfo.GetVersionInfo(file);
|
||||
if (!string.IsNullOrEmpty(versionInfo.FileDescription))
|
||||
{
|
||||
p.Title = versionInfo.FileDescription;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
break;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
23
Plugins/Wox.Plugin.Program/ProgramSources/StartMenu.cs
Normal file
23
Plugins/Wox.Plugin.Program/ProgramSources/StartMenu.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Wox.Plugin.Program.ProgramSources
|
||||
{
|
||||
[Serializable]
|
||||
class StartMenu : Win32
|
||||
{
|
||||
public override List<Program> LoadPrograms()
|
||||
{
|
||||
var directory1 = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
|
||||
var directory2 = Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms);
|
||||
var programs = new List<Program>();
|
||||
GetAppFromDirectory(programs, directory1, MaxDepth);
|
||||
GetAppFromDirectory(programs, directory2, MaxDepth);
|
||||
return programs;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Wox.Plugin.Program.ProgramSources
|
||||
{
|
||||
|
||||
[Serializable]
|
||||
public sealed class UserStartMenuProgramSource : FileSystemProgramSource
|
||||
{
|
||||
public UserStartMenuProgramSource()
|
||||
{
|
||||
Location = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
|
||||
}
|
||||
}
|
||||
}
|
||||
79
Plugins/Wox.Plugin.Program/ProgramSources/Win32.cs
Normal file
79
Plugins/Wox.Plugin.Program/ProgramSources/Win32.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Wox.Infrastructure.Exception;
|
||||
using Wox.Infrastructure.Logger;
|
||||
|
||||
namespace Wox.Plugin.Program.ProgramSources
|
||||
{
|
||||
[Serializable]
|
||||
public abstract class Win32 : ProgramSource
|
||||
{
|
||||
public int MaxDepth { get; set; } = -1;
|
||||
public string[] Suffixes { get; set; } = { "" };
|
||||
|
||||
protected Program CreateEntry(string file)
|
||||
{
|
||||
var p = new Program
|
||||
{
|
||||
Title = Path.GetFileNameWithoutExtension(file),
|
||||
IcoPath = file,
|
||||
Path = file,
|
||||
Directory = Directory.GetParent(file).FullName
|
||||
};
|
||||
|
||||
switch (Path.GetExtension(file).ToLower())
|
||||
{
|
||||
case ".exe":
|
||||
p.ExecutableName = Path.GetFileName(file);
|
||||
try
|
||||
{
|
||||
var versionInfo = FileVersionInfo.GetVersionInfo(file);
|
||||
if (!string.IsNullOrEmpty(versionInfo.FileDescription))
|
||||
{
|
||||
p.Title = versionInfo.FileDescription;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
break;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
protected void GetAppFromDirectory(List<Program> apps, string directory, int depth)
|
||||
{
|
||||
if (MaxDepth == -1 || MaxDepth < depth)
|
||||
{
|
||||
foreach (var f in Directory.GetFiles(directory))
|
||||
{
|
||||
if (Suffixes.Any(o => f.EndsWith("." + o)))
|
||||
{
|
||||
Program p;
|
||||
try
|
||||
{
|
||||
p = CreateEntry(f);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var woxPluginException = new WoxPluginException("Program",
|
||||
$"GetAppFromDirectory failed: {directory}", e);
|
||||
Log.Exception(woxPluginException);
|
||||
continue;
|
||||
}
|
||||
apps.Add(p);
|
||||
}
|
||||
}
|
||||
foreach (var d in Directory.GetDirectories(directory))
|
||||
{
|
||||
GetAppFromDirectory(apps, d, depth - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,9 @@
|
||||
<DependentUpon>AddProgramSource.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="FileChangeWatcher.cs" />
|
||||
<Compile Include="ProgramSources\StartMenu.cs" />
|
||||
<Compile Include="ProgramSources\UWP.cs" />
|
||||
<Compile Include="ProgramSources\Win32.cs" />
|
||||
<Compile Include="SuffixesConverter.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="ProgramIndexCache.cs" />
|
||||
@@ -81,9 +83,7 @@
|
||||
</Compile>
|
||||
<Compile Include="ProgramSources\ProgramSource.cs" />
|
||||
<Compile Include="ProgramSources\AppPathsProgramSource.cs" />
|
||||
<Compile Include="ProgramSources\CommonStartMenuProgramSource.cs" />
|
||||
<Compile Include="ProgramSources\FileSystemProgramSource.cs" />
|
||||
<Compile Include="ProgramSources\UserStartMenuProgramSource.cs" />
|
||||
<Compile Include="Settings.cs" />
|
||||
<Compile Include="ProgramSuffixes.xaml.cs">
|
||||
<DependentUpon>ProgramSuffixes.xaml</DependentUpon>
|
||||
|
||||
Reference in New Issue
Block a user