Skip to content

Commit

Permalink
Added SDL_ClickTrayEntry()
Browse files Browse the repository at this point in the history
Also removed the app delegate from the tray code on Cocoa and folded that into SDL3AppDelegate.

Fixes #11906
  • Loading branch information
slouken committed Jan 10, 2025
1 parent fb0f6a1 commit 0428989
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 31 deletions.
9 changes: 9 additions & 0 deletions include/SDL3/SDL_tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,15 @@ extern SDL_DECLSPEC bool SDLCALL SDL_GetTrayEntryEnabled(SDL_TrayEntry *entry);
*/
extern SDL_DECLSPEC void SDLCALL SDL_SetTrayEntryCallback(SDL_TrayEntry *entry, SDL_TrayCallback callback, void *userdata);

/**
* Simulate a click on a tray entry.
*
* \param entry The entry to activate.
*
* \since This function is available since SDL 3.1.9.
*/
extern SDL_DECLSPEC void SDLCALL SDL_ClickTrayEntry(SDL_TrayEntry *entry);

/**
* Destroys a tray object.
*
Expand Down
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi.sym
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ SDL3_0.0.0 {
SDL_GetTrayMenuParentTray;
SDL_GetThreadState;
SDL_AudioStreamDevicePaused;
SDL_ClickTrayEntry;
# extra symbols go here (don't modify this line)
local: *;
};
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -1256,3 +1256,4 @@
#define SDL_GetTrayMenuParentTray SDL_GetTrayMenuParentTray_REAL
#define SDL_GetThreadState SDL_GetThreadState_REAL
#define SDL_AudioStreamDevicePaused SDL_AudioStreamDevicePaused_REAL
#define SDL_ClickTrayEntry SDL_ClickTrayEntry_REAL
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1264,3 +1264,4 @@ SDL_DYNAPI_PROC(SDL_TrayEntry*,SDL_GetTrayMenuParentEntry,(SDL_TrayMenu *a),(a),
SDL_DYNAPI_PROC(SDL_Tray*,SDL_GetTrayMenuParentTray,(SDL_TrayMenu *a),(a),return)
SDL_DYNAPI_PROC(SDL_ThreadState,SDL_GetThreadState,(SDL_Thread *a),(a),return)
SDL_DYNAPI_PROC(bool,SDL_AudioStreamDevicePaused,(SDL_AudioStream *a),(a),return)
SDL_DYNAPI_PROC(void,SDL_ClickTrayEntry,(SDL_TrayEntry *a),(a),)
47 changes: 16 additions & 31 deletions src/tray/cocoa/SDL_tray.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,6 @@
SDL_TrayMenu *menu;
};

static NSApplication *app = NULL;

@interface AppDelegate: NSObject <NSApplicationDelegate>
- (IBAction)menu:(id)sender;
@end

@implementation AppDelegate{}
- (IBAction)menu:(id)sender
{
SDL_TrayEntry *entry = [[sender representedObject] pointerValue];

if (!entry) {
return;
}

if (entry->flags & SDL_TRAYENTRY_CHECKBOX) {
SDL_SetTrayEntryChecked(entry, !SDL_GetTrayEntryChecked(entry));
}

if (entry->callback) {
entry->callback(entry->userdata, entry);
}
}
@end

static void DestroySDLMenu(SDL_TrayMenu *menu)
{
for (int i = 0; i < menu->nEntries; i++) {
Expand All @@ -106,19 +81,14 @@ static void DestroySDLMenu(SDL_TrayMenu *menu)
SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
{
SDL_Tray *tray = (SDL_Tray *)SDL_calloc(1, sizeof(*tray));

AppDelegate *delegate = [[AppDelegate alloc] init];
app = [NSApplication sharedApplication];
[app setDelegate:delegate];

if (!tray) {
return NULL;
}

tray->statusItem = nil;
tray->statusBar = [NSStatusBar systemStatusBar];
tray->statusItem = [tray->statusBar statusItemWithLength:NSVariableStatusItemLength];
[app activateIgnoringOtherApps:TRUE];
[[NSApplication sharedApplication] activateIgnoringOtherApps:TRUE];

if (tooltip) {
tray->statusItem.button.toolTip = [NSString stringWithUTF8String:tooltip];
Expand Down Expand Up @@ -421,6 +391,21 @@ void SDL_SetTrayEntryCallback(SDL_TrayEntry *entry, SDL_TrayCallback callback, v
entry->userdata = userdata;
}

void SDL_ClickTrayEntry(SDL_TrayEntry *entry)
{
if (!entry) {
return;
}

if (entry->flags & SDL_TRAYENTRY_CHECKBOX) {
SDL_SetTrayEntryChecked(entry, !SDL_GetTrayEntryChecked(entry));
}

if (entry->callback) {
entry->callback(entry->userdata, entry);
}
}

SDL_TrayMenu *SDL_GetTrayEntryParent(SDL_TrayEntry *entry)
{
return entry->parent;
Expand Down
4 changes: 4 additions & 0 deletions src/tray/dummy/SDL_tray.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ void SDL_SetTrayEntryCallback(SDL_TrayEntry *entry, SDL_TrayCallback callback, v
SDL_Unsupported();
}

void SDL_ClickTrayEntry(SDL_TrayEntry *entry)
{
}

SDL_TrayMenu *SDL_GetTrayEntryParent(SDL_TrayEntry *entry)
{
SDL_Unsupported();
Expand Down
15 changes: 15 additions & 0 deletions src/tray/unix/SDL_tray.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,21 @@ void SDL_SetTrayEntryCallback(SDL_TrayEntry *entry, SDL_TrayCallback callback, v
entry->userdata = userdata;
}

void SDL_ClickTrayEntry(SDL_TrayEntry *entry)
{
if (!entry) {
return;
}

if (entry->flags & SDL_TRAYENTRY_CHECKBOX) {
SDL_SetTrayEntryChecked(entry, !SDL_GetTrayEntryChecked(entry));
}

if (entry->callback) {
entry->callback(entry->userdata, entry);
}
}

SDL_TrayMenu *SDL_GetTrayEntryParent(SDL_TrayEntry *entry)
{
return entry->parent;
Expand Down
15 changes: 15 additions & 0 deletions src/tray/windows/SDL_tray.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,21 @@ void SDL_SetTrayEntryCallback(SDL_TrayEntry *entry, SDL_TrayCallback callback, v
entry->userdata = userdata;
}

void SDL_ClickTrayEntry(SDL_TrayEntry *entry)
{
if (!entry) {
return;
}

if (entry->flags & SDL_TRAYENTRY_CHECKBOX) {
SDL_SetTrayEntryChecked(entry, !SDL_GetTrayEntryChecked(entry));
}

if (entry->callback) {
entry->callback(entry->userdata, entry);
}
}

SDL_TrayMenu *SDL_GetTrayEntryParent(SDL_TrayEntry *entry)
{
return entry->parent;
Expand Down
8 changes: 8 additions & 0 deletions src/video/cocoa/SDL_cocoaevents.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
change:(NSDictionary *)change
context:(void *)context;
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app;
- (IBAction)menu:(id)sender;
@end

@implementation SDL3AppDelegate : NSObject
Expand Down Expand Up @@ -358,6 +359,13 @@ - (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app
return YES;
}

- (IBAction)menu:(id)sender
{
SDL_TrayEntry *entry = [[sender representedObject] pointerValue];

SDL_ClickTrayEntry(entry);
}

@end

static SDL3AppDelegate *appDelegate = nil;
Expand Down

0 comments on commit 0428989

Please sign in to comment.