Skip to content

Commit b3b635e

Browse files
committed
Optimizing the code
1 parent 4f6662a commit b3b635e

File tree

1 file changed

+74
-71
lines changed

1 file changed

+74
-71
lines changed

src/GlobalHotKeys/HotKeyManager.cs

+74-71
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,53 @@ public class HotKeyManager : IDisposable
1818
/// </summary>
1919
public HotKeyManager()
2020
{
21-
// messageLoopThreadHwnd:
2221
// Create a TaskCompletionSource to receive the window handle.
2322
var tcsHwnd = new TaskCompletionSource<IntPtr>();
2423

24+
_messageLoopThread = new Thread(new ThreadStart(HotKeyThreadEntry))
25+
{
26+
Name = "GlobalHotKeyManager Message Loop"
27+
};
28+
_messageLoopThread.Start();
29+
_hWnd = tcsHwnd.Task.Result;
30+
return;
31+
2532
// Thread entry method.
26-
void ThreadEntry()
33+
void HotKeyThreadEntry()
2734
{
35+
// Dictionary to keep track of registrations.
36+
var registrations = new Dictionary<int, HotKey>();
37+
2838
// Retrieve the module handle.
2939
var hInstance = NativeFunctions.GetModuleHandle(null);
3040

31-
// Dictionary to keep track of registrations.
32-
var registrations = new Dictionary<int, HotKey>();
41+
// Create the window class from the window procedure.
42+
var wndProcDelegate = new WndProc(MessageHandler);
43+
44+
// Convert the WndProc delegate into a structure.
45+
var wndClassEx = WNDCLASSEX.FromWndProc(wndProcDelegate);
46+
47+
// Register the window class.
48+
var registeredClass = NativeFunctions.RegisterClassEx(ref wndClassEx);
49+
50+
// create the window.
51+
var localHWnd = NativeFunctions.CreateWindowEx(0, (uint)registeredClass, null, WindowStyle.WS_OVERLAPPED, 0, 0, 640, 480, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
52+
53+
// Signal that the window has been created.
54+
tcsHwnd.SetResult(localHWnd);
55+
56+
// enter message loop.
57+
MessageLoop(localHWnd);
58+
59+
// cleanup the resources after wards.
60+
Cleanup(localHWnd);
61+
62+
return;
3363

3464
// nextId: find the next free id from 0x0000 to 0xBFFF.
3565
int? NextId()
3666
{
37-
for (int i = 0x0000; i <= 0xBFFF; i++)
67+
for (var i = 0x0000; i <= 0xBFFF; i++)
3868
{
3969
if (!registrations.ContainsKey(i))
4070
{
@@ -44,8 +74,8 @@ void ThreadEntry()
4474
return null;
4575
}
4676

47-
// register: wrapper for calling RegisterHotKey and updating registrations.
48-
bool register(IntPtr hWnd, VirtualKeyCode key, Modifiers modifiers, int id)
77+
// RegisterKey: wrapper for calling RegisterHotKey and updating registrations.
78+
bool RegisterKey(IntPtr hWnd, VirtualKeyCode key, Modifiers modifiers, int id)
4979
{
5080
if (NativeFunctions.RegisterHotKey(hWnd, id, modifiers, key))
5181
{
@@ -58,8 +88,8 @@ bool register(IntPtr hWnd, VirtualKeyCode key, Modifiers modifiers, int id)
5888
}
5989
}
6090

61-
// unregister: wrapper for calling UnregisterHotKey and updating registrations.
62-
bool unregister(IntPtr hWnd, int id)
91+
// UnregisterKey: wrapper for calling UnregisterHotKey and updating registrations.
92+
bool UnregisterKey(IntPtr hWnd, int id)
6393
{
6494
var registration = registrations.GetValueOrDefault(id);
6595
if (registration != null)
@@ -76,49 +106,42 @@ bool unregister(IntPtr hWnd, int id)
76106
// messageHandler: processes window messages.
77107
IntPtr MessageHandler(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
78108
{
79-
if (uMsg == RegisterHotKeyMsg)
109+
switch (uMsg)
80110
{
81-
// Extract key and modifiers.
82-
var key = (VirtualKeyCode)wParam.ToInt32();
83-
var modifiers = (Modifiers)lParam.ToInt32();
84-
var id = NextId();
85-
if (id.HasValue)
86-
{
87-
return register(hWnd, key, modifiers, id.Value) ? new IntPtr(id.Value) : new IntPtr(-1);
88-
}
89-
else
90-
{
91-
return IntPtr.Zero;
92-
}
93-
}
94-
else if (uMsg == UnregisterHotKeyMsg)
95-
{
96-
var id = wParam.ToInt32();
97-
return unregister(hWnd, id) ? new IntPtr(id) : new IntPtr(-1);
98-
}
99-
else if (uMsg == HotKeyMsg)
100-
{
101-
var registration = registrations.GetValueOrDefault(wParam.ToInt32());
102-
if (registration != null)
103-
{
104-
_hotkey.OnNext(registration);
105-
}
106-
return new IntPtr(1);
107-
}
108-
else
109-
{
110-
return NativeFunctions.DefWindowProc(hWnd, uMsg, wParam, lParam);
111+
case RegisterHotKeyMsg:
112+
{
113+
// Extract key and modifiers.
114+
var key = (VirtualKeyCode)wParam.ToInt32();
115+
var modifiers = (Modifiers)lParam.ToInt32();
116+
var id = NextId();
117+
if (id.HasValue)
118+
{
119+
return RegisterKey(hWnd, key, modifiers, id.Value) ? new IntPtr(id.Value) : new IntPtr(-1);
120+
}
121+
else
122+
{
123+
return IntPtr.Zero;
124+
}
125+
}
126+
case UnregisterHotKeyMsg:
127+
{
128+
var id = wParam.ToInt32();
129+
return UnregisterKey(hWnd, id) ? new IntPtr(id) : new IntPtr(-1);
130+
}
131+
case HotKeyMsg:
132+
{
133+
var registration = registrations.GetValueOrDefault(wParam.ToInt32());
134+
if (registration != null)
135+
{
136+
_hotkey.OnNext(registration);
137+
}
138+
return new IntPtr(1);
139+
}
140+
default:
141+
return NativeFunctions.DefWindowProc(hWnd, uMsg, wParam, lParam);
111142
}
112143
}
113144

114-
// Create the window class from the window procedure.
115-
var wndProcDelegate = new WndProc(MessageHandler);
116-
// Convert the WndProc delegate into a WNDCLASSEX structure.
117-
var wndClassEx = WNDCLASSEX.FromWndProc(wndProcDelegate);
118-
119-
// Register the window class.
120-
var registeredClass = NativeFunctions.RegisterClassEx(ref wndClassEx);
121-
122145
// messageLoop: processes messages until quit.
123146
void MessageLoop(IntPtr hWnd)
124147
{
@@ -136,32 +159,13 @@ void Cleanup(IntPtr hWnd)
136159
{
137160
foreach (var key in registrations.Keys.ToArray())
138161
{
139-
unregister(hWnd, key);
162+
UnregisterKey(hWnd, key);
140163
}
141164

142165
NativeFunctions.DestroyWindow(hWnd);
143166
NativeFunctions.UnregisterClass(wndClassEx.lpszClassName, hInstance);
144167
}
145-
146-
// create the window.
147-
var localHWnd = NativeFunctions.CreateWindowEx(0, (uint)registeredClass, null, WindowStyle.WS_OVERLAPPED, 0, 0, 640, 480, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
148-
149-
// Signal that the window has been created.
150-
tcsHwnd.SetResult(localHWnd);
151-
152-
// enter message loop.
153-
MessageLoop(localHWnd);
154-
155-
// cleanup the resources afterwards.
156-
Cleanup(localHWnd);
157168
}
158-
159-
_messageLoopThread = new Thread(new ThreadStart(ThreadEntry))
160-
{
161-
Name = "GlobalHotKeyManager Message Loop"
162-
};
163-
_messageLoopThread.Start();
164-
_hWnd = tcsHwnd.Task.Result;
165169
}
166170

167171
/// <summary>
@@ -173,13 +177,12 @@ void Cleanup(IntPtr hWnd)
173177
public IRegistration Register(VirtualKeyCode key, Modifiers modifiers)
174178
{
175179
// Retrieve the window handle.
176-
var hWnd = _hWnd;
177180

178181
// tell the message loop to register the _hotkey.
179-
var result = NativeFunctions.SendMessage(hWnd, RegisterHotKeyMsg, new IntPtr((int)key), new IntPtr((int)modifiers));
182+
var result = NativeFunctions.SendMessage(_hWnd, RegisterHotKeyMsg, new IntPtr((int)key), new IntPtr((int)modifiers));
180183

181184
// return a disposable that instructs the message loop to unregister the _hotkey on disposal.
182-
return new Registration(hWnd, result);
185+
return new Registration(_hWnd, result);
183186
}
184187

185188
/// <summary>

0 commit comments

Comments
 (0)