Skip to content

Feature: Added advanced options when creating archives #10524

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

Merged
merged 29 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
169 changes: 169 additions & 0 deletions src/Files.App/Dialogs/CompressArchiveDialog.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<ContentDialog
x:Class="Files.App.Dialogs.CompressArchiveDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:Files.App.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="using:Files.App.Helpers"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:settings="using:Files.App.UserControls.Settings"
Title="{helpers:ResourceString Name=CreateArchive}"
d:DesignHeight="300"
d:DesignWidth="400"
CloseButtonText="{helpers:ResourceString Name=Cancel}"
Closing="ContentDialog_Closing"
CornerRadius="{StaticResource OverlayCornerRadius}"
DefaultButton="Primary"
IsPrimaryButtonEnabled="True"
Loaded="ContentDialog_Loaded"
PrimaryButtonText="{helpers:ResourceString Name=Create}"
RequestedTheme="{x:Bind helpers:ThemeHelper.RootTheme}"
Style="{StaticResource DefaultContentDialogStyle}"
mc:Ignorable="d">

<ContentDialog.Resources>
<ResourceDictionary>
<converters:VisibilityInvertConverter x:Key="VisibilityInvertConverter" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourceDictionaries/RightAlignedToggleSwitchStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ContentDialog.Resources>

<StackPanel Spacing="24">

<Grid Margin="12,16,12,0" ColumnSpacing="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox
x:Name="FileNameBox"
Width="340"
VerticalAlignment="Bottom"
Header="{helpers:ResourceString Name=ArchiveName}"
PlaceholderText="{helpers:ResourceString Name=EnterName}"
Text="{x:Bind FileName, Mode=TwoWay}" />
<ComboBox
x:Name="FileFormatSelector"
Grid.Column="1"
MinWidth="100"
VerticalAlignment="Bottom"
ItemsSource="{x:Bind FileFormats}"
SelectedValue="{x:Bind FileFormat, Mode=TwoWay}"
SelectedValuePath="Key"
SelectionChanged="FileFormatSelector_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid ColumnSpacing="24">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Label}" />
<TextBlock
Grid.Column="1"
VerticalAlignment="Bottom"
FontSize="12"
FontWeight="Light"
Text="{Binding Description}"
TextWrapping="Wrap"
Visibility="{Binding IsDropDownOpen, ElementName=FileFormatSelector}" />
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>

<ScrollViewer
Height="268"
Margin="0"
Padding="12,0"
HorizontalAlignment="Stretch"
VerticalScrollBarVisibility="Visible"
VerticalScrollMode="Enabled">
<StackPanel
Grid.Row="1"
Grid.ColumnSpan="2"
Spacing="4">
<StackPanel.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition />
</TransitionCollection>
</StackPanel.ChildrenTransitions>

<settings:SettingsBlockControl Title="{helpers:ResourceString Name=DoNotCompress}" HorizontalAlignment="Stretch">
<settings:SettingsBlockControl.Icon>
<FontIcon Glyph="&#xE73F;" />
</settings:SettingsBlockControl.Icon>
<ToggleSwitch
x:Name="DoNotCompressSwitch"
HorizontalAlignment="Right"
IsOn="{x:Bind DoNotCompress, Mode=TwoWay}"
Style="{StaticResource RightAlignedToggleSwitchStyle}" />
</settings:SettingsBlockControl>

<settings:SettingsBlockControl
Title="{helpers:ResourceString Name=SplittingSize}"
HorizontalAlignment="Stretch"
Description="{helpers:ResourceString Name=SplitArchiveDescription}">
<settings:SettingsBlockControl.Icon>
<FontIcon Glyph="&#xE8C6;" />
</settings:SettingsBlockControl.Icon>
<ComboBox
x:Name="SplittingSizeSelector"
HorizontalAlignment="Right"
DisplayMemberPath="Label"
IsEnabled="False"
ItemsSource="{x:Bind SplittingSizes}"
SelectedValue="{x:Bind SplittingSize, Mode=TwoWay}"
SelectedValuePath="Key" />
</settings:SettingsBlockControl>

<StackPanel>
<settings:SettingsBlockControl Title="{helpers:ResourceString Name=Encryption}" HorizontalAlignment="Stretch">
<settings:SettingsBlockControl.Icon>
<FontIcon Glyph="&#xE72E;" />
</settings:SettingsBlockControl.Icon>
<ToggleSwitch
x:Name="EncryptionSwitch"
HorizontalAlignment="Right"
Style="{StaticResource RightAlignedToggleSwitchStyle}"
Toggled="EncryptionSwitch_Toggled" />
</settings:SettingsBlockControl>
<Grid
HorizontalAlignment="Stretch"
Background="{StaticResource ExpanderDropDownBackground}"
BorderBrush="{StaticResource ExpanderDropDownBorderBrush}"
BorderThickness="{StaticResource ExpanderDropdownDownBorderThickness}"
CornerRadius="{StaticResource ControlCornerRadius}"
Visibility="{Binding IsOn, ElementName=EncryptionSwitch, Mode=OneWay}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock
Margin="24,0"
VerticalAlignment="Center"
Text="{helpers:ResourceString Name=Password}" />
<PasswordBox
x:Name="PasswordBox"
Grid.Column="1"
MinWidth="160"
MaxWidth="280"
Margin="16,8"
HorizontalAlignment="Right"
IsPasswordRevealButtonEnabled="True"
Password="{x:Bind Password, Mode=TwoWay}"
PasswordChanging="PasswordBox_PasswordChanging"
PasswordRevealMode="Peek"
PlaceholderText="{helpers:ResourceString Name=Password}" />
</Grid>
</StackPanel>

</StackPanel>
</ScrollViewer>

</StackPanel>

</ContentDialog>
143 changes: 143 additions & 0 deletions src/Files.App/Dialogs/CompressArchiveDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using Files.App.Extensions;
using Files.App.Filesystem.Archive;
using Files.Backend.Models;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.Foundation.Metadata;

namespace Files.App.Dialogs
{
public sealed partial class CompressArchiveDialog : ContentDialog
{
public static readonly DependencyProperty FileNameProperty = DependencyProperty
.Register(nameof(FileName), typeof(string), typeof(CompressArchiveDialog), new(string.Empty));

public static readonly DependencyProperty PasswordProperty = DependencyProperty
.Register(nameof(Password), typeof(string), typeof(CompressArchiveDialog), new(string.Empty));

public static readonly DependencyProperty FileFormatProperty = DependencyProperty
.Register(nameof(FileFormat), typeof(ArchiveFormats), typeof(CompressArchiveDialog), new(ArchiveFormats.Zip));

public static readonly DependencyProperty DoNotCompressProperty = DependencyProperty
.Register(nameof(DoNotCompress), typeof(bool), typeof(CompressArchiveDialog), new(false));

public static readonly DependencyProperty SplittingSizeProperty = DependencyProperty
.Register(nameof(SplittingSize), typeof(ArchiveSplittingSizes), typeof(CompressArchiveDialog), new(ArchiveSplittingSizes.None));

private bool canCreate = false;
public bool CanCreate => canCreate;

public string FileName
{
get => (string)GetValue(FileNameProperty);
set => SetValue(FileNameProperty, value);
}

public string Password
{
get => (string)GetValue(PasswordProperty);
set => SetValue(PasswordProperty, value);
}

public ArchiveFormats FileFormat
{
get => (ArchiveFormats)GetValue(FileFormatProperty);
set => SetValue(FileFormatProperty, (int)value);
}

public bool DoNotCompress
{
get => (bool)GetValue(DoNotCompressProperty);
set => SetValue(DoNotCompressProperty, value);
}

public ArchiveSplittingSizes SplittingSize
{
get => (ArchiveSplittingSizes)GetValue(SplittingSizeProperty);
set => SetValue(SplittingSizeProperty, (int)value);
}

private IImmutableList<FileFormatItem> FileFormats { get; } = new List<FileFormatItem>
{
new(ArchiveFormats.Zip, ".zip", "CompressionFormatZipDescription".GetLocalizedResource()),
new(ArchiveFormats.SevenZip, ".7z", "CompressionFormatSevenZipDescription".GetLocalizedResource()),
}.ToImmutableList();

private IImmutableList<SplittingSizeItem> SplittingSizes { get; } = new List<SplittingSizeItem>
{
new(ArchiveSplittingSizes.None, "Do not split".GetLocalizedResource()),
new(ArchiveSplittingSizes.Mo10, ToSizeText(10)),
new(ArchiveSplittingSizes.Mo100, ToSizeText(100)),
new(ArchiveSplittingSizes.Cd650, ToSizeText(650, "CD".GetLocalizedResource())),
new(ArchiveSplittingSizes.Cd700, ToSizeText(700, "CD".GetLocalizedResource())),
new(ArchiveSplittingSizes.Mo1024, ToSizeText(1024)),
new(ArchiveSplittingSizes.Fat4092, ToSizeText(4092, "FAT".GetLocalizedResource())),
new(ArchiveSplittingSizes.Dvd4480, ToSizeText(4480, "DVD".GetLocalizedResource())),
new(ArchiveSplittingSizes.Mo5120, ToSizeText(5120)),
new(ArchiveSplittingSizes.Dvd8128, ToSizeText(8128, "DVD".GetLocalizedResource())),
new(ArchiveSplittingSizes.Bd23040, ToSizeText(23040, "Bluray".GetLocalizedResource())),
}.ToImmutableList();

public CompressArchiveDialog() => InitializeComponent();

public new Task<ContentDialogResult> ShowAsync() => SetContentDialogRoot(this).ShowAsync().AsTask();

private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
{
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
contentDialog.XamlRoot = App.Window.Content.XamlRoot; // WinUi3
return contentDialog;
}

private static string ToSizeText(ulong size) => ByteSize.FromMebiBytes(size).ShortString;
private static string ToSizeText(ulong size, string labelKey) => $"{ToSizeText(size)} - {labelKey}";

private void ContentDialog_Loaded(object _, RoutedEventArgs e)
{
Loaded -= ContentDialog_Loaded;

FileFormatSelector.SelectedItem = FileFormats.First(format => format.Key == FileFormat);
DoNotCompressSwitch.IsOn = DoNotCompress;
SplittingSizeSelector.SelectedItem = SplittingSizes.First(size => size.Key == SplittingSize);
SplittingSizeSelector.IsEnabled = FileFormat is ArchiveFormats.SevenZip;
EncryptionSwitch.IsOn = Password.Length > 0;

FileNameBox.SelectionStart = FileNameBox.Text.Length;
FileNameBox.Focus(FocusState.Programmatic);
}
private void ContentDialog_Closing(ContentDialog _, ContentDialogClosingEventArgs e)
{
Closing -= ContentDialog_Closing;
FileFormatSelector.SelectionChanged -= FileFormatSelector_SelectionChanged;

if (e.Result is ContentDialogResult.Primary)
canCreate = true;
}

private void FileFormatSelector_SelectionChanged(object _, SelectionChangedEventArgs e)
{
SplittingSizeSelector.IsEnabled = FileFormat is ArchiveFormats.SevenZip;
}
private void EncryptionSwitch_Toggled(object _, RoutedEventArgs e)
{
if (EncryptionSwitch.IsOn)
PasswordBox.Focus(FocusState.Programmatic);
else
Password = string.Empty;
}
private void PasswordBox_PasswordChanging(PasswordBox _, PasswordBoxPasswordChangingEventArgs e)
{
if (PasswordBox.Password.Length > 0)
EncryptionSwitch.IsOn = true;
}

private record FileFormatItem(ArchiveFormats Key, string Label, string Description);
private record SplittingSizeItem(ArchiveSplittingSizes Key, string Label);
}
}
8 changes: 8 additions & 0 deletions src/Files.App/Files.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
</Content>
<Content Remove="Assets\FilesHome.png" />
</ItemGroup>
<ItemGroup>
<None Remove="Dialogs\CompressArchiveDialog.xaml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ByteSize" Version="2.1.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
Expand Down Expand Up @@ -120,4 +123,9 @@
<TrimmerRootAssembly Include="CommunityToolkit.WinUI.UI.Controls.Media" />
<TrimmerRootAssembly Include="CommunityToolkit.WinUI.UI.Controls.Primitives" />
</ItemGroup>
<ItemGroup>
<Page Update="Dialogs\CompressArchiveDialog.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
</ItemGroup>
</Project>
Loading