using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PictureMover { public partial class CoverForm : Form { private readonly MainForm _mainForm; private string? _currentPath; public CoverForm(MainForm mainForm) { _mainForm = mainForm; InitializeComponent(); MouseWheel += Form_MouseWheel; PreviewKeyDown += Form_PreviewKeyDown; Shown += CoverForm_Shown; } private async void CoverForm_Shown(object? sender, EventArgs e) { if (Program.CommandLineArgs.Length == 1 && File.Exists(Program.CommandLineArgs[0])) await ShowFileAsync(Program.CommandLineArgs[0]); } private async void Form_MouseWheel(object? sender, MouseEventArgs e) { Console.WriteLine("w " + e.Delta); if (_currentPath == null) return; await NavFile(e.Delta < 0); } private async void Form_PreviewKeyDown(object? sender, PreviewKeyDownEventArgs e) { Console.WriteLine($"k {e.KeyCode}"); if (e.KeyCode == Keys.Escape) _mainForm.Close(); if (_currentPath == null) return; switch (e.KeyCode) { case Keys.Home: await JumpToFile(true); break; case Keys.End: await JumpToFile(false); break; case Keys.Space: case Keys.Enter: case Keys.PageDown: await NavFile(true); break; case Keys.Back: case Keys.PageUp: await NavFile(false); break; case Keys.B: { var src = _currentPath; await NavFile(true); MoveToOuterDotSuffixDir(src, "b"); break; } case Keys.S: { var src = _currentPath; await NavFile(true); MoveToOuterDotSuffixDir(src, "s"); break; } case Keys.M: { var src = _currentPath; await NavFile(true); MoveToOuterDotSuffixDir(src, "m"); break; } } } private void Form_DragEnter(object sender, DragEventArgs e) { if (e.Data?.GetDataPresent(DataFormats.FileDrop) != true) return; if (e.Data.GetData(DataFormats.FileDrop) is not string[] arr) return; if (arr.Length == 1 && File.Exists(arr[0])) e.Effect = DragDropEffects.Link; } private async void Form_DragDrop(object sender, DragEventArgs e) { if (e.Data?.GetData(DataFormats.FileDrop) is not string[] arr) return; if (arr.Length != 1 || !File.Exists(arr[0])) return; var path = arr[0]; await ShowFileAsync(path); } private void timer1_Tick(object sender, EventArgs e) { var location = _mainForm.PointToScreen(Point.Empty); if (Location != location) Location = location; var size = _mainForm.ClientSize; if (Size != size) Size = size; } private async Task NavFile(bool isNext) { if (_currentPath == null) return; var nears = GetFilesAround(_currentPath); if (nears.Length == 0) return; var index = Array.IndexOf(nears, _currentPath); if (isNext) { var lastIndex = nears.Length - 1; if (index == lastIndex) return; var nextIndex = index + 1; if (nextIndex >= nears.Length) nextIndex = lastIndex; var next = nears[nextIndex]; await ShowFileAsync(next); } else { if (index > 0) { var nextIndex = index - 1; var next = nears[nextIndex]; await ShowFileAsync(next); } } } private async Task JumpToFile(bool first) { if (_currentPath == null) return; var nears = GetFilesAround(_currentPath); if (nears.Length == 0) return; var nextIndex = first ? 0 : nears.Length - 1; var next = nears[nextIndex]; await ShowFileAsync(next); } private async Task ShowFileAsync(string path) { await _mainForm.webView21.EnsureCoreWebView2Async(); var nears = GetFilesAround(path); if (nears.Length == 0) { _mainForm.Text = "- no file -"; _mainForm.webView21.CoreWebView2.Navigate("about:blank"); _currentPath = null; return; } var index = Array.IndexOf(nears, path); _mainForm.Text = $"({nears.Length}/{index + 1}) {path}"; _mainForm.webView21.CoreWebView2.Navigate("file:///" + path); _currentPath = path; } private void MoveToOuterDotSuffixDir(string src, string s) { if (File.Exists(src)) { var dir = Path.GetDirectoryName(src); var parentDir = Path.GetDirectoryName(dir); var name = Path.GetFileName(dir) + "." + s; var dstDir = Path.Combine(parentDir, name); if (Directory.Exists(dstDir) == false) Directory.CreateDirectory(dstDir); var dst = Path.Combine(dstDir, Path.GetFileName(src)); File.Move(src, dst); } if (_currentPath == null) return; var nears = GetFilesAround(_currentPath); var index = Array.IndexOf(nears, _currentPath); _mainForm.Text = $"({nears.Length}/{index + 1}) {_currentPath}"; } private static string[] GetFilesAround(string filePath) { var ext = Path.GetExtension(filePath); var dir = Path.GetDirectoryName(filePath); return Directory.GetFiles(dir, "*" + ext); } } }