mirror of https://github.com/abpframework/abp.git
11 changed files with 206 additions and 1 deletions
@ -1,6 +1,11 @@ |
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Layout; |
|||
using System; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Layout; |
|||
|
|||
public static class StandardLayouts |
|||
{ |
|||
public const string Application = "Application"; |
|||
public const string Account = "Account"; |
|||
public const string Public = "Public"; |
|||
public const string Empty = "Empty"; |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Theming; |
|||
|
|||
public class |
|||
{ |
|||
public ThemeDictionary Themes { get; } |
|||
|
|||
public string DefaultThemeName { get; set; } |
|||
|
|||
public AbpThemingOptions() |
|||
{ |
|||
Themes = new ThemeDictionary(); |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Theming; |
|||
|
|||
public class DefaultThemeManager : IThemeManager, IScopedDependency, IServiceProviderAccessor |
|||
{ |
|||
public IServiceProvider ServiceProvider { get; } |
|||
public ITheme CurrentTheme => GetCurrentTheme(); |
|||
|
|||
private ITheme _currentTheme; |
|||
|
|||
protected IThemeSelector ThemeSelector { get; } |
|||
|
|||
public DefaultThemeManager( |
|||
IServiceProvider serviceProvider, |
|||
IThemeSelector themeSelector) |
|||
{ |
|||
ServiceProvider = serviceProvider; |
|||
ThemeSelector = themeSelector; |
|||
} |
|||
|
|||
protected virtual ITheme GetCurrentTheme() |
|||
{ |
|||
if (_currentTheme != null) |
|||
{ |
|||
return _currentTheme; |
|||
} |
|||
|
|||
_currentTheme = (ITheme)ServiceProvider.GetRequiredService(ThemeSelector.GetCurrentThemeInfo().ThemeType); |
|||
return CurrentTheme; |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
using System.Linq; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Theming; |
|||
|
|||
public class DefaultThemeSelector : IThemeSelector, ITransientDependency |
|||
{ |
|||
protected AbpThemingOptions Options { get; } |
|||
|
|||
public DefaultThemeSelector(IOptions<AbpThemingOptions> options) |
|||
{ |
|||
Options = options.Value; |
|||
} |
|||
|
|||
public virtual ThemeInfo GetCurrentThemeInfo() |
|||
{ |
|||
if (!Options.Themes.Any()) |
|||
{ |
|||
throw new AbpException($"No theme registered! Use {nameof(AbpThemingOptions)} to register themes."); |
|||
} |
|||
|
|||
if (Options.DefaultThemeName == null) |
|||
{ |
|||
return Options.Themes.Values.First(); |
|||
} |
|||
|
|||
var themeInfo = Options.Themes.Values.FirstOrDefault(t => t.Name == Options.DefaultThemeName); |
|||
if (themeInfo == null) |
|||
{ |
|||
throw new AbpException("Default theme is configured but it's not found in the registered themes: " + Options.DefaultThemeName); |
|||
} |
|||
|
|||
return themeInfo; |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Theming; |
|||
|
|||
public interface ITheme |
|||
{ |
|||
Type GetLayout(string name, bool fallbackToDefault = true); |
|||
} |
|||
@ -0,0 +1,6 @@ |
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Theming; |
|||
|
|||
public interface IThemeManager |
|||
{ |
|||
ITheme CurrentTheme { get; } |
|||
} |
|||
@ -0,0 +1,6 @@ |
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Theming; |
|||
|
|||
public interface IThemeSelector |
|||
{ |
|||
ThemeInfo GetCurrentThemeInfo(); |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Theming; |
|||
|
|||
public class ThemeDictionary : Dictionary<Type, ThemeInfo> |
|||
{ |
|||
public ThemeInfo Add<TTheme>() |
|||
where TTheme : ITheme |
|||
{ |
|||
return Add(typeof(TTheme)); |
|||
} |
|||
|
|||
public ThemeInfo Add(Type themeType) |
|||
{ |
|||
if (ContainsKey(themeType)) |
|||
{ |
|||
throw new AbpException("This theme is already added before: " + themeType.AssemblyQualifiedName); |
|||
} |
|||
|
|||
return this[themeType] = new ThemeInfo(themeType); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
using System; |
|||
using Volo.Abp.AspNetCore.Components.Web.Theming.Layout; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Theming; |
|||
|
|||
public static class ThemeExtensions |
|||
{ |
|||
public static Type GetApplicationLayout(this ITheme theme, bool fallbackToDefault = true) |
|||
{ |
|||
return theme.GetLayout(StandardLayouts.Application, fallbackToDefault); |
|||
} |
|||
|
|||
public static Type GetAccountLayout(this ITheme theme, bool fallbackToDefault = true) |
|||
{ |
|||
return theme.GetLayout(StandardLayouts.Account, fallbackToDefault); |
|||
} |
|||
|
|||
public static Type GetPublicLayout(this ITheme theme, bool fallbackToDefault = true) |
|||
{ |
|||
return theme.GetLayout(StandardLayouts.Public, fallbackToDefault); |
|||
} |
|||
|
|||
public static Type GetEmptyLayout(this ITheme theme, bool fallbackToDefault = true) |
|||
{ |
|||
return theme.GetLayout(StandardLayouts.Empty, fallbackToDefault); |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Theming; |
|||
|
|||
public class ThemeInfo |
|||
{ |
|||
public Type ThemeType { get; } |
|||
|
|||
public string Name { get; } |
|||
|
|||
public ThemeInfo([NotNull] Type themeType) |
|||
{ |
|||
Check.NotNull(themeType, nameof(themeType)); |
|||
|
|||
if (!typeof(ITheme).IsAssignableFrom(themeType)) |
|||
{ |
|||
throw new AbpException($"Given {nameof(themeType)} ({themeType.AssemblyQualifiedName}) should be type of {typeof(ITheme).AssemblyQualifiedName}"); |
|||
} |
|||
|
|||
ThemeType = themeType; |
|||
Name = ThemeNameAttribute.GetName(themeType); |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Linq; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Theming.Theming; |
|||
|
|||
[AttributeUsage(AttributeTargets.Class)] |
|||
public class ThemeNameAttribute : Attribute |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public ThemeNameAttribute(string name) |
|||
{ |
|||
Name = name; |
|||
} |
|||
|
|||
public static string GetName(Type themeType) |
|||
{ |
|||
return themeType |
|||
.GetCustomAttributes(true) |
|||
.OfType<ThemeNameAttribute>() |
|||
.FirstOrDefault()?.Name ?? themeType.Name; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue