|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
2 | 4 | using Avalonia; |
3 | 5 | using Avalonia.Media; |
4 | 6 | using Avalonia.Styling; |
5 | 7 | using ReactiveUI; |
| 8 | +using YMouseButtonControl.Core.Repositories; |
6 | 9 | using YMouseButtonControl.Core.Services.Settings; |
7 | 10 | using YMouseButtonControl.Core.ViewModels.Models; |
8 | | -using YMouseButtonControl.DataAccess.Models; |
9 | 11 |
|
10 | 12 | namespace YMouseButtonControl.Core.Services.Theme; |
11 | 13 |
|
12 | 14 | public interface IThemeService |
13 | 15 | { |
14 | | - IBrush HighlightLight { get; set; } |
15 | | - IBrush HighlightDark { get; set; } |
16 | | - IBrush BackgroundDark { get; set; } |
17 | | - IBrush BackgroundLight { get; set; } |
18 | | - IBrush CurBackground { get; set; } |
19 | | - IBrush CurHighlight { get; set; } |
| 16 | + public IBrush Background { get; } |
| 17 | + public IBrush Highlight { get; } |
20 | 18 | ThemeVariant ThemeVariant { get; } |
| 19 | + List<ThemeVm> Themes { get; } |
21 | 20 | } |
22 | 21 |
|
23 | 22 | public class ThemeService : ReactiveObject, IThemeService |
24 | 23 | { |
| 24 | + private readonly IRepository<DataAccess.Models.Theme, ThemeVm> _themeRepo; |
25 | 25 | private readonly SettingIntVm _themeSetting; |
26 | | - private IBrush _highlightLight = Brushes.Yellow; |
27 | | - private IBrush _highlightDark = Brush.Parse("#3700b3"); |
28 | | - private IBrush _backgroundDark = Brushes.Black; |
29 | | - private IBrush _backgroundLight = Brushes.White; |
30 | | - private IBrush _curBackground; |
31 | | - private IBrush _curHighlight; |
32 | | - private ThemeVariant _themeVariant; |
33 | | - |
34 | | - public ThemeService(ISettingsService settingsService) |
| 26 | + private readonly ThemeVm _themeVm; |
| 27 | + private IBrush _background; |
| 28 | + private IBrush _highlight; |
| 29 | + private readonly ThemeVariant _themeVariant; |
| 30 | + |
| 31 | + public ThemeService( |
| 32 | + IRepository<DataAccess.Models.Theme, ThemeVm> themeRepo, |
| 33 | + ISettingsService settingsService |
| 34 | + ) |
35 | 35 | { |
| 36 | + _themeRepo = themeRepo; |
36 | 37 | _themeSetting = |
37 | 38 | settingsService.GetSetting("Theme") as SettingIntVm |
38 | 39 | ?? throw new Exception("Error retrieving theme setting"); |
39 | | - _curBackground = GetCurrentThemeBackground(); |
40 | | - _curHighlight = GetCurrentThemeHighlight(); |
| 40 | + _themeVm = |
| 41 | + _themeRepo.GetById(_themeSetting.IntValue) |
| 42 | + ?? throw new Exception($"Error retrieving theme with id: {_themeSetting.IntValue}"); |
41 | 43 | _themeVariant = GetThemeVariant(); |
| 44 | + _background = GetBackground(); |
| 45 | + _highlight = GetHighlight(); |
42 | 46 | } |
43 | 47 |
|
44 | | - public IBrush HighlightLight |
45 | | - { |
46 | | - get => _highlightLight; |
47 | | - set => this.RaiseAndSetIfChanged(ref _highlightLight, value); |
48 | | - } |
49 | | - |
50 | | - public IBrush HighlightDark |
51 | | - { |
52 | | - get => _highlightDark; |
53 | | - set => this.RaiseAndSetIfChanged(ref _highlightDark, value); |
54 | | - } |
| 48 | + public List<ThemeVm> Themes => [.. _themeRepo.GetAll().OrderBy(x => x.Id)]; |
55 | 49 |
|
56 | | - public IBrush BackgroundDark |
57 | | - { |
58 | | - get => _backgroundDark; |
59 | | - set => this.RaiseAndSetIfChanged(ref _backgroundDark, value); |
60 | | - } |
61 | | - |
62 | | - public IBrush BackgroundLight |
63 | | - { |
64 | | - get => _backgroundLight; |
65 | | - set => this.RaiseAndSetIfChanged(ref _backgroundLight, value); |
66 | | - } |
| 50 | + public ThemeVariant ThemeVariant => _themeVariant; |
67 | 51 |
|
68 | | - public IBrush CurBackground |
| 52 | + public IBrush Background |
69 | 53 | { |
70 | | - get => _curBackground; |
71 | | - set => this.RaiseAndSetIfChanged(ref _curBackground, value); |
| 54 | + get => _background; |
| 55 | + set => this.RaiseAndSetIfChanged(ref _background, value); |
72 | 56 | } |
73 | 57 |
|
74 | | - public IBrush CurHighlight |
| 58 | + public IBrush Highlight |
75 | 59 | { |
76 | | - get => _curHighlight; |
77 | | - set => this.RaiseAndSetIfChanged(ref _curHighlight, value); |
| 60 | + get => _highlight; |
| 61 | + set => this.RaiseAndSetIfChanged(ref _highlight, value); |
78 | 62 | } |
79 | 63 |
|
80 | | - public ThemeVariant ThemeVariant => _themeVariant; |
81 | | - |
82 | | - private ThemeVariant GetThemeVariant() |
| 64 | + private IBrush GetBackground() |
83 | 65 | { |
84 | | - var theme = (ThemeEnum)_themeSetting.IntValue; |
85 | | - return theme switch |
| 66 | + // Background is of the form #aarrggbb |
| 67 | + if (_themeVm.Background.StartsWith('#')) |
86 | 68 | { |
87 | | - ThemeEnum.Default => ThemeVariant.Default, |
88 | | - ThemeEnum.Light => ThemeVariant.Light, |
89 | | - ThemeEnum.Dark => ThemeVariant.Dark, |
90 | | - _ => throw new ArgumentOutOfRangeException($"Invalid theme {theme}"), |
91 | | - }; |
| 69 | + return Brush.Parse(_themeVm.Background); |
| 70 | + } |
| 71 | + |
| 72 | + // Background is an avalonia resource like SystemAltHighColor |
| 73 | + if ( |
| 74 | + Application.Current!.TryGetResource( |
| 75 | + _themeVm.Background, |
| 76 | + Application.Current.ActualThemeVariant, |
| 77 | + out var backgroundBrush |
| 78 | + ) |
| 79 | + ) |
| 80 | + { |
| 81 | + if (backgroundBrush is null) |
| 82 | + { |
| 83 | + throw new Exception("Error retrieving background brush"); |
| 84 | + } |
| 85 | + var bbStr = backgroundBrush.ToString(); |
| 86 | + if (string.IsNullOrWhiteSpace(bbStr)) |
| 87 | + { |
| 88 | + throw new Exception("Error retrieving background brush"); |
| 89 | + } |
| 90 | + var brush = Brush.Parse(bbStr); |
| 91 | + return brush; |
| 92 | + } |
| 93 | + |
| 94 | + // Background may be a color like White, Black, etc. |
| 95 | + return Brush.Parse(_themeVm.Background); |
92 | 96 | } |
93 | 97 |
|
94 | | - private IBrush GetCurrentThemeBackground() |
| 98 | + private IBrush GetHighlight() |
95 | 99 | { |
96 | | - var theme = (ThemeEnum)_themeSetting.IntValue; |
97 | | - |
98 | | - return theme switch |
| 100 | + // Highlight is of the form #aarrggbb |
| 101 | + if (_themeVm.Highlight.StartsWith('#')) |
99 | 102 | { |
100 | | - ThemeEnum.Default when Application.Current?.ActualThemeVariant == ThemeVariant.Light => |
101 | | - _backgroundLight, |
102 | | - ThemeEnum.Default when Application.Current?.ActualThemeVariant == ThemeVariant.Dark => |
103 | | - _backgroundDark, |
104 | | - ThemeEnum.Light => _backgroundLight, |
105 | | - ThemeEnum.Dark => _backgroundDark, |
106 | | - _ => throw new ArgumentOutOfRangeException($"Unknown theme {theme}"), |
107 | | - }; |
| 103 | + return Brush.Parse(_themeVm.Highlight); |
| 104 | + } |
| 105 | + |
| 106 | + // Highlight is an avalonia resource like SystemAltHighColor |
| 107 | + if ( |
| 108 | + Application.Current!.TryGetResource( |
| 109 | + _themeVm.Highlight, |
| 110 | + Application.Current.ActualThemeVariant, |
| 111 | + out var highlightBrush |
| 112 | + ) |
| 113 | + ) |
| 114 | + { |
| 115 | + if (highlightBrush is null) |
| 116 | + { |
| 117 | + throw new Exception("Error retrieving highlight brush"); |
| 118 | + } |
| 119 | + var bbStr = highlightBrush.ToString(); |
| 120 | + if (string.IsNullOrWhiteSpace(bbStr)) |
| 121 | + { |
| 122 | + throw new Exception("Error retrieving highlight brush"); |
| 123 | + } |
| 124 | + var brush = Brush.Parse(bbStr); |
| 125 | + return brush; |
| 126 | + } |
| 127 | + |
| 128 | + // Highlight may be a color like White, Black, etc. |
| 129 | + return Brush.Parse(_themeVm.Highlight); |
108 | 130 | } |
109 | 131 |
|
110 | | - private IBrush GetCurrentThemeHighlight() |
| 132 | + private ThemeVariant GetThemeVariant() |
111 | 133 | { |
112 | | - var theme = (ThemeEnum)_themeSetting.IntValue; |
113 | | - |
114 | | - return theme switch |
| 134 | + return _themeSetting.IntValue switch |
115 | 135 | { |
116 | | - ThemeEnum.Default when Application.Current?.ActualThemeVariant == ThemeVariant.Light => |
117 | | - _highlightLight, |
118 | | - ThemeEnum.Default when Application.Current?.ActualThemeVariant == ThemeVariant.Dark => |
119 | | - _highlightDark, |
120 | | - ThemeEnum.Light => _highlightLight, |
121 | | - ThemeEnum.Dark => _highlightDark, |
122 | | - _ => throw new Exception($"Unknown theme {theme}"), |
| 136 | + 1 => Application.Current!.ActualThemeVariant == ThemeVariant.Light |
| 137 | + ? ThemeVariant.Light |
| 138 | + : ThemeVariant.Dark, |
| 139 | + 2 => ThemeVariant.Light, |
| 140 | + 3 => ThemeVariant.Dark, |
| 141 | + _ => throw new ArgumentOutOfRangeException( |
| 142 | + $"Invalid theme id: {_themeSetting.IntValue}" |
| 143 | + ), |
123 | 144 | }; |
124 | 145 | } |
125 | 146 | } |
0 commit comments