about summary refs log tree commit diff
path: root/ModerationClient/Views/MainWindow.axaml.cs
blob: ccabd71dc5aee9bbbc16022b65b53eff4b517cea (plain) (blame)
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
using System;
using System.Threading;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Diagnostics;
using Avalonia.Input;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModerationClient.Services;
using ModerationClient.ViewModels;

namespace ModerationClient.Views;

public partial class MainWindow : Window {
    //viewmodel
    private MainWindowViewModel? _viewModel { get; set; }

    public MainWindow(CommandLineConfiguration cfg, MainWindowViewModel dataContext, IHostApplicationLifetime appLifetime) {
        InitializeComponent();
        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(Height):
                case nameof(Width): {
                    if (_viewModel is null) {
                        Console.WriteLine("WARN: MainWindowViewModel is null, ignoring height/width change!");
                        return;
                    }

                    // Console.WriteLine("height/width changed");
                    _viewModel.Scale = _viewModel.Scale;
                    break;
                }
            }
        };
        DataContext = _viewModel = dataContext;
        _ = dataContext.AuthService.LoadProfileAsync();
        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>();
                }
                else {
                    dataContext.CurrentViewModel = new LoginViewModel(dataContext.AuthService);
                }
            }
        };
        dataContext.MainWindow = this;
        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);

    private void OnKeyDown(object? _, KeyEventArgs e) {
        if (_viewModel is null) {
            Console.WriteLine("WARN: MainWindowViewModel is 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;
            }
        }
    }
}