Skip to content

Commit f2ba57a

Browse files
Update the theme variant dynamically
Updating the theme dynamically leaves remnants of the previous theme, so it's not implemented. GH-102
1 parent 08e2d3b commit f2ba57a

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

src/KeyboardSwitch.Core/KeyboardSwitch.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageReference Include="System.Interactive.Async" Version="6.0.3" />
1717
<PackageReference Include="System.Reactive" Version="6.0.1" />
1818
<PackageReference Include="System.Text.Json" Version="9.0.6" />
19-
<PackageReference Include="TestableIO.System.IO.Abstractions.Extensions" Version="22.0.3" />
19+
<PackageReference Include="TestableIO.System.IO.Abstractions.Extensions" Version="22.0.4" />
2020
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="22.0.14" />
2121
</ItemGroup>
2222
</Project>

src/KeyboardSwitch.Settings.Core/ViewModels/MainContentViewModel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public MainContentViewModel(
2626

2727
this.SaveCharMappingSettings = ReactiveCommand.CreateFromTask<CharMappingModel>(
2828
this.SaveCharMappingSettingsAsync);
29-
this.SavePreferences = ReactiveCommand.CreateFromTask<PreferencesModel>(
29+
this.SavePreferences = ReactiveCommand.CreateFromTask<PreferencesModel, PreferencesModel>(
3030
this.SavePreferencesAsync);
3131
this.OpenAboutTab = ReactiveCommand.Create(() => { });
3232

@@ -43,7 +43,7 @@ public MainContentViewModel(
4343
public AboutViewModel AboutViewModel { get; }
4444

4545
public ReactiveCommand<CharMappingModel, Unit> SaveCharMappingSettings { get; }
46-
public ReactiveCommand<PreferencesModel, Unit> SavePreferences { get; }
46+
public ReactiveCommand<PreferencesModel, PreferencesModel> SavePreferences { get; }
4747
public ReactiveCommand<Unit, Unit> OpenAboutTab { get; }
4848

4949
private async Task SaveCharMappingSettingsAsync(CharMappingModel charMappingModel)
@@ -70,7 +70,7 @@ private async Task SaveCharMappingSettingsAsync(CharMappingModel charMappingMode
7070
await this.appSettingsService.SaveAppSettings(newSettings);
7171
}
7272

73-
private async Task SavePreferencesAsync(PreferencesModel preferencesModel)
73+
private async Task<PreferencesModel> SavePreferencesAsync(PreferencesModel preferencesModel)
7474
{
7575
var settings = await this.appSettingsService.GetAppSettings();
7676

@@ -91,5 +91,7 @@ private async Task SavePreferencesAsync(PreferencesModel preferencesModel)
9191
{
9292
this.startupService.ConfigureStartup(preferencesModel.Startup);
9393
}
94+
95+
return preferencesModel;
9496
}
9597
}

src/KeyboardSwitch.Settings.Core/ViewModels/MainViewModel.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1+
using KeyboardSwitch.Core.Keyboard;
2+
13
namespace KeyboardSwitch.Settings.Core.ViewModels;
24

35
public class MainViewModel : ReactiveObject
46
{
5-
private readonly AppSettings appSettings;
6-
private readonly ILayoutService layoutService;
7+
private readonly Subject<PreferencesModel> preferencesSaved = new();
78

89
public MainViewModel(
910
AppSettings appSettings,
1011
ILayoutService? layoutService = null,
1112
IStartupService? startupService = null)
1213
{
13-
this.appSettings = appSettings;
14-
this.layoutService = layoutService ?? GetRequiredService<ILayoutService>();
14+
layoutService ??= GetRequiredService<ILayoutService>();
1515
startupService ??= GetRequiredService<IStartupService>();
1616

1717
this.MainContentViewModel = new MainContentViewModel(
18-
this.CreateCharMappingModel(),
18+
this.CreateCharMappingModel(appSettings, layoutService.GetKeyboardLayouts()),
1919
new PreferencesModel(appSettings, startupService.IsStartupConfigured()));
2020

2121
this.ServiceViewModel = new ServiceViewModel();
@@ -28,7 +28,11 @@ public MainViewModel(
2828
.Merge(this.MainContentViewModel.SavePreferences.Discard())
2929
.InvokeCommand(this.ServiceViewModel.ReloadSettings);
3030

31+
this.MainContentViewModel.SavePreferences.Subscribe(this.preferencesSaved);
32+
3133
this.OpenAboutTab.InvokeCommand(this.MainContentViewModel.OpenAboutTab);
34+
35+
this.PreferencesSaved = this.preferencesSaved.AsObservable();
3236
}
3337

3438
public MainContentViewModel MainContentViewModel { get; }
@@ -37,11 +41,11 @@ public MainViewModel(
3741
public ReactiveCommand<Unit, Unit> OpenExternally { get; }
3842
public ReactiveCommand<Unit, Unit> OpenAboutTab { get; }
3943

40-
private CharMappingModel CreateCharMappingModel()
41-
{
42-
var layouts = this.layoutService.GetKeyboardLayouts();
44+
public IObservable<PreferencesModel> PreferencesSaved { get; }
4345

44-
var charsByLayoutId = this.appSettings.CharsByKeyboardLayoutId;
46+
private CharMappingModel CreateCharMappingModel(AppSettings appSettings, IReadOnlyList<KeyboardLayout> layouts)
47+
{
48+
var charsByLayoutId = appSettings.CharsByKeyboardLayoutId;
4549

4650
var layoutModels = layouts
4751
.Select((layout, index) => new LayoutModel

src/KeyboardSwitch.Settings/App.axaml.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,17 @@ private async Task<MainViewModel> InitializeApp()
7777
try
7878
{
7979
var appSettings = await this.serviceProvider.GetRequiredService<IAppSettingsService>().GetAppSettings();
80+
81+
this.SetTheme(appSettings.AppTheme);
82+
8083
var mainViewModel = new MainViewModel(appSettings);
8184
openExternally.InvokeCommand(mainViewModel.OpenExternally);
8285

83-
this.SetTheme(appSettings);
86+
mainViewModel.PreferencesSaved
87+
.Select(p => p.AppThemeVariant)
88+
.StartWith(appSettings.AppThemeVariant)
89+
.DistinctUntilChanged()
90+
.Subscribe(this.SetThemeVariant);
8491

8592
return mainViewModel;
8693
} catch (IncompatibleAppVersionException e)
@@ -199,21 +206,22 @@ private async Task<MainWindow> CreateMainWindow(MainViewModel viewModel)
199206
return window;
200207
}
201208

202-
private void SetTheme(AppSettings appSettings)
209+
private void SetTheme(AppTheme appTheme)
203210
{
204-
if (appSettings.AppTheme == AppTheme.MacOS)
211+
this.Styles.Insert(0, appTheme switch
205212
{
206-
this.Styles.Insert(0, new MacOSTheme());
207-
} else
208-
{
209-
this.Styles.Insert(0, new FluentAvaloniaTheme
213+
AppTheme.MacOS => new MacOSTheme(),
214+
_ => new FluentAvaloniaTheme
210215
{
211216
PreferUserAccentColor = true,
212217
PreferSystemTheme = true
213-
});
214-
}
218+
}
219+
});
220+
}
215221

216-
this.RequestedThemeVariant = appSettings.AppThemeVariant switch
222+
private void SetThemeVariant(AppThemeVariant appThemeVariant)
223+
{
224+
this.RequestedThemeVariant = appThemeVariant switch
217225
{
218226
AppThemeVariant.Light => ThemeVariant.Light,
219227
AppThemeVariant.Dark => ThemeVariant.Dark,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public PreferencesView()
3838
this.BindValidations(disposables);
3939
this.BindCommands(disposables);
4040

41-
if (this.ViewModel?.ShowUseXsel == false)
41+
if (this.ViewModel?.ShowUseXsel == true)
4242
{
43-
this.UseXselCheckBox.IsVisible = false;
43+
this.UseXselCheckBox.IsVisible = true;
4444
}
4545
});
4646
}

0 commit comments

Comments
 (0)