1+ #include < AppKit/AppKit.h>
2+ #import < Cocoa/Cocoa.h>
3+ #include < iostream>
4+ #include " nativeapi.h"
5+
6+ using nativeapi::Window;
7+ using nativeapi::WindowManager;
8+
9+ @interface AppDelegate : NSObject <NSApplicationDelegate >
10+ @property (strong ) NSWindow * window;
11+ @end
12+
13+ @implementation AppDelegate
14+
15+ - (void )applicationDidFinishLaunching : (NSNotification *)notification {
16+ // Create a window
17+ NSRect frame = NSMakeRect (0 , 0 , 400 , 300 );
18+ self.window = [[NSWindow alloc ]
19+ initWithContentRect: frame
20+ styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
21+ NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable
22+ backing: NSBackingStoreBuffered
23+ defer: NO ];
24+ [self .window setTitle: @" Native API Example" ];
25+ [self .window center ];
26+ [self .window makeKeyAndOrderFront: nil ];
27+ [self .window makeMainWindow ];
28+ [NSApp activateIgnoringOtherApps: YES ];
29+
30+ // dispatch_async(dispatch_get_main_queue(), ^{
31+
32+ // });
33+
34+ // 延迟检查主窗口
35+ dispatch_async (dispatch_get_main_queue (), ^{
36+ NSLog (@" 主窗口: %@ " , NSApp .mainWindow);
37+ NSLog (@" 关键窗口: %@ " , NSApp .keyWindow);
38+ NSLog (@" 所有窗口: %@ " , NSApp .windows);
39+ });
40+
41+ [[NSNotificationCenter defaultCenter ]
42+ addObserverForName: NSWindowDidBecomeMainNotification
43+ object: nil
44+ queue: nil
45+ usingBlock: ^(NSNotification * note) {
46+ NSWindow * mainWindow = [[NSApplication sharedApplication ] mainWindow ];
47+ NSLog (@" 主窗口: %@ " , mainWindow);
48+
49+ // Initialize WindowManager
50+ WindowManager windowManager = WindowManager ();
51+
52+ // Get current window information
53+ Window currentWindow = windowManager.GetCurrent ();
54+ std::cout << " Current Window Information:" << std::endl;
55+ std::cout << " ID: " << currentWindow.id << std::endl;
56+
57+ // Get window size
58+ auto size = currentWindow.GetSize ();
59+ std::cout << " Window Size: " << size.width << " x" << size.height << std::endl;
60+
61+ // Get all windows
62+ std::vector<Window> windowList = windowManager.GetAll ();
63+ std::cout << " \n All Windows Information:" << std::endl;
64+ for (size_t i = 0 ; i < windowList.size (); i++) {
65+ const Window& window = windowList[i];
66+ std::cout << " Window " << (i + 1 ) << " :" << std::endl;
67+ std::cout << " ID: " << window.id << std::endl;
68+ auto windowSize = window.GetSize ();
69+ std::cout << " Size: " << windowSize.width << " x" << windowSize.height
70+ << std::endl;
71+ }
72+ }];
73+ }
74+
75+ - (BOOL )applicationShouldTerminateAfterLastWindowClosed : (NSApplication *)sender {
76+ return YES ;
77+ }
78+
79+ @end
80+
81+ int main (int argc, const char * argv[]) {
82+ @autoreleasepool {
83+ NSApplication * app = [NSApplication sharedApplication ];
84+ AppDelegate* delegate = [[AppDelegate alloc ] init ];
85+ [app setDelegate: delegate];
86+ [app run ];
87+ }
88+ return 0 ;
89+ }
0 commit comments