Skip to content

Commit d75a1d1

Browse files
Select a theme during startup depending on settings
GH-102
1 parent dbf8727 commit d75a1d1

File tree

7 files changed

+49
-18
lines changed

7 files changed

+49
-18
lines changed

src/KeyboardSwitch.Settings/App.axaml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<Application xmlns="https://github.com/avaloniaui"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:sys="using:System"
4-
xmlns:ui="using:FluentAvalonia.UI.Controls"
5-
xmlns:uis="using:FluentAvalonia.Styling"
64
xmlns:l="using:KeyboardSwitch.Settings.Localization"
75
xmlns:local="using:KeyboardSwitch.Settings"
86
x:Class="KeyboardSwitch.Settings.App">
@@ -11,8 +9,6 @@
119
</Application.DataTemplates>
1210

1311
<Application.Styles>
14-
<uis:FluentAvaloniaTheme PreferUserAccentColor="True" PreferSystemTheme="True" />
15-
1612
<Style Selector="TextBlock.Error">
1713
<Setter Property="Foreground" Value="{DynamicResource SystemFillColorCriticalBrush}" />
1814
<Setter Property="Margin" Value="10 2" />
@@ -35,15 +31,13 @@
3531
<Setter Property="VerticalContentAlignment" Value="Center" />
3632
</Style>
3733

38-
<Style Selector="ui|NumberBox.FormControl">
39-
<Setter Property="Width" Value="150" />
40-
<Setter Property="SpinButtonPlacementMode" Value="Inline" />
34+
<Style Selector="NumericUpDown.FormControl">
35+
<Setter Property="HorizontalAlignment" Value="Stretch" />
4136
<Setter Property="ToolTip.Tip" Value="{l:Translate NumberBoxHint}" />
42-
<Setter Property="ValidationMode" Value="InvalidInputOverwritten" />
4337
<Setter Property="Margin" Value="0 0 10 0" />
4438
</Style>
4539

46-
<Style Selector="ui|NumberBox.FormControl /template/ TextBox#InputBox">
40+
<Style Selector="NumericUpDown.FormControl /template/ TextBox#InputBox">
4741
<Setter Property="VerticalContentAlignment" Value="Center" />
4842
</Style>
4943

src/KeyboardSwitch.Settings/App.axaml.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
using System.Reflection;
33

44
using Avalonia.Controls.ApplicationLifetimes;
5+
using Avalonia.Styling;
6+
7+
using FluentAvalonia.Styling;
58

69
using KeyboardSwitch.Core.Exceptions;
710
using KeyboardSwitch.Core.Logging;
11+
using KeyboardSwitch.Settings.Themes;
812

913
#if WINDOWS
1014
using KeyboardSwitch.Windows;
@@ -76,6 +80,8 @@ private async Task<MainViewModel> InitializeApp()
7680
var mainViewModel = new MainViewModel(appSettings);
7781
openExternally.InvokeCommand(mainViewModel.OpenExternally);
7882

83+
this.SetTheme(appSettings);
84+
7985
return mainViewModel;
8086
} catch (IncompatibleAppVersionException e)
8187
{
@@ -193,6 +199,28 @@ private async Task<MainWindow> CreateMainWindow(MainViewModel viewModel)
193199
return window;
194200
}
195201

202+
private void SetTheme(AppSettings appSettings)
203+
{
204+
if (appSettings.AppTheme == AppTheme.MacOS)
205+
{
206+
this.Styles.Insert(0, new MacOSTheme());
207+
} else
208+
{
209+
this.Styles.Insert(0, new FluentAvaloniaTheme
210+
{
211+
PreferUserAccentColor = true,
212+
PreferSystemTheme = true
213+
});
214+
}
215+
216+
this.RequestedThemeVariant = appSettings.AppThemeVariant switch
217+
{
218+
AppThemeVariant.Light => ThemeVariant.Light,
219+
AppThemeVariant.Dark => ThemeVariant.Dark,
220+
_ => ThemeVariant.Default
221+
};
222+
}
223+
196224
private void SaveAppState()
197225
{
198226
if (this.desktop.MainWindow == null)

src/KeyboardSwitch.Settings/KeyboardSwitch.Settings.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<PackageReference Include="Avalonia.Desktop" Version="11.3.1" />
2020
<PackageReference Include="Avalonia.Diagnostics" Version="11.3.1" />
2121
<PackageReference Include="Avalonia.ReactiveUI" Version="11.3.1" />
22-
<PackageReference Include="Devolutions.AvaloniaTheme.MacOS" Version="2025.6.10" />
22+
<PackageReference Include="Devolutions.AvaloniaTheme.MacOS" Version="2025.6.12" />
2323
<PackageReference Include="DynamicData" Version="9.4.1" />
2424
<PackageReference Include="FluentAvaloniaUI" Version="2.3.0" />
2525
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.6" />
@@ -42,7 +42,6 @@
4242
<PackageReference Include="System.Interactive.Async" Version="6.0.1" />
4343
<PackageReference Include="System.Reactive" Version="6.0.1" />
4444
<PackageReference Include="System.Text.Json" Version="9.0.6" />
45-
<PackageReference Include="ThemeManager" Version="11.3.0" />
4645
</ItemGroup>
4746

4847
<ItemGroup>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Styles xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
x:Class="KeyboardSwitch.Settings.Themes.MacOSTheme">
4+
<StyleInclude Source="avares://Devolutions.AvaloniaTheme.MacOS/MacOSTheme.axaml" />
5+
</Styles>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using Avalonia.Styling;
2+
3+
namespace KeyboardSwitch.Settings.Themes;
4+
5+
public class MacOSTheme : Styles
6+
{ }

src/KeyboardSwitch.Settings/Views/PreferencesView.axaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<UserControl xmlns="https://github.com/avaloniaui"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3-
xmlns:ui="using:FluentAvalonia.UI.Controls"
43
xmlns:c="using:KeyboardSwitch.Settings.Controls"
54
xmlns:l="using:KeyboardSwitch.Settings.Localization"
65
x:Class="KeyboardSwitch.Settings.Views.PreferencesView">
@@ -44,8 +43,8 @@
4443
Classes="FormControl ModifierKey" />
4544

4645
<TextBlock Grid.Row="0" Grid.Column="6" Text="{l:Translate PressCount}" Classes="Label" />
47-
<ui:NumberBox x:Name="PressCountBox" Grid.Row="0" Grid.Column="7" Classes="FormControl"
48-
Minimum="1" Maximum="10" />
46+
<NumericUpDown x:Name="PressCountBox" Grid.Row="0" Grid.Column="7" Classes="FormControl"
47+
Minimum="1" Maximum="10" FormatString="N0" />
4948

5049
<TextBlock Grid.Row="2" Grid.Column="0" Text="{l:Translate Backward}" Classes="Label" />
5150
<ComboBox x:Name="BackwardFirstComboBox" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch"
@@ -58,8 +57,8 @@
5857
Classes="FormControl ModifierKey" />
5958

6059
<TextBlock Grid.Row="2" Grid.Column="6" Text="{l:Translate WaitMilliseconds}" Classes="Label" />
61-
<ui:NumberBox x:Name="WaitMillisecondsBox" Grid.Row="2" Grid.Column="7" Classes="FormControl"
62-
Minimum="100" Maximum="1000" />
60+
<NumericUpDown x:Name="WaitMillisecondsBox" Grid.Row="2" Grid.Column="7" Classes="FormControl"
61+
Minimum="100" Maximum="1000" Increment="50" FormatString="N0" />
6362
</Grid>
6463

6564
<StackPanel Orientation="Horizontal" Margin="10 10 0 0">

src/KeyboardSwitch.Settings/Views/PreferencesView.axaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ private void BindControls(CompositeDisposable disposables)
8686
this.Bind(this.ViewModel, vm => vm.BackwardModifierThird, v => v.BackwardThirdComboBox.SelectedItem)
8787
.DisposeWith(disposables);
8888

89-
this.Bind(this.ViewModel, vm => vm.PressCount, v => v.PressCountBox.Value)
89+
this.Bind(this.ViewModel, vm => vm.PressCount, v => v.PressCountBox.Value, v => v, v => (int)(v ?? 0M))
9090
.DisposeWith(disposables);
9191

92-
this.Bind(this.ViewModel, vm => vm.WaitMilliseconds, v => v.WaitMillisecondsBox.Value)
92+
this.Bind(this.ViewModel, vm => vm.WaitMilliseconds, v => v.WaitMillisecondsBox.Value, v => v, v => (int)(v ?? 0M))
9393
.DisposeWith(disposables);
9494

9595
this.Bind(this.ViewModel, vm => vm.AppTheme, v => v.AppThemeComboBox.SelectedItem)

0 commit comments

Comments
 (0)