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; } private async void Form_MouseWheel(object? sender, MouseEventArgs e) { Console.WriteLine("w " + e.Delta); if (_currentPath == null) return; if (e.Delta < 0) { //next await NextFile(true); } else if (_currentPath != null) { //prev await NextFile(false); } } 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; if (e.KeyCode == Keys.Home) await JumpToBoundFile(true); if (e.KeyCode == Keys.End) await JumpToBoundFile(false); if (e.KeyCode == Keys.Space || e.KeyCode == Keys.Enter || e.KeyCode == Keys.PageDown) await NextFile(true); if (e.KeyCode == Keys.Back || e.KeyCode == Keys.PageUp) await NextFile(false); if (e.KeyCode == Keys.B) { var src = _currentPath; await NextFile(true); MoveToOuterDotSuffixDir(src, "b"); } if (e.KeyCode == Keys.S) { var src = _currentPath; await NextFile(true); MoveToOuterDotSuffixDir(src, "s"); } } private void Form_DragEnter(object sender, DragEventArgs e) { if (e.Data?.GetDataPresent(DataFormats.FileDrop) == true) { if (e.Data.GetData(DataFormats.FileDrop) is string[] arr) { 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 string[] arr) { if (arr.Length == 1 && File.Exists(arr[0])) { 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 NextFile(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 JumpToBoundFile(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) { var nears = GetFilesAround(_currentPath); var index = Array.IndexOf(nears, _currentPath); _mainForm.Text = $"({nears.Length}/{index + 1}) {_currentPath}"; } } private string[] GetFilesAround(string filePath) { var ext = Path.GetExtension(filePath); var dir = Path.GetDirectoryName(filePath); return Directory.GetFiles(dir, "*" + ext); } } }