Skip to content

Commit

Permalink
ThemeProvider now also allows user registered themes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokyyn committed Aug 31, 2023
1 parent fa33d37 commit cc39ffb
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions source/Lucid/Theming/ThemeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Lucid.Theming;
public class ThemeProvider
{
private static List<ITheme> _allThemes;
private static List<ITheme> _userRegisteredThemes;

Check warning on line 9 in source/Lucid/Theming/ThemeProvider.cs

View workflow job for this annotation

GitHub Actions / build

Field 'ThemeProvider._userRegisteredThemes' is never assigned to, and will always have its default value null

Check warning on line 9 in source/Lucid/Theming/ThemeProvider.cs

View workflow job for this annotation

GitHub Actions / build

Field 'ThemeProvider._userRegisteredThemes' is never assigned to, and will always have its default value null

Check warning on line 9 in source/Lucid/Theming/ThemeProvider.cs

View workflow job for this annotation

GitHub Actions / publish

Field 'ThemeProvider._userRegisteredThemes' is never assigned to, and will always have its default value null

Check warning on line 9 in source/Lucid/Theming/ThemeProvider.cs

View workflow job for this annotation

GitHub Actions / publish

Field 'ThemeProvider._userRegisteredThemes' is never assigned to, and will always have its default value null
private static ITheme theme;

static ThemeProvider()
Expand Down Expand Up @@ -43,6 +44,10 @@ public static ITheme Theme
}
}

/// <summary>
/// Sets a theme with the given theme name if it exists. Other wise it sets <see cref="DarkTheme"/> as the current theme.
/// </summary>
/// <param name="theme"></param>
public static void SetThemeWithAlias(string theme)
{
var newTheme = GetAllThemes.FirstOrDefault(u => u.ThemeName == theme);
Expand All @@ -53,6 +58,19 @@ public static void SetThemeWithAlias(string theme)
Theme = new DarkTheme();
}

/// <summary>
/// Registers the given theme to the <see cref="ThemeProvider"/>.
/// </summary>
/// <param name="theme"></param>
/// <exception cref="NotSupportedException"></exception>
public static void RegisterTheme(ITheme theme)
{
if (theme.GetType().BaseType != typeof(BaseDarkTheme) && theme.GetType().BaseType != typeof(BaseLightTheme))
throw new NotSupportedException($"This method only allows themes with inheritence of the base dark or light theme. For more information see {typeof(BaseLightTheme).Namespace}.");

_userRegisteredThemes.Add(theme);
}

private static void LoadAddtionalThemes()
{
if (ManagerOS.IsWindows10OrWindows11)
Expand All @@ -72,8 +90,8 @@ private static void LoadAddtionalThemes()
}

/// <summary>
/// Returns a list with all available themes
/// Returns a list with all available themes (including user registered themes)
/// <br><i> NOTE: Disabled themes won't appear in this list </i></br>
/// </summary>
public static List<ITheme> GetAllThemes => _allThemes;
public static List<ITheme> GetAllThemes => new List<ITheme>(_allThemes).Concat(_userRegisteredThemes).ToList();
}

0 comments on commit cc39ffb

Please sign in to comment.