Skip to content

Commit 787b96c

Browse files
committed
feat: Update Window and WindowManager classes to support window ID retrieval and management on macOS
1 parent 4bbc34a commit 787b96c

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

src/window.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44

55
namespace nativeapi {
66

7+
typedef int32_t WindowID;
8+
79
class Window {
810
public:
911
Window();
10-
Window(void* window); // Constructor that takes NSWindow*
12+
Window(void* window);
1113
virtual ~Window();
1214

13-
std::string id;
14-
std::string name;
15+
WindowID id;
1516

16-
void* GetNSWindow() const; // Returns NSWindow* on macOS
17-
Size GetSize() const; // Get window size
17+
void* GetNSWindow() const;
18+
Size GetSize() const;
1819

1920
private:
2021
class Impl;
21-
Impl* pimpl_; // Pointer to implementation
22+
Impl* pimpl_;
2223
};
2324

2425
} // namespace nativeapi

src/window_macos.mm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
#include <iostream>
12
#include "window.h"
23
#include "window_manager.h"
3-
#include <iostream>
44

55
// Import Cocoa headers
66
#import <Cocoa/Cocoa.h>
@@ -15,12 +15,12 @@
1515
};
1616

1717
Window::Window() : pimpl_(nullptr) {
18-
id = "window1";
18+
id = -1;
1919
std::cout << "Window created with null pimpl_" << std::endl;
2020
}
2121

22-
Window::Window(void* window) : pimpl_(new Impl((NSWindow*)window)) {
23-
id = "window1";
22+
Window::Window(void* window) : pimpl_(new Impl((__bridge NSWindow*)window)) {
23+
id = pimpl_->ns_window_ ? [pimpl_->ns_window_ windowNumber] : 0;
2424
std::cout << "Window created with NSWindow: " << pimpl_->ns_window_ << std::endl;
2525
}
2626

@@ -39,7 +39,7 @@
3939
return nullptr;
4040
}
4141
std::cout << "GetNSWindow: returning valid NSWindow pointer" << std::endl;
42-
return pimpl_->ns_window_;
42+
return (__bridge void*)pimpl_->ns_window_;
4343
}
4444

4545
Size Window::GetSize() const {

src/window_manager.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ class WindowManager {
1111
WindowManager();
1212
virtual ~WindowManager();
1313

14-
Window Create();
15-
16-
// Get the current window.
17-
Window GetCurrent();
14+
// Get a window by its ID.
15+
Window Get(WindowID id);
1816

1917
// Get all windows.
2018
std::vector<Window> GetAll();
19+
20+
// Get the current window.
21+
Window GetCurrent();
2122
};
2223

2324
} // namespace nativeapi

src/window_manager_macos.mm

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,24 @@
1414

1515
WindowManager::~WindowManager() {}
1616

17-
Window WindowManager::Create() {
18-
return *new Window();
17+
Window WindowManager::Get(WindowID id) {
18+
NSArray* ns_windows = [[NSApplication sharedApplication] windows];
19+
for (NSWindow* ns_window in ns_windows) {
20+
if ([ns_window windowNumber] == id) {
21+
std::cout << "Found window with ID: " << id << std::endl;
22+
return Window((__bridge void*)ns_window);
23+
}
24+
}
25+
return nullptr;
26+
}
27+
28+
std::vector<Window> WindowManager::GetAll() {
29+
std::vector<Window> windows;
30+
NSArray* ns_windows = [[NSApplication sharedApplication] windows];
31+
for (NSWindow* ns_window in ns_windows) {
32+
windows.push_back(Window((__bridge void*)ns_window));
33+
}
34+
return windows;
1935
}
2036

2137
Window WindowManager::GetCurrent() {
@@ -28,23 +44,7 @@
2844
std::cout << "Main window found." << std::endl;
2945
std::cout << "Window title: " << [[ns_window title] UTF8String] << std::endl;
3046
}
31-
return *new Window(ns_window);
32-
}
33-
34-
std::vector<Window> WindowManager::GetAll() {
35-
std::vector<Window> windowList;
36-
NSApplication* app = [NSApplication sharedApplication];
37-
NSArray* windows = [app windows];
38-
for (NSWindow* ns_window in windows) {
39-
if ([ns_window isVisible]) {
40-
NSRect frame = [ns_window frame];
41-
std::cout << "Window title: " << [[ns_window title] UTF8String] << std::endl;
42-
std::cout << "Window size: " << frame.size.width << "x" << frame.size.height
43-
<< std::endl;
44-
windowList.push_back(*new Window(ns_window));
45-
}
46-
}
47-
return windowList;
47+
return Window((__bridge void*)ns_window);
4848
}
4949

5050
} // namespace nativeapi

0 commit comments

Comments
 (0)