Prechádzať zdrojové kódy

Changed reconnection window to be better

Kenric Nugteren 1 rok pred
rodič
commit
fc01414a5d

+ 53 - 0
prs.desktop/Forms/ReconnectionWindow.xaml

@@ -0,0 +1,53 @@
+<Window x:Class="PRSDesktop.Forms.ReconnectionWindow"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:local="clr-namespace:PRSDesktop.Forms"
+        mc:Ignorable="d"
+        WindowStartupLocation="CenterScreen"
+        WindowStyle="None"
+        BorderThickness="0"
+        BorderBrush="Transparent"
+        AllowsTransparency="True"
+        Background="Transparent"
+        MouseDown="Window_MouseDown"
+        Width="350" Height="200"
+        Title="Reconnecting">
+    <Border CornerRadius="10" BorderBrush="Gray" BorderThickness="0.75" Padding="5,5,5,0" Background="White">
+        <Grid Margin="5">
+            <Grid.RowDefinitions>
+                <RowDefinition Height="Auto"/>
+                <RowDefinition Height="*"/>
+                <RowDefinition Height="Auto"/>
+            </Grid.RowDefinitions>
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition Width="*"/>
+                <ColumnDefinition Width="Auto"/>
+            </Grid.ColumnDefinitions>
+            <Image Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="2"
+                   x:Name="Splash" Margin="20,10,20,10" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
+            <Button x:Name="CloseButton" Content="Exit PRS"
+                    Grid.Row="0" Grid.Column="1"
+                    Background="LightYellow" Padding="5" Margin="5"
+                    Click="CloseButton_Click">
+                <Button.Style>
+                    <Style TargetType="Button">
+                        <Style.Resources>
+                            <Style TargetType="Border">
+                                <Setter Property="CornerRadius" Value="10"/>
+                            </Style>
+                        </Style.Resources>
+                    </Style>
+                </Button.Style>
+            </Button>
+
+            <Label Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
+                   VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
+                   HorizontalContentAlignment="Center"
+                   FontSize="14" FontStyle="Oblique"
+                   Margin="0"
+                   Content="Connection lost; attempting to reconnect ..."/>
+        </Grid>
+    </Border>
+</Window>

+ 46 - 0
prs.desktop/Forms/ReconnectionWindow.xaml.cs

@@ -0,0 +1,46 @@
+using InABox.WPF;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+
+namespace PRSDesktop.Forms;
+/// <summary>
+/// Interaction logic for ReconnectionWindow.xaml
+/// </summary>
+public partial class ReconnectionWindow : Window
+{
+    public bool Cancelled { get; set; } = false;
+
+    public Action? OnCancelled { get; set; }
+
+    public ReconnectionWindow()
+    {
+        InitializeComponent();
+
+        Splash.Source = Progress.DisplayImage;
+    }
+
+    private void CloseButton_Click(object sender, RoutedEventArgs e)
+    {
+        Cancelled = true;
+        OnCancelled?.Invoke();
+    }
+
+    private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
+    {
+        if(e.ChangedButton == System.Windows.Input.MouseButton.Left)
+        {
+            DragMove();
+        }
+    }
+}

+ 22 - 6
prs.desktop/MainWindow.xaml.cs

@@ -63,6 +63,7 @@ using SortDirection = InABox.Core.SortDirection;
 using PRSDesktop.Components.Spreadsheet;
 using InABox.Wpf.Reports;
 using Comal.Classes.SecurityDescriptors;
+using System.Threading;
 
 namespace PRSDesktop;
 
@@ -462,15 +463,19 @@ public partial class MainWindow : IPanelHostControl
         {
             Dispatcher.Invoke(() =>
             {
-                var cancelled = false;
-                var success = Progress.ShowModal("Reconnecting", "Exit PRS", (progress, ct) =>
+                var reconnection = new ReconnectionWindow();
+                var cancellationTokenSource = new CancellationTokenSource();
+
+                reconnection.OnCancelled = () => cancellationTokenSource.Cancel();
+
+                var ct = cancellationTokenSource.Token;
+                var work = () =>
                 {
                     try
                     {
                         DateTime lost = DateTime.Now;
                         while (!client.IsConnected() && !ct.IsCancellationRequested)
                         {
-                            progress.Report($"Connection lost - ({(DateTime.Now - lost):hh\\:mm})");
                             try
                             {
                                 Logger.Send(LogType.Error, ClientFactory.UserID, "Reconnecting - ({0:hh\\:mm})", DateTime.Now - lost);
@@ -486,7 +491,6 @@ public partial class MainWindow : IPanelHostControl
                                 // TODO: Remove this suppression
                                 if (e1.Message.StartsWith("The socket is connected, you needn't connect again!"))
                                 {
-                                    cancelled = true;
                                     break;
                                 }
                             }
@@ -496,14 +500,26 @@ public partial class MainWindow : IPanelHostControl
                             Logger.Send(LogType.Information, ClientFactory.UserID, "Reconnected");
                             ClientFactory.Validate(ClientFactory.SessionID);
                             Logger.Send(LogType.Information, ClientFactory.UserID, "Validated");
+                            return true;
                         }
                     }
                     catch (Exception e)
                     {
                         Logger.Send(LogType.Error, ClientFactory.UserID, $"Reconnect Failed: {e.Message}");
                     }
-                });
-                if (!success || cancelled)
+                    return false;
+                };
+                var task = Task.Run(() =>
+                {
+                    var result = work();
+                    Dispatcher.Invoke(() =>
+                    {
+                        reconnection.Close();
+                    });
+                    return result;
+                }, ct);
+                reconnection.ShowDialog();
+                if (!task.Result)
                 {
                     Close();
                 }