Code/ThemePanel.cs
namespace BetterUI;
/// <summary>
/// A panel that implements the <see cref="IThemeEvent"/> interface. This panel
/// will automatically receive the <see cref="IThemeEvent.OnThemeChanged"/> event
/// when the theme is changed.
/// </summary>
/// <remarks>
/// This is a base class for panels that need to be notified when the theme is
/// changed. It provides a <see cref="ThemeProvider"/> that is automatically
/// bound to the global theme.
/// </remarks>
public abstract class ThemePanel : Panel, IThemeEvent
{
/// <summary>
/// The theme provider that this panel is bound to.
/// </summary>
/// <remarks>
/// This is a cascading property that will automatically inherit the
/// global theme.
/// </remarks>
[CascadingProperty]
protected ThemeProvider Provider { get; set; } = null!;
/// <summary>
/// Called every tick to update the theme class.
/// </summary>
public override void Tick()
{
BindClass( "light", () => Provider.Theme is Theme.Light );
BindClass( "dark", () => Provider.Theme is Theme.Dark );
}
/// <summary>
/// Sets the theme of the provider.
/// </summary>
/// <param name="theme">The theme to set.</param>
protected void SetTheme( Theme theme )
{
Provider.SetTheme( theme );
}
/// <summary>
/// Switches the theme of the provider between light and dark.
/// </summary>
protected void SwitchTheme()
{
Provider.SwitchTheme();
}
/// <summary>
/// Called when the theme is changed.
/// </summary>
protected virtual void OnThemeChanged() { }
/// <inheritdoc />
void IThemeEvent.OnThemeChanged( Theme theme )
{
OnThemeChanged();
}
}