-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNotifyIconDataMarshaller.cs
296 lines (268 loc) · 11.9 KB
/
NotifyIconDataMarshaller.cs
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
//-----------------------------------------------------------------------
// <copyright>
// Created by Matt Weber <matt@badecho.com>
// Copyright @ 2025 Bad Echo LLC. All rights reserved.
//
// Bad Echo Technologies are licensed under the
// GNU Affero General Public License v3.0.
//
// See accompanying file LICENSE.md or a copy at:
// https://www.gnu.org/licenses/agpl-3.0.html
// </copyright>
//-----------------------------------------------------------------------
using BadEcho.Extensions;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using BadEcho.Properties;
namespace BadEcho.Interop;
/// <summary>
/// Provides a custom marshaller for notification area information.
/// </summary>
[CustomMarshaller(typeof(NotifyIconData), MarshalMode.ManagedToUnmanagedRef, typeof(ManagedToUnmanagedRef))]
internal static unsafe class NotifyIconDataMarshaller
{
/// <summary>
/// Represents a stateful marshaller for notification area information.
/// </summary>
public ref struct ManagedToUnmanagedRef
{
private NOTIFYICONDATAW _unmanaged;
private WindowHandle _windowHandle;
private IconHandle? _iconHandle;
private IconHandle? _balloonIconHandle;
private bool _windowHandleAddRefd;
private bool _iconHandleAddRefd;
private bool _balloonIconHandleAddRefd;
private IntPtr _originalWindowHandleValue;
private IntPtr _originalIconHandleValue;
private IntPtr _originalBalloonIconHandleValue;
/// <summary>
/// Converts a managed <see cref="NotifyIconData"/> instance to its unmanaged counterpart,
/// loading the result into the marshaller.
/// </summary>
/// <param name="iconData">A managed instance of notification area information.</param>
public void FromManaged(NotifyIconData iconData)
{
Require.NotNull(iconData, nameof(iconData));
_windowHandleAddRefd = false;
_iconHandleAddRefd = false;
_balloonIconHandleAddRefd = false;
_windowHandle = iconData.Window;
_iconHandle = iconData.Icon;
_balloonIconHandle = iconData.BalloonIcon;
_unmanaged.cbSize = (uint) sizeof(NOTIFYICONDATAW);
_windowHandle.DangerousAddRef(ref _windowHandleAddRefd);
_unmanaged.hWnd = _originalWindowHandleValue = _windowHandle.DangerousGetHandle();
_unmanaged.guidItem = GuidMarshaller.ConvertToUnmanaged(iconData.Id);
_unmanaged.uFlags = iconData.Flags;
_unmanaged.uCallbackMessage = iconData.CallbackMessage;
if (_iconHandle != null)
{
_iconHandle.DangerousAddRef(ref _iconHandleAddRefd);
_unmanaged.hIcon = _originalIconHandleValue = _iconHandle.DangerousGetHandle();
}
_unmanaged.dwState = iconData.State;
_unmanaged.dwStateMask = iconData.StateMask;
_unmanaged.uTimeoutOrVersion = iconData.TimeoutOrVersion;
_unmanaged.dwInfoFlags = iconData.InfoFlags;
if (_balloonIconHandle != null)
{
_balloonIconHandle.DangerousAddRef(ref _balloonIconHandleAddRefd);
_unmanaged.hBalloonIcon = _originalBalloonIconHandleValue = _balloonIconHandle.DangerousGetHandle();
}
_unmanaged.Tip = iconData.Tip;
_unmanaged.Info = iconData.Info;
_unmanaged.InfoTitle = iconData.InfoTitle;
}
/// <summary>
/// Provides the unmanaged notification area information currently loaded into the marshaller.
/// </summary>
/// <returns>The converted <see cref="NOTIFYICONDATAW"/> value.</returns>
public readonly NOTIFYICONDATAW ToUnmanaged()
=> _unmanaged;
/// <summary>
/// Loads the provided unmanaged notification area information into the marshaller.
/// </summary>
/// <param name="unmanaged">The unmanaged notification area information.</param>
public void FromUnmanaged(NOTIFYICONDATAW unmanaged)
=> _unmanaged = unmanaged;
/// <summary>
/// Converts the unmanaged <see cref="NOTIFYICONDATAW"/> value currently loaded into the marshaller
/// into its managed counterpart, returning the result.
/// </summary>
/// <returns>The converted <see cref="NotifyIconData"/> instance.</returns>
/// <exception cref="NotSupportedException">A handle originating from a <see cref="SafeHandle"/> was changed.</exception>
public readonly NotifyIconData ToManaged()
{
// SafeHandle fields must match the underlying handle value during marshalling. They cannot change.
if (_unmanaged.hWnd != _originalWindowHandleValue)
throw new NotSupportedException(Strings.HandleCannotChangeDuringMarshalling);
if (_unmanaged.hIcon != _originalIconHandleValue)
throw new NotSupportedException(Strings.HandleCannotChangeDuringMarshalling);
if (_unmanaged.hBalloonIcon != _originalBalloonIconHandleValue)
throw new NotSupportedException(Strings.HandleCannotChangeDuringMarshalling);
Guid managedId = GuidMarshaller.ConvertToManaged(_unmanaged.guidItem);
return new NotifyIconData(_windowHandle, managedId, _unmanaged.uFlags)
{
CallbackMessage = _unmanaged.uCallbackMessage,
Icon = _iconHandle,
State = _unmanaged.dwState,
StateMask = _unmanaged.dwStateMask,
TimeoutOrVersion = _unmanaged.uTimeoutOrVersion,
InfoFlags = _unmanaged.dwInfoFlags,
BalloonIcon = _balloonIconHandle,
Tip = new string(_unmanaged.Tip),
Info = new string(_unmanaged.Info),
InfoTitle = new string(_unmanaged.InfoTitle)
};
}
/// <summary>
/// Releases all resources in use by the marshaller.
/// </summary>
public readonly void Free()
{
if (_windowHandleAddRefd)
_windowHandle.DangerousRelease();
if (_iconHandle != null && _iconHandleAddRefd)
_iconHandle.DangerousRelease();
if (_balloonIconHandle != null && _balloonIconHandleAddRefd)
_balloonIconHandle.DangerousRelease();
}
/// <summary>
/// Represents information that the system needs to display notifications in the notification area.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct NOTIFYICONDATAW
{
/// <summary>
/// The size of this structure in bytes.
/// </summary>
public uint cbSize;
/// <summary>
/// A handle to the window that receives notifications associated with an icon in
/// the notification area.
/// </summary>
public IntPtr hWnd;
/// <summary>
/// The application-defined identifier of the taskbar icon.
/// </summary>
public uint uID;
/// <summary>
/// Flags that either indicate which of the other members of the structure contain valid data
/// or provide additional information to the tooltip as to how it should display.
/// </summary>
public NotifyIconFlags uFlags;
/// <summary>
/// An application-defined message identifier.
/// </summary>
public uint uCallbackMessage;
/// <summary>
/// A handle to the icon to be added, modified, or deleted.
/// </summary>
public IntPtr hIcon;
/// <summary>
/// A null-terminated string that specifies the text for a standard tooltip.
/// </summary>
public fixed char szTip[128];
/// <summary>
/// The state of the icon.
/// </summary>
public uint dwState;
/// <summary>
/// A value that specifies which bits of the <see cref="dwState"/> member are retrieved
/// or modified.
/// </summary>
public uint dwStateMask;
/// <summary>
/// A null-terminated string that specifies the text to display in a balloon notification.
/// </summary>
public fixed char szInfo[256];
/// <summary>
/// Either the timeout value, in milliseconds, for the notification, or a specification of which
/// version of the Shell notification icon interface should be used.
/// </summary>
public uint uTimeoutOrVersion;
/// <summary>
/// A null-terminated string that specifies the title for a balloon notification.
/// </summary>
public fixed char szInfoTitle[64];
/// <summary>
/// Flags that can be set to modify the behavior and appearance of a balloon notification.
/// </summary>
public NotifyIconInfoFlags dwInfoFlags;
/// <summary>
/// A registered <see cref="GUID"/> that identifies the icon. This is the preferred way to identify
/// an icon in modern OS versions.
/// </summary>
public GUID guidItem;
/// <summary>
/// A handle to a customized notification icon that should be used independently of the
/// notification area icon.
/// </summary>
public IntPtr hBalloonIcon;
/// <summary>
/// Gets or sets a string that specifies the text for a standard tooltip.
/// </summary>
public ReadOnlySpan<char> Tip
{
readonly get => SzTip.SliceAtFirstNull();
set => value.CopyToAndTerminate(SzTip);
}
/// <summary>
/// Gets or sets a string that specifies the text to display in a balloon notification.
/// </summary>
public ReadOnlySpan<char> Info
{
readonly get => SzInfo.SliceAtFirstNull();
set => value.CopyToAndTerminate(SzInfo);
}
/// <summary>
/// Gets or sets a string that specifies a title for a balloon notification.
/// </summary>
public ReadOnlySpan<char> InfoTitle
{
readonly get => SzInfoTitle.SliceAtFirstNull();
set => value.CopyToAndTerminate(SzInfoTitle);
}
/// <summary>
/// Gets a null-terminated string that specifies the text for a standard tooltip.
/// </summary>
private readonly Span<char> SzTip
{
get
{
fixed (char* c = szTip)
{
return new Span<char>(c, 128);
}
}
}
/// <summary>
/// Gets a null-terminated string that specifies the text to display in a balloon notification.
/// </summary>
private readonly Span<char> SzInfo
{
get
{
fixed (char* c = szInfo)
{
return new Span<char>(c, 256);
}
}
}
/// <summary>
/// Gets a null-terminated string that specifies a title for a balloon notification.
/// </summary>
private readonly Span<char> SzInfoTitle
{
get
{
fixed (char* c = szInfoTitle)
{
return new Span<char>(c, 64);
}
}
}
}
}
}