Skip to content

Commit 7211f18

Browse files
committed
feat: Implement Window and WindowManager classes for macOS
1 parent a5751e6 commit 7211f18

File tree

9 files changed

+154
-2
lines changed

9 files changed

+154
-2
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ add_subdirectory(src)
77

88
# Add example programs subdirectory
99
add_subdirectory(examples/screen_info)
10+
add_subdirectory(examples/window_example)
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(window_example VERSION 0.0.1 LANGUAGES CXX)
4+
5+
# Set C++ standard
6+
set(CMAKE_CXX_STANDARD 11)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
9+
# Add example program
10+
add_executable(window_example
11+
"main.cpp"
12+
)
13+
14+
# Link main library
15+
target_link_libraries(window_example PRIVATE libnativeapi)
16+
17+
# Set example program properties
18+
set_target_properties(window_example PROPERTIES
19+
OUTPUT_NAME "window_example"
20+
)
21+
22+
# Set example program compile options (macOS only)
23+
if(APPLE)
24+
set_source_files_properties("main.cpp"
25+
PROPERTIES
26+
COMPILE_FLAGS "-x objective-c++"
27+
)
28+
endif()

examples/window_example/main.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include "nativeapi.h"
3+
4+
using nativeapi::Window;
5+
using nativeapi::WindowManager;
6+
7+
int main() {
8+
WindowManager windowManager = WindowManager();
9+
10+
windowManager.Create();
11+
12+
// Get primary display information
13+
Window currentWindow = windowManager.GetCurrent();
14+
std::cout << "Current Window Information:" << std::endl;
15+
std::cout << "ID: " << currentWindow.id << std::endl;
16+
std::cout << "Name: " << currentWindow.name << std::endl;
17+
std::cout << std::endl;
18+
19+
// Get all displays information
20+
std::vector<Window> windowList = windowManager.GetAll();
21+
std::cout << "All Windows Information:" << std::endl;
22+
for (int i = 0; i < windowList.size(); i++) {
23+
Window& window = windowList[i];
24+
std::cout << "Window " << (i + 1) << ":" << std::endl;
25+
std::cout << "ID: " << window.id << std::endl;
26+
std::cout << "Name: " << window.name << std::endl;
27+
std::cout << std::endl;
28+
}
29+
return 0;
30+
}

include/nativeapi.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
#include "../src/display.h"
44
#include "../src/geometry.h"
5-
#include "../src/screen_retriever.h"
5+
#include "../src/screen_retriever.h"
6+
#include "../src/window.h"
7+
#include "../src/window_manager.h"

src/screen_retriever.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ enum class ScreenEventType : uint8_t {
1616
DisplayRemoved = 0x02, // Display disconnected
1717
};
1818

19-
// Abstract base class for ScreenRetriever
19+
// ScreenRetriever is a singleton that manages all screens on the system.
2020
class ScreenRetriever {
2121
public:
2222
ScreenRetriever();

src/window.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include <string>
3+
4+
namespace nativeapi {
5+
6+
class Window {
7+
public:
8+
Window();
9+
virtual ~Window();
10+
11+
std::string id;
12+
std::string name;
13+
};
14+
15+
} // namespace nativeapi

src/window_macos.mm

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "window.h"
2+
#include "window_manager.h"
3+
4+
// Import Cocoa headers
5+
#import <Cocoa/Cocoa.h>
6+
7+
namespace nativeapi {
8+
9+
Window::Window() {
10+
id = "window1";
11+
name = "Window 1";
12+
}
13+
14+
Window::~Window() {}
15+
16+
} // namespace nativeapi

src/window_manager.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include <vector>
4+
5+
namespace nativeapi {
6+
7+
// WindowManager is a singleton that manages all windows on the system.
8+
class WindowManager {
9+
public:
10+
WindowManager();
11+
virtual ~WindowManager();
12+
13+
Window Create();
14+
15+
// Get the current window.
16+
Window GetCurrent();
17+
18+
// Get all windows.
19+
std::vector<Window> GetAll();
20+
};
21+
22+
} // namespace nativeapi

src/window_manager_macos.mm

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <cstring>
2+
#include <iostream>
3+
#include <string>
4+
#include "window.h"
5+
#include "window_manager.h"
6+
7+
// Import Cocoa headers
8+
#import <Cocoa/Cocoa.h>
9+
10+
namespace nativeapi {
11+
12+
WindowManager::WindowManager() {}
13+
14+
WindowManager::~WindowManager() {}
15+
16+
Window WindowManager::Create() {
17+
return *new Window();
18+
}
19+
20+
Window WindowManager::GetCurrent() {
21+
NSApplication* app = [NSApplication sharedApplication];
22+
NSWindow* window = [app mainWindow];
23+
if (window == nil) {
24+
std::cerr << "No main window found." << std::endl;
25+
return Window();
26+
}
27+
return *new Window();
28+
}
29+
30+
std::vector<Window> WindowManager::GetAll() {
31+
std::vector<Window> displayList;
32+
33+
displayList.push_back(GetCurrent());
34+
35+
return displayList;
36+
}
37+
38+
} // namespace nativeapi

0 commit comments

Comments
 (0)