using Avalonia; using Avalonia.Controls; using Avalonia.Media; using Avalonia.VisualTree; using Avalonia.Threading; using Project_Periodensystem.Model; using System; using System.Collections.Generic; using System.Linq; using static Project_Periodensystem.Model.AppTheme; namespace Project_Periodensystem.View { public partial class MainWindow : Window { private ContentControl? mainContent; public static AppTheme CurrentTheme { get; set; } = AppTheme.Dark; public static MainWindow? Instance { get; private set; } // Add this line public static Dictionary ThemeColors { get; } = new() { { AppTheme.Dark, ("#2F2F2F", "#FFFFFF") }, { AppTheme.Light, ("#FFFFFF", "#000000") }, { AppTheme.Classic, ("#E8E8E8", "#000000") } }; public MainWindow() { InitializeComponent(); Instance = this; // Set the static reference mainContent = this.FindControl("MainContent"); ShowLandingPage(); } public void ShowLandingPage() { mainContent!.Content = new LandingPage(); Dispatcher.UIThread.Post(() => UpdateTheme(CurrentTheme), DispatcherPriority.Loaded); } public void ShowPeriodicTable() { Logger.Log("ShowPeriodicTable called"); try { mainContent!.Content = new PeriodicTablePage(); Logger.Log("PeriodicTablePage created and set"); Dispatcher.UIThread.Post(() => UpdateTheme(CurrentTheme), DispatcherPriority.Loaded); Logger.Log("Theme update posted"); } catch (Exception ex) { Logger.Log($"Error in ShowPeriodicTable: {ex.Message}"); } } public void ShowAboutPage() { mainContent!.Content = new AboutPage(); Dispatcher.UIThread.Post(() => UpdateTheme(CurrentTheme), DispatcherPriority.Loaded); } public void UpdateTheme(AppTheme theme) { CurrentTheme = theme; if (mainContent?.Content is UserControl control) { var (background, foreground) = ThemeColors[theme]; control.Background = new SolidColorBrush(Color.Parse(background)); // For PeriodicTablePage, use a timer to ensure visual tree is ready if (control is PeriodicTablePage) { // Wait a bit longer for the visual tree to be fully constructed var timer = new System.Timers.Timer(100); // 100ms delay timer.Elapsed += (sender, e) => { timer.Stop(); timer.Dispose(); Dispatcher.UIThread.Post(() => { try { var allButtons = control.GetVisualDescendants().OfType