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

Implemented #890: Implemented group images by directory option #891

Merged
merged 4 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions Source/Components/ImageGlass.Library/Comparer/IdentityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
ImageGlass Project - Image viewer for Windows
Copyright (C) 2020 DUONG DIEU PHAP
Project homepage: https://imageglass.org

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Collections.Generic;

namespace ImageGlass.Library.Comparer {
public class IdentityComparer: IComparer<string> {
public int Compare(string filePath1, string filePath2) {
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
ImageGlass Project - Image viewer for Windows
Copyright (C) 2020 DUONG DIEU PHAP
Project homepage: https://imageglass.org

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

namespace ImageGlass.Library.Comparer {
public class WindowsDirectoryNaturalSort: IComparer<string> {
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern int StrCmpLogicalW(string x, string y);

public int Compare(string filePath1, string filePath2) {
var basename1 = Path.GetDirectoryName(filePath1);
var basename2 = Path.GetDirectoryName(filePath2);

return StrCmpLogicalW(basename1, basename2);
}
}

public class ReverseWindowsDirectoryNaturalSort: IComparer<string> {
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern int StrCmpLogicalW(string x, string y);

public int Compare(string filePath1, string filePath2) {
var basename1 = Path.GetDirectoryName(filePath2);
var basename2 = Path.GetDirectoryName(filePath1);

return StrCmpLogicalW(basename1, basename2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Comparer\WindowsDirectoryNaturalSort.cs" />
<Compile Include="Comparer\IdentityComparer.cs" />
<Compile Include="Comparer\WindowsNaturalSort.cs" />
<Compile Include="FileAssociations\RegistryHelper.cs" />
<Compile Include="Helper.cs" />
Expand Down
1 change: 1 addition & 0 deletions Source/Components/ImageGlass.Library/Language/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ private void InitDefaultLanguageDictionary() {
Items.Add("frmSetting.cmbImageOrderType._Asc", "Ascending"); // V7.0
Items.Add("frmSetting.cmbImageOrderType._Desc", "Descending"); // V7.0
Items.Add("frmSetting.chkUseFileExplorerSortOrder", "Use Windows File Explorer sort order if possible"); //v7.0
Items.Add("frmSetting.chkGroupByDirectory", "Group images by directory"); //v7.7
Items.Add("frmSetting.lblImageBoosterCachedCount", "Number of images cached by ImageBooster (one direction)"); //v7.0
#endregion

Expand Down
7 changes: 7 additions & 0 deletions Source/Components/ImageGlass.Settings/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ public static class Configs {
/// </summary>
public static bool IsUseFileExplorerSortOrder { get; set; } = true;

/// <summary>
/// Gets, sets the value indicates that images order should be grouped by directory
/// </summary>
public static bool IsGroupImagesByDirectory { get; set; } = false;

/// <summary>
/// Gets, sets showing/loading hidden images
/// </summary>
Expand Down Expand Up @@ -555,6 +560,7 @@ public static void Load() {
IsShowCheckerboardOnlyImageRegion = Get<bool>(nameof(IsShowCheckerboardOnlyImageRegion), IsShowCheckerboardOnlyImageRegion);
IsRecursiveLoading = Get<bool>(nameof(IsRecursiveLoading), IsRecursiveLoading);
IsUseFileExplorerSortOrder = Get<bool>(nameof(IsUseFileExplorerSortOrder), IsUseFileExplorerSortOrder);
IsGroupImagesByDirectory = Get<bool>(nameof(IsGroupImagesByDirectory), IsGroupImagesByDirectory);
IsShowingHiddenImages = Get<bool>(nameof(IsShowingHiddenImages), IsShowingHiddenImages);
IsShowColorPickerOnStartup = Get<bool>(nameof(IsShowColorPickerOnStartup), IsShowColorPickerOnStartup);
IsShowPageNavOnStartup = Get<bool>(nameof(IsShowPageNavOnStartup), IsShowPageNavOnStartup);
Expand Down Expand Up @@ -782,6 +788,7 @@ public static void Write() {
Set(nameof(IsShowCheckerboardOnlyImageRegion), IsShowCheckerboardOnlyImageRegion);
Set(nameof(IsRecursiveLoading), IsRecursiveLoading);
Set(nameof(IsUseFileExplorerSortOrder), IsUseFileExplorerSortOrder);
Set(nameof(IsGroupImagesByDirectory), IsGroupImagesByDirectory);
Set(nameof(IsShowingHiddenImages), IsShowingHiddenImages);
Set(nameof(IsShowColorPickerOnStartup), IsShowColorPickerOnStartup);
Set(nameof(IsShowPageNavOnStartup), IsShowPageNavOnStartup);
Expand Down
50 changes: 36 additions & 14 deletions Source/ImageGlass/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,78 +459,100 @@ private static List<string> SortImageList(IEnumerable<string> fileList) {
? (IComparer<string>)new ReverseWindowsNaturalSort()
: new WindowsNaturalSort();

// initiate directory sorter to a comparer that does nothing
// if user wants to group by directory, we initiate the real comparer
var directorySortComparer = (IComparer<string>)new IdentityComparer();
if (Configs.IsGroupImagesByDirectory) {
if (Local.ActiveImageLoadingOrderType == ImageOrderType.Desc) {
directorySortComparer = new ReverseWindowsDirectoryNaturalSort();
} else {
directorySortComparer = new WindowsDirectoryNaturalSort();
}
}

// KBR 20190605 Fix observed discrepancy: using UTC for create, but not for write/access times

// Sort image file
if (Local.ActiveImageLoadingOrder == ImageOrderBy.Name) {
var arr = fileList.ToArray();
Array.Sort(arr, naturalSortComparer);
list.AddRange(arr);
list.AddRange(fileList
.OrderBy(f => f, directorySortComparer)
.ThenBy(f => f, naturalSortComparer));
}
else if (Local.ActiveImageLoadingOrder == ImageOrderBy.Length) {
if (Local.ActiveImageLoadingOrderType == ImageOrderType.Desc) {
list.AddRange(fileList
.OrderByDescending(f => new FileInfo(f).Length)
.OrderBy(f => f, directorySortComparer)
.ThenByDescending(f => new FileInfo(f).Length)
.ThenBy(f => f, naturalSortComparer));
}
else {
list.AddRange(fileList
.OrderBy(f => new FileInfo(f).Length)
.OrderBy(f => f, directorySortComparer)
.ThenBy(f => new FileInfo(f).Length)
.ThenBy(f => f, naturalSortComparer));
}
}
else if (Local.ActiveImageLoadingOrder == ImageOrderBy.CreationTime) {
if (Local.ActiveImageLoadingOrderType == ImageOrderType.Desc) {
list.AddRange(fileList
.OrderByDescending(f => new FileInfo(f).CreationTimeUtc)
.OrderBy(f => f, directorySortComparer)
.ThenByDescending(f => new FileInfo(f).CreationTimeUtc)
.ThenBy(f => f, naturalSortComparer));
}
else {
list.AddRange(fileList
.OrderBy(f => new FileInfo(f).CreationTimeUtc)
.OrderBy(f => f, directorySortComparer)
.ThenBy(f => new FileInfo(f).CreationTimeUtc)
.ThenBy(f => f, naturalSortComparer));
}
}
else if (Local.ActiveImageLoadingOrder == ImageOrderBy.Extension) {
if (Local.ActiveImageLoadingOrderType == ImageOrderType.Desc) {
list.AddRange(fileList
.OrderByDescending(f => new FileInfo(f).Extension)
.OrderBy(f => f, directorySortComparer)
.ThenByDescending(f => new FileInfo(f).Extension)
.ThenBy(f => f, naturalSortComparer));
}
else {
list.AddRange(fileList
.OrderBy(f => new FileInfo(f).Extension)
.OrderBy(f => f, directorySortComparer)
.ThenBy(f => new FileInfo(f).Extension)
.ThenBy(f => f, naturalSortComparer));
}
}
else if (Local.ActiveImageLoadingOrder == ImageOrderBy.LastAccessTime) {
if (Local.ActiveImageLoadingOrderType == ImageOrderType.Desc) {
list.AddRange(fileList
.OrderByDescending(f => new FileInfo(f).LastAccessTimeUtc)
.OrderBy(f => f, directorySortComparer)
.ThenByDescending(f => new FileInfo(f).LastAccessTimeUtc)
.ThenBy(f => f, naturalSortComparer));
}
else {
list.AddRange(fileList
.OrderBy(f => new FileInfo(f).LastAccessTimeUtc)
.OrderBy(f => f, directorySortComparer)
.ThenBy(f => new FileInfo(f).LastAccessTimeUtc)
.ThenBy(f => f, naturalSortComparer));
}
}
else if (Local.ActiveImageLoadingOrder == ImageOrderBy.LastWriteTime) {
if (Local.ActiveImageLoadingOrderType == ImageOrderType.Desc) {
list.AddRange(fileList
.OrderByDescending(f => new FileInfo(f).LastWriteTimeUtc)
.OrderBy(f => f, directorySortComparer)
.ThenByDescending(f => new FileInfo(f).LastWriteTimeUtc)
.ThenBy(f => f, naturalSortComparer));
}
else {
list.AddRange(fileList
.OrderBy(f => new FileInfo(f).LastWriteTimeUtc)
.OrderBy(f => f, directorySortComparer)
.ThenBy(f => new FileInfo(f).LastWriteTimeUtc)
.ThenBy(f => f, naturalSortComparer));
}
}
else if (Local.ActiveImageLoadingOrder == ImageOrderBy.Random) {
// NOTE: ignoring the 'descending order' setting
list.AddRange(fileList
.OrderBy(_ => Guid.NewGuid()));
.OrderBy(f => f, directorySortComparer)
.ThenBy(_ => Guid.NewGuid()));
}

return list;
Expand Down
Loading