Skip to content

Commit 3448b04

Browse files
change defaults instead of adding a new setting
1 parent 16a0021 commit 3448b04

File tree

4 files changed

+110
-208
lines changed

4 files changed

+110
-208
lines changed

DesktopClock/MainWindow.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
<Setter Property="Background">
112112
<Setter.Value>
113113
<SolidColorBrush Opacity="{Binding BackgroundOpacity, Source={x:Static p:Settings.Default}, Mode=OneWay}"
114-
Color="{Binding EffectiveOuterColor, Source={x:Static p:Settings.Default}, Mode=OneWay}" />
114+
Color="{Binding OuterColor, Source={x:Static p:Settings.Default}, Mode=OneWay}" />
115115
</Setter.Value>
116116
</Setter>
117117
</MultiDataTrigger.Setters>
@@ -126,7 +126,7 @@
126126

127127
<local:OutlinedTextBlock.Fill>
128128
<SolidColorBrush Opacity="{Binding TextOpacity, Source={x:Static p:Settings.Default}, Mode=OneWay}"
129-
Color="{Binding EffectiveTextColor, Source={x:Static p:Settings.Default}, Mode=OneWay}" />
129+
Color="{Binding TextColor, Source={x:Static p:Settings.Default}, Mode=OneWay}" />
130130
</local:OutlinedTextBlock.Fill>
131131

132132
<local:OutlinedTextBlock.Style>
@@ -141,7 +141,7 @@
141141
<Setter Property="Stroke">
142142
<Setter.Value>
143143
<SolidColorBrush Opacity="{Binding BackgroundOpacity, Source={x:Static p:Settings.Default}, Mode=OneWay}"
144-
Color="{Binding EffectiveOuterColor, Source={x:Static p:Settings.Default}, Mode=OneWay}" />
144+
Color="{Binding OuterColor, Source={x:Static p:Settings.Default}, Mode=OneWay}" />
145145
</Setter.Value>
146146
</Setter>
147147

DesktopClock/Properties/Settings.cs

Lines changed: 12 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
using System;
22
using System.ComponentModel;
33
using System.IO;
4-
using System.Windows;
54
using System.Windows.Media;
6-
using Microsoft.Win32;
75
using Newtonsoft.Json;
6+
using DesktopClock.Utilities;
87
using WpfWindowPlacement;
98

109
namespace DesktopClock.Properties;
1110

1211
public sealed class Settings : INotifyPropertyChanged, IDisposable
1312
{
1413
private readonly FileSystemWatcher _watcher;
15-
private bool _followSystemTheme;
16-
private bool _systemThemeIsLight = true;
17-
private Color _systemAccentColor = DefaultAccentColor;
18-
private Color _textColor = Color.FromRgb(33, 33, 33);
19-
private Color _outerColor = Color.FromRgb(247, 247, 247);
2014

2115
private static readonly Lazy<Settings> _default = new(LoadAndAttemptSave);
2216

@@ -32,12 +26,6 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
3226
public static readonly double MaxSizeLog = 6.5;
3327

3428
public static readonly double MinSizeLog = 2.7;
35-
private static readonly Color DefaultAccentColor = Color.FromRgb(0, 120, 215);
36-
private static readonly Color LightThemeOuterColor = Color.FromRgb(247, 247, 247);
37-
private static readonly Color DarkThemeOuterColor = Color.FromRgb(32, 32, 32);
38-
private const string PersonalizeKeyPath = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
39-
private const string AppsUseLightThemeValueName = "AppsUseLightTheme";
40-
4129
static Settings()
4230
{
4331
// Settings file path from the same directory as the executable.
@@ -54,10 +42,6 @@ private Settings()
5442
EnableRaisingEvents = true,
5543
};
5644
_watcher.Changed += FileChanged;
57-
58-
RefreshSystemThemeColors(notifyIfFollowing: false);
59-
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
60-
SystemParameters.StaticPropertyChanged += SystemParameters_StaticPropertyChanged;
6145
}
6246

6347
#pragma warning disable CS0067 // The event 'Settings.PropertyChanged' is never used. Handled by Fody.
@@ -138,19 +122,7 @@ private Settings()
138122
/// <summary>
139123
/// Text color for the clock's text.
140124
/// </summary>
141-
public Color TextColor
142-
{
143-
get => _textColor;
144-
set
145-
{
146-
if (_textColor.Equals(value))
147-
return;
148-
149-
_textColor = value;
150-
NotifyPropertyChanged(nameof(TextColor));
151-
NotifyEffectiveThemeChanged();
152-
}
153-
}
125+
public Color TextColor { get; set; } = Color.FromRgb(33, 33, 33);
154126

155127
/// <summary>
156128
/// Opacity of the text.
@@ -160,51 +132,7 @@ public Color TextColor
160132
/// <summary>
161133
/// The outer color, for either the background or the outline.
162134
/// </summary>
163-
public Color OuterColor
164-
{
165-
get => _outerColor;
166-
set
167-
{
168-
if (_outerColor.Equals(value))
169-
return;
170-
171-
_outerColor = value;
172-
NotifyPropertyChanged(nameof(OuterColor));
173-
NotifyEffectiveThemeChanged();
174-
}
175-
}
176-
177-
/// <summary>
178-
/// Follow the system theme and accent color.
179-
/// </summary>
180-
public bool FollowSystemTheme
181-
{
182-
get => _followSystemTheme;
183-
set
184-
{
185-
if (_followSystemTheme == value)
186-
return;
187-
188-
_followSystemTheme = value;
189-
NotifyPropertyChanged(nameof(FollowSystemTheme));
190-
RefreshSystemThemeColors(notifyIfFollowing: false);
191-
NotifyEffectiveThemeChanged();
192-
}
193-
}
194-
195-
/// <summary>
196-
/// Effective text color based on theme-following preference.
197-
/// </summary>
198-
[JsonIgnore]
199-
public Color EffectiveTextColor => FollowSystemTheme ? _systemAccentColor : TextColor;
200-
201-
/// <summary>
202-
/// Effective outer color based on theme-following preference.
203-
/// </summary>
204-
[JsonIgnore]
205-
public Color EffectiveOuterColor => FollowSystemTheme
206-
? (_systemThemeIsLight ? LightThemeOuterColor : DarkThemeOuterColor)
207-
: OuterColor;
135+
public Color OuterColor { get; set; } = Color.FromRgb(247, 247, 247);
208136

209137
/// <summary>
210138
/// Shows a solid background instead of an outline.
@@ -421,6 +349,11 @@ private static Settings LoadAndAttemptSave()
421349
{
422350
var settings = LoadFromFile();
423351

352+
if (!File.Exists(FilePath))
353+
{
354+
settings.ApplySystemThemeDefaultsIfAvailable();
355+
}
356+
424357
CanBeSaved = settings.Save();
425358

426359
return settings;
@@ -454,82 +387,13 @@ public void ScaleHeight(double steps)
454387
Height = (int)exp;
455388
}
456389

457-
private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
390+
private void ApplySystemThemeDefaultsIfAvailable()
458391
{
459-
if (!FollowSystemTheme)
392+
if (!SystemThemeService.TryGetThemeDefaults(out var textColor, out var outerColor))
460393
return;
461394

462-
if (e.Category == UserPreferenceCategory.General ||
463-
e.Category == UserPreferenceCategory.Color ||
464-
e.Category == UserPreferenceCategory.VisualStyle)
465-
{
466-
RefreshSystemThemeColors(notifyIfFollowing: true);
467-
}
468-
}
469-
470-
private void SystemParameters_StaticPropertyChanged(object sender, PropertyChangedEventArgs e)
471-
{
472-
if (!FollowSystemTheme)
473-
return;
474-
475-
if (e.PropertyName == nameof(SystemParameters.WindowGlassColor))
476-
{
477-
RefreshSystemThemeColors(notifyIfFollowing: true);
478-
}
479-
}
480-
481-
private void RefreshSystemThemeColors(bool notifyIfFollowing)
482-
{
483-
var dispatcher = Application.Current?.Dispatcher;
484-
if (dispatcher != null && !dispatcher.CheckAccess())
485-
{
486-
dispatcher.BeginInvoke(new Action(() => RefreshSystemThemeColors(notifyIfFollowing)));
487-
return;
488-
}
489-
490-
var isLightTheme = GetSystemThemeIsLight();
491-
var accentColor = GetSystemAccentColor();
492-
var themeChanged = isLightTheme != _systemThemeIsLight;
493-
var accentChanged = !_systemAccentColor.Equals(accentColor);
494-
495-
_systemThemeIsLight = isLightTheme;
496-
_systemAccentColor = accentColor;
497-
498-
if (notifyIfFollowing && (themeChanged || accentChanged) && FollowSystemTheme)
499-
{
500-
NotifyEffectiveThemeChanged();
501-
}
502-
}
503-
504-
private static bool GetSystemThemeIsLight()
505-
{
506-
var value = Registry.GetValue(PersonalizeKeyPath, AppsUseLightThemeValueName, 1);
507-
return value switch
508-
{
509-
int intValue => intValue > 0,
510-
byte byteValue => byteValue > 0,
511-
_ => true,
512-
};
513-
}
514-
515-
private static Color GetSystemAccentColor()
516-
{
517-
var accent = SystemParameters.WindowGlassColor;
518-
if (accent.A == 0)
519-
return DefaultAccentColor;
520-
521-
return Color.FromArgb(255, accent.R, accent.G, accent.B);
522-
}
523-
524-
private void NotifyEffectiveThemeChanged()
525-
{
526-
NotifyPropertyChanged(nameof(EffectiveTextColor));
527-
NotifyPropertyChanged(nameof(EffectiveOuterColor));
528-
}
529-
530-
private void NotifyPropertyChanged(string propertyName)
531-
{
532-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
395+
TextColor = textColor;
396+
OuterColor = outerColor;
533397
}
534398

535399
public void Dispose()

DesktopClock/SettingsWindow.xaml

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@
4444
<Setter Property="Margin" Value="0,0,0,4" />
4545
</Style>
4646

47-
<Style x:Key="DisableWhenFollowingSystemTheme"
48-
TargetType="StackPanel">
49-
<Setter Property="IsEnabled" Value="True" />
50-
<Style.Triggers>
51-
<DataTrigger Binding="{Binding Settings.FollowSystemTheme}" Value="True">
52-
<Setter Property="IsEnabled" Value="False" />
53-
</DataTrigger>
54-
</Style.Triggers>
55-
</Style>
56-
5747
<Style x:Key="LabelTextBlock"
5848
TargetType="TextBlock">
5949
<Setter Property="FontWeight" Value="SemiBold" />
@@ -160,11 +150,6 @@
160150
<ScrollViewer VerticalScrollBarVisibility="Auto"
161151
Padding="0">
162152
<StackPanel>
163-
<CheckBox Content="Follow system theme and accent color"
164-
IsChecked="{Binding Settings.FollowSystemTheme, Mode=TwoWay}" />
165-
<TextBlock Text="Use Windows light/dark mode and your accent color for the clock."
166-
Style="{StaticResource DescriptionTextBlock}" />
167-
168153
<TextBlock Text="Font Family:"
169154
Style="{StaticResource LabelTextBlock}" />
170155
<ComboBox ItemsSource="{Binding FontFamilies}"
@@ -193,55 +178,53 @@
193178
<TextBlock Text="Change text to uppercase or lowercase."
194179
Style="{StaticResource DescriptionTextBlock}" />
195180

196-
<StackPanel Style="{StaticResource DisableWhenFollowingSystemTheme}">
197-
<TextBlock Text="Text Color:"
198-
Style="{StaticResource LabelTextBlock}" />
199-
<Grid>
200-
<Grid.ColumnDefinitions>
201-
<ColumnDefinition Width="*" />
202-
<ColumnDefinition Width="4" />
203-
<ColumnDefinition Width="Auto" />
204-
</Grid.ColumnDefinitions>
205-
206-
<TextBox Text="{Binding Settings.TextColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
207-
Grid.Column="0" />
208-
<Button Content="Pick..."
209-
Click="PickTextColor"
210-
Grid.Column="2" />
211-
</Grid>
212-
<TextBlock Text="Change the color of the text (hex code or name)."
213-
Style="{StaticResource DescriptionTextBlock}" />
214-
215-
<TextBlock Text="Text Opacity:"
216-
Style="{StaticResource LabelTextBlock}" />
217-
<Grid Margin="0,0,0,4">
218-
<Grid.ColumnDefinitions>
219-
<ColumnDefinition Width="*" />
220-
<ColumnDefinition Width="30" />
221-
</Grid.ColumnDefinitions>
222-
<Slider Value="{Binding Settings.TextOpacity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
223-
Minimum="0"
224-
Maximum="1"
225-
TickFrequency="0.01"
226-
IsSnapToTickEnabled="True"
227-
Grid.Column="0"
228-
Margin="0" />
229-
<TextBlock Text="{Binding Settings.TextOpacity, StringFormat=0.00}"
230-
VerticalAlignment="Center"
231-
HorizontalAlignment="Right"
232-
Grid.Column="1" />
233-
</Grid>
234-
<TextBlock Text="Adjust how transparent the text is."
235-
Style="{StaticResource DescriptionTextBlock}" />
236-
</StackPanel>
181+
<TextBlock Text="Text Color:"
182+
Style="{StaticResource LabelTextBlock}" />
183+
<Grid>
184+
<Grid.ColumnDefinitions>
185+
<ColumnDefinition Width="*" />
186+
<ColumnDefinition Width="4" />
187+
<ColumnDefinition Width="Auto" />
188+
</Grid.ColumnDefinitions>
189+
190+
<TextBox Text="{Binding Settings.TextColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
191+
Grid.Column="0" />
192+
<Button Content="Pick..."
193+
Click="PickTextColor"
194+
Grid.Column="2" />
195+
</Grid>
196+
<TextBlock Text="Change the color of the text (hex code or name)."
197+
Style="{StaticResource DescriptionTextBlock}" />
198+
199+
<TextBlock Text="Text Opacity:"
200+
Style="{StaticResource LabelTextBlock}" />
201+
<Grid Margin="0,0,0,4">
202+
<Grid.ColumnDefinitions>
203+
<ColumnDefinition Width="*" />
204+
<ColumnDefinition Width="30" />
205+
</Grid.ColumnDefinitions>
206+
<Slider Value="{Binding Settings.TextOpacity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
207+
Minimum="0"
208+
Maximum="1"
209+
TickFrequency="0.01"
210+
IsSnapToTickEnabled="True"
211+
Grid.Column="0"
212+
Margin="0" />
213+
<TextBlock Text="{Binding Settings.TextOpacity, StringFormat=0.00}"
214+
VerticalAlignment="Center"
215+
HorizontalAlignment="Right"
216+
Grid.Column="1" />
217+
</Grid>
218+
<TextBlock Text="Adjust how transparent the text is."
219+
Style="{StaticResource DescriptionTextBlock}" />
237220
</StackPanel>
238221
</ScrollViewer>
239222
</TabItem>
240223

241224
<TabItem Header="_Background">
242225
<ScrollViewer VerticalScrollBarVisibility="Auto"
243226
Padding="0">
244-
<StackPanel Style="{StaticResource DisableWhenFollowingSystemTheme}">
227+
<StackPanel>
245228
<TextBlock Text="Background/Outline Color:"
246229
Style="{StaticResource LabelTextBlock}" />
247230
<Grid>

0 commit comments

Comments
 (0)