-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
146 lines (127 loc) · 4.53 KB
/
Copy pathApp.xaml.cs
File metadata and controls
146 lines (127 loc) · 4.53 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
using Microsoft.Win32;
using ScrollV.Core;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Threading;
namespace ScrollV
{
public partial class App : Application
{
private ScrollVManager? _manager;
private DispatcherTimer? _memoryTimer;
// Win32 API for memory management
[DllImport("kernel32.dll")]
private static extern bool SetProcessWorkingSetSize(IntPtr hProcess, IntPtr dwMinimumWorkingSetSize, IntPtr dwMaximumWorkingSetSize);
[DllImport("kernel32.dll")]
private static extern IntPtr GetCurrentProcess();
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Set high priority immediately to reduce input delay
try
{
System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.High;
}
catch { }
// Initialize manager
_manager = new ScrollVManager();
// First-run: Enable "Start with Windows" by default
// If registry key doesn't exist and settings say it should be enabled
if (_manager.Settings.StartWithWindows && !IsStartWithWindowsEnabled())
{
SetStartWithWindows(true);
}
// Start smooth scrolling immediately
_manager.Start();
// Setup memory optimization timer (every 60 seconds)
_memoryTimer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(60)
};
_memoryTimer.Tick += (s, args) => OptimizeMemory();
_memoryTimer.Start();
// Check if started with --show flag (user explicitly opened)
bool showWindow = e.Args.Contains("--show");
// Create the main window
var mainWindow = new MainWindow();
MainWindow = mainWindow;
if (showWindow)
{
// User explicitly opened - show the window
mainWindow.Show();
}
else
{
// Start hidden - run in system tray
mainWindow.Hide();
// Optimize memory when starting hidden
OptimizeMemory();
}
}
protected override void OnExit(ExitEventArgs e)
{
_memoryTimer?.Stop();
_manager?.Dispose();
base.OnExit(e);
}
/// <summary>
/// Optimizes memory usage by trimming working set and requesting GC
/// </summary>
public static void OptimizeMemory()
{
try
{
// Request garbage collection
GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized, false);
GC.WaitForPendingFinalizers();
// Trim working set (reduce physical memory usage)
SetProcessWorkingSetSize(GetCurrentProcess(), (IntPtr)(-1), (IntPtr)(-1));
}
catch
{
// Ignore memory optimization errors
}
}
public ScrollVManager? GetManager() => _manager;
public static void SetStartWithWindows(bool enable)
{
const string appName = "Scroll-V";
string exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName ?? "";
try
{
using var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (key != null)
{
if (enable)
{
// Start without --show flag = hidden mode
key.SetValue(appName, $"\"{exePath}\"");
}
else
{
key.DeleteValue(appName, false);
}
}
}
catch
{
// Ignore registry errors
}
}
public static bool IsStartWithWindowsEnabled()
{
const string appName = "Scroll-V";
try
{
using var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false);
return key?.GetValue(appName) != null;
}
catch
{
return false;
}
}
}
}