Skip to content

Commit e447310

Browse files
committed
Refactor window management to use WindowRegistry
Replaces platform-specific window storage with the shared WindowRegistry for Android, iOS, and OpenHarmony. This centralizes window tracking and simplifies window management logic across platforms.
1 parent 8930e89 commit e447310

File tree

3 files changed

+36
-57
lines changed

3 files changed

+36
-57
lines changed

src/platform/android/window_manager_android.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <unordered_map>
77
#include "../../window.h"
88
#include "../../window_manager.h"
9+
#include "../../window_registry.h"
910

1011
#define LOG_TAG "NativeApi"
1112
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
@@ -90,49 +91,41 @@ void WindowManager::DispatchWindowEvent(const WindowEvent& event) {
9091
}
9192

9293
std::shared_ptr<Window> WindowManager::Get(WindowId id) {
93-
auto it = windows_.find(id);
94-
if (it != windows_.end()) {
95-
return it->second;
94+
auto cached = WindowRegistry::GetInstance().Get(id);
95+
if (cached) {
96+
return cached;
9697
}
9798

9899
// Try to find the window by ID
99100
ANativeWindow* native_window = FindNativeWindowById(id);
100101
if (native_window) {
101102
auto window = std::make_shared<Window>(static_cast<void*>(native_window));
102-
windows_[id] = window;
103+
WindowRegistry::GetInstance().Add(id, window);
103104
return window;
104105
}
105106

106107
return nullptr;
107108
}
108109

109110
std::vector<std::shared_ptr<Window>> WindowManager::GetAll() {
110-
std::vector<std::shared_ptr<Window>> windows;
111-
112-
for (const auto& [id, window] : windows_) {
113-
windows.push_back(window);
114-
}
115-
116-
return windows;
111+
return WindowRegistry::GetInstance().GetAll();
117112
}
118113

119114
std::shared_ptr<Window> WindowManager::GetCurrent() {
120115
// On Android, the current window is typically the Activity's native window
121116
// This would need to be set by the Activity lifecycle callbacks
122-
if (!windows_.empty()) {
123-
return windows_.begin()->second;
124-
}
125-
return nullptr;
117+
auto all = WindowRegistry::GetInstance().GetAll();
118+
return all.empty() ? nullptr : all.front();
126119
}
127120

128121
bool WindowManager::Destroy(WindowId id) {
129-
auto it = windows_.find(id);
130-
if (it != windows_.end()) {
131-
windows_.erase(it);
132-
Emit<WindowClosedEvent>(id);
133-
return true;
122+
auto window = WindowRegistry::GetInstance().Get(id);
123+
if (!window) {
124+
return false;
134125
}
135-
return false;
126+
WindowRegistry::GetInstance().Remove(id);
127+
Emit<WindowClosedEvent>(id);
128+
return true;
136129
}
137130

138131
std::shared_ptr<Window> WindowManager::Create(const WindowOptions& options) {

src/platform/ios/window_manager_ios.mm

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import <Foundation/Foundation.h>
22
#import <UIKit/UIKit.h>
33
#include "../../window_manager.h"
4+
#include "../../window_registry.h"
45

56
namespace nativeapi {
67

@@ -28,32 +29,24 @@
2829
auto window = std::make_shared<Window>((__bridge void*)uiWindow);
2930

3031
if (window) {
31-
windows_[window->GetId()] = window;
32+
WindowRegistry::GetInstance().Add(window->GetId(), window);
3233
Emit<WindowCreatedEvent>(window->GetId());
3334
}
3435

3536
return window;
3637
}
3738

3839
std::shared_ptr<Window> WindowManager::Get(WindowId id) {
39-
auto it = windows_.find(id);
40-
return (it != windows_.end()) ? it->second : nullptr;
40+
return WindowRegistry::GetInstance().Get(id);
4141
}
4242

4343
std::vector<std::shared_ptr<Window>> WindowManager::GetAll() {
44-
std::vector<std::shared_ptr<Window>> result;
45-
result.reserve(windows_.size());
46-
47-
for (const auto& [id, window] : windows_) {
48-
result.push_back(window);
49-
}
50-
51-
return result;
44+
return WindowRegistry::GetInstance().GetAll();
5245
}
5346

5447
std::shared_ptr<Window> WindowManager::GetCurrent() {
5548
// Find the first key window
56-
for (const auto& [id, window] : windows_) {
49+
for (const auto& window : WindowRegistry::GetInstance().GetAll()) {
5750
if (window->IsFocused()) {
5851
return window;
5952
}
@@ -62,12 +55,12 @@
6255
}
6356

6457
bool WindowManager::Destroy(WindowId id) {
65-
auto it = windows_.find(id);
66-
if (it == windows_.end()) {
58+
auto window = WindowRegistry::GetInstance().Get(id);
59+
if (!window) {
6760
return false;
6861
}
6962

70-
windows_.erase(it);
63+
WindowRegistry::GetInstance().Remove(id);
7164
Emit<WindowClosedEvent>(id);
7265

7366
return true;

src/platform/ohos/window_manager_ohos.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <unordered_map>
88
#include "../../window.h"
99
#include "../../window_manager.h"
10+
#include "../../window_registry.h"
1011

1112
namespace nativeapi {
1213

@@ -87,48 +88,40 @@ void WindowManager::DispatchWindowEvent(const WindowEvent& event) {
8788
}
8889

8990
std::shared_ptr<Window> WindowManager::Get(WindowId id) {
90-
auto it = windows_.find(id);
91-
if (it != windows_.end()) {
92-
return it->second;
91+
auto cached = WindowRegistry::GetInstance().Get(id);
92+
if (cached) {
93+
return cached;
9394
}
9495

9596
// Try to find the window by ID
9697
void* native_window = FindNativeWindowById(id);
9798
if (native_window) {
9899
auto window = std::make_shared<Window>(native_window);
99-
windows_[id] = window;
100+
WindowRegistry::GetInstance().Add(id, window);
100101
return window;
101102
}
102103

103104
return nullptr;
104105
}
105106

106107
std::vector<std::shared_ptr<Window>> WindowManager::GetAll() {
107-
std::vector<std::shared_ptr<Window>> windows;
108-
109-
for (const auto& [id, window] : windows_) {
110-
windows.push_back(window);
111-
}
112-
113-
return windows;
108+
return WindowRegistry::GetInstance().GetAll();
114109
}
115110

116111
std::shared_ptr<Window> WindowManager::GetCurrent() {
117112
// On OpenHarmony, the current window is typically the Ability's window
118-
if (!windows_.empty()) {
119-
return windows_.begin()->second;
120-
}
121-
return nullptr;
113+
auto all = WindowRegistry::GetInstance().GetAll();
114+
return all.empty() ? nullptr : all.front();
122115
}
123116

124117
bool WindowManager::Destroy(WindowId id) {
125-
auto it = windows_.find(id);
126-
if (it != windows_.end()) {
127-
windows_.erase(it);
128-
Emit<WindowClosedEvent>(id);
129-
return true;
118+
auto window = WindowRegistry::GetInstance().Get(id);
119+
if (!window) {
120+
return false;
130121
}
131-
return false;
122+
WindowRegistry::GetInstance().Remove(id);
123+
Emit<WindowClosedEvent>(id);
124+
return true;
132125
}
133126

134127
std::shared_ptr<Window> WindowManager::Create(const WindowOptions& options) {

0 commit comments

Comments
 (0)