-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from y0ung3r/feature/19.default-icon
(#19) Add extraction of the stock icon for processes without an executable path
- Loading branch information
Showing
18 changed files
with
185 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
using SkiaSharp; | ||
|
||
namespace ProcessDoctor.Backend.Core; | ||
|
||
public abstract record SystemProcess( | ||
uint Id, | ||
uint? ParentId, | ||
string Name, | ||
string? CommandLine, | ||
string? ExecutablePath); | ||
string? ExecutablePath) | ||
{ | ||
public abstract SKBitmap ExtractIcon(); | ||
} |
19 changes: 19 additions & 0 deletions
19
ProcessDoctor.Backend.Windows/Imaging/Extensions/IconExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Drawing; | ||
using System.Drawing.Imaging; | ||
using SkiaSharp; | ||
|
||
namespace ProcessDoctor.Backend.Windows.Imaging.Extensions; | ||
|
||
public static class IconExtensions | ||
{ | ||
public static SKBitmap ToSkBitmap(this Icon icon) | ||
{ | ||
using var nativeBitmap = icon.ToBitmap(); | ||
|
||
using var stream = new MemoryStream(); | ||
nativeBitmap.Save(stream, ImageFormat.Bmp); | ||
stream.Position = 0; | ||
|
||
return SKBitmap.Decode(stream); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace ProcessDoctor.Backend.Windows.Imaging; | ||
|
||
[Flags] | ||
internal enum IconFlags : uint | ||
{ | ||
LargeSize = 0x0, | ||
SmallSize = 0x1, | ||
Icon = 0x100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace ProcessDoctor.Backend.Windows.Imaging; | ||
|
||
internal enum IconType : uint | ||
{ | ||
Application = 0x2 | ||
} |
22 changes: 22 additions & 0 deletions
22
ProcessDoctor.Backend.Windows/Imaging/Native/DestroyIconSafeHandle.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace ProcessDoctor.Backend.Windows.Imaging.Native; | ||
|
||
internal sealed class DestroyIconSafeHandle : SafeHandle | ||
{ | ||
private static readonly IntPtr InvalidValue = new(-1L); | ||
|
||
internal DestroyIconSafeHandle() | ||
: base(InvalidValue, ownsHandle: true) | ||
{ } | ||
|
||
internal DestroyIconSafeHandle(IntPtr preexistingHandle, bool ownsHandle = true) | ||
: base(InvalidValue, ownsHandle) | ||
=> SetHandle(preexistingHandle); | ||
|
||
public override bool IsInvalid | ||
=> handle.ToInt64() == -1L || handle.ToInt64() == 0L; | ||
|
||
protected override bool ReleaseHandle() | ||
=> User32.DestroyIcon(handle); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace ProcessDoctor.Backend.Windows.Imaging.Native; | ||
|
||
internal readonly struct HRESULT | ||
{ | ||
private readonly int _value; | ||
|
||
internal HRESULT(int value) | ||
=> _value = value; | ||
|
||
internal bool Succeeded | ||
=> _value >= 0; | ||
|
||
|
||
internal bool Failed | ||
=> _value < 0; | ||
} |
17 changes: 17 additions & 0 deletions
17
ProcessDoctor.Backend.Windows/Imaging/Native/SH_STOCK_ICON_INFO.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace ProcessDoctor.Backend.Windows.Imaging.Native; | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
public unsafe struct SH_STOCK_ICON_INFO | ||
{ | ||
public uint cbSize; | ||
|
||
public IntPtr hIcon; | ||
|
||
public int iSysIconIndex; | ||
|
||
public int iIcon; | ||
|
||
public fixed char szPath[260]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace ProcessDoctor.Backend.Windows.Imaging.Native; | ||
|
||
internal static class Shell32 | ||
{ | ||
[DllImport("shell32.dll")] | ||
public static extern HRESULT SHGetStockIconInfo(IconType siid, IconFlags uFlags, ref SH_STOCK_ICON_INFO psii); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace ProcessDoctor.Backend.Windows.Imaging.Native; | ||
|
||
internal static class User32 | ||
{ | ||
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] | ||
internal static extern bool DestroyIcon(IntPtr hIcon); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Drawing; | ||
using System.Runtime.InteropServices; | ||
using ProcessDoctor.Backend.Windows.Imaging.Native; | ||
|
||
namespace ProcessDoctor.Backend.Windows.Imaging; | ||
|
||
internal static class StockIcon | ||
{ | ||
private const string ErrorMessage = "An error occured while creating the stock icon: {0}"; | ||
|
||
internal static Icon Create(IconType type) | ||
{ | ||
var iconInformation = new SH_STOCK_ICON_INFO(); | ||
iconInformation.cbSize = (uint)Marshal.SizeOf(iconInformation); | ||
|
||
var result = Shell32.SHGetStockIconInfo(type, IconFlags.Icon | IconFlags.SmallSize, ref iconInformation); | ||
|
||
if (result.Failed) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format(ErrorMessage, type)); | ||
} | ||
|
||
using var iconHandle = new DestroyIconSafeHandle(iconInformation.hIcon, ownsHandle: true); | ||
|
||
if (iconHandle.IsInvalid) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format(ErrorMessage, type)); | ||
} | ||
|
||
return (Icon)Icon.FromHandle(iconInformation.hIcon).Clone(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
ProcessDoctor/Imaging/Extensions/ProcessModelExtensions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
using System.Reactive.Linq; | ||
using System.Threading.Tasks; | ||
using ProcessDoctor.Backend.Core; | ||
using ProcessDoctor.Imaging.Extensions; | ||
using Bitmap = Avalonia.Media.Imaging.Bitmap; | ||
using SkiaSharp; | ||
|
||
namespace ProcessDoctor.ViewModels; | ||
|
||
public record ProcessViewModel( | ||
uint Id, | ||
string Name, | ||
string? CommandLine, | ||
Task<Bitmap?> Image, | ||
IObservable<SKBitmap?> Image, | ||
ObservableCollection<ProcessViewModel> Children) | ||
{ | ||
public static ProcessViewModel Of(SystemProcess model) => new( | ||
model.Id, | ||
model.Name, | ||
model.CommandLine, | ||
model.ExtractAssociatedBitmapAsync(), | ||
Observable.FromAsync(() => Task.Run(model.ExtractIcon)), | ||
[]); | ||
} |