Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions src/platform/linux/window_manager_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,25 @@

namespace nativeapi {

WindowManager::WindowManager() {
// Private implementation for Linux (stub for now)
class WindowManager::WindowManagerImpl {
public:
WindowManagerImpl(WindowManager* manager) : manager_(manager) {}
~WindowManagerImpl() {}

void SetupEventMonitoring() {
// TODO: Implement Linux-specific event monitoring using GTK signals
}

void CleanupEventMonitoring() {
// TODO: Implement Linux-specific cleanup
}

private:
WindowManager* manager_;
};

WindowManager::WindowManager() : impl_(std::make_unique<WindowManagerImpl>(this)) {
// Try to initialize GTK if not already initialized
// In headless environments, this may fail, which is acceptable
if (!gdk_display_get_default()) {
Expand All @@ -29,9 +47,25 @@ WindowManager::WindowManager() {
// gtk_init_check returns FALSE if initialization failed (e.g., no display)
// This is acceptable for headless environments
}

SetupEventMonitoring();
}

WindowManager::~WindowManager() {
CleanupEventMonitoring();
}

void WindowManager::SetupEventMonitoring() {
impl_->SetupEventMonitoring();
}

WindowManager::~WindowManager() {}
void WindowManager::CleanupEventMonitoring() {
impl_->CleanupEventMonitoring();
}

void WindowManager::DispatchWindowEvent(const Event& event) {
event_dispatcher_.DispatchSync(event);
}

std::shared_ptr<Window> WindowManager::Get(WindowID id) {
auto it = windows_.find(id);
Expand Down Expand Up @@ -131,4 +165,20 @@ std::shared_ptr<Window> WindowManager::GetCurrent() {
return nullptr;
}

std::shared_ptr<Window> WindowManager::Create(const WindowOptions& options) {
// TODO: Implement Linux window creation using GTK
// For now, return nullptr as this is a stub implementation
return nullptr;
}

bool WindowManager::Destroy(WindowID id) {
auto it = windows_.find(id);
if (it != windows_.end()) {
// TODO: Implement proper GTK window destruction
windows_.erase(it);
return true;
}
return false;
}

} // namespace nativeapi
215 changes: 213 additions & 2 deletions src/platform/macos/window_manager_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,198 @@
#include "../../window.h"
#include "../../window_manager.h"

// Forward declaration for the delegate
@class NativeAPIWindowManagerDelegate;

namespace nativeapi {

// Private implementation to hide Objective-C details
class WindowManager::WindowManagerImpl {
public:
WindowManagerImpl(WindowManager* manager);
~WindowManagerImpl();

void SetupEventMonitoring();
void CleanupEventMonitoring();
void OnWindowEvent(NSWindow* window, const std::string& event_type);

private:
WindowManager* manager_;
NativeAPIWindowManagerDelegate* delegate_;
};

} // namespace nativeapi

// Objective-C delegate class to handle NSWindow notifications
@interface NativeAPIWindowManagerDelegate : NSObject
@property (nonatomic, assign) nativeapi::WindowManager::WindowManagerImpl* impl;
- (instancetype)initWithImpl:(nativeapi::WindowManager::WindowManagerImpl*)impl;
@end

@implementation NativeAPIWindowManagerDelegate

- (instancetype)initWithImpl:(nativeapi::WindowManager::WindowManagerImpl*)impl {
if (self = [super init]) {
_impl = impl;
}
return self;
}

- (void)windowDidBecomeKey:(NSNotification*)notification {
NSWindow* window = [notification object];
if (_impl) {
_impl->OnWindowEvent(window, "focused");
}
}

- (void)windowDidResignKey:(NSNotification*)notification {
NSWindow* window = [notification object];
if (_impl) {
_impl->OnWindowEvent(window, "blurred");
}
}

- (void)windowDidMiniaturize:(NSNotification*)notification {
NSWindow* window = [notification object];
if (_impl) {
_impl->OnWindowEvent(window, "minimized");
}
}

- (void)windowDidDeminiaturize:(NSNotification*)notification {
NSWindow* window = [notification object];
if (_impl) {
_impl->OnWindowEvent(window, "restored");
}
}

- (void)windowDidResize:(NSNotification*)notification {
NSWindow* window = [notification object];
if (_impl) {
_impl->OnWindowEvent(window, "resized");
}
}

- (void)windowDidMove:(NSNotification*)notification {
NSWindow* window = [notification object];
if (_impl) {
_impl->OnWindowEvent(window, "moved");
}
}

- (void)windowWillClose:(NSNotification*)notification {
NSWindow* window = [notification object];
if (_impl) {
_impl->OnWindowEvent(window, "closing");
}
}

@end

namespace nativeapi {

WindowManager::WindowManager() {}
WindowManager::WindowManagerImpl::WindowManagerImpl(WindowManager* manager)
: manager_(manager), delegate_(nullptr) {
}

WindowManager::WindowManagerImpl::~WindowManagerImpl() {
CleanupEventMonitoring();
}

void WindowManager::WindowManagerImpl::SetupEventMonitoring() {
if (!delegate_) {
delegate_ = [[NativeAPIWindowManagerDelegate alloc] initWithImpl:this];

NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:delegate_
selector:@selector(windowDidBecomeKey:)
name:NSWindowDidBecomeKeyNotification
object:nil];
[center addObserver:delegate_
selector:@selector(windowDidResignKey:)
name:NSWindowDidResignKeyNotification
object:nil];
[center addObserver:delegate_
selector:@selector(windowDidMiniaturize:)
name:NSWindowDidMiniaturizeNotification
object:nil];
[center addObserver:delegate_
selector:@selector(windowDidDeminiaturize:)
name:NSWindowDidDeminiaturizeNotification
object:nil];
[center addObserver:delegate_
selector:@selector(windowDidResize:)
name:NSWindowDidResizeNotification
object:nil];
[center addObserver:delegate_
selector:@selector(windowDidMove:)
name:NSWindowDidMoveNotification
object:nil];
[center addObserver:delegate_
selector:@selector(windowWillClose:)
name:NSWindowWillCloseNotification
object:nil];
}
}

void WindowManager::WindowManagerImpl::CleanupEventMonitoring() {
if (delegate_) {
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center removeObserver:delegate_];
delegate_ = nil;
}
}

void WindowManager::WindowManagerImpl::OnWindowEvent(NSWindow* window, const std::string& event_type) {
WindowID window_id = [window windowNumber];

if (event_type == "focused") {
WindowFocusedEvent event(window_id);
manager_->DispatchWindowEvent(event);
} else if (event_type == "blurred") {
WindowBlurredEvent event(window_id);
manager_->DispatchWindowEvent(event);
} else if (event_type == "minimized") {
WindowMinimizedEvent event(window_id);
manager_->DispatchWindowEvent(event);
} else if (event_type == "restored") {
WindowRestoredEvent event(window_id);
manager_->DispatchWindowEvent(event);
} else if (event_type == "resized") {
NSRect frame = [window frame];
Size new_size = {frame.size.width, frame.size.height};
WindowResizedEvent event(window_id, new_size);
manager_->DispatchWindowEvent(event);
} else if (event_type == "moved") {
NSRect frame = [window frame];
Point new_position = {frame.origin.x, frame.origin.y};
WindowMovedEvent event(window_id, new_position);
manager_->DispatchWindowEvent(event);
} else if (event_type == "closing") {
WindowClosedEvent event(window_id);
manager_->DispatchWindowEvent(event);
}
}

WindowManager::WindowManager() : impl_(std::make_unique<WindowManagerImpl>(this)) {
SetupEventMonitoring();
}

WindowManager::~WindowManager() {
CleanupEventMonitoring();
}

void WindowManager::SetupEventMonitoring() {
impl_->SetupEventMonitoring();
}

WindowManager::~WindowManager() {}
void WindowManager::CleanupEventMonitoring() {
impl_->CleanupEventMonitoring();
}

void WindowManager::DispatchWindowEvent(const Event& event) {
event_dispatcher_.DispatchSync(event);
}

// Create a new window with the given options.
std::shared_ptr<Window> WindowManager::Create(const WindowOptions& options) {
Expand All @@ -28,9 +215,33 @@
WindowID window_id = [ns_window windowNumber];
auto window = std::make_shared<Window>((__bridge void*)ns_window);
windows_[window_id] = window;

// Dispatch window created event
WindowCreatedEvent created_event(window_id);
DispatchWindowEvent(created_event);

return window;
}

// Destroy a window by its ID. Returns true if window was destroyed.
bool WindowManager::Destroy(WindowID id) {
auto it = windows_.find(id);
if (it != windows_.end()) {
// Get the NSWindow to close it
NSArray* ns_windows = [[NSApplication sharedApplication] windows];
for (NSWindow* ns_window in ns_windows) {
if ([ns_window windowNumber] == id) {
[ns_window close];
windows_.erase(it);
return true;
}
}
// Remove from our map even if we couldn't find the NSWindow
windows_.erase(it);
}
return false;
}

std::shared_ptr<Window> WindowManager::Get(WindowID id) {
auto it = windows_.find(id);
if (it != windows_.end()) {
Expand Down
57 changes: 55 additions & 2 deletions src/platform/windows/window_manager_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,43 @@

namespace nativeapi {

WindowManager::WindowManager() {}
// Private implementation for Windows (stub for now)
class WindowManager::WindowManagerImpl {
public:
WindowManagerImpl(WindowManager* manager) : manager_(manager) {}
~WindowManagerImpl() {}

void SetupEventMonitoring() {
// TODO: Implement Windows-specific event monitoring
}

void CleanupEventMonitoring() {
// TODO: Implement Windows-specific cleanup
}

private:
WindowManager* manager_;
};

WindowManager::~WindowManager() {}
WindowManager::WindowManager() : impl_(std::make_unique<WindowManagerImpl>(this)) {
SetupEventMonitoring();
}

WindowManager::~WindowManager() {
CleanupEventMonitoring();
}

void WindowManager::SetupEventMonitoring() {
impl_->SetupEventMonitoring();
}

void WindowManager::CleanupEventMonitoring() {
impl_->CleanupEventMonitoring();
}

void WindowManager::DispatchWindowEvent(const Event& event) {
event_dispatcher_.DispatchSync(event);
}

std::shared_ptr<Window> WindowManager::Get(WindowID id) {
auto it = windows_.find(id);
Expand Down Expand Up @@ -76,6 +110,25 @@ std::vector<std::shared_ptr<Window>> WindowManager::GetAll() {
return result;
}

std::shared_ptr<Window> WindowManager::Create(const WindowOptions& options) {
// TODO: Implement Windows window creation
// For now, return nullptr as this is a stub implementation
return nullptr;
}

bool WindowManager::Destroy(WindowID id) {
auto it = windows_.find(id);
if (it != windows_.end()) {
HWND hwnd = reinterpret_cast<HWND>(id);
if (IsWindow(hwnd)) {
DestroyWindow(hwnd);
}
windows_.erase(it);
return true;
}
return false;
}

std::shared_ptr<Window> WindowManager::GetCurrent() {
HWND hwnd = GetForegroundWindow();
if (hwnd) {
Expand Down
Loading
Loading