|
| 1 | +#region ========================================================================= USING ===================================================================================== |
| 2 | +using System; |
| 3 | +using Avalonia; |
| 4 | +using Avalonia.Markup.Xaml.Styling; |
| 5 | +using DatasetTag.Common.Enums; |
| 6 | +#endregion |
| 7 | + |
| 8 | +namespace DatasetTag; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Class for managing the application's styles |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// Creation Date: 25th of July, 2021 |
| 15 | +/// </remarks> |
| 16 | +public class StyleManager |
| 17 | +{ |
| 18 | + #region ================================================================== FIELD MEMBERS ================================================================================ |
| 19 | + private readonly Application application; |
| 20 | + private readonly StyleInclude darkStyle = CreateStyle("avares://DatasetTag/Common/Styles/Dark.xaml"); |
| 21 | + private readonly StyleInclude lightStyle = CreateStyle("avares://DatasetTag/Common/Styles/Light.xaml"); |
| 22 | + #endregion |
| 23 | + |
| 24 | + #region ==================================================================== PROPERTIES ================================================================================= |
| 25 | + public Themes CurrentTheme { get; private set; } = Themes.Light; |
| 26 | + #endregion |
| 27 | + |
| 28 | + #region ====================================================================== CTOR ===================================================================================== |
| 29 | + /// <summary> |
| 30 | + /// Overload C-tor |
| 31 | + /// </summary> |
| 32 | + /// <param name="application">The application for which to manage the style</param> |
| 33 | + public StyleManager(Application application) |
| 34 | + { |
| 35 | + this.application = application; |
| 36 | + // safe guard |
| 37 | + if (application.Styles.Count == 0) |
| 38 | + application.Styles.Add(darkStyle); |
| 39 | + else |
| 40 | + application.Styles[1] = darkStyle; |
| 41 | + } |
| 42 | + #endregion |
| 43 | + |
| 44 | + #region ===================================================================== METHODS =================================================================================== |
| 45 | + /// <summary> |
| 46 | + /// Switches the current theme with the oposite theme |
| 47 | + /// </summary> |
| 48 | + public void SwitchTheme() |
| 49 | + { |
| 50 | + SetTheme(CurrentTheme switch |
| 51 | + { |
| 52 | + Themes.Dark => Themes.Light, |
| 53 | + Themes.Light => Themes.Dark, |
| 54 | + _ => throw new ArgumentOutOfRangeException(nameof(CurrentTheme)) |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Switches the curent theme to a theme whose index is equal to <paramref name="themeIndex"/> |
| 60 | + /// </summary> |
| 61 | + /// <param name="themeIndex">The index of the theme to set</param> |
| 62 | + public void SwitchThemeByIndex(int themeIndex) |
| 63 | + { |
| 64 | + application.Styles[1] = themeIndex == 0 ? darkStyle : lightStyle; |
| 65 | + CurrentTheme = themeIndex == 0 ? Themes.Dark : Themes.Light; |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Sets the currently used theme |
| 70 | + /// </summary> |
| 71 | + /// <param name="theme">The theme to be set</param> |
| 72 | + public void SetTheme(Themes theme) |
| 73 | + { |
| 74 | + // change the first style in the main window styles section, and the main window instantly refreshes |
| 75 | + // (invoke only from the UI thread!) |
| 76 | + application.Styles[1] = theme switch |
| 77 | + { |
| 78 | + Themes.Dark => lightStyle, |
| 79 | + Themes.Light => darkStyle, |
| 80 | + _ => throw new ArgumentOutOfRangeException(nameof(theme)) |
| 81 | + }; |
| 82 | + CurrentTheme = theme; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Creates the style used for theming |
| 87 | + /// </summary> |
| 88 | + /// <param name="url">The url of the theme file to be used</param> |
| 89 | + /// <returns>The <see cref="StyleInclude"/> containing the styles inside the theme identified by <paramref name="url"/></returns> |
| 90 | + private static StyleInclude CreateStyle(string url) |
| 91 | + { |
| 92 | + return new StyleInclude(new Uri("resm:Styles?assembly=DatasetTag")) |
| 93 | + { |
| 94 | + Source = new Uri(url) |
| 95 | + }; |
| 96 | + } |
| 97 | + #endregion |
| 98 | +} |
0 commit comments