Skip to content

Commit dd1727f

Browse files
committed
refactor: Update Window and WindowManager classes to utilize smart pointers for better memory management and enhance window retrieval methods
1 parent c8a68aa commit dd1727f

File tree

4 files changed

+60
-49
lines changed

4 files changed

+60
-49
lines changed

examples/nswindow_example/main.mm

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,12 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification {
2727
[self.window makeMainWindow];
2828
[NSApp activateIgnoringOtherApps:YES];
2929

30-
// dispatch_async(dispatch_get_main_queue(), ^{
31-
32-
// });
33-
3430
// 延迟检查主窗口
35-
dispatch_async(dispatch_get_main_queue(), ^{
36-
NSLog(@"主窗口: %@", NSApp.mainWindow);
37-
NSLog(@"关键窗口: %@", NSApp.keyWindow);
38-
NSLog(@"所有窗口: %@", NSApp.windows);
39-
});
31+
dispatch_async(dispatch_get_main_queue(), ^{
32+
NSLog(@"主窗口: %@", NSApp.mainWindow);
33+
NSLog(@"关键窗口: %@", NSApp.keyWindow);
34+
NSLog(@"所有窗口: %@", NSApp.windows);
35+
});
4036

4137
[[NSNotificationCenter defaultCenter]
4238
addObserverForName:NSWindowDidBecomeMainNotification
@@ -50,25 +46,28 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification {
5046
WindowManager windowManager = WindowManager();
5147

5248
// 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;
49+
std::shared_ptr<Window> currentWindowPtr = windowManager.GetCurrent();
50+
if (currentWindowPtr != nullptr) {
51+
Window& currentWindow = *currentWindowPtr;
52+
std::cout << "Current Window Information:" << std::endl;
53+
std::cout << "ID: " << currentWindow.id << std::endl;
5654

57-
// Get window size
58-
auto size = currentWindow.GetSize();
59-
std::cout << "Window Size: " << size.width << "x" << size.height << std::endl;
55+
// Get window size
56+
auto size = currentWindow.GetSize();
57+
std::cout << "Window Size: " << size.width << "x" << size.height << std::endl;
58+
}
6059

61-
// Get all windows
62-
std::vector<Window> windowList = windowManager.GetAll();
63-
std::cout << "\nAll 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-
}
60+
// Get all windows
61+
std::vector<std::shared_ptr<Window>> windowList = (windowManager.GetAll());
62+
std::cout << "\nAll Windows Information:" << std::endl;
63+
for (size_t i = 0; i < windowList.size(); i++) {
64+
const Window& window = *windowList[i];
65+
std::cout << "Window " << (i + 1) << ":" << std::endl;
66+
std::cout << "ID: " << window.id << std::endl;
67+
auto windowSize = window.GetSize();
68+
std::cout << "Size: " << windowSize.width << "x" << windowSize.height
69+
<< std::endl;
70+
}
7271
}];
7372
}
7473

src/window_macos.mm

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414
NSWindow* ns_window_;
1515
};
1616

17-
Window::Window() : pimpl_(nullptr) {
17+
Window::Window() : pimpl_(new Impl(nil)) {
1818
id = -1;
19-
std::cout << "Window created with null pimpl_" << std::endl;
2019
}
2120

2221
Window::Window(void* window) : pimpl_(new Impl((__bridge NSWindow*)window)) {
2322
id = pimpl_->ns_window_ ? [pimpl_->ns_window_ windowNumber] : 0;
24-
std::cout << "Window created with NSWindow: " << pimpl_->ns_window_ << std::endl;
2523
}
2624

2725
Window::~Window() {
28-
std::cout << "Window destroyed, pimpl_: " << pimpl_ << std::endl;
2926
delete pimpl_;
3027
}
3128

src/window_manager.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <memory>
4+
#include <unordered_map>
35
#include <vector>
46

57
#include "window.h"
@@ -11,14 +13,18 @@ class WindowManager {
1113
WindowManager();
1214
virtual ~WindowManager();
1315

14-
// Get a window by its ID.
15-
Window Get(WindowID id);
16+
// Get a window by its ID. Returns nullptr if window not found.
17+
std::shared_ptr<Window> Get(WindowID id);
1618

1719
// Get all windows.
18-
std::vector<Window> GetAll();
20+
std::vector<std::shared_ptr<Window>> GetAll();
1921

20-
// Get the current window.
21-
Window GetCurrent();
22+
// Get the current window. Returns nullptr if no window is active.
23+
std::shared_ptr<Window> GetCurrent();
24+
25+
private:
26+
// Store window instances
27+
std::unordered_map<WindowID, std::shared_ptr<Window>> windows_;
2228
};
2329

2430
} // namespace nativeapi

src/window_manager_macos.mm

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,46 @@
1414

1515
WindowManager::~WindowManager() {}
1616

17-
Window WindowManager::Get(WindowID id) {
17+
std::shared_ptr<Window> WindowManager::Get(WindowID id) {
18+
auto it = windows_.find(id);
19+
if (it != windows_.end()) {
20+
return it->second;
21+
}
1822
NSArray* ns_windows = [[NSApplication sharedApplication] windows];
1923
for (NSWindow* ns_window in ns_windows) {
2024
if ([ns_window windowNumber] == id) {
21-
std::cout << "Found window with ID: " << id << std::endl;
22-
return Window((__bridge void*)ns_window);
25+
auto window = std::make_shared<Window>((__bridge void*)ns_window);
26+
windows_[id] = window;
27+
return window;
2328
}
2429
}
2530
return nullptr;
2631
}
2732

28-
std::vector<Window> WindowManager::GetAll() {
29-
std::vector<Window> windows;
33+
std::vector<std::shared_ptr<Window>> WindowManager::GetAll() {
34+
std::vector<std::shared_ptr<Window>> windows;
3035
NSArray* ns_windows = [[NSApplication sharedApplication] windows];
3136
for (NSWindow* ns_window in ns_windows) {
32-
windows.push_back(Window((__bridge void*)ns_window));
37+
WindowID window_id = [ns_window windowNumber];
38+
auto it = windows_.find(window_id);
39+
if (it == windows_.end()) {
40+
auto window = std::make_shared<Window>((__bridge void*)ns_window);
41+
windows_[window_id] = window;
42+
}
43+
}
44+
for (auto& window : windows_) {
45+
windows.push_back(window.second);
3346
}
3447
return windows;
3548
}
3649

37-
Window WindowManager::GetCurrent() {
50+
std::shared_ptr<Window> WindowManager::GetCurrent() {
3851
NSApplication* app = [NSApplication sharedApplication];
3952
NSWindow* ns_window = [app mainWindow];
40-
if (ns_window == nil) {
41-
std::cerr << "No main window found." << std::endl;
42-
return Window();
43-
} else {
44-
std::cout << "Main window found." << std::endl;
45-
std::cout << "Window title: " << [[ns_window title] UTF8String] << std::endl;
53+
if (ns_window != nil) {
54+
return Get([ns_window windowNumber]);
4655
}
47-
return Window((__bridge void*)ns_window);
56+
return nullptr;
4857
}
4958

5059
} // namespace nativeapi

0 commit comments

Comments
 (0)