Skip to content

Commit

Permalink
can change the sort order in the list view, can close fullscreen imag…
Browse files Browse the repository at this point in the history
…e viewer with right click
  • Loading branch information
Minnowo committed Feb 3, 2022
1 parent 5187780 commit e88ce92
Show file tree
Hide file tree
Showing 12 changed files with 1,227 additions and 178 deletions.
8 changes: 8 additions & 0 deletions src/Cat.HelperLibs/Controls/DrawingBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public partial class DrawingBoard : UserControl
public delegate void ScrollPositionChanged(object sender, EventArgs e);
public event ScrollPositionChanged ScrollChanged;

public delegate void RightClickedEvent();
public event RightClickedEvent RightClicked;


public Image Image
{
get
Expand Down Expand Up @@ -241,6 +245,10 @@ private void ImageViewer_MouseUp(object sender, MouseEventArgs e)
case MouseButtons.Left:
isLeftClicking = false;
break;
case MouseButtons.Right:
if (RightClicked != null)
RightClicked.Invoke();
break;
}

this.Focus();
Expand Down
5 changes: 4 additions & 1 deletion src/Cat.HelperLibs/Controls/ImageView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ namespace WinkingCat.HelperLibs
{
public partial class ImageView : UserControl
{

public DrawingBoard db
{
get { return this.drawingBoard1; }
}
public Image Image
{
get
Expand Down
7 changes: 5 additions & 2 deletions src/Cat.HelperLibs/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ namespace WinkingCat.HelperLibs
{
public static class Extensions
{
public static IEnumerable<T> OrderByNatural<T>(this IEnumerable<T> items, Func<T, string> selector, StringComparer stringComparer = null)
public static IEnumerable<T> OrderByNatural<T>(this IEnumerable<T> items, Func<T, string> selector, StringComparer stringComparer = null, bool ascendingOrder = true)
{
Regex regex = new Regex(@"\d+", RegexOptions.Compiled);

int maxDigits = items
.SelectMany(i => regex.Matches(selector(i)).Cast<Match>().Select(digitChunk => (int?)digitChunk.Value.Length))
.Max() ?? 0;

return items.OrderBy(i => regex.Replace(selector(i), match => match.Value.PadLeft(maxDigits, '0')), stringComparer ?? StringComparer.CurrentCulture);
if(ascendingOrder)
return items.OrderBy(i => regex.Replace(selector(i), match => match.Value.PadLeft(maxDigits, '0')), stringComparer ?? StringComparer.CurrentCulture);

return items.OrderByDescending(i => regex.Replace(selector(i), match => match.Value.PadLeft(maxDigits, '0')), stringComparer ?? StringComparer.CurrentCulture);
}

public static Bitmap Copy(this Image image)
Expand Down
60 changes: 31 additions & 29 deletions src/Cat.HelperLibs/Forms/ImageViewerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,49 @@ public ImageViewerForm(Image img)

public static void ShowDisposeImage(Image img)
{
if (img != null)
if (img == null)
return;
using (Image tempImage = img)
{
using (Image tempImage = img)
if (tempImage == null)
return;

using (ImageViewerForm viewer = new ImageViewerForm(tempImage))
{
if (tempImage != null)
{
using (ImageViewerForm viewer = new ImageViewerForm(tempImage))
{
viewer.ShowDialog();
}
}
viewer.ShowDialog();
}
}
}

public static void ShowImage(Image img)
{
if (img != null)
if (img == null)
return;

using (Image tempImage = img.CloneSafe())
{
using (Image tempImage = img.CloneSafe())
if (tempImage == null)
return;

using (ImageViewerForm viewer = new ImageViewerForm(tempImage))
{
if (tempImage != null)
{
using (ImageViewerForm viewer = new ImageViewerForm(tempImage))
{
viewer.ShowDialog();
}
}
viewer.ShowDialog();
}
}
}

public static void ShowImage(string path)
{
using (Image tempImage = ImageHelper.LoadImage(path))
using (Image tempImage = ImageHelper.LoadImage(path))
{
if (tempImage == null)
return;

using (ImageViewerForm viewer = new ImageViewerForm(tempImage))
{
if (tempImage != null)
{
using (ImageViewerForm viewer = new ImageViewerForm(tempImage))
{
viewer.ShowDialog();
}
}
viewer.ShowDialog();
}
}

}

Expand All @@ -85,10 +84,9 @@ private void ImageViewerForm_KeyDown(object sender, KeyEventArgs e)
}
}

private Size ResizeWidth(int newWidth)
private void RightClicked()
{
int newHeight = (int)(newWidth * (initialSize.Height / (float)initialSize.Width));
return new Size(newWidth, newHeight);
Close();
}

#region Windows Form Designer generated code
Expand Down Expand Up @@ -138,15 +136,19 @@ private void InitializeComponent()
ivMain.ScrollbarsVisible = false;
ivMain.Dock = DockStyle.Fill;
ivMain.Image = (Bitmap)this.image;

this.Controls.Add(ivMain);

this.KeyDown += ImageViewerForm_KeyDown;
ivMain.db.RightClicked+= RightClicked;
this.BringToFront();
this.Activate();
this.ResumeLayout();

}



private ImageView ivMain;
//private PictureBox pbMain;
#endregion
Expand Down
6 changes: 6 additions & 0 deletions src/Cat.HelperLibs/Settings/MainFormSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public class MainFormSettings
[Browsable(false)]
public int Image_Counter { get; set; } = 0;

[Browsable(false)]
public int FileSortOrder { get; set; } = -1;

[Browsable(false)]
public int FolderSortOrder { get; set; } = 1;

[XmlIgnore]
[Browsable(false)]
public Function On_Tray_Left_Click { get; set; } = Function.RegionCapture;
Expand Down
81 changes: 71 additions & 10 deletions src/Cat.HelperLibs/Types/FolderWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace WinkingCat.HelperLibs
{
public class FolderWatcher : IDisposable
{
public delegate void SortOrderChangedEvent(int order);
public event SortOrderChangedEvent SortOrderChanged;

public delegate void FileAddedEvent(string name);
public event FileAddedEvent FileAdded;

Expand All @@ -34,7 +37,65 @@ public class FolderWatcher : IDisposable
public bool AboveDrives { get { return _AboveDrives; } }
private bool _AboveDrives = false;

public bool GetAbsolutePaths = true;
/// <summary>
/// 1 for ascending
/// -1 for descending
/// </summary>
public int SortOrderFolder
{
get { return _sortOrderFolder; }
set
{
if (value < 0)
{
if (_sortOrderFolder == -1)
return;

_sortOrderFolder = -1;
}
if (value >= 0)
{
if (_sortOrderFolder == 1)
return;

_sortOrderFolder = 1;
}

DirectoryCache.Reverse();

if (SortOrderChanged != null)
SortOrderChanged.Invoke(_sortOrderFolder);
}
}
private int _sortOrderFolder = 1;

public int SortOrderFile
{
get { return _sortOrderFile; }
set
{
if (value < 0)
{
if (_sortOrderFile == -1)
return;

_sortOrderFile = -1;
}
if (value >= 0)
{
if (_sortOrderFile == 1)
return;

_sortOrderFile = 1;
}

FileCache.Reverse();

if (SortOrderChanged != null)
SortOrderChanged.Invoke(_sortOrderFile);
}
}
public int _sortOrderFile = -1;

public string CurrentDirectory
{
Expand Down Expand Up @@ -253,7 +314,7 @@ public void WaitThreadsFinished()
/// <param name="arr"></param>
/// <param name="name"></param>
/// <returns></returns>
private int BinarySearchItemIndex(List<string> arr, string name)
private int BinarySearchItemIndex(List<string> arr, string name, int sortorder = 1)
{
int L = 0;
int R = arr.Count - 1;
Expand All @@ -264,7 +325,7 @@ private int BinarySearchItemIndex(List<string> arr, string name)
{
mid = (L + R) / 2;

com = Helper.StringCompareNatural(arr[mid], name);
com = Helper.StringCompareNatural(arr[mid], name) * sortorder;

if (com == 0)
{
Expand All @@ -289,7 +350,7 @@ private int BinarySearchItemIndex(List<string> arr, string name)
/// <param name="arr">The array to search.</param>
/// <param name="name">The filename to search.</param>
/// <returns></returns>
private int BinarySearchIndex(List<string> arr, string name)
private int BinarySearchIndex(List<string> arr, string name, int sortorder = 1)
{
int L = 0;
int R = arr.Count;
Expand All @@ -299,7 +360,7 @@ private int BinarySearchIndex(List<string> arr, string name)
{
mid = (L + R) / 2;

if (Helper.StringCompareNatural(arr[mid], name) <= 0)
if (sortorder * Helper.StringCompareNatural(arr[mid], name) <= 0)
{
L = mid + 1;
}
Expand All @@ -317,7 +378,7 @@ private void BinaryInsertFileCache(string name, bool fireEvent = true)
if (string.IsNullOrEmpty(name))
return;

int index = BinarySearchIndex(FileCache, name);
int index = BinarySearchIndex(FileCache, name, SortOrderFile);
FileCache.Insert(index, name);
if (fireEvent)
OnFileAdded(name);
Expand All @@ -329,7 +390,7 @@ private void BinaryInsertDirectoryCache(string name, bool fireEvent = true)
if (string.IsNullOrEmpty(name))
return;

int index = BinarySearchIndex(DirectoryCache, name);
int index = BinarySearchIndex(DirectoryCache, name, SortOrderFolder);
DirectoryCache.Insert(index, name);
if (fireEvent)
OnDirectoryAdded(name);
Expand Down Expand Up @@ -365,14 +426,14 @@ private void SetFiles(string path)
FileCache.Clear();
if (FilterFileExtensions == null)
{
foreach (string i in Directory.EnumerateFiles(path).OrderByNatural(e => e))
foreach (string i in Directory.EnumerateFiles(path).OrderByNatural(e => e, StringComparer.CurrentCulture, SortOrderFile != -1))
{
FileCache.Add(Path.GetFileName(i));
}
}
else
{
foreach (string i in Directory.EnumerateFiles(path).OrderByNatural(e => e))
foreach (string i in Directory.EnumerateFiles(path).OrderByNatural(e => e, StringComparer.CurrentCulture, SortOrderFile != -1))
{
if (FilterFileExtensions.Contains(PathHelper.GetFilenameExtension(i)))
{
Expand All @@ -385,7 +446,7 @@ private void SetFiles(string path)
DirectorySortThread = Task.Run(() =>
{
DirectoryCache.Clear();
foreach (string i in Directory.EnumerateDirectories(path).OrderByNatural(e => e))
foreach (string i in Directory.EnumerateDirectories(path).OrderByNatural(e => e, StringComparer.CurrentCulture, SortOrderFolder != -1))
{
DirectoryCache.Add(Path.GetFileName(i));
}
Expand Down
Loading

0 comments on commit e88ce92

Please sign in to comment.