reformatting
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<Window xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:vm="using:finder2e_foundry_converter.ViewModels"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="600"
|
||||
x:Class="finder2e_foundry_converter.Views.MainWindow"
|
||||
x:DataType="vm:MainWindowViewModel"
|
||||
Icon="/assets/avalonia-logo.ico"
|
||||
Title="Finder2e Foundry Converter"
|
||||
Width="500" Height="600"
|
||||
DragDrop.AllowDrop="True">
|
||||
|
||||
<Design.DataContext>
|
||||
<!-- This only sets the DataContext for the previewer in an IDE,
|
||||
to set the actual DataContext at runtime, set the DataContext property in code (look at App.axaml.cs) -->
|
||||
<vm:MainWindowViewModel/>
|
||||
</Design.DataContext>
|
||||
|
||||
<ScrollViewer DragDrop.AllowDrop="True">
|
||||
<StackPanel Margin="20" Spacing="10" DragDrop.AllowDrop="True">
|
||||
<Label Content="Provider:"/>
|
||||
<Grid ColumnDefinitions="*,Auto">
|
||||
<ComboBox Grid.Column="0" HorizontalAlignment="Stretch"
|
||||
ItemsSource="{Binding Providers}"
|
||||
SelectedItem="{Binding SelectedProvider}"/>
|
||||
<Button Grid.Column="1" Content="Address" Command="{Binding ToggleAddressCommand}" Margin="5,0,0,0"/>
|
||||
</Grid>
|
||||
|
||||
<TextBlock Text="{Binding ErrorText}" Foreground="Red" HorizontalAlignment="Center" IsVisible="{Binding IsErrorVisible}"/>
|
||||
|
||||
<StackPanel IsVisible="{Binding IsAddressVisible}" Spacing="5">
|
||||
<Label Content="{Binding SelectedProvider, StringFormat='{}{0} Address:'}"/>
|
||||
<TextBox Text="{Binding CurrentAddress}" PlaceholderText="http://localhost:11434"/>
|
||||
</StackPanel>
|
||||
|
||||
<Label Content="Model:"/>
|
||||
<Grid ColumnDefinitions="*,Auto">
|
||||
<ComboBox Grid.Column="0" HorizontalAlignment="Stretch"
|
||||
ItemsSource="{Binding Models}"
|
||||
SelectedItem="{Binding SelectedModel}"
|
||||
PlaceholderText="Select Model"/>
|
||||
<Button Grid.Column="1" Content="Refresh" Command="{Binding RefreshModelsCommand}" Margin="5,0,0,0"/>
|
||||
</Grid>
|
||||
|
||||
<Label Content="Description:"/>
|
||||
<TextBox Text="{Binding Description}" AcceptsReturn="True" Height="100" PlaceholderText="Enter description here..."/>
|
||||
|
||||
<Label Content="Images:"/>
|
||||
<ScrollViewer Height="180">
|
||||
<ItemsControl ItemsSource="{Binding ImageItems}">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<WrapPanel />
|
||||
</ItemsPanelTemplate>
|
||||
</ItemsControl.ItemsPanel>
|
||||
<ItemsControl.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Grid Margin="5">
|
||||
<Border BorderBrush="Gray" BorderThickness="1" CornerRadius="4" ClipToBounds="True">
|
||||
<Image Source="{Binding Thumbnail}" Width="100" Height="100" Stretch="UniformToFill" />
|
||||
</Border>
|
||||
<Button Content="X"
|
||||
Command="{Binding $parent[Window].((vm:MainWindowViewModel)DataContext).RemoveImageCommand}"
|
||||
CommandParameter="{Binding}"
|
||||
HorizontalAlignment="Right" VerticalAlignment="Top"
|
||||
Background="#80000000" Foreground="White" Padding="5,2"/>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
</ScrollViewer>
|
||||
|
||||
<Button Content="Select Image" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Click="SelectImage_Click"/>
|
||||
|
||||
<Button Content="Generate" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center"
|
||||
Command="{Binding GenerateCommand}" FontWeight="Bold"/>
|
||||
|
||||
<TextBlock Text="{Binding StatusMessage}" TextWrapping="Wrap" Margin="0,10,0,0"/>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
|
||||
</Window>
|
||||
@@ -0,0 +1,179 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Input.Platform;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Platform.Storage;
|
||||
using Avalonia.Media.Imaging;
|
||||
using finder2e_foundry_converter.ViewModels;
|
||||
|
||||
namespace finder2e_foundry_converter.Views
|
||||
{
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
AddHandler(DragDrop.DropEvent, Drop);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
// Handle multiple lines (multiple files)
|
||||
var lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var path = line.Trim();
|
||||
if (path.StartsWith("file://"))
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Try to get bitmap directly
|
||||
var bitmap = await topLevel.Clipboard.TryGetBitmapAsync();
|
||||
if (bitmap != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var tempDir = Path.Combine(Path.GetTempPath(), "Finder2eFoundryConverter");
|
||||
if (!Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir);
|
||||
|
||||
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}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 vm = DataContext as MainWindowViewModel;
|
||||
if (vm == null) return;
|
||||
|
||||
var files = e.DataTransfer.TryGetFiles();
|
||||
if (files != null)
|
||||
{
|
||||
foreach (var file in files)
|
||||
{
|
||||
var path = file.Path.LocalPath;
|
||||
if (path == null) continue;
|
||||
|
||||
var ext = 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)
|
||||
{
|
||||
try
|
||||
{
|
||||
var tempDir = Path.Combine(Path.GetTempPath(), "Finder2eFoundryConverter");
|
||||
if (!Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir);
|
||||
|
||||
var tempPath = Path.Combine(tempDir, $"dropped_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 dropped image: {ex.Message}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user