Skip to content

Commit 16c31a8

Browse files
committed
Refactor WindowId handling and implement MessageDialog
Replaces pointer casting with a unique ID allocator for WindowId in window_windows.cpp and updates window_manager_windows.cpp to use the new approach. Implements the MessageDialog for Windows using MessageBoxW in message_dialog_windows.cpp, including proper modality handling and macro cleanup. Adds necessary includes and minor code cleanups.
1 parent c27b91d commit 16c31a8

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

src/platform/windows/menu_windows.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <windows.h>
2+
#include <cmath>
23
#include <cstring>
34
#include <functional>
45
#include <iostream>

src/platform/windows/message_dialog_windows.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
1+
// clang-format off
2+
#include <windows.h>
3+
#include <windowsx.h>
4+
// clang-format on
5+
6+
// Undefine Windows API macros that conflict with our method names
7+
#ifdef GetMessage
8+
#undef GetMessage
9+
#endif
10+
11+
#include <memory>
12+
#include <string>
13+
114
#include "../../message_dialog.h"
215
#include "../../dialog.h"
16+
#include "string_utils_windows.h"
317

418
namespace nativeapi {
519

6-
// Private implementation class for MessageDialog (Windows stub)
20+
// Private implementation class for MessageDialog using Win32 MessageBox
721
class MessageDialog::Impl {
822
public:
923
Impl(const std::string& title, const std::string& message)
10-
: title_(title), message_(message) {
11-
// TODO: Implement Windows MessageBox or Task Dialog
12-
}
24+
: title_(title), message_(message) {}
1325

14-
~Impl() {
15-
// TODO: Cleanup if needed
16-
}
26+
~Impl() = default;
1727

1828
void SetTitle(const std::string& title) {
1929
title_ = title;
2030
}
2131

22-
std::string GetTitle() const {
23-
return title_;
32+
std::string GetTitle() const {
33+
return title_;
2434
}
2535

2636
void SetMessage(const std::string& message) {
2737
message_ = message;
2838
}
2939

30-
std::string GetMessage() const {
31-
return message_;
40+
std::string GetMessage() const {
41+
return message_;
3242
}
3343

3444
bool Open(DialogModality modality) {
35-
// TODO: Implement using MessageBox or TaskDialogIndirect
36-
// For now, return false (not implemented)
37-
return false;
45+
std::wstring wtitle = StringToWString(title_);
46+
std::wstring wmessage = StringToWString(message_);
47+
48+
UINT uType = MB_OK | MB_ICONINFORMATION;
49+
50+
// Set modality
51+
if (modality == DialogModality::Application) {
52+
uType |= MB_APPLMODAL;
53+
} else if (modality == DialogModality::Window) {
54+
uType |= MB_SYSTEMMODAL; // Use system modal as approximation
55+
}
56+
57+
int result = MessageBoxW(nullptr, wmessage.c_str(), wtitle.c_str(), uType);
58+
return result != 0;
3859
}
3960

4061
bool Close() {
41-
// TODO: Implement closing logic
62+
// MessageBox doesn't support programmatic closing
4263
return false;
4364
}
4465

@@ -82,4 +103,3 @@ bool MessageDialog::Close() {
82103
}
83104

84105
} // namespace nativeapi
85-

src/platform/windows/window_manager_windows.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class WindowManager::Impl {
4848
}
4949

5050
void OnWindowEvent(HWND hwnd, const std::string& event_type) {
51-
WindowId window_id = reinterpret_cast<WindowId>(hwnd);
51+
// Create a temporary Window object to get the proper WindowId
52+
Window temp_window(hwnd);
53+
WindowId window_id = temp_window.GetId();
5254

5355
if (event_type == "focused") {
5456
WindowFocusedEvent event(window_id);
@@ -151,8 +153,8 @@ std::shared_ptr<Window> WindowManager::Create(const WindowOptions& options) {
151153
ShowWindow(hwnd, SW_SHOW);
152154
UpdateWindow(hwnd);
153155

154-
WindowId window_id = reinterpret_cast<WindowId>(hwnd);
155156
auto window = std::make_shared<Window>(hwnd);
157+
WindowId window_id = window->GetId();
156158
windows_[window_id] = window;
157159

158160
// Dispatch window created event
@@ -166,7 +168,7 @@ std::shared_ptr<Window> WindowManager::Create(const WindowOptions& options) {
166168
bool WindowManager::Destroy(WindowId id) {
167169
auto it = windows_.find(id);
168170
if (it != windows_.end()) {
169-
HWND hwnd = reinterpret_cast<HWND>(id);
171+
HWND hwnd = static_cast<HWND>(it->second->GetNativeObject());
170172
if (IsWindow(hwnd)) {
171173
DestroyWindow(hwnd);
172174
}
@@ -208,7 +210,8 @@ std::vector<std::shared_ptr<Window>> WindowManager::GetAll() {
208210
std::shared_ptr<Window> WindowManager::GetCurrent() {
209211
HWND hwnd = GetForegroundWindow();
210212
if (hwnd) {
211-
WindowId window_id = reinterpret_cast<WindowId>(hwnd);
213+
Window temp_window(hwnd);
214+
WindowId window_id = temp_window.GetId();
212215
return Get(window_id);
213216
}
214217
return nullptr;

src/platform/windows/window_windows.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <windows.h>
22
#include <iostream>
3+
#include <mutex>
4+
#include <unordered_map>
35
#include "../../window.h"
46
#include "../../window_manager.h"
57
#include "dpi_utils_windows.h"
@@ -546,7 +548,27 @@ void Window::StartResizing() {
546548
}
547549

548550
WindowId Window::GetId() const {
549-
return pimpl_ && pimpl_->hwnd_ ? reinterpret_cast<WindowId>(pimpl_->hwnd_) : -1;
551+
// Use IdAllocator to generate unique IDs instead of casting pointers
552+
if (!pimpl_->hwnd_) {
553+
return IdAllocator::kInvalidId;
554+
}
555+
556+
// Store the allocated ID in a static map to ensure consistency
557+
static std::unordered_map<HWND, WindowId> window_id_map;
558+
static std::mutex map_mutex;
559+
560+
std::lock_guard<std::mutex> lock(map_mutex);
561+
auto it = window_id_map.find(pimpl_->hwnd_);
562+
if (it != window_id_map.end()) {
563+
return it->second;
564+
}
565+
566+
// Allocate new ID using the IdAllocator
567+
WindowId new_id = IdAllocator::Allocate<Window>();
568+
if (new_id != IdAllocator::kInvalidId) {
569+
window_id_map[pimpl_->hwnd_] = new_id;
570+
}
571+
return new_id;
550572
}
551573

552574
void* Window::GetNativeObjectInternal() const {

0 commit comments

Comments
 (0)