Search code examples
c++objective-cmacoscocoasdl

Minimalist cocoa app does not appear in dock


As part of a broader project, I am implementing my own window management for a Cocoa app. I'm using the "objective c++" flag when compiling because much of the codebase is in c++.

I have put some stub code in my main.cpp to get a window to open but the window icon is not going in the applications dock and isn't raising itself to the front of the window stack.

I have this bit in my main.cpp :

main.cpp

    TestAppDelegate* appDel = [TestAppDelegate alloc];
    [NSApp setDelegate:appDel];

    NSApplication* app = [NSApplication sharedApplication];

    app.delegate = appDel;
    [app run];

    return NSApplicationMain(argc, argv);

And my AppDelegate object looks like this

test.h

@interface TestAppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate> {

NSWindow * win;

}
@end

test.cpp

#include "test.h"

@implementation TestAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application

    NSRect rect;
    rect.origin.x = 0;
    rect.origin.y = 0;
    rect.size.width = 500;
    rect.size.height = 500;

    unsigned int style = (NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskMiniaturizable);

    NSWindow* win = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];

    NSButton* butan = [NSButton buttonWithTitle:@"SomeText" target:nil action:nil];
    butan.frame = NSMakeRect(20, 20, 100, 30);
    butan.title = [NSString stringWithUTF8String:"Things"];
    [win.contentView addSubview:butan];
    [win makeKeyAndOrderFront:nil];

}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
    return YES;
}

@end

It's a bunch of useless test code for now but the window opens up although it appears behind the Xcode editor. The button is responsive to clicks and the window is not frozen since I can move it around freely.

My problem is that the window doesn't seem "active". It's not in the dock, I can't "command+tab+ to reach it and when I click on it and give it focus the top-left buttons don't light up like the other applications.

What am I forgetting here ?

As a reference, I am looking at the source of SDL, which my project mainly use except for this little experiment. If I start a graphical window with SDL, which uses Cocoa as its driver. I will see the window on the dock and it will be more "active" than my little window.


Solution

  • Since your program is not a bundled app, you'll need to configure its "activation policy" to make it show up in the Dock and Command-Tab switcher:

    [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
    

    You'll also want to activate it on launch:

    [NSApp activateIgnoringOtherApps:YES];