-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
171 lines (154 loc) · 6.36 KB
/
Copy pathApp.xaml.cs
File metadata and controls
171 lines (154 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.Themes.Simple;
using Avalonia.Themes.Fluent;
using ControlCatalog.Models;
using ControlCatalog.ViewModels;
namespace ControlCatalog
{
public class App : Application
{
private readonly Styles _themeStylesContainer = new();
private FluentTheme? _fluentTheme;
private Avalonia.Themes.Fluent2.Fluent2Theme? _fluent2Theme;
private SimpleTheme? _simpleTheme;
private IStyle? _colorPickerFluent, _colorPickerSimple;
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"]!;
_colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!;
SetCatalogThemes(CatalogTheme.Fluent2);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
{
desktopLifetime.MainWindow = new MainWindow { DataContext = new MainWindowViewModel() };
}
else if (ApplicationLifetime is IActivityApplicationLifetime singleViewFactoryApplicationLifetime)
{
singleViewFactoryApplicationLifetime.MainViewFactory = () => new PageNavigationHost()
{
Page = new MainView { DataContext = new MainWindowViewModel() }
};
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
{
singleViewLifetime.MainView = new PageNavigationHost()
{
Page = new MainView { DataContext = new 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 static void SetCatalogThemes(CatalogTheme theme)
{
var app = (App)Current!;
var prevTheme = app._prevTheme;
app._prevTheme = theme;
var shouldReopenWindow = prevTheme != theme;
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._colorPickerFluent!;
}
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;
var newWindow = new MainWindow()
{
DataContext = new MainWindowViewModel()
};
desktopLifetime.MainWindow = newWindow;
newWindow.Show();
oldWindow?.Close();
}
else if (app.ApplicationLifetime is IActivityApplicationLifetime singleViewFactoryApplicationLifetime)
{
singleViewFactoryApplicationLifetime.MainViewFactory = () => new PageNavigationHost()
{
Page = new MainView { DataContext = new MainWindowViewModel() }
};
}
else if (app.ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
{
singleViewLifetime.MainView = new PageNavigationHost()
{
Page = new MainView { DataContext = new MainWindowViewModel() }
};
}
}
}
}
}