CoverForm.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. Shown += CoverForm_Shown;
  24. }
  25. private async void CoverForm_Shown(object? sender, EventArgs e)
  26. {
  27. if (Program.CommandLineArgs.Length == 1 && File.Exists(Program.CommandLineArgs[0])) await ShowFileAsync(Program.CommandLineArgs[0]);
  28. }
  29. private async void Form_MouseWheel(object? sender, MouseEventArgs e)
  30. {
  31. Console.WriteLine("w " + e.Delta);
  32. if (_currentPath == null) return;
  33. await NavFile(e.Delta < 0);
  34. }
  35. private async void Form_PreviewKeyDown(object? sender, PreviewKeyDownEventArgs e)
  36. {
  37. Console.WriteLine($"k {e.KeyCode}");
  38. if (e.KeyCode == Keys.Escape) _mainForm.Close();
  39. if (_currentPath == null) return;
  40. switch (e.KeyCode)
  41. {
  42. case Keys.Home:
  43. await JumpToFile(true);
  44. break;
  45. case Keys.End:
  46. await JumpToFile(false);
  47. break;
  48. case Keys.Space:
  49. case Keys.Enter:
  50. case Keys.PageDown:
  51. await NavFile(true);
  52. break;
  53. case Keys.Back:
  54. case Keys.PageUp:
  55. await NavFile(false);
  56. break;
  57. case Keys.B:
  58. {
  59. var src = _currentPath;
  60. await NavFile(true);
  61. MoveToOuterDotSuffixDir(src, "b");
  62. break;
  63. }
  64. case Keys.S:
  65. {
  66. var src = _currentPath;
  67. await NavFile(true);
  68. MoveToOuterDotSuffixDir(src, "s");
  69. break;
  70. }
  71. case Keys.M:
  72. {
  73. var src = _currentPath;
  74. await NavFile(true);
  75. MoveToOuterDotSuffixDir(src, "m");
  76. break;
  77. }
  78. }
  79. }
  80. private void Form_DragEnter(object sender, DragEventArgs e)
  81. {
  82. if (e.Data?.GetDataPresent(DataFormats.FileDrop) != true) return;
  83. if (e.Data.GetData(DataFormats.FileDrop) is not string[] arr) return;
  84. if (arr.Length == 1 && File.Exists(arr[0])) e.Effect = DragDropEffects.Link;
  85. }
  86. private async void Form_DragDrop(object sender, DragEventArgs e)
  87. {
  88. if (e.Data?.GetData(DataFormats.FileDrop) is not string[] arr) return;
  89. if (arr.Length != 1 || !File.Exists(arr[0])) return;
  90. var path = arr[0];
  91. await ShowFileAsync(path);
  92. }
  93. private void timer1_Tick(object sender, EventArgs e)
  94. {
  95. var location = _mainForm.PointToScreen(Point.Empty);
  96. if (Location != location) Location = location;
  97. var size = _mainForm.ClientSize;
  98. if (Size != size) Size = size;
  99. }
  100. private async Task NavFile(bool isNext)
  101. {
  102. if (_currentPath == null) return;
  103. var nears = GetFilesAround(_currentPath);
  104. if (nears.Length == 0) return;
  105. var index = Array.IndexOf(nears, _currentPath);
  106. if (isNext)
  107. {
  108. var lastIndex = nears.Length - 1;
  109. if (index == lastIndex) return;
  110. var nextIndex = index + 1;
  111. if (nextIndex >= nears.Length) nextIndex = lastIndex;
  112. var next = nears[nextIndex];
  113. await ShowFileAsync(next);
  114. }
  115. else
  116. {
  117. if (index > 0)
  118. {
  119. var nextIndex = index - 1;
  120. var next = nears[nextIndex];
  121. await ShowFileAsync(next);
  122. }
  123. }
  124. }
  125. private async Task JumpToFile(bool first)
  126. {
  127. if (_currentPath == null) return;
  128. var nears = GetFilesAround(_currentPath);
  129. if (nears.Length == 0) return;
  130. var nextIndex = first ? 0 : nears.Length - 1;
  131. var next = nears[nextIndex];
  132. await ShowFileAsync(next);
  133. }
  134. private async Task ShowFileAsync(string path)
  135. {
  136. await _mainForm.webView21.EnsureCoreWebView2Async();
  137. var nears = GetFilesAround(path);
  138. if (nears.Length == 0)
  139. {
  140. _mainForm.Text = "- no file -";
  141. _mainForm.webView21.CoreWebView2.Navigate("about:blank");
  142. _currentPath = null;
  143. return;
  144. }
  145. var index = Array.IndexOf(nears, path);
  146. _mainForm.Text = $"({nears.Length}/{index + 1}) {path}";
  147. _mainForm.webView21.CoreWebView2.Navigate("file:///" + path);
  148. _currentPath = path;
  149. }
  150. private void MoveToOuterDotSuffixDir(string src, string s)
  151. {
  152. if (File.Exists(src))
  153. {
  154. var dir = Path.GetDirectoryName(src);
  155. var parentDir = Path.GetDirectoryName(dir);
  156. var name = Path.GetFileName(dir) + "." + s;
  157. var dstDir = Path.Combine(parentDir, name);
  158. if (Directory.Exists(dstDir) == false) Directory.CreateDirectory(dstDir);
  159. var dst = Path.Combine(dstDir, Path.GetFileName(src));
  160. File.Move(src, dst);
  161. }
  162. if (_currentPath == null) return;
  163. var nears = GetFilesAround(_currentPath);
  164. var index = Array.IndexOf(nears, _currentPath);
  165. _mainForm.Text = $"({nears.Length}/{index + 1}) {_currentPath}";
  166. }
  167. private static string[] GetFilesAround(string filePath)
  168. {
  169. var ext = Path.GetExtension(filePath);
  170. var dir = Path.GetDirectoryName(filePath);
  171. return Directory.GetFiles(dir, "*" + ext);
  172. }
  173. }
  174. }