CoverForm.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace PictureMover
  12. {
  13. public partial class CoverForm : Form
  14. {
  15. private readonly MainForm _mainForm;
  16. private string? _currentPath;
  17. public CoverForm(MainForm mainForm)
  18. {
  19. _mainForm = mainForm;
  20. InitializeComponent();
  21. MouseWheel += Form_MouseWheel;
  22. PreviewKeyDown += Form_PreviewKeyDown;
  23. }
  24. private async void Form_MouseWheel(object? sender, MouseEventArgs e)
  25. {
  26. Console.WriteLine("w " + e.Delta);
  27. if (_currentPath == null) return;
  28. if (e.Delta < 0)
  29. {
  30. //next
  31. await NextFile(true);
  32. }
  33. else if (_currentPath != null)
  34. {
  35. //prev
  36. await NextFile(false);
  37. }
  38. }
  39. private async void Form_PreviewKeyDown(object? sender, PreviewKeyDownEventArgs e)
  40. {
  41. Console.WriteLine($"k {e.KeyCode}");
  42. if (e.KeyCode == Keys.Escape) _mainForm.Close();
  43. if (_currentPath == null) return;
  44. if (e.KeyCode == Keys.Home) await JumpToBoundFile(true);
  45. if (e.KeyCode == Keys.End) await JumpToBoundFile(false);
  46. if (e.KeyCode == Keys.Space || e.KeyCode == Keys.Enter || e.KeyCode == Keys.PageDown) await NextFile(true);
  47. if (e.KeyCode == Keys.Back || e.KeyCode == Keys.PageUp) await NextFile(false);
  48. if (e.KeyCode == Keys.B)
  49. {
  50. var src = _currentPath;
  51. await NextFile(true);
  52. MoveToOuterDotSuffixDir(src, "b");
  53. }
  54. if (e.KeyCode == Keys.S)
  55. {
  56. var src = _currentPath;
  57. await NextFile(true);
  58. MoveToOuterDotSuffixDir(src, "s");
  59. }
  60. }
  61. private void Form_DragEnter(object sender, DragEventArgs e)
  62. {
  63. if (e.Data?.GetDataPresent(DataFormats.FileDrop) == true)
  64. {
  65. if (e.Data.GetData(DataFormats.FileDrop) is string[] arr)
  66. {
  67. if (arr.Length == 1 && File.Exists(arr[0])) e.Effect = DragDropEffects.Link;
  68. }
  69. }
  70. }
  71. private async void Form_DragDrop(object sender, DragEventArgs e)
  72. {
  73. if (e.Data.GetData(DataFormats.FileDrop) is string[] arr)
  74. {
  75. if (arr.Length == 1 && File.Exists(arr[0]))
  76. {
  77. var path = arr[0];
  78. await ShowFileAsync(path);
  79. }
  80. }
  81. }
  82. private void timer1_Tick(object sender, EventArgs e)
  83. {
  84. var location = _mainForm.PointToScreen(Point.Empty);
  85. if (Location != location) Location = location;
  86. var size = _mainForm.ClientSize;
  87. if (Size != size) Size = size;
  88. }
  89. private async Task NextFile(bool isNext)
  90. {
  91. if (_currentPath == null) return;
  92. var nears = GetFilesAround(_currentPath);
  93. if (nears.Length == 0) return;
  94. var index = Array.IndexOf(nears, _currentPath);
  95. if (isNext)
  96. {
  97. var lastIndex = nears.Length - 1;
  98. if (index == lastIndex) return;
  99. var nextIndex = index + 1;
  100. if (nextIndex >= nears.Length) nextIndex = lastIndex;
  101. var next = nears[nextIndex];
  102. await ShowFileAsync(next);
  103. }
  104. else
  105. {
  106. if (index > 0)
  107. {
  108. var nextIndex = index - 1;
  109. var next = nears[nextIndex];
  110. await ShowFileAsync(next);
  111. }
  112. }
  113. }
  114. private async Task JumpToBoundFile(bool first)
  115. {
  116. if (_currentPath == null) return;
  117. var nears = GetFilesAround(_currentPath);
  118. if (nears.Length == 0) return;
  119. var nextIndex = first ? 0 : nears.Length - 1;
  120. var next = nears[nextIndex];
  121. await ShowFileAsync(next);
  122. }
  123. private async Task ShowFileAsync(string path)
  124. {
  125. await _mainForm.webView21.EnsureCoreWebView2Async();
  126. var nears = GetFilesAround(path);
  127. if (nears.Length == 0)
  128. {
  129. _mainForm.Text = "- no file -";
  130. _mainForm.webView21.CoreWebView2.Navigate("about:blank");
  131. _currentPath = null;
  132. return;
  133. }
  134. var index = Array.IndexOf(nears, path);
  135. _mainForm.Text = $"({nears.Length}/{index + 1}) {path}";
  136. _mainForm.webView21.CoreWebView2.Navigate("file:///" + path);
  137. _currentPath = path;
  138. }
  139. private void MoveToOuterDotSuffixDir(string src, string s)
  140. {
  141. if (File.Exists(src))
  142. {
  143. var dir = Path.GetDirectoryName(src);
  144. var parentDir = Path.GetDirectoryName(dir);
  145. var name = Path.GetFileName(dir) + "." + s;
  146. var dstDir = Path.Combine(parentDir, name);
  147. if (Directory.Exists(dstDir) == false) Directory.CreateDirectory(dstDir);
  148. var dst = Path.Combine(dstDir, Path.GetFileName(src));
  149. File.Move(src, dst);
  150. }
  151. if (_currentPath != null)
  152. {
  153. var nears = GetFilesAround(_currentPath);
  154. var index = Array.IndexOf(nears, _currentPath);
  155. _mainForm.Text = $"({nears.Length}/{index + 1}) {_currentPath}";
  156. }
  157. }
  158. private string[] GetFilesAround(string filePath)
  159. {
  160. var ext = Path.GetExtension(filePath);
  161. var dir = Path.GetDirectoryName(filePath);
  162. return Directory.GetFiles(dir, "*" + ext);
  163. }
  164. }
  165. }