Skip to content

Commit 429fa1a

Browse files
committed
Refactor window creation API and remove WindowOptions
Window creation now uses default settings and is configured post-creation, removing the WindowOptions struct and related C API. WindowManager::Create and platform-specific implementations are removed; window registration is handled automatically. WindowCreatedEvent and WindowClosedEvent are no longer emitted, and related event types are removed from the C API. WindowRegistry gains a Contains method. Example code updated to reflect new window creation and configuration flow.
1 parent a3c106e commit 429fa1a

File tree

23 files changed

+242
-549
lines changed

23 files changed

+242
-549
lines changed

examples/application_c_example/main.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,17 @@ int main() {
4747
return 1;
4848
}
4949

50-
// Create a simple window
51-
native_window_options_t* window_options = native_window_options_create();
52-
if (!window_options) {
53-
fprintf(stderr, "Failed to create window options\n");
54-
return 1;
55-
}
56-
57-
native_window_options_set_title(window_options, "Application C Example Window");
58-
native_window_options_set_size(window_options, 400.0, 300.0);
59-
60-
native_window_t window = native_window_manager_create(window_options);
50+
// Create a simple window with default settings
51+
native_window_t window = native_window_manager_create();
6152
if (!window) {
6253
fprintf(stderr, "Failed to create window\n");
63-
native_window_options_destroy(window_options);
6454
return 1;
6555
}
6656

57+
// Configure the window
58+
native_window_set_title(window, "Application C Example Window");
59+
native_window_set_size(window, 400.0, 300.0, false);
60+
6761
printf("Window created successfully\n");
6862
printf("Window ID: %ld\n", native_window_get_id(window));
6963

@@ -80,7 +74,6 @@ int main() {
8074

8175
// Clean up
8276
native_application_remove_event_listener(app, listener_id);
83-
native_window_options_destroy(window_options);
8477

8578
return exit_code;
8679
}

examples/application_example/main.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ int main() {
3535
std::cout << "Application deactivated event received" << std::endl;
3636
});
3737

38-
// Create a simple window
38+
// Create a simple window (automatically registered)
3939
auto& window_manager = WindowManager::GetInstance();
4040

41-
WindowOptions window_options;
42-
window_options.title = "Application Example Window";
41+
auto window = std::make_shared<Window>();
42+
window->SetTitle("Application Example Window");
4343

44-
auto window = window_manager.Create(window_options);
4544
if (!window) {
4645
std::cerr << "Failed to create window" << std::endl;
4746
return 1;

examples/window_c_example/main.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,15 @@ native_menu_t create_context_menu(void) {
309309
}
310310

311311
int main() {
312-
// Create a new window with options
313-
native_window_options_t* options = native_window_options_create();
314-
native_window_options_set_title(options, "Window Example");
315-
native_window_options_set_size(options, 800, 600);
316-
native_window_options_set_minimum_size(options, 400, 300);
317-
native_window_options_set_maximum_size(options, 1920, 1080);
318-
native_window_options_set_centered(options, true);
319-
320-
g_window = native_window_manager_create(options);
321-
native_window_options_destroy(options);
312+
// Create a new window with default settings
313+
g_window = native_window_manager_create();
314+
315+
// Configure the window
316+
native_window_set_title(g_window, "Window Example");
317+
native_window_set_size(g_window, 800, 600, false);
318+
native_window_set_minimum_size(g_window, 400, 300);
319+
native_window_set_maximum_size(g_window, 1920, 1080);
320+
native_window_center(g_window);
322321

323322
// Create tray icon
324323
g_tray_icon = native_tray_icon_create();

examples/window_example/main.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ using nativeapi::TrayIconRightClickedEvent;
2222
using nativeapi::TrayManager;
2323
using nativeapi::Window;
2424
using nativeapi::WindowManager;
25-
using nativeapi::WindowOptions;
2625

2726
int main() {
2827
native_accessibility_manager_enable();
@@ -33,14 +32,13 @@ int main() {
3332
TrayManager& tray_manager = TrayManager::GetInstance();
3433
WindowManager& window_manager = WindowManager::GetInstance();
3534

36-
// Create a new window with options
37-
WindowOptions options;
38-
options.title = "Window Example";
39-
options.size = {800, 600};
40-
options.minimum_size = {400, 300};
41-
options.maximum_size = {1920, 1080};
42-
options.centered = true;
43-
std::shared_ptr<Window> window_ptr = window_manager.Create(options);
35+
// Create a new window (automatically registered)
36+
std::shared_ptr<Window> window_ptr = std::make_shared<Window>();
37+
window_ptr->SetTitle("Window Example");
38+
window_ptr->SetSize({800, 600}, false);
39+
window_ptr->SetMinimumSize({400, 300});
40+
window_ptr->SetMaximumSize({1920, 1080});
41+
window_ptr->Center();
4442

4543
std::shared_ptr<TrayIcon> tray_icon_ptr = std::make_shared<TrayIcon>();
4644
if (tray_icon_ptr != nullptr) {

src/capi/window_c.cpp

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,6 @@
55

66
using namespace nativeapi;
77

8-
// Window options creation and destruction
9-
FFI_PLUGIN_EXPORT
10-
native_window_options_t* native_window_options_create(void) {
11-
auto* options = new (std::nothrow) native_window_options_t;
12-
if (!options)
13-
return nullptr;
14-
15-
// Initialize with default values
16-
options->title = nullptr;
17-
options->size = {800.0, 600.0};
18-
options->minimum_size = {0.0, 0.0};
19-
options->maximum_size = {0.0, 0.0};
20-
options->centered = true;
21-
22-
return options;
23-
}
24-
25-
FFI_PLUGIN_EXPORT
26-
void native_window_options_destroy(native_window_options_t* options) {
27-
if (!options)
28-
return;
29-
30-
if (options->title) {
31-
free_c_str(options->title);
32-
}
33-
delete options;
34-
}
35-
36-
FFI_PLUGIN_EXPORT
37-
bool native_window_options_set_title(native_window_options_t* options, const char* title) {
38-
if (!options)
39-
return false;
40-
41-
// Free existing title
42-
if (options->title) {
43-
free_c_str(options->title);
44-
options->title = nullptr;
45-
}
46-
47-
if (title) {
48-
std::string title_str(title);
49-
options->title = to_c_str(title_str);
50-
// to_c_str returns nullptr for empty strings, which is acceptable for title
51-
// For non-empty strings, nullptr indicates allocation failure
52-
if (!options->title && !title_str.empty()) {
53-
return false;
54-
}
55-
}
56-
57-
return true;
58-
}
59-
60-
FFI_PLUGIN_EXPORT
61-
void native_window_options_set_size(native_window_options_t* options, double width, double height) {
62-
if (!options)
63-
return;
64-
options->size.width = width;
65-
options->size.height = height;
66-
}
67-
68-
FFI_PLUGIN_EXPORT
69-
void native_window_options_set_minimum_size(native_window_options_t* options,
70-
double width,
71-
double height) {
72-
if (!options)
73-
return;
74-
options->minimum_size.width = width;
75-
options->minimum_size.height = height;
76-
}
77-
78-
FFI_PLUGIN_EXPORT
79-
void native_window_options_set_maximum_size(native_window_options_t* options,
80-
double width,
81-
double height) {
82-
if (!options)
83-
return;
84-
options->maximum_size.width = width;
85-
options->maximum_size.height = height;
86-
}
87-
88-
FFI_PLUGIN_EXPORT
89-
void native_window_options_set_centered(native_window_options_t* options, bool centered) {
90-
if (!options)
91-
return;
92-
options->centered = centered;
93-
}
94-
958
// Window basic operations
969
FFI_PLUGIN_EXPORT
9710
native_window_id_t native_window_get_id(native_window_t window) {

src/capi/window_c.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ extern "C" {
2020
*/
2121
typedef long native_window_id_t;
2222

23-
/**
24-
* Window options structure for creating windows
25-
*/
26-
typedef struct {
27-
char* title; // Window title
28-
native_size_t size; // Initial window size
29-
native_size_t minimum_size; // Minimum window size
30-
native_size_t maximum_size; // Maximum window size
31-
bool centered; // Whether to center the window on screen
32-
} native_window_options_t;
33-
3423
/**
3524
* Opaque window handle
3625
*/
@@ -44,32 +33,6 @@ typedef struct {
4433
long count;
4534
} native_window_list_t;
4635

47-
// Window creation and destruction
48-
FFI_PLUGIN_EXPORT
49-
native_window_options_t* native_window_options_create(void);
50-
51-
FFI_PLUGIN_EXPORT
52-
void native_window_options_destroy(native_window_options_t* options);
53-
54-
FFI_PLUGIN_EXPORT
55-
bool native_window_options_set_title(native_window_options_t* options, const char* title);
56-
57-
FFI_PLUGIN_EXPORT
58-
void native_window_options_set_size(native_window_options_t* options, double width, double height);
59-
60-
FFI_PLUGIN_EXPORT
61-
void native_window_options_set_minimum_size(native_window_options_t* options,
62-
double width,
63-
double height);
64-
65-
FFI_PLUGIN_EXPORT
66-
void native_window_options_set_maximum_size(native_window_options_t* options,
67-
double width,
68-
double height);
69-
70-
FFI_PLUGIN_EXPORT
71-
void native_window_options_set_centered(native_window_options_t* options, bool centered);
72-
7336
// Window basic operations
7437
FFI_PLUGIN_EXPORT
7538
native_window_id_t native_window_get_id(native_window_t window);

src/capi/window_manager_c.cpp

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,6 @@ static std::mutex g_callback_mutex;
1919
static std::unordered_map<int, EventCallbackInfo> g_event_callbacks;
2020
static int g_next_callback_id = 1;
2121

22-
// Helper function to convert C++ WindowOptions to C options
23-
static WindowOptions ConvertToWindowOptions(const native_window_options_t* options) {
24-
WindowOptions cpp_options;
25-
26-
if (options->title) {
27-
cpp_options.title = std::string(options->title);
28-
}
29-
30-
cpp_options.size.width = options->size.width;
31-
cpp_options.size.height = options->size.height;
32-
cpp_options.minimum_size.width = options->minimum_size.width;
33-
cpp_options.minimum_size.height = options->minimum_size.height;
34-
cpp_options.maximum_size.width = options->maximum_size.width;
35-
cpp_options.maximum_size.height = options->maximum_size.height;
36-
cpp_options.centered = options->centered;
37-
38-
return cpp_options;
39-
}
40-
4122
// Helper function to create native_window_t from shared_ptr<Window>
4223
static native_window_t CreateNativeWindowHandle(std::shared_ptr<Window> window) {
4324
if (!window)
@@ -67,20 +48,6 @@ class CEventListener {
6748
auto& manager = WindowManager::GetInstance();
6849

6950
// Register for various window events
70-
manager.AddListener<WindowCreatedEvent>([this](const WindowCreatedEvent& e) {
71-
native_window_event_t event;
72-
event.type = NATIVE_WINDOW_EVENT_CREATED;
73-
event.window_id = e.GetWindowId();
74-
DispatchEvent(event);
75-
});
76-
77-
manager.AddListener<WindowClosedEvent>([this](const WindowClosedEvent& e) {
78-
native_window_event_t event;
79-
event.type = NATIVE_WINDOW_EVENT_CLOSED;
80-
event.window_id = e.GetWindowId();
81-
DispatchEvent(event);
82-
});
83-
8451
manager.AddListener<WindowFocusedEvent>([this](const WindowFocusedEvent& e) {
8552
native_window_event_t event;
8653
event.type = NATIVE_WINDOW_EVENT_FOCUSED;
@@ -147,15 +114,10 @@ static void* g_will_hide_ud = nullptr;
147114

148115
// Window manager operations
149116
FFI_PLUGIN_EXPORT
150-
native_window_t native_window_manager_create(const native_window_options_t* options) {
151-
if (!options)
152-
return nullptr;
153-
117+
native_window_t native_window_manager_create(void) {
154118
try {
155-
WindowOptions cpp_options = ConvertToWindowOptions(options);
156-
auto& manager = WindowManager::GetInstance();
157-
auto window = manager.Create(cpp_options);
158-
119+
// Create window with default settings (automatically registered)
120+
auto window = std::make_shared<Window>();
159121
return CreateNativeWindowHandle(window);
160122
} catch (...) {
161123
return nullptr;

src/capi/window_manager_c.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ extern "C" {
1919
* Window event types
2020
*/
2121
typedef enum {
22-
NATIVE_WINDOW_EVENT_CREATED = 0,
23-
NATIVE_WINDOW_EVENT_CLOSED = 1,
24-
NATIVE_WINDOW_EVENT_FOCUSED = 2,
25-
NATIVE_WINDOW_EVENT_BLURRED = 3,
26-
NATIVE_WINDOW_EVENT_MINIMIZED = 4,
27-
NATIVE_WINDOW_EVENT_MAXIMIZED = 5,
28-
NATIVE_WINDOW_EVENT_RESTORED = 6,
29-
NATIVE_WINDOW_EVENT_MOVED = 7,
30-
NATIVE_WINDOW_EVENT_RESIZED = 8
22+
NATIVE_WINDOW_EVENT_FOCUSED = 0,
23+
NATIVE_WINDOW_EVENT_BLURRED = 1,
24+
NATIVE_WINDOW_EVENT_MINIMIZED = 2,
25+
NATIVE_WINDOW_EVENT_MAXIMIZED = 3,
26+
NATIVE_WINDOW_EVENT_RESTORED = 4,
27+
NATIVE_WINDOW_EVENT_MOVED = 5,
28+
NATIVE_WINDOW_EVENT_RESIZED = 6
3129
} native_window_event_type_t;
3230

3331
/**
@@ -56,12 +54,11 @@ typedef void (*native_window_event_callback_t)(const native_window_event_t* even
5654
*/
5755

5856
/**
59-
* Create a new window with the specified options
60-
* @param options Window creation options
57+
* Create a new window with default settings
6158
* @return Window handle, or NULL if creation failed
6259
*/
6360
FFI_PLUGIN_EXPORT
64-
native_window_t native_window_manager_create(const native_window_options_t* options);
61+
native_window_t native_window_manager_create(void);
6562

6663
/**
6764
* Get a window by its ID

src/foundation/object_registry.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ class ObjectRegistry {
5858
return it == objects_.end() ? nullptr : it->second;
5959
}
6060

61+
/**
62+
* @brief Check if an object exists for the given ID.
63+
*
64+
* This operation is O(1) average-case.
65+
*
66+
* @param id The identifier to check.
67+
* @return true If an entry exists for the given ID.
68+
* @return false If no entry exists for the given ID.
69+
*/
70+
bool Contains(TId id) const {
71+
std::lock_guard<std::mutex> lock(mutex_);
72+
return objects_.find(id) != objects_.end();
73+
}
74+
6175
/**
6276
* @brief Get a snapshot vector of all stored objects.
6377
*

0 commit comments

Comments
 (0)