Repository navigation
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
263 lines (241 loc) · 9.52 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
263 lines (241 loc) · 9.52 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Windows;
using System.Windows.Interop;
using SimRacingHub.ViewModels;
using Wpf.Ui.Controls;
using System.IO;
using System.ComponentModel;
using Newtonsoft.Json;
using SimRacingHub.Core;
namespace SimRacingHub
{
public class WindowSettings
{
public double Width { get; set; } = 1100;
public double Height { get; set; } = 700;
public double Left { get; set; } = double.NaN;
public double Top { get; set; } = double.NaN;
public WindowState WindowState { get; set; } = WindowState.Normal;
public bool IsOutputMonitorExpanded { get; set; } = false;
}
public partial class MainWindow : FluentWindow
{
private static readonly string SettingsFile = StoragePaths.WindowSettingsPath;
public MainWindow(MainViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
LoadWindowSettings(viewModel);
SteerMonitorBar.MarkerDragged += MonitorBar_MarkerDragged;
GasMonitorBar.MarkerDragged += MonitorBar_MarkerDragged;
BrakeMonitorBar.MarkerDragged += MonitorBar_MarkerDragged;
this.Closing += MainWindow_Closing;
this.Activated += (s, e) =>
{
if (DataContext is MainViewModel vm && vm.LockMouse)
{
vm.ReapplyMouseLock();
}
};
RootNavigation.SetServiceProvider(App.ServiceProvider);
Loaded += (s, e) =>
{
RootNavigation.Navigate(typeof(Views.Pages.DashboardPage));
if (viewModel.AutoStartHub)
{
viewModel.IsActive = true;
}
};
}
private void MonitorBar_MarkerDragged(string markerName, double value)
{
if (DataContext is MainViewModel vm)
{
vm.OnMonitorMarkerDragged(markerName, value);
}
}
private void LoadWindowSettings(MainViewModel viewModel)
{
try
{
if (File.Exists(SettingsFile))
{
var settings = JsonConvert.DeserializeObject<WindowSettings>(File.ReadAllText(SettingsFile));
if (settings != null)
{
Width = Math.Max(1100, settings.Width);
Height = Math.Max(600, settings.Height);
if (!double.IsNaN(settings.Left) && !double.IsNaN(settings.Top))
{
if (IsRectVisibleOnDesktop(settings.Left, settings.Top, Width, Height))
{
WindowStartupLocation = WindowStartupLocation.Manual;
Left = settings.Left;
Top = settings.Top;
}
else
{
WindowStartupLocation = WindowStartupLocation.CenterScreen;
AppLogger.Instance.LogWarning($"Saved window position ({settings.Left:F0}, {settings.Top:F0}) is outside the current desktop area, so the window was centered.");
}
}
WindowState = settings.WindowState;
if (viewModel != null)
{
viewModel.IsOutputMonitorExpanded = settings.IsOutputMonitorExpanded;
}
}
}
}
catch (Exception ex)
{
AppLogger.Instance.LogError("Failed to load window settings", ex);
}
}
private static bool IsRectVisibleOnDesktop(double left, double top, double width, double height)
{
var desktop = new Rect(
SystemParameters.VirtualScreenLeft,
SystemParameters.VirtualScreenTop,
SystemParameters.VirtualScreenWidth,
SystemParameters.VirtualScreenHeight);
var window = new Rect(left, top, Math.Max(1, width), Math.Max(1, height));
window.Intersect(desktop);
return !window.IsEmpty && window.Width >= 120 && window.Height >= 40;
}
private void MainWindow_Closing(object? sender, CancelEventArgs e)
{
try
{
var vm = DataContext as MainViewModel;
var settings = new WindowSettings
{
Width = this.RestoreBounds.Width > 0 ? this.RestoreBounds.Width : this.Width,
Height = this.RestoreBounds.Height > 0 ? this.RestoreBounds.Height : this.Height,
Left = this.RestoreBounds.Left != 0 ? this.RestoreBounds.Left : this.Left,
Top = this.RestoreBounds.Top != 0 ? this.RestoreBounds.Top : this.Top,
WindowState = this.WindowState,
IsOutputMonitorExpanded = vm?.IsOutputMonitorExpanded ?? false
};
string tempPath = SettingsFile + ".tmp";
File.WriteAllText(tempPath, JsonConvert.SerializeObject(settings, Formatting.Indented));
File.Move(tempPath, SettingsFile, overwrite: true);
}
catch (Exception ex)
{
AppLogger.Instance.LogError("Failed to save window settings", ex);
TryDeleteTempSettings();
}
finally
{
if (DataContext is MainViewModel vm)
{
vm.Shutdown();
}
}
}
private static void TryDeleteTempSettings()
{
try
{
string tempPath = SettingsFile + ".tmp";
if (File.Exists(tempPath)) File.Delete(tempPath);
}
catch
{
}
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// Pass the HWND to the ViewModel for ClipCursor
if (DataContext is MainViewModel vm)
{
var helper = new WindowInteropHelper(this);
vm.WindowHandle = helper.Handle;
}
}
private bool IsValidProfileDrag(DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files != null && files.Length > 0)
{
string ext = Path.GetExtension(files[0]).ToLowerInvariant();
return ext == ".vwh" || ext == ".json";
}
}
return false;
}
private void MainWindow_PreviewDragEnter(object sender, DragEventArgs e)
{
if (IsValidProfileDrag(e))
{
e.Effects = DragDropEffects.Copy;
DropOverlay.Visibility = Visibility.Visible;
e.Handled = true;
}
else
{
e.Effects = DragDropEffects.None;
}
}
private void MainWindow_PreviewDragOver(object sender, DragEventArgs e)
{
if (IsValidProfileDrag(e))
{
e.Effects = DragDropEffects.Copy;
if (DropOverlay.Visibility != Visibility.Visible)
{
DropOverlay.Visibility = Visibility.Visible;
}
e.Handled = true;
}
else
{
e.Effects = DragDropEffects.None;
if (DropOverlay.Visibility != Visibility.Collapsed)
{
DropOverlay.Visibility = Visibility.Collapsed;
}
}
}
private void MainWindow_PreviewDragLeave(object sender, DragEventArgs e)
{
DropOverlay.Visibility = Visibility.Collapsed;
}
private void MainWindow_PreviewDrop(object sender, DragEventArgs e)
{
DropOverlay.Visibility = Visibility.Collapsed;
if (IsValidProfileDrag(e))
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files != null && files.Length > 0)
{
string filePath = files[0];
if (DataContext is MainViewModel vm)
{
var pkg = vm.ProfileSharingService.ImportFromFile(filePath);
if (pkg != null)
{
vm.ProcessImportPackage(pkg);
}
else
{
AppLogger.Instance.LogWarning($"Failed to parse dropped preset file: {Path.GetFileName(filePath)}");
var box = new Wpf.Ui.Controls.MessageBox
{
Title = "Invalid Preset File",
Content = $"The file '{Path.GetFileName(filePath)}' could not be parsed as a valid vWheel Hub profile preset.",
CloseButtonText = "OK"
};
_ = box.ShowDialogAsync();
}
}
}
e.Handled = true;
}
}
}
}