using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Platform.Storage; using Avalonia.Styling; using Avalonia.Threading; using System.IO; namespace ImageCatalog_2; public partial class AvaloniaMainWindow : Window { private readonly DataModel _model; private bool _isDarkTheme; public AvaloniaMainWindow(DataModel model) { InitializeComponent(); _model = model; DataContext = _model; Opened += (_, _) => SyncThemeStateFromCurrentTheme(); // Let DataModel marshal callbacks onto Avalonia UI thread. _model.UiInvoker = action => Dispatcher.UIThread.Invoke(action); _model.SelectSourceFolderRequested += async (_, _) => { var folders = await StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions { Title = "Seleziona cartella sorgente" }); if (folders.Count > 0) { _model.SourcePath = folders[0].Path.LocalPath + Path.DirectorySeparatorChar; } }; _model.SelectDestinationFolderRequested += async (_, _) => { var folders = await StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions { Title = "Seleziona cartella destinazione" }); if (folders.Count > 0) { _model.DestinationPath = folders[0].Path.LocalPath + Path.DirectorySeparatorChar; } }; _model.SelectLogoFileRequested += async (_, _) => { var files = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions { Title = "Seleziona logo", FileTypeFilter = [ new FilePickerFileType("Immagini") { Patterns = ["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif"] } ] }); if (files.Count > 0) { _model.LogoFile = files[0].Path.LocalPath; } }; _model.SelectModelsFolderRequested += async (_, _) => { var folders = await StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions { Title = "Seleziona cartella modelli" }); if (folders.Count > 0) { _model.ModelsFolderPath = folders[0].Path.LocalPath + Path.DirectorySeparatorChar; } }; _model.SelectCsvOutputRequested += async (_, _) => { var file = await StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions { Title = "Salva CSV", DefaultExtension = "csv", FileTypeChoices = [new FilePickerFileType("CSV") { Patterns = ["*.csv"] }] }); if (file is not null) { _model.CsvOutputPath = file.Path.LocalPath; } }; _model.SaveSettingsRequested += async (_, _) => { var file = await StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions { Title = "Salva impostazioni", DefaultExtension = "xml", FileTypeChoices = [new FilePickerFileType("Setup") { Patterns = ["*.xml"] }] }); if (file is not null) { await _model.SaveSettingsToFileAsync(file.Path.LocalPath); } }; _model.LoadSettingsRequested += async (_, _) => { var files = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions { Title = "Carica impostazioni", FileTypeFilter = [new FilePickerFileType("Setup") { Patterns = ["*.xml"] }] }); if (files.Count > 0) { await _model.LoadSettingsFromFileAsync(files[0].Path.LocalPath); } }; _model.SelectColorRequested += (_, _) => { // Color is set by typing hex directly in the TextBox. }; _model.SelectTransparentColorRequested += (_, _) => { // Color is set by typing hex directly in the TextBox. }; } private void ToggleTheme_Click(object? sender, RoutedEventArgs e) { _isDarkTheme = !_isDarkTheme; if (Avalonia.Application.Current is not null) { Avalonia.Application.Current.RequestedThemeVariant = _isDarkTheme ? ThemeVariant.Dark : ThemeVariant.Light; } UpdateThemeToggleButtonContent(); } private void SyncThemeStateFromCurrentTheme() { _isDarkTheme = ActualThemeVariant == ThemeVariant.Dark; UpdateThemeToggleButtonContent(); } private void UpdateThemeToggleButtonContent() { var toggleButton = this.FindControl("ThemeToggleButton"); if (toggleButton is null) { return; } toggleButton.Content = _isDarkTheme ? "☀" : "🌙"; } }