Skip to content

Commit

Permalink
修复 搜索图标获取
Browse files Browse the repository at this point in the history
修复 搜索快速更改时报错
  • Loading branch information
MakesYT committed Oct 16, 2023
1 parent e28feab commit f5e1609
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 119 deletions.
4 changes: 2 additions & 2 deletions Core/SDKs/Tools/AppTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public static async Task AppSolverA(Dictionary<string, SearchViewItem> collectio

if (ConfigManger.Config.ignoreItems.Contains(targetPath))
{
log.Debug("忽略索引:\n" + targetPath);
log.Debug("忽略索引:" + targetPath);
return;
}
}
Expand Down Expand Up @@ -422,7 +422,7 @@ public static async Task AppSolverA(Dictionary<string, SearchViewItem> collectio

if (ConfigManger.Config.ignoreItems.Contains(url))
{
log.Debug("忽略索引:\n" + url);
log.Debug("忽略索引:" + url);
return;
}

Expand Down
94 changes: 69 additions & 25 deletions Core/SDKs/Tools/IconTools.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#region

using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Xml;
using Core.SDKs.Services;
using log4net;
using Size = System.Drawing.Size;

#endregion

Expand All @@ -20,7 +23,6 @@ public class IconTools
private const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
private const uint SHGFI_OPENICON = 0x000000002;
private static readonly Dictionary<string, Icon> _icons = new(250);
private static Semaphore semaphore = new(3, 6);


[DllImport("User32.dll")]
Expand Down Expand Up @@ -55,8 +57,16 @@ IntPtr hIcon //A handle to the icon to be destroyed. The icon must not be in use
using var iconBm = new Bitmap(bm, new Size(64, 64));
//如果是windows调用,直接下面一行代码就可以了
//此代码不能在web程序中调用,会有安全异常抛出
var icon = Icon.FromHandle(iconBm.GetHicon());
return icon;
retry:
try
{
var icon = Icon.FromHandle(iconBm.GetHicon());
return icon;
}
catch (Exception e)
{
goto retry;
}
}
}

Expand Down Expand Up @@ -139,12 +149,19 @@ IntPtr hIcon //A handle to the icon to be destroyed. The icon must not be in use
SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES | SHGFI_OPENICON);


return (Icon)Icon.FromHandle(shinfo.hIcon);
try
{
return (Icon)Icon.FromHandle(shinfo.hIcon);
}
catch (Exception e)
{
return null;
}
}

public static Icon GetIcon(string path)
public static void GetIcon(string path, BindingList<SearchViewItem> items, SearchViewItem item)
{
semaphore.WaitOne();
//log.Debug(1);
var mscFile = false;
string cacheKey;
switch (path.ToLower().Split(".").Last())
Expand Down Expand Up @@ -173,19 +190,30 @@ public static Icon GetIcon(string path)
}
}

log.Debug(cacheKey);

//缓存
if (_icons.TryGetValue(cacheKey, out var icon2))
{
return icon2;
item.Icon = icon2;
}

var iconBase = GetIconBase(path, cacheKey, mscFile);
var clone = (Icon)iconBase.Clone();
iconBase.Dispose();
semaphore.Release();
return clone;
Task.Run(() =>
{
retry:
var iconBase = GetIconBase(path, cacheKey, mscFile);
if (iconBase == null)
{
goto retry;
}
var clone = (Icon)iconBase.Clone();
_icons.TryAdd(cacheKey, clone);
iconBase.Dispose();
item.Icon = clone;
Application.Current.Dispatcher.BeginInvoke(() =>
{
items.ResetItem(items.IndexOf(item));
});
});
}

public Icon? GetFormClipboard()
Expand Down Expand Up @@ -214,22 +242,38 @@ public static Icon GetIcon(string path)
}


public static Icon ExtractFromPath(string path)
public static void ExtractFromPath(string path, BindingList<SearchViewItem> items, SearchViewItem item)
{
if (_icons.TryGetValue(path, out var fromPath))
{
return fromPath;
item.Icon = fromPath;
}

var shinfo = new SHFILEINFO();
SHGetFileInfo(
path,
0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
SHGFI_ICON | SHGFI_LARGEICON);
var independenceIcon12 = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
DestroyIcon(shinfo.hIcon);
_icons.TryAdd(path, independenceIcon12);
return independenceIcon12;
Task.Run(() =>
{
retry:
try
{
var shinfo = new SHFILEINFO();
SHGetFileInfo(
path,
0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
SHGFI_ICON | SHGFI_LARGEICON);
var independenceIcon12 = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
DestroyIcon(shinfo.hIcon);
_icons.TryAdd(path, independenceIcon12);
item.Icon = independenceIcon12;
}
catch (Exception e)
{
goto retry;
}
Application.Current.Dispatcher.BeginInvoke(() =>
{
items.ResetItem(items.IndexOf(item));
});
});
}

[DllImport("shell32.dll")]
Expand Down
2 changes: 1 addition & 1 deletion Core/SDKs/Tools/UWPTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static async Task AppContainerAnalyse(FirewallApi.INET_FIREWALL_APP_CONT
{
if (ConfigManger.Config.ignoreItems.Contains(appContainer.appContainerName))
{
Log.Debug("忽略索引:\n" + appContainer.appContainerName);
Log.Debug("忽略索引:" + appContainer.appContainerName);
return;
}

Expand Down
106 changes: 52 additions & 54 deletions Core/ViewModel/SearchWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void CheckClipboard(bool loadLast = false)
{
value.FileName = $"打开文件: {value.FileName} ?";
Items.Insert(0, value);
GetIconInItems(value);
GetIconInItemsAsync(value);
}
}
}
Expand Down Expand Up @@ -199,7 +199,7 @@ private void LoadLast()
{
if (_collection.TryGetValue(configAlwayShow, out var searchViewItem))
{
var item = (SearchViewItem)searchViewItem.Clone();
var item = (SearchViewItem)searchViewItem;

Log.Debug("加载常驻:" + item.OnlyKey);

Expand All @@ -208,7 +208,7 @@ private void LoadLast()
Items.Add(item);
if (item.Icon is null)
{
GetIconInItems(item);
GetIconInItemsAsync(item);
}


Expand Down Expand Up @@ -239,7 +239,7 @@ private void LoadLast()
break;
}

var item = (SearchViewItem)item2.Clone();
var item = (SearchViewItem)item2;

Log.Debug("加载历史:" + item.OnlyKey);

Expand All @@ -249,7 +249,7 @@ private void LoadLast()
Items.Add(item);
if (item.Icon is null)
{
GetIconInItems(item);
GetIconInItemsAsync(item);
}

limit++;
Expand All @@ -264,56 +264,54 @@ private void LoadLast()
// GetItemsIcon();
}

private async Task GetIconInItems(SearchViewItem t)
private void GetIconInItemsAsync(SearchViewItem t)
{
await Task.Run(() =>
//Log.Debug($"为{t.OnlyKey}生成Icon");
switch (t.FileType)
{
//Log.Debug($"为{t.OnlyKey}生成Icon");
switch (t.FileType)
{
case FileType.文件夹:
t.Icon = IconTools.ExtractFromPath(t.DirectoryInfo!.FullName);
break;
case FileType.命令:
case FileType.URL:
if (t.FileInfo is not null)
{
t.Icon = IconTools.GetIcon(t.FileInfo!.FullName);
}
break;
case FileType.数学运算:
case FileType.剪贴板图像:
case FileType.None:
break;
case FileType.自定义:
if (t.GetIconAction != null)
{
var icon3 = t.GetIconAction(t);
t.Icon = icon3;
}
case FileType.文件夹:
IconTools.ExtractFromPath(t.DirectoryInfo!.FullName, Items, t);
break;
case FileType.命令:
case FileType.URL:
if (t.FileInfo is not null)
{
IconTools.GetIcon(t.FileInfo!.FullName, Items, t);
}

break;
case FileType.UWP应用:
t.Icon = IconTools.GetIcon(t.IconPath!);
break;
case FileType.应用程序:
case FileType.Word文档:
case FileType.PPT文档:
case FileType.Excel文档:
case FileType.PDF文档:
case FileType.图像:
case FileType.文件:
break;
case FileType.数学运算:
case FileType.剪贴板图像:
case FileType.None:
break;
case FileType.自定义:
if (t.GetIconAction != null)
{
var icon3 = t.GetIconAction(t);
t.Icon = icon3;
}

case FileType.自定义情景:
case FileType.便签:
default:
t.Icon = IconTools.GetIcon(t.FileInfo!.FullName);
break;
}
break;
case FileType.UWP应用:
IconTools.GetIcon(t.IconPath!, Items, t);
break;
case FileType.应用程序:
case FileType.Word文档:
case FileType.PPT文档:
case FileType.Excel文档:
case FileType.PDF文档:
case FileType.图像:
case FileType.文件:

case FileType.自定义情景:
case FileType.便签:
default:
IconTools.GetIcon(t.FileInfo!.FullName, Items, t);
break;
}
//Log.Debug(t.OnlyKey);

OnPropertyChanged(nameof(Items));
});
//
}

private readonly DelayAction _searchDelayAction = new();
Expand Down Expand Up @@ -471,7 +469,7 @@ partial void OnSearchChanged(string? value)
{
if (ConfigManger.Config.lastOpens.TryGetValue(sorted[i].Item.OnlyKey, out var open))
{
nowHasLastOpens.Add((SearchViewItem)sorted[i].Item.Clone(), open);
nowHasLastOpens.Add((SearchViewItem)sorted[i].Item, open);
sorted.RemoveAt(i);
}
}
Expand All @@ -489,7 +487,7 @@ partial void OnSearchChanged(string? value)
if (searchViewItem.Icon is null)
{
GetIconInItems(searchViewItem);
GetIconInItemsAsync(searchViewItem);
}
count++;
Expand All @@ -504,7 +502,7 @@ partial void OnSearchChanged(string? value)
break; // 跳出循环
}
var searchViewItem = (SearchViewItem)x.Item.Clone();
var searchViewItem = (SearchViewItem)x.Item;
{
Log.Debug("添加搜索结果" + x.Item.OnlyKey);
Expand All @@ -517,7 +515,7 @@ partial void OnSearchChanged(string? value)
if (searchViewItem.Icon is null)
{
GetIconInItems(searchViewItem);
GetIconInItemsAsync(searchViewItem);
}
Items.Add(searchViewItem); // 添加元素
Expand Down
Loading

0 comments on commit f5e1609

Please sign in to comment.