about summary refs log tree commit diff
path: root/ModerationClient/Views/MainWindow
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-07-29 22:45:36 +0200
committerRory& <root@rory.gay>2024-08-08 03:02:10 +0200
commit03f1669d98e1fad81bc4832900ae149ac6510ebc (patch)
tree625361563a92452297f3e2700946e51e513e78b9 /ModerationClient/Views/MainWindow
downloadModerationClient-03f1669d98e1fad81bc4832900ae149ac6510ebc.tar.xz
Initial commit
Diffstat (limited to '')
-rw-r--r--ModerationClient/Views/MainWindow.axaml42
-rw-r--r--ModerationClient/Views/MainWindow.axaml.cs93
2 files changed, 135 insertions, 0 deletions
diff --git a/ModerationClient/Views/MainWindow.axaml b/ModerationClient/Views/MainWindow.axaml
new file mode 100644

index 0000000..1c2b396 --- /dev/null +++ b/ModerationClient/Views/MainWindow.axaml
@@ -0,0 +1,42 @@ +<Window xmlns="https://github.com/avaloniaui" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:vm="using:ModerationClient.ViewModels" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:views="clr-namespace:ModerationClient.Views" + mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" + x:Class="ModerationClient.Views.MainWindow" + x:DataType="vm:MainWindowViewModel" + Icon="/Assets/avalonia-logo.ico" + Title="ModerationClient" + Width="640" Height="480"> + + <Design.DataContext> + <!-- This only sets the DataContext for the previewer in an IDE, + to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) --> + <vm:MainWindowViewModel /> + </Design.DataContext> + + <StackPanel> + <Grid ColumnDefinitions="Auto, *, Auto"> + <StackPanel Orientation="Horizontal" Grid.Column="0"> + <Label Content="{CompiledBinding Scale}" /> + <Label>x</Label> + <Rectangle Width="32" /> + <Label Content="{CompiledBinding ChildTargetWidth}" /> + <Label>x</Label> + <Label Content="{CompiledBinding ChildTargetHeight}" /> + </StackPanel> + <Label Grid.Column="2">Press '?' for keybinds</Label> + </Grid> + <Viewbox> + <ContentControl + Width="{CompiledBinding ChildTargetWidth}" + Height="{CompiledBinding ChildTargetHeight}" + Background="#222222" + Content="{CompiledBinding CurrentViewModel}" /> + </Viewbox> + + </StackPanel> + +</Window> \ No newline at end of file diff --git a/ModerationClient/Views/MainWindow.axaml.cs b/ModerationClient/Views/MainWindow.axaml.cs new file mode 100644
index 0000000..ccabd71 --- /dev/null +++ b/ModerationClient/Views/MainWindow.axaml.cs
@@ -0,0 +1,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; + } + } + } +} \ No newline at end of file