Browse Source

wip

pull/7353/head
Takoooooo 5 years ago
parent
commit
25c5156725
  1. 21
      samples/ControlCatalog/App.xaml.cs
  2. 2
      samples/ControlCatalog/MainView.xaml.cs
  3. 44
      src/Avalonia.Themes.Default/SimpleTheme.cs

21
samples/ControlCatalog/App.xaml.cs

@ -5,6 +5,7 @@ using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.Markup.Xaml.Styling; using Avalonia.Markup.Xaml.Styling;
using Avalonia.Styling; using Avalonia.Styling;
using Avalonia.Themes.Default;
using Avalonia.Themes.Fluent; using Avalonia.Themes.Fluent;
using ControlCatalog.ViewModels; using ControlCatalog.ViewModels;
@ -29,6 +30,8 @@ namespace ControlCatalog
public static FluentTheme Fluent = new FluentTheme(new Uri("avares://ControlCatalog/Styles")); public static FluentTheme Fluent = new FluentTheme(new Uri("avares://ControlCatalog/Styles"));
public static SimpleTheme Default = new SimpleTheme(new Uri("avares://ControlCatalog/Styles"));
public static Styles DefaultLight = new Styles public static Styles DefaultLight = new Styles
{ {
new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog")) new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog"))
@ -43,14 +46,7 @@ namespace ControlCatalog
{ {
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseLight.xaml") Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseLight.xaml")
}, },
new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog")) Default
{
Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseLight.xaml")
},
new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog"))
{
Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml")
}
}; };
public static Styles DefaultDark = new Styles public static Styles DefaultDark = new Styles
@ -67,14 +63,7 @@ namespace ControlCatalog
{ {
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseDark.xaml") Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseDark.xaml")
}, },
new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog")) Default
{
Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseDark.xaml")
},
new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog"))
{
Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml")
}
}; };
public override void Initialize() public override void Initialize()

2
samples/ControlCatalog/MainView.xaml.cs

@ -63,11 +63,13 @@ namespace ControlCatalog
} }
else if (theme == CatalogTheme.DefaultLight) else if (theme == CatalogTheme.DefaultLight)
{ {
App.Default.Mode = Avalonia.Themes.Default.SimpleThemeMode.Light;
Application.Current.Styles[0] = App.DefaultLight; Application.Current.Styles[0] = App.DefaultLight;
Application.Current.Styles[1] = App.DataGridDefault; Application.Current.Styles[1] = App.DataGridDefault;
} }
else if (theme == CatalogTheme.DefaultDark) else if (theme == CatalogTheme.DefaultDark)
{ {
App.Default.Mode = Avalonia.Themes.Default.SimpleThemeMode.Dark;
Application.Current.Styles[0] = App.DefaultDark; Application.Current.Styles[0] = App.DefaultDark;
Application.Current.Styles[1] = App.DataGridDefault; Application.Current.Styles[1] = App.DataGridDefault;
} }

44
src/Avalonia.Themes.Default/SimpleTheme.cs

@ -14,6 +14,7 @@ namespace Avalonia.Themes.Default
private bool _isLoading; private bool _isLoading;
private Styles _simpleDark = new(); private Styles _simpleDark = new();
private Styles _simpleLight = new(); private Styles _simpleLight = new();
private Styles _sharedStyles = new();
private IStyle? _loaded; private IStyle? _loaded;
/// <summary> /// <summary>
@ -32,7 +33,12 @@ namespace Avalonia.Themes.Default
/// <param name="serviceProvider">The XAML service provider.</param> /// <param name="serviceProvider">The XAML service provider.</param>
public SimpleTheme(IServiceProvider serviceProvider) public SimpleTheme(IServiceProvider serviceProvider)
{ {
_baseUri = ((IUriContext)serviceProvider.GetService(typeof(IUriContext))).BaseUri; var service = serviceProvider.GetService(typeof(IUriContext));
if (service == null)
{
throw new Exception("There is no service object of type IUriContext!");
}
_baseUri = ((IUriContext)service).BaseUri;
InitStyles(_baseUri); InitStyles(_baseUri);
} }
@ -46,6 +52,22 @@ namespace Avalonia.Themes.Default
get => GetValue(ModeProperty); get => GetValue(ModeProperty);
set => SetValue(ModeProperty, value); set => SetValue(ModeProperty, value);
} }
protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
{
base.OnPropertyChanged(change);
if (change.Property == ModeProperty)
{
if (Mode == SimpleThemeMode.Dark)
{
(Loaded as Styles)![1] = _simpleDark[0];
}
else
{
(Loaded as Styles)![1] = _simpleLight[0];
}
}
}
public IStyle Loaded public IStyle Loaded
{ {
get get
@ -56,11 +78,11 @@ namespace Avalonia.Themes.Default
if (Mode == SimpleThemeMode.Light) if (Mode == SimpleThemeMode.Light)
{ {
_loaded = _simpleLight; _loaded = new Styles { _sharedStyles , _simpleLight};
} }
else if (Mode == SimpleThemeMode.Dark) else if (Mode == SimpleThemeMode.Dark)
{ {
_loaded = _simpleDark; _loaded = new Styles { _sharedStyles, _simpleDark };
} }
_isLoading = false; _isLoading = false;
} }
@ -110,16 +132,18 @@ namespace Avalonia.Themes.Default
void IResourceProvider.RemoveOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.RemoveOwner(owner); void IResourceProvider.RemoveOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.RemoveOwner(owner);
private void InitStyles(Uri baseUri) private void InitStyles(Uri baseUri)
{ {
_sharedStyles = new Styles
_simpleLight = new Styles
{ {
new StyleInclude(baseUri) new StyleInclude(baseUri)
{ {
Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseLight.xaml") Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml")
}, }
};
_simpleLight = new Styles
{
new StyleInclude(baseUri) new StyleInclude(baseUri)
{ {
Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml") Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseLight.xaml")
} }
}; };
@ -128,10 +152,6 @@ namespace Avalonia.Themes.Default
new StyleInclude(baseUri) new StyleInclude(baseUri)
{ {
Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseDark.xaml") Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseDark.xaml")
},
new StyleInclude(baseUri)
{
Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml")
} }
}; };
} }

Loading…
Cancel
Save