Reopen window when theme was changed

This commit is contained in:
Maksym Katsydan 2022-11-12 22:23:50 -05:00
parent 18c8883f18
commit e52a5869ed
2 changed files with 35 additions and 7 deletions

View File

@ -1,10 +1,9 @@
<Application xmlns="https://github.com/avaloniaui" <Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaCoreRTDemo.App"> x:Class="AvaloniaCoreRTDemo.App">
<Application.Styles>
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml" />
</Application.Styles>
<Application.Resources> <Application.Resources>
<StyleInclude x:Key="fluentDataGrid" Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml" />
<StyleInclude x:Key="simpleDataGrid" Source="avares://Avalonia.Controls.DataGrid/Themes/Simple.xaml" />
<FluentTheme x:Key="fluentTheme" Mode="Light" /> <FluentTheme x:Key="fluentTheme" Mode="Light" />
<SimpleTheme x:Key="simpleTheme" Mode="Light" /> <SimpleTheme x:Key="simpleTheme" Mode="Light" />
</Application.Resources> </Application.Resources>

View File

@ -1,4 +1,5 @@
using System;
using Avalonia; using Avalonia;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
@ -14,6 +15,8 @@ namespace AvaloniaCoreRTDemo
{ {
private FluentTheme _fluentTheme; private FluentTheme _fluentTheme;
private SimpleTheme _simpleTheme; private SimpleTheme _simpleTheme;
private IStyle _fluentDataGrid;
private IStyle _simpleDataGrid;
private ApplicationTheme _currentTheme; private ApplicationTheme _currentTheme;
@ -38,7 +41,10 @@ namespace AvaloniaCoreRTDemo
{ {
this._fluentTheme = (FluentTheme)this.Resources["fluentTheme"]!; this._fluentTheme = (FluentTheme)this.Resources["fluentTheme"]!;
this._simpleTheme = (SimpleTheme)this.Resources["simpleTheme"]!; this._simpleTheme = (SimpleTheme)this.Resources["simpleTheme"]!;
this._fluentDataGrid = (IStyle)this.Resources["fluentDataGrid"]!;
this._simpleDataGrid = (IStyle)this.Resources["simpleDataGrid"]!;
Styles.Add(_fluentTheme); Styles.Add(_fluentTheme);
Styles.Add(_fluentDataGrid);
this._currentTheme = ApplicationTheme.FluentLight; this._currentTheme = ApplicationTheme.FluentLight;
} }
@ -47,26 +53,49 @@ namespace AvaloniaCoreRTDemo
void IThemeSwitch.ChangeTheme(ApplicationTheme theme) void IThemeSwitch.ChangeTheme(ApplicationTheme theme)
{ {
var themeChanged = theme switch
{
ApplicationTheme.SimpleLight => _currentTheme is ApplicationTheme.FluentDark or ApplicationTheme.FluentLight,
ApplicationTheme.SimpleDark => _currentTheme is ApplicationTheme.FluentDark or ApplicationTheme.FluentLight,
ApplicationTheme.FluentLight => _currentTheme is ApplicationTheme.SimpleLight or ApplicationTheme.SimpleDark,
ApplicationTheme.FluentDark => _currentTheme is ApplicationTheme.SimpleLight or ApplicationTheme.SimpleDark,
_ => throw new ArgumentOutOfRangeException(nameof(theme), theme, null)
};
this._currentTheme = theme; this._currentTheme = theme;
switch (theme) switch (theme)
{ {
case ApplicationTheme.SimpleLight: case ApplicationTheme.SimpleLight:
this._simpleTheme.Mode = SimpleThemeMode.Light; this._simpleTheme.Mode = SimpleThemeMode.Light;
this.Styles[1] = this._simpleTheme; this.Styles[0] = this._simpleTheme;
this.Styles[1] = this._simpleDataGrid;
break; break;
case ApplicationTheme.SimpleDark: case ApplicationTheme.SimpleDark:
this._simpleTheme.Mode = SimpleThemeMode.Dark; this._simpleTheme.Mode = SimpleThemeMode.Dark;
this.Styles[1] = this._simpleTheme; this.Styles[0] = this._simpleTheme;
this.Styles[1] = this._simpleDataGrid;
break; break;
case ApplicationTheme.FluentLight: case ApplicationTheme.FluentLight:
this._fluentTheme.Mode = FluentThemeMode.Light; this._fluentTheme.Mode = FluentThemeMode.Light;
this.Styles[1] = this._fluentTheme; this.Styles[0] = this._fluentTheme;
this.Styles[1] = this._fluentDataGrid;
break; break;
case ApplicationTheme.FluentDark: case ApplicationTheme.FluentDark:
this._fluentTheme.Mode = FluentThemeMode.Dark; this._fluentTheme.Mode = FluentThemeMode.Dark;
this.Styles[1] = this._fluentTheme; this.Styles[0] = this._fluentTheme;
this.Styles[1] = this._fluentDataGrid;
break; break;
} }
if (themeChanged && ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var oldWindow = desktop.MainWindow;
var newWindow = new MainWindow();
desktop.MainWindow = newWindow;
newWindow.Show();
oldWindow.Close();
this.DataContext = desktop.MainWindow.DataContext;
}
} }
} }
} }