|
1 | 1 | #include "window_c.h" |
2 | 2 | #include <cstring> |
| 3 | +#include <memory> |
| 4 | +#include <mutex> |
| 5 | +#include <unordered_map> |
3 | 6 | #include "../window.h" |
4 | 7 | #include "string_utils_c.h" |
5 | 8 |
|
6 | 9 | using namespace nativeapi; |
7 | 10 |
|
| 11 | +// Internal registry to manage window lifetimes for C API |
| 12 | +// This keeps shared_ptr alive while C code holds the handle |
| 13 | +namespace { |
| 14 | +std::mutex g_windows_mutex; |
| 15 | +std::unordered_map<WindowId, std::shared_ptr<Window>> g_windows; |
| 16 | +} // namespace |
| 17 | + |
| 18 | +// Window creation and destruction |
| 19 | +FFI_PLUGIN_EXPORT |
| 20 | +native_window_t native_window_create(void) { |
| 21 | + try { |
| 22 | + // Create window with default settings |
| 23 | + auto window = std::make_shared<Window>(); |
| 24 | + |
| 25 | + // Store in internal registry to keep it alive |
| 26 | + { |
| 27 | + std::lock_guard<std::mutex> lock(g_windows_mutex); |
| 28 | + g_windows[window->GetId()] = window; |
| 29 | + } |
| 30 | + |
| 31 | + // Return raw pointer (internal registry holds the shared_ptr) |
| 32 | + return static_cast<void*>(window.get()); |
| 33 | + } catch (...) { |
| 34 | + return nullptr; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +FFI_PLUGIN_EXPORT |
| 39 | +native_window_t native_window_create_from_native(void* native_window) { |
| 40 | + if (!native_window) |
| 41 | + return nullptr; |
| 42 | + |
| 43 | + try { |
| 44 | + // Wrap existing native window |
| 45 | + auto window = std::make_shared<Window>(native_window); |
| 46 | + |
| 47 | + // Store in internal registry to keep it alive |
| 48 | + { |
| 49 | + std::lock_guard<std::mutex> lock(g_windows_mutex); |
| 50 | + g_windows[window->GetId()] = window; |
| 51 | + } |
| 52 | + |
| 53 | + // Return raw pointer (internal registry holds the shared_ptr) |
| 54 | + return static_cast<void*>(window.get()); |
| 55 | + } catch (...) { |
| 56 | + return nullptr; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +FFI_PLUGIN_EXPORT |
| 61 | +void native_window_destroy(native_window_t window) { |
| 62 | + if (!window) |
| 63 | + return; |
| 64 | + |
| 65 | + try { |
| 66 | + auto* win = static_cast<nativeapi::Window*>(window); |
| 67 | + WindowId window_id = win->GetId(); |
| 68 | + |
| 69 | + // Remove from internal registry (this will destroy it if no other references exist) |
| 70 | + { |
| 71 | + std::lock_guard<std::mutex> lock(g_windows_mutex); |
| 72 | + g_windows.erase(window_id); |
| 73 | + } |
| 74 | + } catch (...) { |
| 75 | + // Silently fail |
| 76 | + } |
| 77 | +} |
| 78 | + |
8 | 79 | // Window basic operations |
9 | 80 | FFI_PLUGIN_EXPORT |
10 | 81 | native_window_id_t native_window_get_id(native_window_t window) { |
@@ -443,6 +514,36 @@ char* native_window_get_title(native_window_t window) { |
443 | 514 | } |
444 | 515 | } |
445 | 516 |
|
| 517 | +FFI_PLUGIN_EXPORT |
| 518 | +void native_window_set_title_bar_style(native_window_t window, native_title_bar_style_t style) { |
| 519 | + if (!window) |
| 520 | + return; |
| 521 | + |
| 522 | + try { |
| 523 | + auto* win = static_cast<nativeapi::Window*>(window); |
| 524 | + TitleBarStyle cpp_style = |
| 525 | + (style == NATIVE_TITLE_BAR_STYLE_HIDDEN) ? TitleBarStyle::Hidden : TitleBarStyle::Normal; |
| 526 | + win->SetTitleBarStyle(cpp_style); |
| 527 | + } catch (...) { |
| 528 | + // Silently fail |
| 529 | + } |
| 530 | +} |
| 531 | + |
| 532 | +FFI_PLUGIN_EXPORT |
| 533 | +native_title_bar_style_t native_window_get_title_bar_style(native_window_t window) { |
| 534 | + if (!window) |
| 535 | + return NATIVE_TITLE_BAR_STYLE_NORMAL; |
| 536 | + |
| 537 | + try { |
| 538 | + auto* win = static_cast<nativeapi::Window*>(window); |
| 539 | + TitleBarStyle cpp_style = win->GetTitleBarStyle(); |
| 540 | + return (cpp_style == TitleBarStyle::Hidden) ? NATIVE_TITLE_BAR_STYLE_HIDDEN |
| 541 | + : NATIVE_TITLE_BAR_STYLE_NORMAL; |
| 542 | + } catch (...) { |
| 543 | + return NATIVE_TITLE_BAR_STYLE_NORMAL; |
| 544 | + } |
| 545 | +} |
| 546 | + |
446 | 547 | FFI_PLUGIN_EXPORT |
447 | 548 | void native_window_set_has_shadow(native_window_t window, bool has_shadow) { |
448 | 549 | if (!window) |
|
0 commit comments