forked from burkeholland/RecordMe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
506 lines (436 loc) · 17.1 KB
/
MainWindow.xaml.cs
File metadata and controls
506 lines (436 loc) · 17.1 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using RecordMe.Models;
using RecordMe.Models.Annotations;
using RecordMe.ViewModels;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
namespace RecordMe;
public partial class MainWindow : FluentWindow
{
private bool _isScrubbing;
private MainViewModel? _viewModel;
public MainWindow()
{
InitializeComponent();
// Apply system theme (light/dark) automatically
SystemThemeWatcher.Watch(this);
// Subscribe to ViewModel events when DataContext is set
DataContextChanged += OnDataContextChanged;
}
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
// Unsubscribe from old ViewModel
if (_viewModel != null)
{
_viewModel.CropResetRequested -= OnCropResetRequested;
_viewModel.AnnotationsChanged -= OnAnnotationsChanged;
_viewModel.ImageBoundsChanged -= OnImageBoundsChanged;
_viewModel.PropertyChanged -= OnViewModelPropertyChanged;
}
// Subscribe to new ViewModel
_viewModel = e.NewValue as MainViewModel;
if (_viewModel != null)
{
_viewModel.CropResetRequested += OnCropResetRequested;
_viewModel.AnnotationsChanged += OnAnnotationsChanged;
_viewModel.ImageBoundsChanged += OnImageBoundsChanged;
_viewModel.PropertyChanged += OnViewModelPropertyChanged;
}
}
private void OnViewModelPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// Update crop control position when the image changes
if (e.PropertyName == nameof(MainViewModel.CurrentFrameImage) ||
e.PropertyName == nameof(MainViewModel.ShowCropOverlay) ||
e.PropertyName == nameof(MainViewModel.ShowAnnotationCanvas))
{
// Defer the update to ensure WPF binding has updated the Image.Source first
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, () =>
{
UpdateCropControlBounds();
UpdateAnnotationCanvasBounds();
// Also refresh annotations after bounds are updated
AnnotationCanvasControl?.RefreshAnnotations();
});
}
}
private void OnCropResetRequested(object? sender, EventArgs e)
{
// Reset the crop control to full image bounds
CropControl?.ResetCropRect();
}
private void OnImageBoundsChanged(object? sender, MainViewModel.ImageDimensionsEventArgs e)
{
// Synchronously update canvas bounds when image dimensions change (e.g., after crop)
// Use the dimensions passed in the event since binding hasn't updated yet
UpdateAnnotationCanvasBoundsWithDimensions(e.Width, e.Height);
}
private void OnAnnotationsChanged(object? sender, EventArgs e)
{
// Refresh the annotation canvas when annotations change
// Use DispatcherPriority.Render to match the property changed handler timing
// and ensure bounds are updated before refreshing
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, () =>
{
UpdateAnnotationCanvasBounds();
AnnotationCanvasControl?.RefreshAnnotations();
});
}
private void ImagePreviewContainer_SizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateCropControlBounds();
UpdateAnnotationCanvasBounds();
}
/// <summary>
/// Updates the annotation canvas's size and position to match the actual rendered image bounds.
/// Also calculates the scale factors for coordinate transformation.
/// </summary>
private void UpdateAnnotationCanvasBounds()
{
if (EditorImage.Source is not BitmapSource bitmapSource) return;
UpdateAnnotationCanvasBoundsWithDimensions(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
}
/// <summary>
/// Updates the annotation canvas with explicit image dimensions.
/// Use this when the binding hasn't updated yet (e.g., during crop).
/// </summary>
private void UpdateAnnotationCanvasBoundsWithDimensions(int imageWidth, int imageHeight)
{
double containerWidth = ImagePreviewContainer.ActualWidth;
double containerHeight = ImagePreviewContainer.ActualHeight;
if (containerWidth <= 0 || containerHeight <= 0) return;
if (imageWidth <= 0 || imageHeight <= 0) return;
// Calculate the rendered size of the image with Stretch="Uniform"
// but capped at the image's actual dimensions
double maxRenderWidth = Math.Min(containerWidth, imageWidth);
double maxRenderHeight = Math.Min(containerHeight, imageHeight);
// CRITICAL: Cast to double to avoid integer division!
double imageAspect = (double)imageWidth / (double)imageHeight;
double containerAspect = maxRenderWidth / maxRenderHeight;
double renderedWidth, renderedHeight;
if (imageAspect > containerAspect)
{
// Image is wider - constrained by width
renderedWidth = maxRenderWidth;
renderedHeight = maxRenderWidth / imageAspect;
}
else
{
// Image is taller - constrained by height
renderedHeight = maxRenderHeight;
renderedWidth = maxRenderHeight * imageAspect;
}
// Center the annotation canvas over the rendered image
double offsetX = (containerWidth - renderedWidth) / 2;
double offsetY = (containerHeight - renderedHeight) / 2;
// Position and size the annotation canvas to match rendered image bounds
AnnotationCanvasControl.Margin = new Thickness(offsetX, offsetY, 0, 0);
AnnotationCanvasControl.Width = renderedWidth;
AnnotationCanvasControl.Height = renderedHeight;
// Set scale factors for coordinate transformation
AnnotationCanvasControl.ImageScaleX = renderedWidth / imageWidth;
AnnotationCanvasControl.ImageScaleY = renderedHeight / imageHeight;
}
/// <summary>
/// Updates the crop control's size and position to match the actual rendered image bounds.
/// Since the Image uses Stretch="Uniform", it may have letterboxing, and we need
/// the crop control to only cover the actual image area.
/// Also limits the image display to its actual pixel dimensions.
/// </summary>
private void UpdateCropControlBounds()
{
if (EditorImage.Source is not BitmapSource bitmapSource) return;
double containerWidth = ImagePreviewContainer.ActualWidth;
double containerHeight = ImagePreviewContainer.ActualHeight;
if (containerWidth <= 0 || containerHeight <= 0) return;
double imageWidth = bitmapSource.PixelWidth;
double imageHeight = bitmapSource.PixelHeight;
if (imageWidth <= 0 || imageHeight <= 0) return;
// Limit the image to its actual pixel dimensions - don't stretch beyond original size
EditorImage.MaxWidth = imageWidth;
EditorImage.MaxHeight = imageHeight;
// Calculate the rendered size of the image with Stretch="Uniform"
// but capped at the image's actual dimensions
double maxRenderWidth = Math.Min(containerWidth, imageWidth);
double maxRenderHeight = Math.Min(containerHeight, imageHeight);
double imageAspect = imageWidth / imageHeight;
double containerAspect = maxRenderWidth / maxRenderHeight;
double renderedWidth, renderedHeight;
if (imageAspect > containerAspect)
{
// Image is wider - constrained by width
renderedWidth = maxRenderWidth;
renderedHeight = maxRenderWidth / imageAspect;
}
else
{
// Image is taller - constrained by height
renderedHeight = maxRenderHeight;
renderedWidth = maxRenderHeight * imageAspect;
}
// Center the crop control over the rendered image
double offsetX = (containerWidth - renderedWidth) / 2;
double offsetY = (containerHeight - renderedHeight) / 2;
// Position and size the crop control to match rendered image bounds
CropControl.Margin = new Thickness(offsetX, offsetY, 0, 0);
CropControl.Width = renderedWidth;
CropControl.Height = renderedHeight;
// Reset crop rect since the bounds changed
CropControl.ResetCropRect();
}
private void Segment_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender is FrameworkElement element && element.Tag is int segmentIndex)
{
if (DataContext is MainViewModel vm)
{
vm.SelectSegment(segmentIndex);
}
}
// Focus the timeline card so Delete key works
TimelineCard.Focus();
// Prevent event from bubbling to timeline track (which would start scrubbing)
e.Handled = true;
}
private void TimelineCard_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (DataContext is MainViewModel vm && vm.DeleteSelectedSegmentCommand.CanExecute(null))
{
vm.DeleteSelectedSegmentCommand.Execute(null);
e.Handled = true;
}
}
}
private void LibraryItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender is FrameworkElement element && element.Tag is LibraryItem item)
{
if (DataContext is MainViewModel vm)
{
vm.SelectLibraryItem(item);
}
}
}
// Timeline track size changed - update ViewModel with new width
private void TimelineTrack_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (DataContext is MainViewModel vm)
{
vm.TimelineWidth = e.NewSize.Width;
}
}
// Mouse scrubbing on timeline track
private void TimelineTrack_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (DataContext is MainViewModel vm)
{
_isScrubbing = true;
TimelineTrack.CaptureMouse();
vm.StartDragging();
var position = e.GetPosition(TimelineTrack);
vm.ScrubToPosition(position.X);
}
}
private void TimelineTrack_MouseMove(object sender, MouseEventArgs e)
{
if (_isScrubbing && DataContext is MainViewModel vm)
{
var position = e.GetPosition(TimelineTrack);
vm.ScrubToPosition(position.X);
}
}
private void TimelineTrack_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_isScrubbing)
{
_isScrubbing = false;
TimelineTrack.ReleaseMouseCapture();
if (DataContext is MainViewModel vm)
{
vm.EndDragging();
}
}
}
// Direct playhead grab - starts scrubbing from playhead position
private void Playhead_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (DataContext is MainViewModel vm)
{
_isScrubbing = true;
TimelineTrack.CaptureMouse();
vm.StartDragging();
// Don't update position here - user is grabbing playhead where it is
e.Handled = true;
}
}
// Crop control event handler
private void CropControl_CropRectChanged(object? sender, Rect e)
{
if (DataContext is MainViewModel vm)
{
vm.UpdateCropRect(e);
}
}
// Shape tool button click handlers
private void ShapeSelectButton_Click(object sender, RoutedEventArgs e)
{
if (DataContext is MainViewModel vm)
{
vm.EnterSelectModeCommand.Execute(null);
}
}
private void ShapeRectangleButton_Click(object sender, RoutedEventArgs e)
{
if (DataContext is MainViewModel vm)
{
vm.SelectShapeTypeCommand.Execute(ShapeType.Rectangle);
}
}
private void ShapeEllipseButton_Click(object sender, RoutedEventArgs e)
{
if (DataContext is MainViewModel vm)
{
vm.SelectShapeTypeCommand.Execute(ShapeType.Ellipse);
}
}
private void ShapeLineButton_Click(object sender, RoutedEventArgs e)
{
if (DataContext is MainViewModel vm)
{
vm.SelectShapeTypeCommand.Execute(ShapeType.Line);
}
}
private void ShapeArrowButton_Click(object sender, RoutedEventArgs e)
{
if (DataContext is MainViewModel vm)
{
vm.SelectShapeTypeCommand.Execute(ShapeType.Arrow);
}
}
// Color preset click handler
private void ColorPreset_Click(object sender, RoutedEventArgs e)
{
// Handle both standard Button and ui:Button
string? colorHex = null;
if (sender is System.Windows.Controls.Button button)
{
colorHex = button.Tag as string;
}
else if (sender is Wpf.Ui.Controls.Button uiButton)
{
colorHex = uiButton.Tag as string;
}
if (colorHex != null && DataContext is MainViewModel vm)
{
try
{
var color = (Color)ColorConverter.ConvertFromString(colorHex);
vm.SetShapeStrokeColorCommand.Execute(color);
vm.SyncHsvFromColor(color); // Sync the HSV sliders
}
catch
{
// Ignore invalid color strings
}
}
}
// Color picker button opens the popup
private void ColorPickerButton_Click(object sender, RoutedEventArgs e)
{
ColorPickerPopup.IsOpen = true;
}
// HSV slider changed - update color from HSV values
private void ColorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (DataContext is MainViewModel vm)
{
vm.UpdateColorFromHsv();
}
}
// Stroke thickness button opens the popup
private void StrokeThicknessButton_Click(object sender, RoutedEventArgs e)
{
StrokeThicknessPopup.IsOpen = true;
}
// Stroke thickness slider in popup changed - also updates selected shape
private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (DataContext is MainViewModel vm)
{
vm.SetShapeStrokeThicknessCommand.Execute(e.NewValue);
}
}
// Stroke thickness button click handler (for preset buttons)
private void StrokeThickness_Click(object sender, RoutedEventArgs e)
{
string? thicknessStr = null;
if (sender is System.Windows.Controls.Button button)
{
thicknessStr = button.Tag as string;
}
else if (sender is Wpf.Ui.Controls.Button uiButton)
{
thicknessStr = uiButton.Tag as string;
}
if (thicknessStr != null &&
double.TryParse(thicknessStr, out double thickness) &&
DataContext is MainViewModel vm)
{
vm.SetShapeStrokeThicknessCommand.Execute(thickness);
}
}
// Stroke thickness slider changed (kept for compatibility)
private void StrokeThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (DataContext is MainViewModel vm)
{
vm.SetShapeStrokeThicknessCommand.Execute(e.NewValue);
}
}
// Annotation canvas event handlers
private void AnnotationCanvasControl_AnnotationAdded(object? sender, ShapeAnnotation e)
{
if (DataContext is MainViewModel vm)
{
vm.OnAnnotationAdded(e);
}
// Focus the container so keyboard shortcuts work
ImagePreviewContainer.Focus();
}
private void AnnotationCanvasControl_AnnotationSelected(object? sender, ShapeAnnotation? e)
{
if (DataContext is MainViewModel vm)
{
vm.OnAnnotationSelected(e);
}
// Focus the container so keyboard shortcuts work
if (e != null)
{
ImagePreviewContainer.Focus();
}
}
private void AnnotationCanvasControl_AnnotationModified(object? sender, ShapeAnnotation e)
{
if (DataContext is MainViewModel vm)
{
vm.OnAnnotationModified(e);
}
}
// Image preview keyboard handling for annotation delete
private void ImagePreviewContainer_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (DataContext is MainViewModel vm && vm.HasSelectedAnnotation)
{
vm.DeleteSelectedAnnotationCommand.Execute(null);
e.Handled = true;
}
}
}
}