Minimalist cocoa 应用未出现在 Dock 中

Minimalist cocoa app does not appear in dock

作为更广泛项目的一部分,我正在为 Cocoa 应用实施我自己的 window 管理。我在编译时使用了 "objective c++" 标志,因为大部分代码库都在 C++ 中。

我已经在我的 main.cpp 中放置了一些存根代码来打开 window 但是 window 图标没有进入应用程序停靠栏并且没有提升到window 堆栈的前面。

我的 main.cpp 中有这个位:

main.cpp

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

    NSApplication* app = [NSApplication sharedApplication];

    app.delegate = appDel;
    [app run];

    return NSApplicationMain(argc, argv);

我的 AppDelegate 对象看起来像这样

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

目前是一堆无用的测试代码,但是 window 打开了,尽管它出现在 Xcode 编辑器后面。该按钮响应点击并且 window 没有冻结,因为我可以自由移动它。

我的问题是 window 似乎不是 "active"。它不在 Dock 中,我无法“command+tab+ 访问它,当我点击它并给它焦点时,左上角的按钮不像其他应用程序那样亮起。

我在这里忘了什么?

作为参考,我正在查看 SDL 的源代码,除了这个小实验外,我的项目主要使用它。如果我使用 SDL 启动图形 window,它使用 Cocoa 作为其驱动程序。我会在码头上看到 window,它会比我的小 window 多 "active"。

由于您的程序不是捆绑应用,因此您需要配置其 "activation policy" 以使其显示在 Dock 和 Command-Tab 切换器中:

[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];

您还需要在启动时激活它:

[NSApp activateIgnoringOtherApps:YES];