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
172
173
174
175
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ArcaneLibs.Extensions;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using LibMatrix.Responses;
using LibMatrix.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModerationClient.Services;
using ModerationClient.ViewModels;
#if DEBUG
using Avalonia.Diagnostics;
#endif
namespace ModerationClient.Views.MainWindow;
public partial class MainWindow : Window {
private readonly CommandLineConfiguration _cfg;
public MainWindow(CommandLineConfiguration cfg, MainWindowViewModel dataContext, IHostApplicationLifetime appLifetime, HomeserverResolverService hsResolver) {
_cfg = cfg;
InitializeComponent();
DataContext = dataContext;
_ = dataContext.AuthService.LoadProfileAsync().ContinueWith(x => {
if (x.IsFaulted) {
Console.WriteLine("Failed to load profile: " + x.Exception);
}
dataContext.CurrentViewModel = dataContext.AuthService.IsLoggedIn
? App.Current.Host.Services.GetRequiredService<ClientViewModel>()
: new LoginViewModel(dataContext.AuthService, hsResolver);
if (!dataContext.AuthService.IsLoggedIn) {
dataContext.AuthService.PropertyChanged += (sender, args) => {
if (args.PropertyName == nameof(MatrixAuthenticationService.IsLoggedIn)) {
if (dataContext.AuthService.IsLoggedIn) {
// dataContext.CurrentViewModel = new ClientViewModel(dataContext.AuthService);
dataContext.CurrentViewModel = App.Current.Host.Services.GetRequiredService<ClientViewModel>();
// var window = App.Current.Host.Services.GetRequiredService<UserManagementWindow>();
// window.Show();
}
else {
dataContext.CurrentViewModel = new LoginViewModel(dataContext.AuthService, hsResolver);
}
}
};
}
});
Console.WriteLine("mainwnd");
#if DEBUG
this.AttachDevTools(new DevToolsOptions() {
ShowAsChildWindow = true,
LaunchView = DevToolsViewKind.LogicalTree,
});
#endif
PropertyChanged += (sender, args) => {
// Console.WriteLine($"MainWindow PropertyChanged: {args.Property.Name} ({args.OldValue} -> {args.NewValue})");
switch (args.Property.Name) {
case nameof(ClientSize): {
if (DataContext is not MainWindowViewModel viewModel) {
Console.WriteLine("WARN: MainWindowViewModel is null, ignoring ClientSize change!");
return;
}
// viewModel.PhysicalSize = new Size(ClientSize.Width, ClientSize.Height - TopPanel.Bounds.Height);
viewModel.PhysicalSize = new Size(ClientSize.Width, ClientSize.Height);
break;
}
}
};
// TopPanel.PropertyChanged += (_, args) => {
// if (args.Property.Name == nameof(Visual.Bounds)) {
// if (DataContext is not MainWindowViewModel viewModel) {
// Console.WriteLine("WARN: MainWindowViewModel is null, ignoring TopPanel.Bounds change!");
// return;
// }
//
// viewModel.PhysicalSize = new Size(ClientSize.Width, ClientSize.Height - TopPanel.Bounds.Height);
// }
// };
dataContext.Scale = cfg.Scale;
Width *= cfg.Scale;
Height *= cfg.Scale;
appLifetime.ApplicationStopping.Register(() => {
Console.WriteLine("ApplicationStopping triggered");
Close();
});
}
protected override void OnKeyDown(KeyEventArgs e) => OnKeyDown(this, e).ContinueWith(t => {
if (t.IsFaulted) {
Console.WriteLine("OnKeyDown faulted: " + t.Exception);
}
});
private async Task OnKeyDown(object? _, KeyEventArgs e) {
if (DataContext is not MainWindowViewModel viewModel) {
Console.WriteLine($"WARN: DataContext is {DataContext?.GetType().Name ?? "null"}, ignoring key press!");
return;
}
// Console.WriteLine("MainWindow KeyDown: " + e.Key);
if (e.Key == Key.Escape) {
viewModel.Scale = 1.0f;
}
else if (e.Key == Key.F1) {
viewModel.Scale -= 0.1f;
if (viewModel.Scale < 0.1f) {
viewModel.Scale = 0.1f;
}
}
else if (e.Key == Key.F2) {
viewModel.Scale += 0.1f;
if (viewModel.Scale > 5.0f) {
viewModel.Scale = 5.0f;
}
}
else if (e.Key == Key.Pause) {
if (viewModel.CurrentViewModel is ClientViewModel clientViewModel) {
clientViewModel.Paused = !clientViewModel.Paused;
}
}
else if (e.KeyModifiers == KeyModifiers.Control) {
if (e.Key == Key.K) {
if (viewModel.CurrentViewModel is ClientViewModel clientViewModel) {
Console.WriteLine("QuickSwitcher invoked");
}
else Console.WriteLine("WARN: CurrentViewModel is not ClientViewModel, ignoring Quick Switcher");
}
else if (e.Key == Key.U) {
Console.WriteLine("UserManagementWindow invoked");
var window = App.Current.Host.Services.GetRequiredService<UserManagementWindow>();
window.Show();
}
else if (e.Key == Key.F5) {
Console.WriteLine("Launching new process");
System.Diagnostics.Process.Start(System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName, Environment.GetCommandLineArgs());
}
else if (e.Key == Key.F9) { }
else if (e.Key == Key.D) {
List<LoginResponse> mxids = new();
var hsps = App.Current.Services.GetRequiredService<HomeserverProviderService>();
var rhs = await hsps.GetRemoteHomeserver("matrixunittests.rory.gay", enableServer: false);
for (int i = 0; i < 64; i++) {
Console.WriteLine("Debugging invoked");
var main = await rhs.RegisterAsync(Guid.NewGuid().ToString(), "password");
mxids.Add(main);
Console.WriteLine($"Registered: {main.UserId} {main.AccessToken} - {mxids.Count}");
}
foreach (var mxid in mxids) {
Console.WriteLine("Launching new process: ");
var args = (_cfg with {
Profile = "mut-" + mxid.UserId,
ProfileDirectory = null,
LoginData = mxid.ToJson(),
TestConfiguration = new() {
Mxids = mxids.Select(x => x.UserId).ToList()
}
}).Serialise();
Console.WriteLine(string.Join(' ', args));
System.Diagnostics.Process.Start(System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName, args);
}
}
}
}
}
|