Skip to content

Commit 0aa319e

Browse files
Scrooge86xslouken
authored andcommitted
Added support for custom tray icon on Windows via SDL hints.
SDL_CreateTray now respects SDL_HINT_WINDOWS_INTRESOURCE_ICON_SMALL and SDL_HINT_WINDOWS_INTRESOURCE_ICON hints and uses the specified icon as the tray icon.
1 parent 5815372 commit 0aa319e

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/tray/windows/SDL_tray.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,28 @@ static wchar_t *escape_label(const char *in)
187187
return out;
188188
}
189189

190+
static HICON load_default_icon()
191+
{
192+
HINSTANCE hInstance = GetModuleHandle(NULL);
193+
if (!hInstance) {
194+
return LoadIcon(NULL, IDI_APPLICATION);
195+
}
196+
197+
const char *hint = SDL_GetHint(SDL_HINT_WINDOWS_INTRESOURCE_ICON_SMALL);
198+
if (hint && *hint) {
199+
HICON icon = LoadIcon(hInstance, MAKEINTRESOURCE(SDL_atoi(hint)));
200+
return icon ? icon : LoadIcon(NULL, IDI_APPLICATION);
201+
}
202+
203+
hint = SDL_GetHint(SDL_HINT_WINDOWS_INTRESOURCE_ICON);
204+
if (hint && *hint) {
205+
HICON icon = LoadIcon(hInstance, MAKEINTRESOURCE(SDL_atoi(hint)));
206+
return icon ? icon : LoadIcon(NULL, IDI_APPLICATION);
207+
}
208+
209+
return LoadIcon(NULL, IDI_APPLICATION);
210+
}
211+
190212
SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
191213
{
192214
SDL_Tray *tray = (SDL_Tray *)SDL_malloc(sizeof(*tray));
@@ -216,12 +238,12 @@ SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
216238
tray->nid.hIcon = CreateIconFromSurface(icon);
217239

218240
if (!tray->nid.hIcon) {
219-
tray->nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
241+
tray->nid.hIcon = load_default_icon();
220242
}
221243

222244
tray->icon = tray->nid.hIcon;
223245
} else {
224-
tray->nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
246+
tray->nid.hIcon = load_default_icon();
225247
tray->icon = tray->nid.hIcon;
226248
}
227249

@@ -245,12 +267,12 @@ void SDL_SetTrayIcon(SDL_Tray *tray, SDL_Surface *icon)
245267
tray->nid.hIcon = CreateIconFromSurface(icon);
246268

247269
if (!tray->nid.hIcon) {
248-
tray->nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
270+
tray->nid.hIcon = load_default_icon();
249271
}
250272

251273
tray->icon = tray->nid.hIcon;
252274
} else {
253-
tray->nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
275+
tray->nid.hIcon = load_default_icon();
254276
tray->icon = tray->nid.hIcon;
255277
}
256278

0 commit comments

Comments
 (0)