Skip to content

Commit edb6898

Browse files
Update the theme instantly, without the need to restart the app
The theme can be updated correctly if the window is closed and reopened. GH-102
1 parent 86d93d3 commit edb6898

File tree

6 files changed

+48
-39
lines changed

6 files changed

+48
-39
lines changed

src/KeyboardSwitch.Settings/App.axaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
xmlns="https://github.com/avaloniaui"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:sys="using:System"
5+
xmlns:ui="using:FluentAvalonia.Styling"
56
xmlns:l="using:KeyboardSwitch.Settings.Localization"
67
xmlns:local="using:KeyboardSwitch.Settings"
78
x:Class="KeyboardSwitch.Settings.App"
@@ -11,6 +12,9 @@
1112
</Application.DataTemplates>
1213

1314
<Application.Styles>
15+
<!-- This is a placeholder for the theme that's set in App.axaml.cs -->
16+
<ui:FluentAvaloniaTheme PreferSystemTheme="True" PreferUserAccentColor="True" />
17+
1418
<Style Selector="TextBlock.Error">
1519
<Setter Property="Foreground" Value="{DynamicResource SystemFillColorCriticalBrush}" />
1620
<Setter Property="Margin" Value="10 2" />

src/KeyboardSwitch.Settings/App.axaml.cs

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Reactive.Linq;
12
using System.Reactive.Subjects;
23
using System.Reflection;
34

@@ -49,7 +50,10 @@ public override async void OnFrameworkInitializationCompleted()
4950

5051
var mainViewModel = await this.InitializeApp();
5152

52-
this.desktop.MainWindow = await this.CreateMainWindow(mainViewModel);
53+
var mainWindow = this.CreateMainWindow(mainViewModel);
54+
await this.SetWindowSizeFromState(mainWindow);
55+
56+
this.desktop.MainWindow = mainWindow;
5357
this.desktop.MainWindow.Show();
5458

5559
this.desktop.Exit += this.OnExit;
@@ -84,10 +88,18 @@ private async Task<MainViewModel> InitializeApp()
8488
var mainViewModel = new MainViewModel(appSettings);
8589
openExternally.InvokeCommand(mainViewModel.OpenExternally);
8690

91+
mainViewModel.PreferencesSaved
92+
.Select(p => p.AppTheme)
93+
.StartWith(appSettings.AppTheme)
94+
.DistinctUntilChanged()
95+
.ObserveOn(RxApp.MainThreadScheduler)
96+
.Subscribe(this.SetTheme);
97+
8798
mainViewModel.PreferencesSaved
8899
.Select(p => p.AppThemeVariant)
89100
.StartWith(appSettings.AppThemeVariant)
90101
.DistinctUntilChanged()
102+
.ObserveOn(RxApp.MainThreadScheduler)
91103
.Subscribe(this.SetThemeVariant);
92104

93105
return mainViewModel;
@@ -169,22 +181,13 @@ private JsonConfigurationProvider JsonProvider(string directory, string fileName
169181
Optional = true
170182
});
171183

172-
private async Task<MainWindow> CreateMainWindow(MainViewModel viewModel)
184+
private MainWindow CreateMainWindow(MainViewModel viewModel)
173185
{
174-
var state = await RxApp.SuspensionHost.ObserveAppState<AppState>().Take(1);
175-
176186
var window = new MainWindow
177187
{
178188
ViewModel = viewModel
179189
};
180190

181-
if (state.IsInitialized)
182-
{
183-
window.Width = state.WindowWidth;
184-
window.Height = state.WindowHeight;
185-
window.WindowState = state.IsWindowMaximized ? WindowState.Maximized : WindowState.Normal;
186-
}
187-
188191
var windowStateChanged = window
189192
.GetObservable(Window.WindowStateProperty)
190193
.DistinctUntilChanged()
@@ -207,9 +210,21 @@ private async Task<MainWindow> CreateMainWindow(MainViewModel viewModel)
207210
return window;
208211
}
209212

213+
private async Task SetWindowSizeFromState(MainWindow window)
214+
{
215+
var state = await RxApp.SuspensionHost.ObserveAppState<AppState>().Take(1);
216+
217+
if (state.IsInitialized)
218+
{
219+
window.Width = state.WindowWidth;
220+
window.Height = state.WindowHeight;
221+
window.WindowState = state.IsWindowMaximized ? WindowState.Maximized : WindowState.Normal;
222+
}
223+
}
224+
210225
private void SetTheme(AppTheme appTheme)
211226
{
212-
this.Styles.Insert(0, appTheme switch
227+
this.Styles[0] = appTheme switch
213228
{
214229
AppTheme.MacOS => new MacOSTheme(),
215230
AppTheme.Simple => new SimpleTheme(),
@@ -218,7 +233,23 @@ private void SetTheme(AppTheme appTheme)
218233
PreferUserAccentColor = true,
219234
PreferSystemTheme = true
220235
}
221-
});
236+
};
237+
238+
if (this.desktop.MainWindow is MainWindow window)
239+
{
240+
var newMainWindow = this.CreateMainWindow(window.ViewModel!);
241+
242+
newMainWindow.Width = window.Width;
243+
newMainWindow.Height = window.Height;
244+
newMainWindow.WindowState = window.WindowState;
245+
246+
this.desktop.MainWindow = newMainWindow;
247+
this.desktop.MainWindow.Show();
248+
249+
newMainWindow.Position = window.Position;
250+
251+
window.Close();
252+
}
222253
}
223254

224255
private void SetThemeVariant(AppThemeVariant appThemeVariant)

src/KeyboardSwitch.Settings/Properties/Messages.Designer.cs

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/KeyboardSwitch.Settings/Properties/Messages.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,6 @@
363363
<data name="SwitchSettings" xml:space="preserve">
364364
<value>Switch settings:</value>
365365
</data>
366-
<data name="UpdateThemeHint" xml:space="preserve">
367-
<value>Theme will be updated after application restart</value>
368-
</data>
369366
<data name="UseXsel" xml:space="preserve">
370367
<value>Use xsel for clipboard integration</value>
371368
</data>

src/KeyboardSwitch.Settings/Views/PreferencesView.axaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,6 @@
192192

193193
<TextBlock Text="{l:Translate AppThemeVariant}" Classes="Label" />
194194
<ComboBox x:Name="AppThemeVariantComboBox" Classes="FormControl" />
195-
196-
<TextBlock
197-
x:Name="UpdateThemeHintTextBlock"
198-
Text="{l:Translate UpdateThemeHint}"
199-
VerticalAlignment="Center"
200-
Margin="10 0 0 0"
201-
IsVisible="False"
202-
/>
203195
</StackPanel>
204196

205197
<TextBlock x:Name="PressCountValidationTextBlock" Classes="Error" IsVisible="False" />

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ private void BindControls(CompositeDisposable disposables)
9898

9999
this.Bind(this.ViewModel, vm => vm.AppThemeVariant, v => v.AppThemeVariantComboBox.SelectedItem)
100100
.DisposeWith(disposables);
101-
102-
this.ViewModel!.WhenAnyValue(vm => vm.AppTheme)
103-
.Merge(this.ViewModel!.Save.Select(p => this.ViewModel!.AppTheme))
104-
.Select(theme => theme != this.ViewModel!.PreferencesModel.AppTheme)
105-
.BindTo(this, v => v.UpdateThemeHintTextBlock.IsVisible)
106-
.DisposeWith(disposables);
107101
}
108102

109103
private void BindValidations(CompositeDisposable disposables)

0 commit comments

Comments
 (0)