Projekt_SS25/Project_Periodensystem.View/MainWindow.axaml.cs

153 lines
6.1 KiB
C#

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<AppTheme, (string Background, string Foreground)> 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<ContentControl>("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<Button>().ToList();
Logger.Log($"Timer: Found {allButtons.Count} buttons in PeriodicTablePage");
foreach (var button in allButtons)
{
// Skip navigation buttons
if ((button.Width == 250 && button.Height == 40) ||
(button.Width == 200 && button.Height == 40))
{
continue;
}
// Force white text for element tiles
button.Foreground = new SolidColorBrush(Colors.WhiteSmoke);
// Force white for TextBlocks inside
var buttonTextBlocks = button.GetVisualDescendants().OfType<TextBlock>().ToList();
foreach (var tb in buttonTextBlocks)
{
tb.Foreground = new SolidColorBrush(Colors.White);
Logger.Log($"Timer: Set element tile text to white: '{tb.Text}'");
}
}
}
catch (Exception ex)
{
Logger.Log($"Timer error: {ex.Message}");
}
});
};
timer.Start();
}
// Update regular text colors
var textBlocks = control.GetVisualDescendants().OfType<TextBlock>().ToList();
foreach (var textBlock in textBlocks)
{
var parent = textBlock.Parent;
if (parent is Button)
continue;
textBlock.Foreground = new SolidColorBrush(Color.Parse(foreground));
}
}
}
private bool IsElementTileButton(Button button)
{
// Check if this button has element-related content or styling
if (button.Content is StackPanel stackPanel)
{
var textBlocks = stackPanel.Children.OfType<TextBlock>().ToList();
if (textBlocks.Count >= 2) // Element tiles typically have symbol and number
{
return true;
}
}
// Check if button content is directly a TextBlock with short text
if (button.Content is TextBlock textBlock &&
textBlock.Text?.Length <= 3)
{
return true;
}
return false;
}
}
}