A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

191 lines
7.1 KiB

using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Media.Immutable;
using Avalonia.Styling;
using Avalonia.Themes.Fluent;
using Avalonia.Themes.Simple;
using ControlCatalog.Controls;
using ControlCatalog.Models;
using ControlCatalog.ViewModels;
namespace ControlCatalog
{
public class App : Application
{
public static readonly TransparentStyles TransparentStyles = new();
private readonly Styles _themeStylesContainer = new();
private FluentTheme? _fluentTheme;
private Avalonia.Themes.Fluent2.Fluent2Theme? _fluent2Theme;
private SimpleTheme? _simpleTheme;
private IStyle? _colorPickerFluent, _colorPickerFluent2, _colorPickerSimple;
private MainWindowViewModel _mainWindowViewModel = new MainWindowViewModel();
public App()
{
DataContext = new ApplicationViewModel();
}
public override void Initialize()
{
Styles.Add(_themeStylesContainer);
AvaloniaXamlLoader.Load(this);
_fluentTheme = (FluentTheme)Resources["FluentTheme"]!;
_fluent2Theme = (Avalonia.Themes.Fluent2.Fluent2Theme)Resources["Fluent2Theme"]!;
_simpleTheme = (SimpleTheme)Resources["SimpleTheme"]!;
_colorPickerFluent = (IStyle)Resources["ColorPickerFluent"]!;
_colorPickerFluent2 = (IStyle)Resources["ColorPickerFluent2"]!;
_colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!;
SetCatalogThemes(CatalogTheme.Fluent2, skipWindowCreation: true);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
{
desktopLifetime.MainWindow = new MainWindow { DataContext = _mainWindowViewModel };
}
else if (ApplicationLifetime is IActivityApplicationLifetime singleViewFactoryApplicationLifetime)
{
singleViewFactoryApplicationLifetime.MainViewFactory = () => new MainViewHost(_mainWindowViewModel);
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
{
singleViewLifetime.MainView = new MainViewHost(_mainWindowViewModel);
}
if (this.TryGetFeature<IActivatableLifetime>() is { } activatableApplicationLifetime)
{
activatableApplicationLifetime.Activated += (sender, args) =>
Console.WriteLine($"App activated: {args.Kind}");
activatableApplicationLifetime.Deactivated += (sender, args) =>
Console.WriteLine($"App deactivated: {args.Kind}");
}
base.OnFrameworkInitializationCompleted();
}
public void OnDockNewWindowClicked(object? sender, EventArgs e)
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime)
{
var window = new MainWindow();
window.Show();
}
}
public void OnDockShowMainWindowClicked(object? sender, EventArgs e)
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
{
desktopLifetime.MainWindow?.Activate();
}
}
private int _dockMenuItemCount;
public void OnDockAddItemClicked(object? sender, EventArgs e)
{
var dockMenu = NativeDock.GetMenu(this);
if (dockMenu is not null)
{
_dockMenuItemCount++;
var item = new NativeMenuItem($"New item {_dockMenuItemCount}");
item.Click += (_, _) =>
{
dockMenu.Items.Remove(item);
};
dockMenu.Items.Insert(0, item);
}
}
private CatalogTheme _prevTheme;
public static CatalogTheme CurrentTheme => ((App)Current!)._prevTheme;
public event EventHandler? ThemeUpdated;
public static void ApplyTopLevelTransparency(TopLevel topLevel, WindowTransparencyLevel windowTransparencyLevel)
{
topLevel.TransparencyLevelHint = [windowTransparencyLevel];
if (topLevel.ActualTransparencyLevel != WindowTransparencyLevel.None &&
windowTransparencyLevel != WindowTransparencyLevel.None)
{
topLevel.Background = new ImmutableSolidColorBrush(Colors.Gray, 0.2);
if (!topLevel.Styles.Contains(TransparentStyles))
topLevel.Styles.Add(TransparentStyles);
}
else
{
topLevel.Background = null;
topLevel.Styles.Remove(TransparentStyles);
}
}
public static void SetCatalogThemes(CatalogTheme theme, bool skipWindowCreation = false)
{
var app = (App)Current!;
var prevTheme = app._prevTheme;
app._prevTheme = theme;
var shouldReopenWindow = prevTheme != theme && !skipWindowCreation;
if (app._themeStylesContainer.Count == 0)
{
app._themeStylesContainer.Add(new Style());
app._themeStylesContainer.Add(new Style());
app._themeStylesContainer.Add(new Style());
}
if (theme == CatalogTheme.Fluent)
{
app._themeStylesContainer[0] = app._fluentTheme!;
app._themeStylesContainer[1] = app._colorPickerFluent!;
}
else if (theme == CatalogTheme.Fluent2)
{
app._themeStylesContainer[0] = app._fluent2Theme!;
app._themeStylesContainer[1] = app._colorPickerFluent2!;
}
else if (theme == CatalogTheme.Simple)
{
app._themeStylesContainer[0] = app._simpleTheme!;
app._themeStylesContainer[1] = app._colorPickerSimple!;
}
if (shouldReopenWindow)
{
if (app.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
{
var oldWindow = desktopLifetime.MainWindow;
if (oldWindow != null)
{
var vm = oldWindow.DataContext;
oldWindow.DataContext = null;
var controlTheme = app.FindResource(app.ActualThemeVariant, typeof(Window)) as ControlTheme;
oldWindow.Theme = controlTheme;
oldWindow.DataContext = vm;
}
else
{
var newWindow = new MainWindow()
{
DataContext = app._mainWindowViewModel
};
desktopLifetime.MainWindow = newWindow;
newWindow.Show();
}
}
app.ThemeUpdated?.Invoke(app, EventArgs.Empty);
}
}
}
}