Catalog/imagecatalog/AvaloniaViews/FaceAiTabView.axaml.cs
MaddoScientisto 25fdb82d2f
Some checks failed
Build Windows Avalonia / build (push) Failing after 1m19s
Build Windows Avalonia / release (push) Has been skipped
feat: Add face encoder settings including GPU support, parallelism, and thumbnail options
2026-05-09 15:46:41 +02:00

224 lines
6.1 KiB
C#

using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
namespace ImageCatalog_2.AvaloniaViews;
public partial class FaceAiTabView : Avalonia.Controls.UserControl
{
private INotifyPropertyChanged? _faceAiPropertySource;
public FaceAiTabView()
{
InitializeComponent();
DataContextChanged += OnDataContextChanged;
}
private void OnDataContextChanged(object? sender, EventArgs e)
{
if (_faceAiPropertySource is not null)
{
_faceAiPropertySource.PropertyChanged -= OnFaceAiPropertyChanged;
}
_faceAiPropertySource = DataContext as INotifyPropertyChanged;
if (_faceAiPropertySource is not null)
{
_faceAiPropertySource.PropertyChanged += OnFaceAiPropertyChanged;
}
}
private void OnFaceAiPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (!string.Equals(e.PropertyName, nameof(DataModel.FaceCommandOutput), StringComparison.Ordinal))
{
return;
}
var outputBox = this.FindControl<Avalonia.Controls.TextBox>("FaceOutputTextBox");
if (outputBox is null)
{
return;
}
Dispatcher.UIThread.Post(() =>
{
var textLength = outputBox.Text?.Length ?? 0;
outputBox.CaretIndex = textLength;
});
}
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 folders = await storageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = "Seleziona la cartella Face Recognition Windows"
});
if (folders.Count > 0)
{
executableBox.Text = folders[0].Path.LocalPath;
if (DataContext is DataModel model)
{
model.FaceExecutablePath = executableBox.Text;
}
}
}
private async void SelectFaceOutputFolder_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 folders = await storageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = "Seleziona la cartella output per encodings e log"
});
if (folders.Count > 0)
{
outputBox.Text = folders[0].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 (Directory.Exists(path))
{
OpenInExplorer(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 (Directory.Exists(outputPath))
{
OpenInExplorer(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;
}
if (Directory.Exists(path))
{
OpenInExplorer(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.
}
}
}