135 lines
3.3 KiB
C#
135 lines
3.3 KiB
C#
using WPFluent.Appearance;
|
|
using WPFluent.Extensions;
|
|
using WPFluent.Markup;
|
|
|
|
|
|
namespace WPFluent;
|
|
|
|
/// <summary>
|
|
/// Represents a UI application.
|
|
/// </summary>
|
|
public class UiApplication
|
|
{
|
|
private static UiApplication? _uiApplication;
|
|
|
|
private readonly Application? _application;
|
|
|
|
private Window? _mainWindow;
|
|
|
|
private ResourceDictionary? _resources;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="UiApplication"/> class.
|
|
/// </summary>
|
|
public UiApplication(Application application)
|
|
{
|
|
if (application is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!ApplicationHasResources(application))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_application = application;
|
|
|
|
System.Diagnostics.Debug
|
|
.WriteLine($"INFO | {typeof(UiApplication)} application is {_application}", "WPFluent");
|
|
}
|
|
|
|
private static bool ApplicationHasResources(Application application)
|
|
{
|
|
return application
|
|
.Resources.MergedDictionaries
|
|
.Where(e => e.Source is not null)
|
|
.Any(
|
|
e => e.Source
|
|
.ToString()
|
|
.Contains(ApplicationThemeManager.LibraryNamespace, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Turns the application's into shutdown mode.
|
|
/// </summary>
|
|
public void Shutdown() { _application?.Shutdown(); }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the application's main window.
|
|
/// </summary>
|
|
public object TryFindResource(object resourceKey) { return Resources[resourceKey]; }
|
|
|
|
/// <summary>
|
|
/// Gets the current application.
|
|
/// </summary>
|
|
public static UiApplication Current
|
|
{
|
|
get
|
|
{
|
|
_uiApplication ??= new UiApplication(Application.Current);
|
|
|
|
return _uiApplication;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the application is running outside of the desktop app context.
|
|
/// </summary>
|
|
public bool IsApplication => _application is not null;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the application's main window.
|
|
/// </summary>
|
|
public Window? MainWindow
|
|
{
|
|
get => _application?.MainWindow ?? _mainWindow;
|
|
set
|
|
{
|
|
if (_application != null)
|
|
{
|
|
_application.MainWindow = value;
|
|
}
|
|
|
|
_mainWindow = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the application's resources.
|
|
/// </summary>
|
|
public ResourceDictionary Resources
|
|
{
|
|
get
|
|
{
|
|
if (_resources is null)
|
|
{
|
|
_resources = [];
|
|
|
|
try
|
|
{
|
|
ApplicationAccentColorManager.ApplySystemAccent();
|
|
var themesDictionary = new ThemesDictionary();
|
|
var controlsDictionary = new ControlsDictionary();
|
|
_resources.MergedDictionaries.Add(themesDictionary);
|
|
_resources.MergedDictionaries.Add(controlsDictionary);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
return _application?.Resources ?? _resources;
|
|
}
|
|
set
|
|
{
|
|
if (_application is not null)
|
|
{
|
|
_application.Resources = value;
|
|
}
|
|
|
|
_resources = value;
|
|
}
|
|
}
|
|
}
|