Files
finder2e_foundry_converter/Views/MainWindow.axaml.cs
T

180 lines
6.4 KiB
C#
Raw Normal View History

2026-06-08 10:39:11 -05:00
using System;
using System.Linq;
using System.Threading.Tasks;
2026-06-08 10:50:01 -05:00
using System.IO;
2026-06-08 10:39:11 -05:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
2026-06-08 10:50:01 -05:00
using Avalonia.Media.Imaging;
2026-06-08 15:18:19 -05:00
using finder2e_foundry_converter.ViewModels;
2026-06-08 10:39:11 -05:00
2026-06-08 15:18:19 -05:00
namespace finder2e_foundry_converter.Views
2026-06-08 10:39:11 -05:00
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AddHandler(DragDrop.DropEvent, Drop);
2026-06-08 10:50:01 -05:00
AddHandler(DragDrop.DragOverEvent, DragOver);
AddHandler(KeyDownEvent, OnKeyDown, RoutingStrategies.Bubble, true);
}
private void DragOver(object? sender, DragEventArgs e)
{
if (e.DataTransfer.TryGetFiles() != null || e.DataTransfer.TryGetBitmap() != null)
{
e.DragEffects = DragDropEffects.Copy;
}
else
{
e.DragEffects = DragDropEffects.None;
}
2026-06-08 10:39:11 -05:00
}
private async void OnKeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Key.V && e.KeyModifiers == KeyModifiers.Control)
{
var topLevel = GetTopLevel(this);
if (topLevel?.Clipboard == null) return;
var vm = DataContext as MainWindowViewModel;
if (vm == null) return;
// Try to get text (for file paths)
var text = await topLevel.Clipboard.TryGetTextAsync();
if (!string.IsNullOrWhiteSpace(text))
{
2026-06-08 10:50:01 -05:00
// Handle multiple lines (multiple files)
var lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
2026-06-08 10:39:11 -05:00
{
2026-06-08 10:50:01 -05:00
var path = line.Trim();
if (path.StartsWith("file://"))
2026-06-08 10:39:11 -05:00
{
2026-06-08 10:50:01 -05:00
try
{
var uri = new Uri(path);
path = uri.LocalPath;
}
catch { path = path.Replace("file://", ""); }
}
if (File.Exists(path))
{
var ext = Path.GetExtension(path).ToLower();
if (new[] { ".png", ".jpg", ".jpeg", ".webp" }.Contains(ext))
{
vm.AddImagePathCommand.Execute(path);
return;
}
2026-06-08 10:39:11 -05:00
}
}
}
// Try to get bitmap directly
var bitmap = await topLevel.Clipboard.TryGetBitmapAsync();
if (bitmap != null)
{
2026-06-08 10:50:01 -05:00
try
{
var tempDir = Path.Combine(Path.GetTempPath(), "Finder2eFoundryConverter");
if (!Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir);
2026-06-08 10:39:11 -05:00
2026-06-08 10:50:01 -05:00
var tempPath = Path.Combine(tempDir, $"pasted_image_{DateTime.Now.Ticks}.png");
using (var stream = File.Create(tempPath))
{
bitmap.Save(stream);
}
vm.AddImagePathCommand.Execute(tempPath);
}
catch (Exception ex)
{
vm.StatusMessage = $"Error saving pasted image: {ex.Message}";
}
2026-06-08 10:39:11 -05:00
}
}
}
private async void SelectImage_Click(object? sender, RoutedEventArgs e)
{
var topLevel = GetTopLevel(this);
if (topLevel == null) return;
var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = "Select Images",
AllowMultiple = true,
FileTypeFilter = new[]
{
new FilePickerFileType("Images")
{
Patterns = new[] { "*.png", "*.jpg", "*.jpeg", "*.webp" }
}
}
});
if (files.Count > 0)
{
var vm = DataContext as MainWindowViewModel;
if (vm == null) return;
foreach (var file in files)
{
vm.AddImagePathCommand.Execute(file.Path.LocalPath);
}
}
}
private async void Drop(object? sender, DragEventArgs e)
{
2026-06-08 10:50:01 -05:00
var vm = DataContext as MainWindowViewModel;
if (vm == null) return;
2026-06-08 10:39:11 -05:00
var files = e.DataTransfer.TryGetFiles();
if (files != null)
{
foreach (var file in files)
{
var path = file.Path.LocalPath;
if (path == null) continue;
2026-06-08 10:50:01 -05:00
var ext = Path.GetExtension(path).ToLower();
2026-06-08 10:39:11 -05:00
if (new[] { ".png", ".jpg", ".jpeg", ".webp" }.Contains(ext))
{
vm.AddImagePathCommand.Execute(path);
}
}
}
// Try to get bitmap directly from drop
var bitmap = e.DataTransfer.TryGetBitmap();
if (bitmap != null)
{
2026-06-08 10:50:01 -05:00
try
2026-06-08 10:39:11 -05:00
{
2026-06-08 10:50:01 -05:00
var tempDir = Path.Combine(Path.GetTempPath(), "Finder2eFoundryConverter");
if (!Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir);
2026-06-08 10:39:11 -05:00
2026-06-08 10:50:01 -05:00
var tempPath = Path.Combine(tempDir, $"dropped_image_{DateTime.Now.Ticks}.png");
using (var stream = File.Create(tempPath))
{
bitmap.Save(stream);
}
2026-06-08 10:39:11 -05:00
vm.AddImagePathCommand.Execute(tempPath);
}
2026-06-08 10:50:01 -05:00
catch (Exception ex)
{
vm.StatusMessage = $"Error saving dropped image: {ex.Message}";
}
2026-06-08 10:39:11 -05:00
}
}
}
}