Browse Source

wip

pull/7353/head
Takoooooo 4 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.Styling;
using Avalonia.Styling;
using Avalonia.Themes.Default;
using Avalonia.Themes.Fluent;
using ControlCatalog.ViewModels;
@ -29,6 +30,8 @@ namespace ControlCatalog
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
{
new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog"))
@ -43,14 +46,7 @@ namespace ControlCatalog
{
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseLight.xaml")
},
new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog"))
{
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")
}
Default
};
public static Styles DefaultDark = new Styles
@ -67,14 +63,7 @@ namespace ControlCatalog
{
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseDark.xaml")
},
new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog"))
{
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")
}
Default
};
public override void Initialize()

2
samples/ControlCatalog/MainView.xaml.cs

@ -63,11 +63,13 @@ namespace ControlCatalog
}
else if (theme == CatalogTheme.DefaultLight)
{
App.Default.Mode = Avalonia.Themes.Default.SimpleThemeMode.Light;
Application.Current.Styles[0] = App.DefaultLight;
Application.Current.Styles[1] = App.DataGridDefault;
}
else if (theme == CatalogTheme.DefaultDark)
{
App.Default.Mode = Avalonia.Themes.Default.SimpleThemeMode.Dark;
Application.Current.Styles[0] = App.DefaultDark;
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 Styles _simpleDark = new();
private Styles _simpleLight = new();
private Styles _sharedStyles = new();
private IStyle? _loaded;
/// <summary>
@ -32,7 +33,12 @@ namespace Avalonia.Themes.Default
/// <param name="serviceProvider">The XAML service provider.</param>
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);
}
@ -46,6 +52,22 @@ namespace Avalonia.Themes.Default
get => GetValue(ModeProperty);
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
{
get
@ -56,11 +78,11 @@ namespace Avalonia.Themes.Default
if (Mode == SimpleThemeMode.Light)
{
_loaded = _simpleLight;
_loaded = new Styles { _sharedStyles , _simpleLight};
}
else if (Mode == SimpleThemeMode.Dark)
{
_loaded = _simpleDark;
_loaded = new Styles { _sharedStyles, _simpleDark };
}
_isLoading = false;
}
@ -110,16 +132,18 @@ namespace Avalonia.Themes.Default
void IResourceProvider.RemoveOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.RemoveOwner(owner);
private void InitStyles(Uri baseUri)
{
_simpleLight = new Styles
_sharedStyles = new Styles
{
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)
{
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)
{
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