Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added 'compress' attribute option to the properties window #16319

Merged
merged 20 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Files.App.CsWin32/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,7 @@ PSGetPropertyKeyFromName
ShellExecuteEx
CoTaskMemFree
QueryDosDevice
DeviceIoControl
GetLastError
CreateFile
GetVolumeInformation
18 changes: 18 additions & 0 deletions src/Files.App/Data/Models/SelectedItemsPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,24 @@ public bool? IsHiddenEditedValue
set => SetProperty(ref isHiddenEditedValue, value);
}

private bool? isContentCompressed;
public bool? IsContentCompressed
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
{
get => isContentCompressed;
set
{
SetProperty(ref isContentCompressed, value);
IsContentCompressedEditedValue = value;
}
}

private bool? isContentCompressedEditedValue;
public bool? IsContentCompressedEditedValue
{
get => isContentCompressedEditedValue;
set => SetProperty(ref isContentCompressedEditedValue, value);
}

private bool runAsAdmin;
public bool RunAsAdmin
{
Expand Down
36 changes: 36 additions & 0 deletions src/Files.App/Helpers/Win32/Win32Helper.Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using System.Windows.Forms;
using Vanara.PInvoke;
using Windows.System;
using Windows.Win32;
using Windows.Win32.Storage.FileSystem;
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
yaira2 marked this conversation as resolved.
Show resolved Hide resolved

namespace Files.App.Helpers
{
Expand Down Expand Up @@ -935,6 +937,40 @@ public static bool UnsetFileAttribute(string lpFileName, FileAttributes dwAttrs)
return Win32PInvoke.SetFileAttributesFromApp(lpFileName, lpFileInfo.dwFileAttributes & ~dwAttrs);
}

public static unsafe bool SetCompressionAttributeIoctl(string lpFileName, bool isCompressed)
{
using var hFile = PInvoke.CreateFile(
lpFileName,
Win32PInvoke.GENERIC_READ | Win32PInvoke.GENERIC_WRITE,
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
FILE_SHARE_MODE.FILE_SHARE_READ | FILE_SHARE_MODE.FILE_SHARE_WRITE,
lpSecurityAttributes: null,
FILE_CREATION_DISPOSITION.OPEN_EXISTING,
FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL,
hTemplateFile: null);

if (hFile.IsInvalid)
return false;

var bytesReturned = 0u;
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
var compressionFormat = isCompressed
? Win32PInvoke.COMPRESSION_FORMAT_DEFAULT
: Win32PInvoke.COMPRESSION_FORMAT_NONE;
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved

var result = PInvoke.DeviceIoControl(
new(hFile.DangerousGetHandle()),
Win32PInvoke.FSCTL_SET_COMPRESSION,
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
&compressionFormat,
sizeof(ushort),
(void*)IntPtr.Zero,
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
0u,
&bytesReturned);

if (result.Value == 0)
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
return false;

return true;
}

public static string ReadStringFromFile(string filePath)
{
IntPtr hFile = Win32PInvoke.CreateFileFromApp(filePath,
Expand Down
14 changes: 11 additions & 3 deletions src/Files.App/Helpers/Win32/Win32PInvoke.Consts.cs
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public static partial class Win32PInvoke
public const int FILE_SHARE_WRITE = 0x00000002;
public const uint FILE_SHARE_DELETE = 0x00000004;
public const int OPEN_EXISTING = 3;
public const int FSCTL_LOCK_VOLUME = 0x00090018;
public const int FSCTL_DISMOUNT_VOLUME = 0x00090020;
public const int IOCTL_STORAGE_EJECT_MEDIA = 0x2D4808;
public const int IOCTL_STORAGE_MEDIA_REMOVAL = 0x002D4804;

Expand All @@ -45,8 +43,18 @@ public static partial class Win32PInvoke
public const uint OPEN_ALWAYS = 4;
public const uint TRUNCATE_EXISTING = 5;

public const int MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 16 * 1024;
// FSCTL
public const int FSCTL_GET_REPARSE_POINT = 0x000900A8;
public const int FSCTL_LOCK_VOLUME = 0x00090018;
public const int FSCTL_DISMOUNT_VOLUME = 0x00090020;
public const uint FSCTL_SET_COMPRESSION = 0x9C040u;
yaira2 marked this conversation as resolved.
Show resolved Hide resolved

// Compression
public const ushort COMPRESSION_FORMAT_NONE = 0x0000;
public const ushort COMPRESSION_FORMAT_DEFAULT = 0x0001;
yaira2 marked this conversation as resolved.
Show resolved Hide resolved


public const int MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 16 * 1024;
public const uint IO_REPARSE_TAG_MOUNT_POINT = 0xA0000003;
public const uint IO_REPARSE_TAG_SYMLINK = 0xA000000C;

Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3950,4 +3950,7 @@
<data name="BulkRename" xml:space="preserve">
<value>Bulk rename</value>
</data>
<data name="CompressContents" xml:space="preserve">
<value>Compress contents</value>
</data>
</root>
22 changes: 22 additions & 0 deletions src/Files.App/ViewModels/Properties/Items/CombinedProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@ private void ViewModel_PropertyChanged(object sender, System.ComponentModel.Prop

}
break;

case "IsContentCompressed":
{
if (ViewModel.IsContentCompressed is not null)
{
if (ViewModel.IsContentCompressed ?? false)
{
List.ForEach(x =>
{
Win32Helper.SetCompressionAttributeIoctl(x.ItemPath, true);
});
}
else
{
List.ForEach(x =>
{
Win32Helper.SetCompressionAttributeIoctl(x.ItemPath, false);
});
}
}
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
}
break;
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/Files.App/ViewModels/Properties/Items/FileProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override void GetBaseProperties()
ViewModel.CustomIconSource = Item.CustomIconSource;
ViewModel.LoadFileIcon = Item.LoadFileIcon;
ViewModel.IsDownloadedFile = Win32Helper.ReadStringFromFile($"{Item.ItemPath}:Zone.Identifier") is not null;
ViewModel.IsEditAlbumCoverVisible =
ViewModel.IsEditAlbumCoverVisible =
FileExtensionHelpers.IsVideoFile(Item.FileExtension) ||
FileExtensionHelpers.IsAudioFile(Item.FileExtension);

Expand Down Expand Up @@ -97,6 +97,8 @@ public override async Task GetSpecialPropertiesAsync()
Item.ItemPath, System.IO.FileAttributes.ReadOnly);
ViewModel.IsHidden = Win32Helper.HasFileAttribute(
Item.ItemPath, System.IO.FileAttributes.Hidden);
ViewModel.IsContentCompressed = Win32Helper.HasFileAttribute(
Item.ItemPath, System.IO.FileAttributes.Compressed);

ViewModel.ItemSizeVisibility = true;
ViewModel.ItemSize = Item.FileSizeBytes.ToLongSizeString();
Expand Down Expand Up @@ -279,13 +281,23 @@ private async void ViewModel_PropertyChanged(object sender, System.ComponentMode
if (ViewModel.IsHidden is not null)
{
if ((bool)ViewModel.IsHidden)
Win32Helper.SetFileAttribute(Item.ItemPath, System.IO.FileAttributes.Hidden);
Win32Helper.SetFileAttribute(Item.ItemPath, System.IO.FileAttributes.Hidden);
else
Win32Helper.UnsetFileAttribute(Item.ItemPath, System.IO.FileAttributes.Hidden);
}

break;

case nameof(ViewModel.IsContentCompressed):
if (ViewModel.IsContentCompressed is not null)
{
if ((bool)ViewModel.IsContentCompressed)
Win32Helper.SetCompressionAttributeIoctl(Item.ItemPath, true);
else
Win32Helper.SetCompressionAttributeIoctl(Item.ItemPath, false);
}
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
break;

case nameof(ViewModel.RunAsAdmin):
case nameof(ViewModel.ShortcutItemPath):
case nameof(ViewModel.ShortcutItemWorkingDir):
Expand Down
13 changes: 13 additions & 0 deletions src/Files.App/ViewModels/Properties/Items/FolderProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public async override Task GetSpecialPropertiesAsync()
ViewModel.IsHidden = Win32Helper.HasFileAttribute(
Item.ItemPath, System.IO.FileAttributes.Hidden);

ViewModel.IsContentCompressed = Win32Helper.HasFileAttribute(
Item.ItemPath, System.IO.FileAttributes.Compressed);

var result = await FileThumbnailHelper.GetIconAsync(
Item.ItemPath,
Constants.ShellIconSizes.ExtraLarge,
Expand Down Expand Up @@ -211,6 +214,16 @@ private async void ViewModel_PropertyChanged(object sender, System.ComponentMode
}
break;

case "IsContentCompressed":
if (ViewModel.IsContentCompressed is not null)
{
if ((bool)ViewModel.IsContentCompressed)
Win32Helper.SetCompressionAttributeIoctl(Item.ItemPath, true);
else
Win32Helper.SetCompressionAttributeIoctl(Item.ItemPath, false);
}
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
break;

case "ShortcutItemPath":
case "ShortcutItemWorkingDir":
case "ShortcutItemArguments":
Expand Down
11 changes: 11 additions & 0 deletions src/Files.App/ViewModels/Properties/Items/LibraryProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public async override Task GetSpecialPropertiesAsync()
{
ViewModel.IsReadOnly = Win32Helper.HasFileAttribute(Library.ItemPath, System.IO.FileAttributes.ReadOnly);
ViewModel.IsHidden = Win32Helper.HasFileAttribute(Library.ItemPath, System.IO.FileAttributes.Hidden);
ViewModel.IsContentCompressed = Win32Helper.HasFileAttribute(Library.ItemPath, System.IO.FileAttributes.Compressed);

var result = await FileThumbnailHelper.GetIconAsync(
Library.ItemPath,
Expand Down Expand Up @@ -161,6 +162,16 @@ private void ViewModel_PropertyChanged(object sender, System.ComponentModel.Prop
}

break;

case "IsContentCompressed":
if (ViewModel.IsContentCompressed is not null)
{
if ((bool)ViewModel.IsContentCompressed)
Win32Helper.SetCompressionAttributeIoctl(Library.ItemPath, true);
else
Win32Helper.SetCompressionAttributeIoctl(Library.ItemPath, false);
}
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/Files.App/Views/Properties/GeneralPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<!-- Read Only -->
Expand Down Expand Up @@ -733,6 +735,32 @@
AutomationProperties.Name="{helpers:ResourceString Name=Hidden}"
IsChecked="{x:Bind ViewModel.IsHiddenEditedValue, Mode=TwoWay}" />

<!-- (Divider) -->
<Border
Grid.Row="3"
Grid.Column="0"
Grid.ColumnSpan="3"
Height="1"
Margin="-16,0"
Background="{ThemeResource DividerStrokeColorDefaultBrush}" />

<!-- Compress Contents -->
<TextBlock
Grid.Row="4"
Grid.Column="0"
VerticalAlignment="Center"
Text="{helpers:ResourceString Name=CompressContents}"
TextWrapping="WrapWholeWords" />

<CheckBox
Grid.Row="4"
Grid.Column="2"
MinWidth="0"
HorizontalAlignment="Right"
VerticalAlignment="Center"
AutomationProperties.Name="{helpers:ResourceString Name=CompressContents}"
IsChecked="{x:Bind ViewModel.IsContentCompressedEditedValue, Mode=TwoWay}" />

</Grid>
</Expander.Content>
</Expander>
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Views/Properties/GeneralPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() =>
}

ViewModel.IsReadOnly = ViewModel.IsReadOnlyEditedValue;
ViewModel.IsContentCompressed = ViewModel.IsContentCompressedEditedValue;

if (ViewModel.IsAblumCoverModified)
{
Expand Down Expand Up @@ -181,6 +182,7 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() =>

ViewModel.IsReadOnly = ViewModel.IsReadOnlyEditedValue;
ViewModel.IsHidden = ViewModel.IsHiddenEditedValue;
ViewModel.IsContentCompressed = ViewModel.IsContentCompressedEditedValue;

if (!GetNewName(out var newName))
return true;
Expand Down