Skip to content

Commit

Permalink
Feature: Added option to prioritize files when sorting (files-communi…
Browse files Browse the repository at this point in the history
  • Loading branch information
hishitetsu authored Dec 17, 2023
1 parent 5e9113a commit d3f06b0
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

namespace Files.App.Actions
{
internal class ToggleSortDirectoriesAlongsideFilesAction : ObservableObject, IToggleAction
internal class SortFilesAndFoldersTogetherAction : ObservableObject, IToggleAction
{
private readonly IDisplayPageContext context;

public string Label
=> "SettingsListAndSortDirectoriesAlongsideFiles".GetLocalizedResource();
=> "SortFilesAndFoldersTogether".GetLocalizedResource();

public string Description
=> "ToggleSortDirectoriesAlongsideFilesDescription".GetLocalizedResource();
=> "SortFilesAndFoldersTogetherDescription".GetLocalizedResource();

public bool IsOn
=> context.SortDirectoriesAlongsideFiles;

public ToggleSortDirectoriesAlongsideFilesAction()
public SortFilesAndFoldersTogetherAction()
{
context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

Expand All @@ -25,7 +25,7 @@ public ToggleSortDirectoriesAlongsideFilesAction()

public Task ExecuteAsync()
{
context.SortDirectoriesAlongsideFiles = !IsOn;
context.SortDirectoriesAlongsideFiles = true;

return Task.CompletedTask;
}
Expand Down
40 changes: 40 additions & 0 deletions src/Files.App/Actions/Display/SortFilesFirstAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal class SortFilesFirstAction : ObservableObject, IToggleAction
{
private readonly IDisplayPageContext context;

public string Label
=> "SortFilesFirst".GetLocalizedResource();

public string Description
=> "SortFilesFirstDescription".GetLocalizedResource();

public bool IsOn
=> context.SortFilesFirst && !context.SortDirectoriesAlongsideFiles;

public SortFilesFirstAction()
{
context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
context.SortFilesFirst = true;
context.SortDirectoriesAlongsideFiles = false;

return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IDisplayPageContext.SortFilesFirst) or nameof(IDisplayPageContext.SortDirectoriesAlongsideFiles))
OnPropertyChanged(nameof(IsOn));
}
}
}
40 changes: 40 additions & 0 deletions src/Files.App/Actions/Display/SortFoldersFirstAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal class SortFoldersFirstAction : ObservableObject, IToggleAction
{
private readonly IDisplayPageContext context;

public string Label
=> "SortFoldersFirst".GetLocalizedResource();

public string Description
=> "SortFoldersFirstDescription".GetLocalizedResource();

public bool IsOn
=> !context.SortFilesFirst && !context.SortDirectoriesAlongsideFiles;

public SortFoldersFirstAction()
{
context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
context.SortFilesFirst = false;
context.SortDirectoriesAlongsideFiles = false;

return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IDisplayPageContext.SortFilesFirst) or nameof(IDisplayPageContext.SortDirectoriesAlongsideFiles))
OnPropertyChanged(nameof(IsOn));
}
}
}
4 changes: 3 additions & 1 deletion src/Files.App/Data/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ public enum CommandCodes
SortAscending,
SortDescending,
ToggleSortDirection,
ToggleSortDirectoriesAlongsideFiles,
SortFoldersFirst,
SortFilesFirst,
SortFilesAndFoldersTogether,

// Group by
GroupByNone,
Expand Down
8 changes: 6 additions & 2 deletions src/Files.App/Data/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ public IRichCommand this[HotKey hotKey]
public IRichCommand SortAscending => commands[CommandCodes.SortAscending];
public IRichCommand SortDescending => commands[CommandCodes.SortDescending];
public IRichCommand ToggleSortDirection => commands[CommandCodes.ToggleSortDirection];
public IRichCommand ToggleSortDirectoriesAlongsideFiles => commands[CommandCodes.ToggleSortDirectoriesAlongsideFiles];
public IRichCommand SortFoldersFirst => commands[CommandCodes.SortFoldersFirst];
public IRichCommand SortFilesFirst => commands[CommandCodes.SortFilesFirst];
public IRichCommand SortFilesAndFoldersTogether => commands[CommandCodes.SortFilesAndFoldersTogether];
public IRichCommand GroupByNone => commands[CommandCodes.GroupByNone];
public IRichCommand GroupByName => commands[CommandCodes.GroupByName];
public IRichCommand GroupByDateModified => commands[CommandCodes.GroupByDateModified];
Expand Down Expand Up @@ -296,7 +298,9 @@ public CommandManager()
[CommandCodes.SortAscending] = new SortAscendingAction(),
[CommandCodes.SortDescending] = new SortDescendingAction(),
[CommandCodes.ToggleSortDirection] = new ToggleSortDirectionAction(),
[CommandCodes.ToggleSortDirectoriesAlongsideFiles] = new ToggleSortDirectoriesAlongsideFilesAction(),
[CommandCodes.SortFoldersFirst] = new SortFoldersFirstAction(),
[CommandCodes.SortFilesFirst] = new SortFilesFirstAction(),
[CommandCodes.SortFilesAndFoldersTogether] = new SortFilesAndFoldersTogetherAction(),
[CommandCodes.GroupByNone] = new GroupByNoneAction(),
[CommandCodes.GroupByName] = new GroupByNameAction(),
[CommandCodes.GroupByDateModified] = new GroupByDateModifiedAction(),
Expand Down
4 changes: 3 additions & 1 deletion src/Files.App/Data/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand SortAscending { get; }
IRichCommand SortDescending { get; }
IRichCommand ToggleSortDirection { get; }
IRichCommand ToggleSortDirectoriesAlongsideFiles { get; }
IRichCommand SortFoldersFirst { get; }
IRichCommand SortFilesFirst { get; }
IRichCommand SortFilesAndFoldersTogether { get; }

IRichCommand GroupByNone { get; }
IRichCommand GroupByName { get; }
Expand Down
15 changes: 15 additions & 0 deletions src/Files.App/Data/Contexts/DisplayPage/DisplayPageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ public bool SortDirectoriesAlongsideFiles
}
}

private bool _SortFilesFirst = false;
public bool SortFilesFirst
{
get => _SortFilesFirst;
set
{
if (FolderSettings is LayoutPreferencesManager viewModel)
viewModel.SortFilesFirst = value;
}
}

private LayoutPreferencesManager? FolderSettings => context.PaneOrColumn?.InstanceViewModel?.FolderSettings;

public DisplayPageContext()
Expand Down Expand Up @@ -181,6 +192,9 @@ private void FolderSettings_PropertyChanged(object? sender, PropertyChangedEvent
case nameof(LayoutPreferencesManager.SortDirectoriesAlongsideFiles):
SetProperty(ref _SortDirectoriesAlongsideFiles, viewModel.SortDirectoriesAlongsideFiles, nameof(SortDirectoriesAlongsideFiles));
break;
case nameof(LayoutPreferencesManager.SortFilesFirst):
SetProperty(ref _SortFilesFirst, viewModel.SortFilesFirst, nameof(SortFilesFirst));
break;
}
}

Expand Down Expand Up @@ -214,6 +228,7 @@ private void Update()
SetProperty(ref _GroupDirection, viewModel.DirectoryGroupDirection, nameof(GroupDirection));
SetProperty(ref _GroupByDateUnit, viewModel.DirectoryGroupByDateUnit, nameof(GroupByDateUnit));
SetProperty(ref _SortDirectoriesAlongsideFiles, viewModel.SortDirectoriesAlongsideFiles, nameof(SortDirectoriesAlongsideFiles));
SetProperty(ref _SortFilesFirst, viewModel.SortFilesFirst, nameof(SortFilesFirst));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface IDisplayPageContext : INotifyPropertyChanging, INotifyPropertyC
GroupByDateUnit GroupByDateUnit { get; set; }

bool SortDirectoriesAlongsideFiles { get; set; }
bool SortFilesFirst { get; set; }

void DecreaseLayoutSize();
void IncreaseLayoutSize();
Expand Down
29 changes: 26 additions & 3 deletions src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ public async Task UpdateSortDirectoriesAlongsideFilesAsync()
await ApplyFilesAndFoldersChangesAsync();
}

public async Task UpdateSortFilesFirstAsync()
{
OnPropertyChanged(nameof(AreFilesSortedFirst));

await OrderFilesAndFoldersAsync();
await ApplyFilesAndFoldersChangesAsync();
}

private void UpdateSortAndGroupOptions()
{
OnPropertyChanged(nameof(IsSortedByName));
Expand All @@ -236,6 +244,7 @@ private void UpdateSortAndGroupOptions()
OnPropertyChanged(nameof(IsSortedAscending));
OnPropertyChanged(nameof(IsSortedDescending));
OnPropertyChanged(nameof(AreDirectoriesSortedAlongsideFiles));
OnPropertyChanged(nameof(AreFilesSortedFirst));
}

public bool IsSortedByName
Expand Down Expand Up @@ -406,6 +415,16 @@ public bool AreDirectoriesSortedAlongsideFiles
}
}

public bool AreFilesSortedFirst
{
get => folderSettings.SortFilesFirst;
set
{
folderSettings.SortFilesFirst = value;
OnPropertyChanged(nameof(AreFilesSortedFirst));
}
}

public bool HasNoWatcher { get; private set; }

public ItemViewModel(LayoutPreferencesManager folderSettingsViewModel)
Expand Down Expand Up @@ -547,6 +566,7 @@ await dispatcherQueue.EnqueueOrInvokeAsync(() =>
case nameof(UserSettingsService.FoldersSettingsService.DefaultSortOption):
case nameof(UserSettingsService.FoldersSettingsService.DefaultGroupOption):
case nameof(UserSettingsService.FoldersSettingsService.DefaultSortDirectoriesAlongsideFiles):
case nameof(UserSettingsService.FoldersSettingsService.DefaultSortFilesFirst):
case nameof(UserSettingsService.FoldersSettingsService.SyncFolderPreferencesAcrossDirectories):
case nameof(UserSettingsService.FoldersSettingsService.DefaultGroupByDateUnit):
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
Expand Down Expand Up @@ -602,7 +622,8 @@ await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
var key = FilesAndFolders.ItemGroupKeySelector?.Invoke(item);
var group = FilesAndFolders.GroupedCollection?.FirstOrDefault(x => x.Model.Key == key);
group?.OrderOne(list => SortingHelper.OrderFileList(list, folderSettings.DirectorySortOption, folderSettings.DirectorySortDirection, folderSettings.SortDirectoriesAlongsideFiles), item);
group?.OrderOne(list => SortingHelper.OrderFileList(list, folderSettings.DirectorySortOption, folderSettings.DirectorySortDirection,
folderSettings.SortDirectoriesAlongsideFiles, folderSettings.SortFilesFirst), item);
}

UpdateEmptyTextType();
Expand Down Expand Up @@ -753,7 +774,8 @@ void OrderEntries()
if (filesAndFolders.Count == 0)
return;

filesAndFolders = new ConcurrentCollection<ListedItem>(SortingHelper.OrderFileList(filesAndFolders.ToList(), folderSettings.DirectorySortOption, folderSettings.DirectorySortDirection, folderSettings.SortDirectoriesAlongsideFiles));
filesAndFolders = new ConcurrentCollection<ListedItem>(SortingHelper.OrderFileList(filesAndFolders.ToList(), folderSettings.DirectorySortOption, folderSettings.DirectorySortDirection,
folderSettings.SortDirectoriesAlongsideFiles, folderSettings.SortFilesFirst));
}

if (NativeWinApiHelper.IsHasThreadAccessPropertyPresent && dispatcherQueue.HasThreadAccess)
Expand All @@ -775,7 +797,8 @@ private void OrderGroups(CancellationToken token = default)
if (token.IsCancellationRequested)
return;

gp.Order(list => SortingHelper.OrderFileList(list, folderSettings.DirectorySortOption, folderSettings.DirectorySortDirection, folderSettings.SortDirectoriesAlongsideFiles));
gp.Order(list => SortingHelper.OrderFileList(list, folderSettings.DirectorySortOption, folderSettings.DirectorySortDirection,
folderSettings.SortDirectoriesAlongsideFiles, folderSettings.SortFilesFirst));
}

if (FilesAndFolders.GroupedCollection is null || FilesAndFolders.GroupedCollection.IsSorted)
Expand Down
4 changes: 4 additions & 0 deletions src/Files.App/Helpers/Layout/LayoutPreferencesItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class LayoutPreferencesItem
public ColumnsViewModel ColumnsViewModel;

public bool SortDirectoriesAlongsideFiles;
public bool SortFilesFirst;
public bool IsAdaptiveLayoutOverridden;
public int GridViewSize;

Expand All @@ -43,6 +44,7 @@ public LayoutPreferencesItem()
DirectoryGroupDirection = UserSettingsService.FoldersSettingsService.DefaultDirectoryGroupDirection;
DirectoryGroupByDateUnit = UserSettingsService.FoldersSettingsService.DefaultGroupByDateUnit;
SortDirectoriesAlongsideFiles = UserSettingsService.FoldersSettingsService.DefaultSortDirectoriesAlongsideFiles;
SortFilesFirst = UserSettingsService.FoldersSettingsService.DefaultSortFilesFirst;
IsAdaptiveLayoutOverridden = defaultLayout is not FolderLayoutModes.Adaptive;

ColumnsViewModel = new ColumnsViewModel();
Expand Down Expand Up @@ -99,6 +101,7 @@ public override bool Equals(object? obj)
item.DirectoryGroupDirection == DirectoryGroupDirection &&
item.DirectoryGroupByDateUnit == DirectoryGroupByDateUnit &&
item.SortDirectoriesAlongsideFiles == SortDirectoriesAlongsideFiles &&
item.SortFilesFirst == SortFilesFirst &&
item.IsAdaptiveLayoutOverridden == IsAdaptiveLayoutOverridden &&
item.ColumnsViewModel.Equals(ColumnsViewModel));
}
Expand All @@ -117,6 +120,7 @@ public override int GetHashCode()
hash.Add(DirectoryGroupDirection);
hash.Add(DirectoryGroupByDateUnit);
hash.Add(SortDirectoriesAlongsideFiles);
hash.Add(SortFilesFirst);
hash.Add(IsAdaptiveLayoutOverridden);
hash.Add(ColumnsViewModel);

Expand Down
19 changes: 19 additions & 0 deletions src/Files.App/Helpers/Layout/LayoutPreferencesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ public bool SortDirectoriesAlongsideFiles
}
}

public bool SortFilesFirst
{
get => LayoutPreferencesItem.SortFilesFirst;
set
{
if (SetProperty(ref LayoutPreferencesItem.SortFilesFirst, value, nameof(SortFilesFirst)))
{
LayoutPreferencesUpdateRequired?.Invoke(this, new LayoutPreferenceEventArgs(LayoutPreferencesItem));
SortFilesFirstPreferenceUpdated?.Invoke(this, SortFilesFirst);
}
}
}

public ColumnsViewModel ColumnsViewModel
{
get => LayoutPreferencesItem.ColumnsViewModel;
Expand Down Expand Up @@ -256,6 +269,7 @@ private set
OnPropertyChanged(nameof(DirectoryGroupDirection));
OnPropertyChanged(nameof(DirectoryGroupByDateUnit));
OnPropertyChanged(nameof(SortDirectoriesAlongsideFiles));
OnPropertyChanged(nameof(SortFilesFirst));
OnPropertyChanged(nameof(ColumnsViewModel));
}
}
Expand All @@ -270,6 +284,7 @@ private set
public event EventHandler<SortDirection>? GroupDirectionPreferenceUpdated;
public event EventHandler<GroupByDateUnit>? GroupByDateUnitPreferenceUpdated;
public event EventHandler<bool>? SortDirectoriesAlongsideFilesPreferenceUpdated;
public event EventHandler<bool>? SortFilesFirstPreferenceUpdated;
public event EventHandler<LayoutModeEventArgs>? LayoutModeChangeRequested;
public event EventHandler? GridViewSizeChangeRequested;

Expand Down Expand Up @@ -429,6 +444,9 @@ public void OnDefaultPreferencesChanged(string path, string settingsName)
case nameof(UserSettingsService.FoldersSettingsService.DefaultSortDirectoriesAlongsideFiles):
SortDirectoriesAlongsideFiles = preferencesItem.SortDirectoriesAlongsideFiles;
break;
case nameof(UserSettingsService.FoldersSettingsService.DefaultSortFilesFirst):
SortFilesFirst = preferencesItem.SortFilesFirst;
break;
case nameof(UserSettingsService.FoldersSettingsService.SyncFolderPreferencesAcrossDirectories):
LayoutPreferencesItem = preferencesItem;
// TODO: Update layout
Expand Down Expand Up @@ -512,6 +530,7 @@ public static void SetLayoutPreferencesForPath(string path, LayoutPreferencesIte
UserSettingsService.FoldersSettingsService.DefaultDirectoryGroupDirection = preferencesItem.DirectoryGroupDirection;
UserSettingsService.FoldersSettingsService.DefaultGroupByDateUnit = preferencesItem.DirectoryGroupByDateUnit;
UserSettingsService.FoldersSettingsService.DefaultSortDirectoriesAlongsideFiles = preferencesItem.SortDirectoriesAlongsideFiles;
UserSettingsService.FoldersSettingsService.DefaultSortFilesFirst = preferencesItem.SortFilesFirst;

UserSettingsService.FoldersSettingsService.NameColumnWidth = preferencesItem.ColumnsViewModel.NameColumn.UserLengthPixels;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
},
new ContextMenuFlyoutItemViewModel()
{
Text = "NavToolbarGroupByRadioButtons/Text".GetLocalizedResource(),
Text = "GroupBy".GetLocalizedResource(),
Glyph = "\uF168",
ShowItem = !itemsSelected,
ShowInRecycleBin = true,
Expand Down
6 changes: 6 additions & 0 deletions src/Files.App/Services/Settings/FoldersSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ public bool DefaultSortDirectoriesAlongsideFiles
set => Set(value);
}

public bool DefaultSortFilesFirst
{
get => Get(false);
set => Set(value);
}

public bool ShowFileExtensions
{
get => Get(true);
Expand Down
Loading

0 comments on commit d3f06b0

Please sign in to comment.