mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 03:07:56 +01:00
[Peek] Support for archives (#26839)
* support for archives in peek * fix spellcheck * horizontal scrolling * fix height * removed redundant helper
This commit is contained in:
committed by
GitHub
parent
67ce81ded8
commit
6ba8596d52
6
.github/actions/spell-check/expect.txt
vendored
6
.github/actions/spell-check/expect.txt
vendored
@@ -1087,6 +1087,7 @@ LIGHTORANGE
|
||||
LIGHTTURQUOISE
|
||||
lindex
|
||||
linkedin
|
||||
LINKOVERLAY
|
||||
linq
|
||||
LINQTo
|
||||
listview
|
||||
@@ -1415,6 +1416,7 @@ nullonfailure
|
||||
numberbox
|
||||
NUMLOCK
|
||||
numpad
|
||||
nupkg
|
||||
nwc
|
||||
Objbase
|
||||
OBJID
|
||||
@@ -1613,6 +1615,7 @@ psapi
|
||||
pscid
|
||||
PSECURITY
|
||||
psfgao
|
||||
psfi
|
||||
Psr
|
||||
psrm
|
||||
psrree
|
||||
@@ -1851,6 +1854,7 @@ shellscalingapi
|
||||
SHFILEINFO
|
||||
SHGDNF
|
||||
SHGFI
|
||||
shinfo
|
||||
Shl
|
||||
shldisp
|
||||
shlobj
|
||||
@@ -2056,6 +2060,7 @@ TEXCOORD
|
||||
textblock
|
||||
TEXTEXTRACTOR
|
||||
TEXTINCLUDE
|
||||
tgz
|
||||
themeresources
|
||||
THH
|
||||
THICKFRAME
|
||||
@@ -2123,6 +2128,7 @@ ULONGLONG
|
||||
unapply
|
||||
unassign
|
||||
uncompilable
|
||||
Uncompress
|
||||
UNCPRIORITY
|
||||
UNDNAME
|
||||
UNICODETEXT
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
<PackageVersion Include="NLog.Extensions.Logging" Version="5.0.4" />
|
||||
<PackageVersion Include="NLog.Schema" Version="5.0.4" />
|
||||
<PackageVersion Include="ScipBe.Common.Office.OneNote" Version="3.0.1" />
|
||||
<PackageVersion Include="SharpCompress" Version="0.33.0" />
|
||||
<PackageVersion Include="StreamJsonRpc" Version="2.14.24" />
|
||||
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435" />
|
||||
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
|
||||
|
||||
@@ -318,6 +318,7 @@ SOFTWARE.
|
||||
- NLog.Extensions.Logging 5.0.4
|
||||
- NLog.Schema 5.0.4
|
||||
- ScipBe.Common.Office.OneNote 3.0.1
|
||||
- SharpCompress 0.33.0
|
||||
- StreamJsonRpc 2.14.24
|
||||
- StyleCop.Analyzers 1.2.0-beta.435
|
||||
- System.CommandLine 2.0.0-beta4.22272.1
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Microsoft.UI.Xaml.Data;
|
||||
using Peek.Common.Helpers;
|
||||
|
||||
namespace Peek.Common.Converters
|
||||
{
|
||||
public class BytesToStringConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, string language)
|
||||
{
|
||||
if (value is ulong size)
|
||||
{
|
||||
return ReadableStringHelper.BytesToReadableString(size);
|
||||
}
|
||||
else
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, string language) => throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
137
src/modules/peek/Peek.FilePreviewer/Controls/ArchiveControl.xaml
Normal file
137
src/modules/peek/Peek.FilePreviewer/Controls/ArchiveControl.xaml
Normal file
@@ -0,0 +1,137 @@
|
||||
<!-- Copyright (c) Microsoft Corporation. All rights reserved. -->
|
||||
<!-- Licensed under the MIT License. See LICENSE in the project root for license information. -->
|
||||
|
||||
<UserControl
|
||||
x:Class="Peek.FilePreviewer.Controls.ArchiveControl"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="using:Peek.FilePreviewer.Controls"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:models="using:Peek.FilePreviewer.Previewers.Archives.Models"
|
||||
xmlns:converters="using:Peek.Common.Converters"
|
||||
mc:Ignorable="d">
|
||||
<UserControl.Resources>
|
||||
<DataTemplate
|
||||
x:Key="DirectoryTemplate"
|
||||
x:DataType="models:ArchiveItem">
|
||||
<TreeViewItem
|
||||
AutomationProperties.Name="{x:Bind Name}"
|
||||
ItemsSource="{x:Bind Children}"
|
||||
IsExpanded="{x:Bind IsExpanded}">
|
||||
<Grid ColumnSpacing="10">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="auto" />
|
||||
<ColumnDefinition Width="auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Image
|
||||
Width="16"
|
||||
Height="16"
|
||||
Grid.Column="0"
|
||||
Source="{x:Bind Icon}" />
|
||||
<TextBlock
|
||||
Grid.Column="1"
|
||||
Text="{x:Bind Name}" />
|
||||
</Grid>
|
||||
</TreeViewItem>
|
||||
</DataTemplate>
|
||||
|
||||
<DataTemplate
|
||||
x:Key="FileTemplate"
|
||||
x:DataType="models:ArchiveItem">
|
||||
<TreeViewItem
|
||||
AutomationProperties.Name="{x:Bind Name}">
|
||||
<Grid ColumnSpacing="10">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="auto" />
|
||||
<ColumnDefinition Width="auto" />
|
||||
<ColumnDefinition Width="auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Image
|
||||
Width="16"
|
||||
Height="16"
|
||||
Grid.Column="0"
|
||||
Source="{x:Bind Icon}" />
|
||||
<TextBlock
|
||||
Grid.Column="1"
|
||||
Text="{x:Bind Name}" />
|
||||
<TextBlock
|
||||
Grid.Column="2"
|
||||
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
|
||||
Text="{Binding Size, Converter={StaticResource BytesToStringConverter}}" />
|
||||
</Grid>
|
||||
</TreeViewItem>
|
||||
</DataTemplate>
|
||||
|
||||
<models:ArchiveItemTemplateSelector
|
||||
x:Key="ArchiveItemTemplateSelector"
|
||||
DirectoryTemplate="{StaticResource DirectoryTemplate}"
|
||||
FileTemplate="{StaticResource FileTemplate}" />
|
||||
|
||||
<converters:BytesToStringConverter x:Key="BytesToStringConverter" />
|
||||
</UserControl.Resources>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<ScrollViewer
|
||||
Grid.Row="0"
|
||||
HorizontalScrollBarVisibility="Auto">
|
||||
<TreeView
|
||||
x:Name="ArchivePreview"
|
||||
ItemsSource="{x:Bind Source, Mode=OneWay}"
|
||||
ItemTemplateSelector="{StaticResource ArchiveItemTemplateSelector}"
|
||||
SelectionMode="None"
|
||||
CanReorderItems="False"
|
||||
CanDragItems="False" />
|
||||
</ScrollViewer>
|
||||
<Border
|
||||
Grid.Row="1"
|
||||
MinWidth="300"
|
||||
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
|
||||
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
|
||||
BorderThickness="1"
|
||||
CornerRadius="8"
|
||||
Margin="16"
|
||||
HorizontalAlignment="Center">
|
||||
<Grid
|
||||
ColumnSpacing="16"
|
||||
Padding="16">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock
|
||||
Grid.Column="0"
|
||||
Text="{x:Bind DirectoryCount, Mode=OneWay}"
|
||||
IsTextSelectionEnabled="True"
|
||||
VerticalAlignment="Center"
|
||||
TextWrapping="Wrap" />
|
||||
<Border
|
||||
Grid.Column="1"
|
||||
BorderBrush="{ThemeResource TextFillColorPrimaryBrush}"
|
||||
BorderThickness="0 0 1 0" />
|
||||
<TextBlock
|
||||
Grid.Column="2"
|
||||
Text="{x:Bind FileCount, Mode=OneWay}"
|
||||
IsTextSelectionEnabled="True"
|
||||
VerticalAlignment="Center"
|
||||
TextWrapping="Wrap" />
|
||||
<Border
|
||||
Grid.Column="3"
|
||||
BorderBrush="{ThemeResource TextFillColorPrimaryBrush}"
|
||||
BorderThickness="0 0 1 0" />
|
||||
<TextBlock
|
||||
Grid.Column="4"
|
||||
Text="{x:Bind Size, Mode=OneWay}"
|
||||
IsTextSelectionEnabled="True"
|
||||
VerticalAlignment="Center"
|
||||
TextWrapping="Wrap" />
|
||||
</Grid>
|
||||
</Border>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,81 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.ObjectModel;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Peek.FilePreviewer.Previewers;
|
||||
using Peek.FilePreviewer.Previewers.Archives;
|
||||
using Peek.FilePreviewer.Previewers.Archives.Models;
|
||||
|
||||
namespace Peek.FilePreviewer.Controls
|
||||
{
|
||||
public sealed partial class ArchiveControl : UserControl
|
||||
{
|
||||
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(
|
||||
nameof(Source),
|
||||
typeof(ObservableCollection<ArchiveItem>),
|
||||
typeof(ArchivePreviewer),
|
||||
new PropertyMetadata(null));
|
||||
|
||||
public static readonly DependencyProperty LoadingStateProperty = DependencyProperty.Register(
|
||||
nameof(LoadingState),
|
||||
typeof(PreviewState),
|
||||
typeof(ArchivePreviewer),
|
||||
new PropertyMetadata(PreviewState.Uninitialized));
|
||||
|
||||
public static readonly DependencyProperty DirectoryCountProperty = DependencyProperty.Register(
|
||||
nameof(DirectoryCount),
|
||||
typeof(string),
|
||||
typeof(ArchivePreviewer),
|
||||
new PropertyMetadata(null));
|
||||
|
||||
public static readonly DependencyProperty FileCountProperty = DependencyProperty.Register(
|
||||
nameof(FileCount),
|
||||
typeof(string),
|
||||
typeof(ArchivePreviewer),
|
||||
new PropertyMetadata(null));
|
||||
|
||||
public static readonly DependencyProperty SizeProperty = DependencyProperty.Register(
|
||||
nameof(Size),
|
||||
typeof(string),
|
||||
typeof(ArchivePreviewer),
|
||||
new PropertyMetadata(null));
|
||||
|
||||
public ObservableCollection<ArchiveItem>? Source
|
||||
{
|
||||
get { return (ObservableCollection<ArchiveItem>)GetValue(SourceProperty); }
|
||||
set { SetValue(SourceProperty, value); }
|
||||
}
|
||||
|
||||
public PreviewState? LoadingState
|
||||
{
|
||||
get { return (PreviewState)GetValue(LoadingStateProperty); }
|
||||
set { SetValue(LoadingStateProperty, value); }
|
||||
}
|
||||
|
||||
public string? DirectoryCount
|
||||
{
|
||||
get { return (string)GetValue(DirectoryCountProperty); }
|
||||
set { SetValue(DirectoryCountProperty, value); }
|
||||
}
|
||||
|
||||
public string? FileCount
|
||||
{
|
||||
get { return (string)GetValue(FileCountProperty); }
|
||||
set { SetValue(FileCountProperty, value); }
|
||||
}
|
||||
|
||||
public string? Size
|
||||
{
|
||||
get { return (string)GetValue(SizeProperty); }
|
||||
set { SetValue(SizeProperty, value); }
|
||||
}
|
||||
|
||||
public ArchiveControl()
|
||||
{
|
||||
this.InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<!-- Copyright (c) Microsoft Corporation and Contributors. -->
|
||||
<!-- Copyright (c) Microsoft Corporation and Contributors. -->
|
||||
<!-- Licensed under the MIT License. -->
|
||||
|
||||
<UserControl
|
||||
@@ -51,6 +51,15 @@
|
||||
Source="{x:Bind BrowserPreviewer.Preview, Mode=OneWay}"
|
||||
Visibility="{x:Bind IsPreviewVisible(BrowserPreviewer, Previewer.State), Mode=OneWay, FallbackValue=Collapsed}" />
|
||||
|
||||
<controls:ArchiveControl
|
||||
x:Name="ArchivePreview"
|
||||
LoadingState="{x:Bind ArchivePreviewer.State, Mode=OneWay}"
|
||||
Source="{x:Bind ArchivePreviewer.Tree, Mode=OneWay}"
|
||||
FileCount="{x:Bind ArchivePreviewer.FileCountText, Mode=OneWay}"
|
||||
DirectoryCount="{x:Bind ArchivePreviewer.DirectoryCountText, Mode=OneWay}"
|
||||
Size="{x:Bind ArchivePreviewer.SizeText, Mode=OneWay}"
|
||||
Visibility="{x:Bind IsPreviewVisible(ArchivePreviewer, Previewer.State), Mode=OneWay}" />
|
||||
|
||||
<controls:UnsupportedFilePreview
|
||||
x:Name="UnsupportedFilePreview"
|
||||
LoadingState="{x:Bind UnsupportedFilePreviewer.State, Mode=OneWay}"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
@@ -49,6 +49,7 @@ namespace Peek.FilePreviewer
|
||||
[NotifyPropertyChangedFor(nameof(ImagePreviewer))]
|
||||
[NotifyPropertyChangedFor(nameof(VideoPreviewer))]
|
||||
[NotifyPropertyChangedFor(nameof(BrowserPreviewer))]
|
||||
[NotifyPropertyChangedFor(nameof(ArchivePreviewer))]
|
||||
[NotifyPropertyChangedFor(nameof(UnsupportedFilePreviewer))]
|
||||
|
||||
private IPreviewer? previewer;
|
||||
@@ -94,6 +95,8 @@ namespace Peek.FilePreviewer
|
||||
|
||||
public IBrowserPreviewer? BrowserPreviewer => Previewer as IBrowserPreviewer;
|
||||
|
||||
public IArchivePreviewer? ArchivePreviewer => Previewer as IArchivePreviewer;
|
||||
|
||||
public IUnsupportedFilePreviewer? UnsupportedFilePreviewer => Previewer as IUnsupportedFilePreviewer;
|
||||
|
||||
public IFileSystemItem Item
|
||||
@@ -145,6 +148,7 @@ namespace Peek.FilePreviewer
|
||||
ImagePreview.Visibility = Visibility.Collapsed;
|
||||
VideoPreview.Visibility = Visibility.Collapsed;
|
||||
BrowserPreview.Visibility = Visibility.Collapsed;
|
||||
ArchivePreview.Visibility = Visibility.Collapsed;
|
||||
UnsupportedFilePreview.Visibility = Visibility.Collapsed;
|
||||
return;
|
||||
}
|
||||
@@ -207,6 +211,7 @@ namespace Peek.FilePreviewer
|
||||
VideoPreview.Source = null;
|
||||
|
||||
ImagePreview.Source = null;
|
||||
ArchivePreview.Source = null;
|
||||
BrowserPreview.Source = null;
|
||||
|
||||
if (Previewer != null)
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
</PRIResource>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="Controls\ArchiveControl.xaml" />
|
||||
<None Remove="Controls\BrowserControl.xaml" />
|
||||
<None Remove="Controls\UnsupportedFilePreview\FailedFallbackPreviewControl.xaml" />
|
||||
<None Remove="Controls\UnsupportedFilePreview\InformationalPreviewControl.xaml" />
|
||||
@@ -25,6 +26,7 @@
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" />
|
||||
<PackageReference Include="Microsoft.WindowsAppSDK" />
|
||||
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" />
|
||||
<PackageReference Include="SharpCompress" />
|
||||
<PackageReference Include="System.Drawing.Common" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -35,6 +37,12 @@
|
||||
<ProjectReference Include="..\Peek.Common\Peek.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Page Update="Controls\ArchiveControl.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Page Update="Controls\UnsupportedFilePreview\FailedFallbackPreviewControl.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Peek.Common.Extensions;
|
||||
using Peek.Common.Helpers;
|
||||
using Peek.Common.Models;
|
||||
using Peek.FilePreviewer.Models;
|
||||
using Peek.FilePreviewer.Previewers.Archives.Helpers;
|
||||
using Peek.FilePreviewer.Previewers.Archives.Models;
|
||||
using Peek.FilePreviewer.Previewers.Interfaces;
|
||||
using SharpCompress.Archives;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Readers;
|
||||
using Windows.ApplicationModel.Resources;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers.Archives
|
||||
{
|
||||
public partial class ArchivePreviewer : ObservableObject, IArchivePreviewer
|
||||
{
|
||||
private readonly IconCache _iconCache = new();
|
||||
private int _directoryCount;
|
||||
private int _fileCount;
|
||||
private ulong _size;
|
||||
private ulong _extractedSize;
|
||||
|
||||
[ObservableProperty]
|
||||
private PreviewState state;
|
||||
|
||||
[ObservableProperty]
|
||||
private string? _directoryCountText;
|
||||
|
||||
[ObservableProperty]
|
||||
private string? _fileCountText;
|
||||
|
||||
[ObservableProperty]
|
||||
private string? _sizeText;
|
||||
|
||||
private IFileSystemItem Item { get; }
|
||||
|
||||
private DispatcherQueue Dispatcher { get; }
|
||||
|
||||
public ObservableCollection<ArchiveItem> Tree { get; }
|
||||
|
||||
public ArchivePreviewer(IFileSystemItem file)
|
||||
{
|
||||
Item = file;
|
||||
Dispatcher = DispatcherQueue.GetForCurrentThread();
|
||||
Tree = new ObservableCollection<ArchiveItem>();
|
||||
}
|
||||
|
||||
public async Task CopyAsync()
|
||||
{
|
||||
await Dispatcher.RunOnUiThread(async () =>
|
||||
{
|
||||
var storageItem = await Item.GetStorageItemAsync();
|
||||
ClipboardHelper.SaveToClipboard(storageItem);
|
||||
});
|
||||
}
|
||||
|
||||
public Task<PreviewSize> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.FromResult(new PreviewSize { MonitorSize = null });
|
||||
}
|
||||
|
||||
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
State = PreviewState.Loading;
|
||||
using var stream = File.OpenRead(Item.Path);
|
||||
|
||||
if (Item.Path.EndsWith(".tar.gz", StringComparison.OrdinalIgnoreCase) || Item.Path.EndsWith(".tgz", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
using var archive = ArchiveFactory.Open(stream);
|
||||
_extractedSize = (ulong)archive.TotalUncompressSize;
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
using var reader = ReaderFactory.Open(stream);
|
||||
while (reader.MoveToNextEntry())
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await AddEntryAsync(reader.Entry, cancellationToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
using var archive = ArchiveFactory.Open(stream);
|
||||
_extractedSize = (ulong)archive.TotalUncompressSize;
|
||||
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await AddEntryAsync(entry, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
_size = (ulong)new FileInfo(Item.Path).Length; // archive.TotalSize isn't accurate
|
||||
DirectoryCountText = string.Format(CultureInfo.CurrentCulture, ResourceLoader.GetForViewIndependentUse().GetString("Archive_Directory_Count"), _directoryCount);
|
||||
FileCountText = string.Format(CultureInfo.CurrentCulture, ResourceLoader.GetForViewIndependentUse().GetString("Archive_File_Count"), _fileCount);
|
||||
SizeText = string.Format(CultureInfo.CurrentCulture, ResourceLoader.GetForViewIndependentUse().GetString("Archive_Size"), ReadableStringHelper.BytesToReadableString(_size), ReadableStringHelper.BytesToReadableString(_extractedSize));
|
||||
|
||||
State = PreviewState.Loaded;
|
||||
}
|
||||
|
||||
public static bool IsFileTypeSupported(string fileExt)
|
||||
{
|
||||
return _supportedFileTypes.Contains(fileExt);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private async Task AddEntryAsync(IEntry entry, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(entry, nameof(entry));
|
||||
|
||||
var levels = entry!.Key
|
||||
.Split('/', '\\')
|
||||
.Where(l => !string.IsNullOrWhiteSpace(l))
|
||||
.ToArray();
|
||||
|
||||
ArchiveItem? parent = null;
|
||||
for (var i = 0; i < levels.Length; i++)
|
||||
{
|
||||
var type = (!entry.IsDirectory && i == levels.Length - 1) ? ArchiveItemType.File : ArchiveItemType.Directory;
|
||||
|
||||
var icon = type == ArchiveItemType.Directory
|
||||
? await _iconCache.GetDirectoryIconAsync(cancellationToken)
|
||||
: await _iconCache.GetFileExtIconAsync(entry.Key, cancellationToken);
|
||||
|
||||
var item = new ArchiveItem(levels[i], type, icon);
|
||||
|
||||
if (type == ArchiveItemType.Directory)
|
||||
{
|
||||
item.IsExpanded = parent == null; // Only the root level is expanded
|
||||
}
|
||||
else if (type == ArchiveItemType.File)
|
||||
{
|
||||
item.Size = (ulong)entry.Size;
|
||||
}
|
||||
|
||||
if (parent == null)
|
||||
{
|
||||
var existing = Tree.FirstOrDefault(e => e.Name == item.Name);
|
||||
if (existing == null)
|
||||
{
|
||||
var index = GetIndex(Tree, item);
|
||||
Tree.Insert(index, item);
|
||||
CountItem(item);
|
||||
}
|
||||
|
||||
parent = existing ?? Tree.First(e => e.Name == item.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
var existing = parent.Children.FirstOrDefault(e => e.Name == item.Name);
|
||||
if (existing == null)
|
||||
{
|
||||
var index = GetIndex(parent.Children, item);
|
||||
parent.Children.Insert(index, item);
|
||||
CountItem(item);
|
||||
}
|
||||
|
||||
parent = existing ?? parent.Children.First(e => e.Name == item.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int GetIndex(ObservableCollection<ArchiveItem> collection, ArchiveItem item)
|
||||
{
|
||||
for (var i = 0; i < collection.Count; i++)
|
||||
{
|
||||
if (item.Type == collection[i].Type && string.Compare(collection[i].Name, item.Name, StringComparison.OrdinalIgnoreCase) > 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return item.Type switch
|
||||
{
|
||||
ArchiveItemType.Directory => collection.Count(e => e.Type == ArchiveItemType.Directory),
|
||||
ArchiveItemType.File => collection.Count,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
|
||||
private void CountItem(ArchiveItem item)
|
||||
{
|
||||
if (item.Type == ArchiveItemType.Directory)
|
||||
{
|
||||
_directoryCount++;
|
||||
}
|
||||
else if (item.Type == ArchiveItemType.File)
|
||||
{
|
||||
_fileCount++;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly HashSet<string> _supportedFileTypes = new()
|
||||
{
|
||||
".zip", ".rar", ".7z", ".tar", ".nupkg", ".jar", ".gz", ".tar", ".tar.gz", ".tgz",
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
using Peek.Common;
|
||||
using Peek.Common.Helpers;
|
||||
using Peek.Common.Models;
|
||||
using Peek.FilePreviewer.Previewers.Helpers;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers.Archives.Helpers
|
||||
{
|
||||
public class IconCache
|
||||
{
|
||||
private readonly Dictionary<string, BitmapSource> _cache = new();
|
||||
|
||||
private BitmapSource? _directoryIconCache;
|
||||
|
||||
public async Task<BitmapSource?> GetFileExtIconAsync(string fileName, CancellationToken cancellationToken)
|
||||
{
|
||||
var extension = Path.GetExtension(fileName);
|
||||
|
||||
if (_cache.TryGetValue(extension, out var cachedIcon))
|
||||
{
|
||||
return cachedIcon;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var shFileInfo = default(SHFILEINFO);
|
||||
if (NativeMethods.SHGetFileInfo(fileName, NativeMethods.FILE_ATTRIBUTE_NORMAL, ref shFileInfo, (uint)Marshal.SizeOf(shFileInfo), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_USEFILEATTRIBUTES) != IntPtr.Zero)
|
||||
{
|
||||
var imageSource = await BitmapHelper.GetBitmapFromHIconAsync(shFileInfo.HIcon, cancellationToken);
|
||||
_cache.Add(extension, imageSource);
|
||||
return imageSource;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.LogError($"Icon extraction for extension {extension} failed with error {Marshal.GetLastWin32Error()}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError($"Icon extraction for extension {extension} failed", ex);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<BitmapSource?> GetDirectoryIconAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_directoryIconCache != null)
|
||||
{
|
||||
return _directoryIconCache;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var shinfo = default(SHFILEINFO);
|
||||
if (NativeMethods.SHGetFileInfo("directory", NativeMethods.FILE_ATTRIBUTE_DIRECTORY, ref shinfo, (uint)Marshal.SizeOf(shinfo), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_USEFILEATTRIBUTES) != IntPtr.Zero)
|
||||
{
|
||||
var imageSource = await BitmapHelper.GetBitmapFromHIconAsync(shinfo.HIcon, cancellationToken);
|
||||
_directoryIconCache = imageSource;
|
||||
return imageSource;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.LogError($"Icon extraction for directory failed with error {Marshal.GetLastWin32Error()}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError($"Icon extraction for directory failed", ex);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers.Archives.Models
|
||||
{
|
||||
public partial class ArchiveItem : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string _name;
|
||||
|
||||
[ObservableProperty]
|
||||
private ArchiveItemType _type;
|
||||
|
||||
[ObservableProperty]
|
||||
private ImageSource? _icon;
|
||||
|
||||
[ObservableProperty]
|
||||
private ulong _size;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _isExpanded;
|
||||
|
||||
public ObservableCollection<ArchiveItem> Children { get; }
|
||||
|
||||
public ArchiveItem(string name, ArchiveItemType type, ImageSource? icon)
|
||||
{
|
||||
Name = name;
|
||||
Type = type;
|
||||
Icon = icon;
|
||||
Children = new ObservableCollection<ArchiveItem>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers.Archives.Models
|
||||
{
|
||||
public class ArchiveItemTemplateSelector : DataTemplateSelector
|
||||
{
|
||||
public DataTemplate? DirectoryTemplate { get; set; }
|
||||
|
||||
public DataTemplate? FileTemplate { get; set; }
|
||||
|
||||
protected override DataTemplate? SelectTemplateCore(object item)
|
||||
{
|
||||
if (item is ArchiveItem archiveItem)
|
||||
{
|
||||
return archiveItem.Type == ArchiveItemType.Directory ? DirectoryTemplate : FileTemplate;
|
||||
}
|
||||
|
||||
throw new ArgumentException("Item must be an ArchiveItem", nameof(item));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers.Archives.Models
|
||||
{
|
||||
public enum ArchiveItemType
|
||||
{
|
||||
Directory = 0,
|
||||
File = 1,
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
@@ -18,7 +19,7 @@ namespace Peek.FilePreviewer.Previewers.Helpers
|
||||
{
|
||||
try
|
||||
{
|
||||
var bitmap = System.Drawing.Image.FromHbitmap(hbitmap);
|
||||
var bitmap = Image.FromHbitmap(hbitmap);
|
||||
if (isSupportingTransparency)
|
||||
{
|
||||
bitmap.MakeTransparent();
|
||||
@@ -44,5 +45,32 @@ namespace Peek.FilePreviewer.Previewers.Helpers
|
||||
NativeMethods.DeleteObject(hbitmap);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<BitmapSource> GetBitmapFromHIconAsync(IntPtr hicon, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var icon = (Icon)Icon.FromHandle(hicon).Clone();
|
||||
var bitmap = icon.ToBitmap();
|
||||
|
||||
var bitmapImage = new BitmapImage();
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
bitmap.Save(stream, ImageFormat.Png);
|
||||
stream.Position = 0;
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await bitmapImage.SetSourceAsync(stream.AsRandomAccessStream());
|
||||
}
|
||||
|
||||
return bitmapImage;
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Delete HIcon to avoid memory leaks
|
||||
_ = NativeMethods.DestroyIcon(hicon);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using Peek.FilePreviewer.Previewers.Archives.Models;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers.Interfaces
|
||||
{
|
||||
public interface IArchivePreviewer : IPreviewer, IDisposable
|
||||
{
|
||||
ObservableCollection<ArchiveItem> Tree { get; }
|
||||
|
||||
string? DirectoryCountText { get; }
|
||||
|
||||
string? FileCountText { get; }
|
||||
|
||||
string? SizeText { get; }
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,13 @@ namespace Peek.Common
|
||||
{
|
||||
public static class NativeMethods
|
||||
{
|
||||
internal const uint SHGFI_ICON = 0x000000100;
|
||||
internal const uint SHGFI_LINKOVERLAY = 0x000008000;
|
||||
internal const uint SHGFI_SMALLICON = 0x000000001;
|
||||
internal const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
|
||||
internal const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
|
||||
internal const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
|
||||
|
||||
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
internal static extern int SHCreateItemFromParsingName(
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string path,
|
||||
@@ -17,8 +24,14 @@ namespace Peek.Common
|
||||
ref Guid riid,
|
||||
[MarshalAs(UnmanagedType.Interface)] out IShellItem shellItem);
|
||||
|
||||
[DllImport("User32.dll", SetLastError = true)]
|
||||
internal static extern int DestroyIcon(IntPtr hIcon);
|
||||
|
||||
[DllImport("gdi32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static extern bool DeleteObject(IntPtr hObject);
|
||||
|
||||
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
internal static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Peek.Common.Models;
|
||||
using Peek.FilePreviewer.Previewers.Archives;
|
||||
using Peek.UI.Telemetry.Events;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers
|
||||
@@ -24,6 +25,10 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
return new WebBrowserPreviewer(file);
|
||||
}
|
||||
else if (ArchivePreviewer.IsFileTypeSupported(file.Extension))
|
||||
{
|
||||
return new ArchivePreviewer(file);
|
||||
}
|
||||
|
||||
// Other previewer types check their supported file types here
|
||||
return CreateDefaultPreviewer(file);
|
||||
|
||||
@@ -221,4 +221,16 @@
|
||||
<value>An error occurred while previewing this file.</value>
|
||||
<comment>Failed fallback preview text.</comment>
|
||||
</data>
|
||||
<data name="Archive_Directory_Count" xml:space="preserve">
|
||||
<value>{0} directories</value>
|
||||
<comment>{0} is the number of directories in the archive</comment>
|
||||
</data>
|
||||
<data name="Archive_File_Count" xml:space="preserve">
|
||||
<value>{0} files</value>
|
||||
<comment>{0} is the number of files in the archive</comment>
|
||||
</data>
|
||||
<data name="Archive_Size" xml:space="preserve">
|
||||
<value>{0} (extracted {1})</value>
|
||||
<comment>{0} is the size of the archive, {1} is the extracted size</comment>
|
||||
</data>
|
||||
</root>
|
||||
Reference in New Issue
Block a user