Browse Source

updated message window

Veloe 2 years ago
parent
commit
aad8354793

+ 2 - 2
VeloeMinecraftLauncher/VeloeMinecraftLauncher.csproj

@@ -10,8 +10,8 @@
     <DebugType>embedded</DebugType>
     <StartupObject>VeloeMinecraftLauncher.Program</StartupObject>
     <PlatformTarget>x64</PlatformTarget>
-    <AssemblyVersion>1.2.2.164</AssemblyVersion>
-    <FileVersion>1.2.2.164</FileVersion>
+    <AssemblyVersion>1.2.2.197</AssemblyVersion>
+    <FileVersion>1.2.2.197</FileVersion>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
     <NoWarn>NU1605</NoWarn>

+ 53 - 13
VeloeMinecraftLauncher/ViewModels/ErrorWindowViewModel.cs → VeloeMinecraftLauncher/ViewModels/MessageWindowViewModel.cs

@@ -1,18 +1,23 @@
-using Avalonia;
-using Avalonia.Controls;
-using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Controls;
 using Avalonia.Layout;
 using ReactiveUI;
 using System.IO;
-using System.Security.Principal;
 
 namespace VeloeMinecraftLauncher.ViewModels;
 
-public class ErrorWindowViewModel : ViewModelBase
+public enum MessageBoxButtons
+{
+    Ok,
+    OkCancel,
+    YesNo
+}
+public class MessageWindowViewModel : ViewModelBase
 { 
-    public ErrorWindowViewModel()
+    public MessageWindowViewModel()
     {
-        ErrorMessage = "Error";
+        SetButtons(MessageBoxButtons.Ok);
+        Title = "Message";
+        Message = "Error";
         ErrorTrace = "ErrorTrace\n1\n2\n3\n4\n5";
         IsErrorTraceVisible = false;
         ButtonText = "Open log";
@@ -20,9 +25,11 @@ public class ErrorWindowViewModel : ViewModelBase
         _logfile = "";
     }
 
-    public ErrorWindowViewModel(string errorMessage, string errorTrace = "", bool centerMessage = false, string logfile = "")
+    public MessageWindowViewModel(string message, string title, MessageBoxButtons buttons = MessageBoxButtons.Ok, string errorTrace = "", string logfile = "")
     {
-        ErrorMessage = errorMessage;
+        SetButtons(buttons);
+        Title = title;
+        Message = message;
         ErrorTrace = errorTrace;
         if (string.IsNullOrEmpty(errorTrace))
         {
@@ -45,7 +52,7 @@ public class ErrorWindowViewModel : ViewModelBase
 
     }
 
-    private string _errorMessage = string.Empty;
+    private string _message = string.Empty;
     private string _errorTrace = string.Empty;
     private bool _isErrorTraceVisible = false;
     private bool _centerMessage = false;
@@ -57,10 +64,10 @@ public class ErrorWindowViewModel : ViewModelBase
         set => this.RaiseAndSetIfChanged(ref _errorTrace, value);
     }
 
-    public string ErrorMessage
+    public string Message
     {
-        get => _errorMessage;
-        set => this.RaiseAndSetIfChanged(ref _errorMessage, value);
+        get => _message;
+        set => this.RaiseAndSetIfChanged(ref _message, value);
     }
 
     public bool CenterMessage
@@ -85,13 +92,38 @@ public class ErrorWindowViewModel : ViewModelBase
         get => CenterMessage ? VerticalAlignment.Center : VerticalAlignment.Stretch;
     }
 
+    public string Title { get; set; } = "Message";
     public string ButtonText { get; set; } = string.Empty;
+    public string ButtonOk { get; set; } = "Ok";
+    public string ButtonCancel { get; set; } = "Cancel";
+    public bool IsCancelButtonVisible { get; set; } = false;
 
     public bool IsLogButtonVisible
     {
         get => !string.IsNullOrEmpty(_logfile);
     }
 
+    private void SetButtons(MessageBoxButtons buttons)
+    {
+        switch (buttons)
+        {
+            case MessageBoxButtons.Ok:
+                ButtonOk = "Ok";
+                IsCancelButtonVisible = false;
+                break;
+            case MessageBoxButtons.OkCancel:
+                ButtonOk = "Ok";
+                ButtonCancel = "Cancel";
+                IsCancelButtonVisible = true;
+                break;
+            case MessageBoxButtons.YesNo:
+                ButtonOk = "Yes";
+                ButtonCancel = "No";
+                IsCancelButtonVisible = true;
+                break;
+        }
+    }
+
     public void OpenLogFile()
     {
         if (Path.Exists(_logfile))
@@ -105,5 +137,13 @@ public class ErrorWindowViewModel : ViewModelBase
         window?.Close();
     }
 
+    public void CloseWindowOk(Window window)
+    {
+        window?.Close(true);
+    }
 
+    public void CloseWindowCancel(Window window)
+    {
+        window?.Close(false);
+    }
 }

+ 7 - 5
VeloeMinecraftLauncher/ViewModels/SettingsWindowViewModel.cs

@@ -10,7 +10,9 @@ using System.Collections.ObjectModel;
 using Serilog.Events;
 using System.Linq;
 using ReactiveUI.Validation.Components;
-using Microsoft.CodeAnalysis.CSharp.Syntax;
+using VeloeMinecraftLauncher.Views;
+using Avalonia.Threading;
+using System.Runtime;
 
 namespace VeloeMinecraftLauncher.ViewModels;
 
@@ -179,9 +181,9 @@ public class SettingsWindowViewModel : ViewModelBase
 
     public void OpenMinecraftPathDialog(Window window)
     {
-        Task.Run(() =>
+        Task.Run(async() =>
         {
-            OpenFolderDialog dialog = new OpenFolderDialog();
+            var dialog = new OpenFolderDialog();
 
             var initPath = String.Empty;
 
@@ -191,10 +193,10 @@ public class SettingsWindowViewModel : ViewModelBase
 
             dialog.Directory = initPath;
 
-            var result = dialog.ShowAsync(window).Result;
+            var result = await dialog.ShowAsync(window);
             if (result is null)
                 return;
-
+            
             if (result == String.Empty)
                 return;
 

+ 15 - 5
VeloeMinecraftLauncher/ViewModels/ViewModelBase.cs

@@ -1,15 +1,19 @@
 using Avalonia;
+using Avalonia.Controls;
 using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Media;
 using Avalonia.Threading;
+using Avalonia.VisualTree;
 using ReactiveUI;
 using ReactiveUI.Validation.Helpers;
 using System;
 using System.ComponentModel;
 using System.IO;
+using System.Linq;
 using System.Net;
 using VeloeMinecraftLauncher.MinecraftLauncher;
 using VeloeMinecraftLauncher.Views;
+using VeloeMinecraftLauncher.Views.TitleBar;
 
 namespace VeloeMinecraftLauncher.ViewModels;
 
@@ -64,7 +68,7 @@ public class ViewModelBase : ReactiveValidationObject
                 break;
             default:
                 message = ex.Message;
-                stackTrace = ex.StackTrace ?? "";
+                stackTrace = ex.StackTrace ?? string.Empty;
                 Settings.logger.Error(ex.Message);
                 if (ex.StackTrace is not null)
                     Settings.logger.Error(ex.StackTrace);
@@ -88,11 +92,14 @@ public class ViewModelBase : ReactiveValidationObject
             {
                 Dispatcher.UIThread.InvokeAsync(() =>
                 {
-                    var ErrorMessageViewModel = new ErrorWindowViewModel(message, stackTrace, logfile:logfile);
-                    var ErrorMessage = new ErrorWindow()
+                    var ErrorMessageViewModel = new MessageWindowViewModel(message, "Error message", MessageBoxButtons.Ok, stackTrace, logfile:logfile);
+                    var ErrorMessage = new MessageWindow()
                     {
                         DataContext = ErrorMessageViewModel
                     };
+                    //TODO change it to binding
+                    ErrorMessage.FindControl<TitleBarWindow>("titleBar").TitleText = "Error Message";
+
                     ErrorMessage.ShowDialog(desktop.MainWindow);
                 });
             }
@@ -109,11 +116,14 @@ public class ViewModelBase : ReactiveValidationObject
             {
                 Dispatcher.UIThread.InvokeAsync(() =>
                 {
-                    var ErrorMessageViewModel = new ErrorWindowViewModel(message, logfile: logfile);
-                    var ErrorMessage = new ErrorWindow()
+                    var ErrorMessageViewModel = new MessageWindowViewModel(message, "Error message", logfile: logfile);
+                    var ErrorMessage = new MessageWindow()
                     {
                         DataContext = ErrorMessageViewModel
                     };
+                    //TODO change it to binding
+                    ErrorMessage.GetControl<TitleBarWindow>("titleBar").TitleText = "Error Message";
+                    
                     ErrorMessage.ShowDialog(desktop.MainWindow);
                 });
             }

+ 20 - 11
VeloeMinecraftLauncher/Views/ErrorWindow.axaml → VeloeMinecraftLauncher/Views/MessageWindow.axaml

@@ -6,7 +6,7 @@
         mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="150"
 		Width = "400" MinHeight="150"
 		SizeToContent="Height"
-        x:Class="VeloeMinecraftLauncher.Views.ErrorWindow"
+        x:Class="VeloeMinecraftLauncher.Views.MessageWindow"
 		xmlns:titlebars="using:VeloeMinecraftLauncher.Views.TitleBar"
         Icon="/Assets/avalonia-logo.ico"
         Title="Error"
@@ -18,7 +18,7 @@
 		ExtendClientAreaTitleBarHeightHint="-1">
 
 	<Design.DataContext>
-		<vm:ErrorWindowViewModel/>
+		<vm:MessageWindowViewModel/>
 	</Design.DataContext>
 
 	<Panel>
@@ -36,8 +36,10 @@
 			  IsSeamless="False"
 			  IsIconVisible="False"
 			  IsMaximizeVisible="False"
-			  TitleText="Error Message"
-			  DockPanel.Dock="Top">
+			  TitleText="Message"
+			  DockPanel.Dock="Top"
+			  x:Name="titleBar"
+			>
 			</titlebars:TitleBarWindow>
 			<Grid
 				DockPanel.Dock="Bottom"
@@ -50,7 +52,7 @@
 
 					<TextBlock
 						Grid.Row="0"
-						Text="{Binding ErrorMessage}" 
+						Text="{Binding Message}" 
 						TextWrapping="Wrap"
 						HorizontalAlignment="{Binding HorizontalAlignment}"
 						VerticalAlignment="{Binding	VerticalAlignment}"
@@ -74,17 +76,24 @@
 						Content="{Binding ButtonText}"
 						IsVisible="{Binding	IsLogButtonVisible}"
 						Command="{Binding OpenLogFile}"
-					>				
-					</Button>
+					/>
 					<Button
-						Content="Ok"
+						Content="{Binding ButtonCancel}"
 						IsCancel="True"
+						IsVisible="{Binding IsCancelButtonVisible}"
+						Width="65"
+						Margin="10 0 0 0"
+						Command="{Binding CloseWindowCancel}"
+						CommandParameter="{Binding $parent[Window]}"/>
+					<Button
+						Content="{Binding ButtonOk}"
+						IsDefault="True"
+						IsCancel="{Binding !IsCancelButtonVisible}"
 						Width="40"
 						Margin="10 0 0 0"
-						Command="{Binding CloseWindow}" 
+						Command="{Binding CloseWindowOk}" 
 						CommandParameter="{Binding $parent[Window]}"
-					>				
-					</Button>
+					/>
 				</StackPanel>	
 			</Grid>
 										

+ 2 - 2
VeloeMinecraftLauncher/Views/ErrorWindow.axaml.cs → VeloeMinecraftLauncher/Views/MessageWindow.axaml.cs

@@ -2,9 +2,9 @@ using Avalonia.Controls;
 
 namespace VeloeMinecraftLauncher.Views
 {
-    public partial class ErrorWindow : Window
+    public partial class MessageWindow : Window
     {
-        public ErrorWindow()
+        public MessageWindow()
         {
             InitializeComponent();
         }