Catalog/imagecatalog/AvaloniaViews/FaceAiTabView.axaml.cs

179 lines
5.1 KiB
C#

using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using System;
using System.Diagnostics;
using System.IO;
namespace ImageCatalog_2.AvaloniaViews;
public partial class FaceAiTabView : Avalonia.Controls.UserControl
{
public FaceAiTabView()
{
InitializeComponent();
}
private async void SelectFaceExecutable_Click(object? sender, RoutedEventArgs e)
{
var executableBox = this.FindControl<Avalonia.Controls.TextBox>("FaceExecutablePathTextBox");
if (executableBox is null)
{
return;
}
var topLevel = TopLevel.GetTopLevel(this);
var storageProvider = topLevel?.StorageProvider;
if (storageProvider is null)
{
return;
}
var files = await storageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = "Seleziona face_encoder_cpu.exe o face_encoder_gpu.exe",
FileTypeFilter =
[
new FilePickerFileType("Eseguibile") { Patterns = ["*.exe"] },
new FilePickerFileType("Tutti i file") { Patterns = ["*.*"] }
]
});
if (files.Count > 0)
{
executableBox.Text = files[0].Path.LocalPath;
if (DataContext is DataModel model)
{
model.FaceExecutablePath = executableBox.Text;
}
}
}
private async void SelectFaceOutputFile_Click(object? sender, RoutedEventArgs e)
{
var outputBox = this.FindControl<Avalonia.Controls.TextBox>("FaceOutputFolderTextBox");
if (outputBox is null)
{
return;
}
var topLevel = TopLevel.GetTopLevel(this);
var storageProvider = topLevel?.StorageProvider;
if (storageProvider is null)
{
return;
}
var files = await storageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = "Seleziona file output encodings (.pkl)",
SuggestedFileName = "encodings.pkl",
DefaultExtension = "pkl",
FileTypeChoices =
[
new FilePickerFileType("Pickle file") { Patterns = ["*.pkl"] }
],
ShowOverwritePrompt = true
});
if (files is not null)
{
outputBox.Text = files.Path.LocalPath;
if (DataContext is DataModel model)
{
model.FaceOutputFolderPath = outputBox.Text;
}
}
}
private void OpenFaceExecutableFolder_Click(object? sender, RoutedEventArgs e)
{
var executableBox = this.FindControl<Avalonia.Controls.TextBox>("FaceExecutablePathTextBox");
if (executableBox is null)
{
return;
}
var path = executableBox.Text?.Trim();
if (string.IsNullOrWhiteSpace(path))
{
return;
}
if (File.Exists(path))
{
OpenInExplorer(path);
return;
}
var directory = Path.GetDirectoryName(path);
OpenInExplorer(string.IsNullOrWhiteSpace(directory) ? path : directory);
}
private void OpenFaceOutputFolder_Click(object? sender, RoutedEventArgs e)
{
var outputBox = this.FindControl<Avalonia.Controls.TextBox>("FaceOutputFolderTextBox");
if (outputBox is null)
{
return;
}
var outputPath = outputBox.Text?.Trim();
if (string.IsNullOrWhiteSpace(outputPath))
{
return;
}
if (File.Exists(outputPath))
{
OpenInExplorer(outputPath);
return;
}
var directory = Path.GetDirectoryName(outputPath);
OpenInExplorer(string.IsNullOrWhiteSpace(directory) ? outputPath : directory);
}
private void OpenFaceDestinationFolder_Click(object? sender, RoutedEventArgs e)
{
var destBox = this.FindControl<Avalonia.Controls.TextBox>("FaceDestinationPathTextBox");
string? path = destBox?.Text?.Trim();
if (string.IsNullOrWhiteSpace(path) && DataContext is DataModel model)
{
path = (model.DestinationPath ?? string.Empty).Trim();
}
if (string.IsNullOrWhiteSpace(path))
{
return;
}
var directory = Path.GetDirectoryName(path);
OpenInExplorer(string.IsNullOrWhiteSpace(directory) ? path : directory);
}
private static void OpenInExplorer(string? path)
{
if (string.IsNullOrWhiteSpace(path))
{
return;
}
var normalizedPath = path.Trim().Trim('"');
try
{
if (File.Exists(normalizedPath))
{
Process.Start("explorer.exe", $"/select,\"{normalizedPath}\"");
}
else if (Directory.Exists(normalizedPath))
{
Process.Start(new ProcessStartInfo { FileName = normalizedPath, UseShellExecute = true });
}
}
catch
{
// Ignore failures when opening Explorer.
}
}
}