Files
finder2e_foundry_converter/Views/MainWindow.axaml.cs
T

135 lines
4.8 KiB
C#
Raw Normal View History

2026-06-08 10:39:11 -05:00
using System;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using Finder2eFoundryConverterCS.ViewModels;
namespace Finder2eFoundryConverterCS.Views
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AddHandler(DragDrop.DropEvent, Drop);
KeyDown += OnKeyDown;
}
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))
{
var path = text.Replace("file://", "").Trim();
if (System.IO.File.Exists(path))
{
var ext = System.IO.Path.GetExtension(path).ToLower();
if (new[] { ".png", ".jpg", ".jpeg", ".webp" }.Contains(ext))
{
vm.AddImagePathCommand.Execute(path);
return;
}
}
}
// Try to get bitmap directly
var bitmap = await topLevel.Clipboard.TryGetBitmapAsync();
if (bitmap != null)
{
// Save bitmap to temp file
var tempDir = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Finder2eFoundryConverter");
if (!System.IO.Directory.Exists(tempDir)) System.IO.Directory.CreateDirectory(tempDir);
var tempPath = System.IO.Path.Combine(tempDir, $"pasted_image_{DateTime.Now.Ticks}.png");
bitmap.Save(tempPath);
vm.AddImagePathCommand.Execute(tempPath);
}
}
}
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)
{
var files = e.DataTransfer.TryGetFiles();
if (files != null)
{
var vm = DataContext as MainWindowViewModel;
if (vm == null) return;
foreach (var file in files)
{
var path = file.Path.LocalPath;
if (path == null) continue;
var ext = System.IO.Path.GetExtension(path).ToLower();
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)
{
var vm = DataContext as MainWindowViewModel;
if (vm != null)
{
var tempDir = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Finder2eFoundryConverter");
if (!System.IO.Directory.Exists(tempDir)) System.IO.Directory.CreateDirectory(tempDir);
var tempPath = System.IO.Path.Combine(tempDir, $"dropped_image_{DateTime.Now.Ticks}.png");
bitmap.Save(tempPath);
vm.AddImagePathCommand.Execute(tempPath);
}
}
}
}
}