Skip to content

Commit

Permalink
Add StockIcon Support to ExplorerBrowser (#8) (#32)
Browse files Browse the repository at this point in the history
* Replace default Folder Image with native StockIcon

* Replace StockIcon folders and / or files
  • Loading branch information
tajbender authored Oct 12, 2024
2 parents 2d1e230 + 1b66f70 commit 87d1d3d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 40 deletions.
52 changes: 16 additions & 36 deletions src/electrifier/Controls/Vanara/ExplorerBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@

namespace electrifier.Controls.Vanara;

// INFO: Care for this: // Remember! We're not the owner of the given PIDL, so we have to make our own copy for our own heap! See Issue #158
// TODO: INFO: See also https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.image.source?view=windows-app-sdk-1.5#microsoft-ui-xaml-controls-image-source

// TODO: WARN: ExplorerBrowser doesn't show anything when hiding Shell32TreeView, cause of missing navigation

// https://github.com/dahall/Vanara/blob/master/Windows.Forms/Controls/ExplorerBrowser.cs
// TODO: See also https://github.com/dahall/Vanara/blob/ac0a1ac301dd4fdea9706688dedf96d596a4908a/Windows.Shell.Common/StockIcon.cs

/* TODO: Research this regarding Visual States
[Microsoft.UI.Xaml.TemplatePart(Name="Image", Type=typeof(System.Object))]
Expand All @@ -32,19 +26,6 @@ namespace electrifier.Controls.Vanara;
*/
public sealed partial class ExplorerBrowser : INotifyPropertyChanged
{
// TODO: Use shell32 stock icons
internal static readonly BitmapImage DefaultFileImage =
new(new Uri("ms-appx:///Assets/Views/Workbench/Shell32 Default unknown File.ico"));

internal static readonly BitmapImage DefaultFolderImage =
new(new Uri("ms-appx:///Assets/Views/Workbench/Shell32 Default Folder.ico"));

internal static readonly BitmapImage DefaultLibraryImage =
new(new Uri("ms-appx:///Assets/Views/Workbench/Shell32 Library.ico"));

/// <summary>
///
/// </summary>
public ExplorerBrowserItem? CurrentFolderBrowserItem
{
get => GetValue(CurrentFolderBrowserItemProperty) as ExplorerBrowserItem;
Expand Down Expand Up @@ -213,14 +194,15 @@ private async Task InitializeViewModel()
var galleryEbItem = new ExplorerBrowserItem(galleryFolder);
rootItems.Add(galleryEbItem);

InitializeStockIcons();
await InitializeStockIcons();

ShellTreeView.ItemsSource = rootItems;
CurrentFolderBrowserItem.IsExpanded = true;
CurrentFolderBrowserItem.IsSelected = true;
}

private SoftwareBitmapSource _defaultFolderImageBitmapSource;
private SoftwareBitmapSource _defaultDocumentAssocImageBitmapSource;

/// <summary>
/// DUMMY: TODO: InitializeStockIcons()
Expand All @@ -229,31 +211,26 @@ private async Task InitializeViewModel()
/// <see cref="GetWinUi3BitmapSourceFromIcon"/>
/// <see cref="GetWinUi3BitmapSourceFromGdiBitmap"/>
/// </summary>
public void InitializeStockIcons()
public async Task InitializeStockIcons()
{
try
{
using var siFolder = new StockIcon(Shell32.SHSTOCKICONID.SIID_FOLDER);

var loc = siFolder.Location; // TODO: Why do we have to call this first, to init the StockIcon?

//using var siFolderOpen = new StockIcon(Shell32.SHSTOCKICONID.SIID_FOLDEROPEN);
// TODO: Opened Folder Icon, use for selected TreeViewItems
//using var siVar = new StockIcon(Shell32.SHSTOCKICONID.SIID_DOCASSOC);

var icnHandle = siFolder.IconHandle.ToIcon();
//HICON handle = siFolder.IconHandle;
//var icon = siFolder.IconHandle.ToIcon();
//if (icnHandle != null)
{
//var icon = Icon.FromHandle((nint)icnHandle);
var idx = siFolder.SystemImageIndex;
var icnHandle = siFolder.IconHandle.ToIcon();
var bmpSource = GetWinUi3BitmapSourceFromIcon(icnHandle);
_defaultFolderImageBitmapSource = await bmpSource;
}


//_defaultFolderImageBitmapSource = bmpSource;
using var siDocument = new StockIcon(Shell32.SHSTOCKICONID.SIID_DOCASSOC); // SIID_DOCNOASSOC
{
var idx = siDocument.SystemImageIndex;
var icnHandle = siDocument.IconHandle.ToIcon();
var bmpSource = GetWinUi3BitmapSourceFromIcon(icnHandle);
_defaultDocumentAssocImageBitmapSource = await bmpSource;
}

//System.Drawing.Icon icn = Icon.FromHandle((IntPtr)siFolder.IconHandle);
}
catch (Exception e)
{
Expand Down Expand Up @@ -311,6 +288,7 @@ public void ExtractChildItems(ExplorerBrowserItem targetFolder)
foreach (var child in shellItems)
{
var ebItem = new ExplorerBrowserItem(child);
ebItem.BitmapSource = ebItem.IsFolder ? this._defaultFolderImageBitmapSource : this._defaultDocumentAssocImageBitmapSource;
targetFolder.Children?.Add(ebItem);
}
}
Expand Down Expand Up @@ -450,6 +428,8 @@ public static async Task<SoftwareBitmapSource> GetWinUi3BitmapSourceFromIcon(Sys

/// <summary>
/// Taken from <see href="https://stackoverflow.com/questions/76640972/convert-system-drawing-icon-to-microsoft-ui-xaml-imagesource"/>
/// See also <see href="https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.image.source?view=windows-app-sdk-1.5#microsoft-ui-xaml-controls-image-source"/>
/// See also <see href="https://github.com/dahall/Vanara/blob/ac0a1ac301dd4fdea9706688dedf96d596a4908a/Windows.Shell.Common/StockIcon.cs"/>
/// </summary>
/// <param name="icon"></param>
/// <returns></returns>
Expand Down
3 changes: 3 additions & 0 deletions src/electrifier/Controls/Vanara/ExplorerBrowserItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.CompilerServices;
using System.Diagnostics;
using System.ComponentModel;
using Microsoft.UI.Xaml.Media.Imaging;

namespace electrifier.Controls.Vanara;

Expand Down Expand Up @@ -63,6 +64,8 @@ public ShellItem ShellItem
get;
}

public SoftwareBitmapSource BitmapSource { get; set; }


public ExplorerBrowserItem(ShellItem shItem)
{
Expand Down
3 changes: 1 addition & 2 deletions src/electrifier/Controls/Vanara/Shell32GridView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
<Image
Name="Image"
Height="48"
Source="ms-appx:///Assets/Views/Workbench/Shell32 Default unknown File_64x64-32.png" />
<!-- Source="{x:Bind ImageIconSource}" -->
Source="{x:Bind BitmapSource}" />
<!-- ImageExOpened="ImageEx_ImageExOpened" -->
<!-- CornerRadius="8" -->
<TextBlock
Expand Down
3 changes: 1 addition & 2 deletions src/electrifier/Controls/Vanara/Shell32TreeView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
<StackPanel MaxHeight="20" Orientation="Horizontal">
<Image
Name="ExplorerBrowserItemImageEx"
Source="ms-appx:///Assets/Views/Workbench/Shell32 Default Folder_16x16-32.png"/>
<!-- Source="{x:Bind ImageIconSource}" -->
Source="{x:Bind BitmapSource}"/>
<TextBlock
x:Name="ExplorerBrowserItemDisplayNameTextBlock"
Margin="5,0,5,0"
Expand Down

0 comments on commit 87d1d3d

Please sign in to comment.