This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
95 lines (83 loc) · 3.09 KB
/
MainWindow.xaml.cs
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
// License: Apache-2.0
/*
* MainWindow.xaml.cs: Back-end source code for main window
*
* (C) Copyright 2024 Lithicsoft Organization
* Author: Bui Nguyen Tan Sang <[email protected]>
*/
using Lithicsoft_Trainer_Studio.UserControls;
using Lithicsoft_Trainer_Studio.UserControls.Pages;
using System.Windows;
using System.Windows.Controls;
using MessageBox = ModernWpf.MessageBox;
namespace Lithicsoft_Trainer_Studio
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void MainWindows_Loaded(object sender, RoutedEventArgs e)
{
try
{
this.WindowStyle = WindowStyle.None;
Startup startup = new();
startup.UserControlClosed += Startup_UserControlClosed;
MainStackPanel.Children.Add(startup);
this.SizeToContent = SizeToContent.WidthAndHeight;
await WaitForUserControlToCloseAsync(startup);
MainStackPanel.Children.Clear();
this.WindowStyle = WindowStyle.SingleBorderWindow;
MainPage mainPage = new();
MainStackPanel.Children.Add(mainPage);
}
catch (Exception ex)
{
MessageBox.Show($"Error starting studio: {ex.Message}", "Exception Error", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(1);
}
}
private void Startup_UserControlClosed(object sender, EventArgs e)
{
if (sender is UserControl userControl)
{
MainStackPanel.Children.Remove(userControl);
}
}
private Task<bool> WaitForUserControlToCloseAsync(Startup userControl)
{
var tcs = new TaskCompletionSource<bool>();
void handler(object? s, EventArgs e)
{
userControl.UserControlClosed -= handler;
tcs.SetResult(true);
}
userControl.UserControlClosed += handler;
return tcs.Task;
}
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (Creator.Instance.isCreating)
{
MessageBox.Show("You can't not exit Trainer Studio while creating project!", "Prevent Closing", MessageBoxButton.OK, MessageBoxImage.Stop);
e.Cancel = true;
}
else
{
if (this.Visibility != Visibility.Hidden)
{
var result = MessageBox.Show("Do you want to exit Trainer Studio?", "Close Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
System.Windows.Application.Current.Shutdown();
else if (result == MessageBoxResult.No)
e.Cancel = true;
}
}
}
}
}