-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
140 lines (127 loc) · 4.8 KB
/
MainWindow.xaml.cs
File metadata and controls
140 lines (127 loc) · 4.8 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
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Interop;
using Wpf.Ui.Controls;
using Wpf.Ui.Appearance;
using ScreenshotSweeper.Views;
using System;
using System.ComponentModel;
namespace ScreenshotSweeper
{
public partial class MainWindow : FluentWindow
{
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_CLOSE = 0xF060;
public MainWindow()
{
InitializeComponent();
// Navigate to Monitor tab on startup
Loaded += MainWindow_Loaded;
SourceInitialized += MainWindow_SourceInitialized;
}
private void MainWindow_SourceInitialized(object? sender, EventArgs e)
{
// Hook into window messages to intercept close
var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
hwndSource?.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Intercept the close command (X button, Alt+F4, taskbar close)
if (msg == WM_SYSCOMMAND && (wParam.ToInt32() & 0xFFF0) == SC_CLOSE)
{
handled = true; // Prevent Windows from closing the window
HideToTray();
return IntPtr.Zero;
}
return IntPtr.Zero;
}
/// <summary>
/// Handle the TitleBar close button click event - this prevents TitleBar from calling Close()
/// </summary>
private void TitleBar_CloseClicked(TitleBar sender, RoutedEventArgs args)
{
Console.WriteLine("[MainWindow] TitleBar_CloseClicked - intercepting close, hiding to tray instead");
args.Handled = true; // Mark as handled to prevent further processing
HideToTray();
}
private void HideToTray()
{
Console.WriteLine("[MainWindow] HideToTray called - hiding window");
try
{
this.ShowInTaskbar = false;
this.WindowState = WindowState.Minimized;
this.Hide();
}
catch (Exception ex)
{
Console.WriteLine($"[MainWindow] Error hiding: {ex.Message}");
}
}
/// <summary>
/// Backup: Override OnClosing in case WndProc doesn't catch it
/// </summary>
protected override void OnClosing(CancelEventArgs e)
{
Console.WriteLine("[MainWindow] OnClosing fired - cancelling and hiding to tray");
e.Cancel = true;
HideToTray();
// Don't call base - we're cancelling
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// Navigate to Monitor tab as the default page
NavView.Navigate(typeof(MonitorTab));
Console.WriteLine("[MainWindow] Initial navigation to MonitorTab complete");
}
private void NavView_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
// Find the ScrollViewer in the current page content and scroll it
var scrollViewer = FindDescendant<ScrollViewer>(NavView);
if (scrollViewer != null)
{
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - (e.Delta / 2.0));
e.Handled = true;
}
}
private static T? FindDescendant<T>(DependencyObject parent) where T : DependencyObject
{
int childCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T found)
return found;
var result = FindDescendant<T>(child);
if (result != null)
return result;
}
return null;
}
/// <summary>
/// Restores the window from the system tray.
/// Called by TrayIconService when user double-clicks the tray icon.
/// </summary>
public void RestoreFromTray()
{
try
{
this.Show();
this.ShowInTaskbar = true;
this.WindowState = WindowState.Normal;
this.Activate();
// Temporarily set topmost to force focus
this.Topmost = true;
this.Topmost = false;
Console.WriteLine("[MainWindow] Restored from system tray");
}
catch (Exception ex)
{
Console.WriteLine($"[MainWindow] Failed to restore from tray: {ex.Message}");
}
}
}
}