using Avalonia.Controls; using Avalonia.Platform.Storage; using Avalonia.Threading; using Microsoft.Extensions.DependencyInjection; using System.Diagnostics; using System.IO; namespace CatalogLite; public partial class MainWindow : Window { private readonly LiteCatalogViewModel _viewModel; public MainWindow() : this(Program.ServiceProvider.GetRequiredService()) { } public MainWindow(LiteCatalogViewModel viewModel) { InitializeComponent(); _viewModel = viewModel; DataContext = _viewModel; _viewModel.UiInvoker = action => Dispatcher.UIThread.Invoke(action); _viewModel.SelectSourceFolderRequested += OnSelectSourceFolderRequested; _viewModel.SelectDestinationFolderRequested += OnSelectDestinationFolderRequested; _viewModel.ShowMessageRequested += OnShowMessageRequested; _viewModel.LoadEmbeddedConfiguration(); } private async void OnSelectSourceFolderRequested(object? sender, EventArgs e) { var folders = await StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions { Title = "Seleziona cartella sorgente" }); if (folders.Count > 0) { _viewModel.SourcePath = LiteCatalogViewModel.NormalizeDirectoryPath(folders[0].Path.LocalPath); } } private async void OnSelectDestinationFolderRequested(object? sender, EventArgs e) { var folders = await StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions { Title = "Seleziona cartella destinazione" }); if (folders.Count > 0) { _viewModel.DestinationPath = LiteCatalogViewModel.NormalizeDirectoryPath(folders[0].Path.LocalPath); } } private void OpenSourcePath_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e) { OpenInExplorer(_viewModel.SourcePath); } private void OpenDestinationPath_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e) { OpenInExplorer(_viewModel.DestinationPath); } private async void OnShowMessageRequested(object? sender, LiteMessageEventArgs e) { await ShowMessageDialogAsync(e.Title, e.Message); } private async Task ShowMessageDialogAsync(string title, string message) { var closeButton = new Button { Content = "OK", HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Right, MinWidth = 92 }; var dialog = new Window { Title = title, Width = 420, Height = 180, CanResize = false, WindowStartupLocation = WindowStartupLocation.CenterOwner, Content = new Border { Padding = new Avalonia.Thickness(20), Child = new StackPanel { Spacing = 14, Children = { new TextBlock { Text = message, TextWrapping = Avalonia.Media.TextWrapping.Wrap }, closeButton } } } }; closeButton.Click += (_, _) => dialog.Close(); await dialog.ShowDialog(this); } 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}\""); return; } if (Directory.Exists(normalizedPath)) { Process.Start(new ProcessStartInfo { FileName = normalizedPath, UseShellExecute = true }); return; } var containingDirectory = Path.GetDirectoryName(normalizedPath); if (!string.IsNullOrWhiteSpace(containingDirectory) && Directory.Exists(containingDirectory)) { Process.Start(new ProcessStartInfo { FileName = containingDirectory, UseShellExecute = true }); } } catch { } } }