forked from FeynmanXie/FastScreeny
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
132 lines (112 loc) · 4.36 KB
/
App.xaml.cs
File metadata and controls
132 lines (112 loc) · 4.36 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
using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows;
using WinForms = System.Windows.Forms;
using FastScreeny.Services;
namespace FastScreeny
{
public partial class App : System.Windows.Application
{
private WinForms.NotifyIcon? _notifyIcon;
private SettingsWindow? _settingsWindow;
private HotkeyManager? _hotkeyManager;
private SettingsService? _settingsService;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
_settingsService = new SettingsService();
_settingsService.Load();
_hotkeyManager = new HotkeyManager(Current);
bool runInBackground = e.Args != null && Array.Exists(e.Args, a => string.Equals(a, "--background", StringComparison.OrdinalIgnoreCase));
if (!runInBackground)
{
ShowSettings();
}
InitializeTray();
RegisterHotkeys();
if (_settingsService.Settings.LaunchOnStartup)
{
AutoStartManager.Enable();
}
else
{
AutoStartManager.Disable();
}
}
private void InitializeTray()
{
_notifyIcon = new WinForms.NotifyIcon
{
Text = "FastScreeny",
Icon = LoadIconFromResource(),
Visible = true
};
var contextMenu = new WinForms.ContextMenuStrip();
contextMenu.Items.Add("Settings", null, (s, e) => ShowSettings());
contextMenu.Items.Add("Region Screenshot", null, async (s, e) => await ScreenCaptureService.CaptureRegionToDefaultAsync(_settingsService!));
contextMenu.Items.Add("Region Screenshot (Save Directly)", null, async (s, e) => await ScreenCaptureService.CaptureRegionAndSaveAsync(_settingsService!));
contextMenu.Items.Add("Region Screenshot (Edit Mode)", null, async (s, e) => await ScreenCaptureService.CaptureRegionAndEditAsync(_settingsService!));
contextMenu.Items.Add(new WinForms.ToolStripSeparator());
contextMenu.Items.Add("Exit", null, (s, e) => ExitApplication());
_notifyIcon.ContextMenuStrip = contextMenu;
_notifyIcon.DoubleClick += (s, e) => ShowSettings();
}
private void RegisterHotkeys()
{
if (_hotkeyManager == null || _settingsService == null) return;
_hotkeyManager.UnregisterAll();
var ok = _hotkeyManager.RegisterHotkey(_settingsService.Settings.HotkeyRegion, async () =>
{
await ScreenCaptureService.CaptureRegionToDefaultAsync(_settingsService);
});
if (!ok)
{
_notifyIcon?.ShowBalloonTip(3000, "Hotkey Registration Failed", $"{_settingsService.Settings.HotkeyRegion} may be occupied by other programs or requires administrator privileges.", WinForms.ToolTipIcon.Warning);
}
}
private void ShowSettings()
{
if (_settingsWindow == null)
{
_settingsWindow = new SettingsWindow(_settingsService!, ApplySettingsChanges);
_settingsWindow.Closed += (s, e) => _settingsWindow = null;
}
_settingsWindow.Show();
_settingsWindow.Activate();
}
private void ApplySettingsChanges()
{
_settingsService!.Save();
RegisterHotkeys();
if (_settingsService.Settings.LaunchOnStartup)
AutoStartManager.Enable();
else
AutoStartManager.Disable();
}
private void ExitApplication()
{
_notifyIcon!.Visible = false;
_hotkeyManager?.Dispose();
Shutdown();
}
private Icon LoadIconFromResource()
{
try
{
var assembly = Assembly.GetExecutingAssembly();
using var stream = assembly.GetManifestResourceStream("FastScreeny.public.favicon.ico");
if (stream != null)
{
return new Icon(stream);
}
}
catch
{
// Fallback to system icon if loading fails
}
return SystemIcons.Application;
}
}
}