Skip to content

Commit b314b39

Browse files
committed
Refactor display example and keyboard example output formatting
- Updated display example to use standard characters for table borders instead of special characters. - Simplified output messages in keyboard example by removing emoji and improving clarity. - Enhanced code readability by adjusting string formatting and comments.
1 parent 34353fe commit b314b39

24 files changed

+1550
-1422
lines changed

examples/display_example/main.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ std::string truncateString(const std::string& str, size_t maxLength) {
3737
// Helper function to format a table row with proper alignment
3838
std::string formatTableRow(const std::string& content, int totalWidth = 70) {
3939
std::string truncated =
40-
truncateString(content, totalWidth - 4); // Leave space for " " and " "
40+
truncateString(content, totalWidth - 4); // Leave space for "| " and " |"
4141
std::ostringstream oss;
42-
oss << " " << std::left << std::setw(totalWidth - 4) << truncated << " ";
42+
oss << "| " << std::left << std::setw(totalWidth - 4) << truncated << " |";
4343
return oss.str();
4444
}
4545

@@ -50,22 +50,22 @@ std::string createTableBorder(const std::string& leftChar,
5050
const std::string& fillChar,
5151
int width = 70) {
5252
std::string border;
53-
if (leftChar == "")
53+
if (leftChar == "+")
5454
border = "+";
55-
else if (leftChar == "")
55+
else if (leftChar == "+")
5656
border = "+";
57-
else if (leftChar == "")
57+
else if (leftChar == "+")
5858
border = "+";
5959
else
6060
border = leftChar;
6161

6262
border += std::string(width - 2, '-');
6363

64-
if (rightChar == "")
64+
if (rightChar == "+")
6565
border += "+";
66-
else if (rightChar == "")
66+
else if (rightChar == "+")
6767
border += "+";
68-
else if (rightChar == "")
68+
else if (rightChar == "+")
6969
border += "+";
7070
else
7171
border += rightChar;
@@ -77,10 +77,10 @@ std::string createTableBorder(const std::string& leftChar,
7777
void printDisplayInfo(const Display& display, bool isPrimary = false) {
7878
const int tableWidth = 70;
7979

80-
std::cout << createTableBorder("", "", "", tableWidth) << std::endl;
80+
std::cout << createTableBorder("+", "+", "-", tableWidth) << std::endl;
8181
std::cout << formatTableRow("Display: " + display.name, tableWidth)
8282
<< std::endl;
83-
std::cout << createTableBorder("", "", "", tableWidth) << std::endl;
83+
std::cout << createTableBorder("+", "+", "-", tableWidth) << std::endl;
8484
std::cout << formatTableRow("ID: " + display.id, tableWidth) << std::endl;
8585

8686
// Format position string
@@ -139,7 +139,7 @@ void printDisplayInfo(const Display& display, bool isPrimary = false) {
139139
std::cout << formatTableRow(modelStr, tableWidth) << std::endl;
140140
}
141141

142-
std::cout << createTableBorder("", "", "", tableWidth) << std::endl;
142+
std::cout << createTableBorder("+", "+", "-", tableWidth) << std::endl;
143143
}
144144

145145
int main() {
@@ -196,8 +196,8 @@ int main() {
196196
}
197197

198198
const int summaryWidth = 60;
199-
std::cout << "📊 SUMMARY:" << std::endl;
200-
std::cout << createTableBorder("", "", "", summaryWidth) << std::endl;
199+
std::cout << "SUMMARY:" << std::endl;
200+
std::cout << createTableBorder("+", "+", "-", summaryWidth) << std::endl;
201201

202202
// Format each summary line properly
203203
std::string totalDisplaysStr =
@@ -218,17 +218,17 @@ int main() {
218218
std::string scaleRangeStr = scaleRangeStream.str();
219219
std::cout << formatTableRow(scaleRangeStr, summaryWidth) << std::endl;
220220

221-
std::cout << createTableBorder("", "", "", summaryWidth) << std::endl;
221+
std::cout << createTableBorder("+", "+", "-", summaryWidth) << std::endl;
222222

223223
} catch (const std::exception& e) {
224-
std::cerr << "Error: " << e.what() << std::endl;
224+
std::cerr << "Error: " << e.what() << std::endl;
225225
return 1;
226226
} catch (...) {
227-
std::cerr << "Unknown error occurred" << std::endl;
227+
std::cerr << "Unknown error occurred" << std::endl;
228228
return 1;
229229
}
230230

231-
std::cout << "\n✅ Display information retrieved successfully!" << std::endl;
231+
std::cout << "\nDisplay information retrieved successfully!" << std::endl;
232232

233233
// Test C API
234234
std::cout << "\n=== Testing C API ===" << std::endl;

examples/keyboard_example/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ int main() {
8686
}
8787

8888
if (native_keyboard_monitor_is_monitoring(g_monitor)) {
89-
std::cout << "Keyboard monitoring is now active\n";
90-
std::cout << "\n📚 This example demonstrates the KeyboardMonitor C API:\n";
89+
std::cout << "Keyboard monitoring is now active\n";
90+
std::cout << "\nThis example demonstrates the KeyboardMonitor C API:\n";
9191
std::cout << "• native_keyboard_monitor_create() - Creates a monitor instance\n";
9292
std::cout << "• native_keyboard_monitor_set_callbacks() - Sets event callbacks\n";
9393
std::cout << "• native_keyboard_monitor_start() - Starts monitoring\n";
9494
std::cout << "• native_keyboard_monitor_is_monitoring() - Checks status\n";
9595
std::cout << "• native_keyboard_monitor_stop() - Stops monitoring\n";
9696
std::cout << "• native_keyboard_monitor_destroy() - Cleans up resources\n";
97-
std::cout << "\n🎹 Press keys to see events. Press Ctrl+C to exit.\n\n";
97+
std::cout << "\nPress keys to see events. Press Ctrl+C to exit.\n\n";
9898
} else {
99-
std::cout << "⚠️ Monitor created but not monitoring (may be due to permissions or display server)\n";
99+
std::cout << "Warning: Monitor created but not monitoring (may be due to permissions or display server)\n";
100100
std::cout << "This is expected in headless environments or without proper permissions.\n";
101101
std::cout << "On a desktop system with X11/Wayland, you would see keyboard events.\n\n";
102102
}

examples/window_example/main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ int main() {
3232
WindowManager& window_manager = WindowManager::GetInstance();
3333

3434
// Create a new window with options
35-
WindowOptions options = {.title = "Window Example",
36-
.size = {800, 600},
37-
.minimum_size = {400, 300},
38-
.maximum_size = {1920, 1080},
39-
.centered = true};
35+
WindowOptions options;
36+
options.title = "Window Example";
37+
options.size = {800, 600};
38+
options.minimum_size = {400, 300};
39+
options.maximum_size = {1920, 1080};
40+
options.centered = true;
4041
std::shared_ptr<Window> window_ptr = window_manager.Create(options);
4142

4243
std::shared_ptr<TrayIcon> tray_icon_ptr = tray_manager.Create();

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ if(APPLE)
6565
target_compile_options(nativeapi PRIVATE "-x" "objective-c++")
6666
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
6767
target_link_libraries(nativeapi PUBLIC PkgConfig::GTK PkgConfig::X11 PkgConfig::XI pthread)
68+
elseif(WIN32)
69+
target_link_libraries(nativeapi PUBLIC user32 shell32 dwmapi)
6870
endif ()

src/capi/run_example_app_c.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ using namespace nativeapi;
99

1010
int native_run_example_app() {
1111
// Create a new window with options
12-
WindowOptions options = {.title = "Window Example",
13-
.size = {800, 600},
14-
.minimum_size = {400, 300},
15-
.maximum_size = {1920, 1080},
16-
.centered = true};
12+
WindowOptions options;
13+
options.title = "Window Example";
14+
options.size = {800, 600};
15+
options.minimum_size = {400, 300};
16+
options.maximum_size = {1920, 1080};
17+
options.centered = true;
1718
std::shared_ptr<Window> window_ptr =
1819
WindowManager::GetInstance().Create(options);
1920
// Run the main event loop

src/capi/window_c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,9 @@ void native_window_start_resizing(native_window_t window) {
481481

482482
// Platform-specific functions
483483
FFI_PLUGIN_EXPORT
484-
void* native_window_get_ns_window(native_window_t window) {
484+
void* native_window_get_native_object(native_window_t window) {
485485
if (!window || !window->window) return nullptr;
486-
return window->window->GetNSWindow();
486+
return window->window->GetNativeObject();
487487
}
488488

489489
// Memory management

src/capi/window_c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void native_window_start_resizing(native_window_t window);
241241

242242
// Platform-specific functions
243243
FFI_PLUGIN_EXPORT
244-
void* native_window_get_ns_window(native_window_t window);
244+
void* native_window_get_native_object(native_window_t window);
245245

246246
// Memory management
247247
FFI_PLUGIN_EXPORT

src/keyboard_monitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class KeyboardMonitor: public EventEmitter {
4646
// Check if the keyboard monitor is monitoring
4747
bool IsMonitoring() const;
4848

49+
// Get access to the event dispatcher for internal use
50+
EventDispatcher& GetInternalEventDispatcher();
51+
4952
private:
5053
class Impl;
5154
std::unique_ptr<Impl> impl_;

src/menu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ class MenuItem : public EventEmitter {
478478
void EmitStateChangedEvent(bool checked);
479479

480480
private:
481+
friend class Menu;
482+
481483
/**
482484
* @brief Private constructor for factory methods.
483485
*

src/platform/linux/window_linux.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,8 @@ void Window::StartResizing() {
347347
// Provide stub implementation
348348
}
349349

350-
void* Window::GetNSWindow() const {
351-
// This method is macOS-specific, return nullptr on Linux
352-
return nullptr;
350+
void* Window::GetNativeObject() const {
351+
return pimpl_ ? pimpl_->gdk_window_ : nullptr;
353352
}
354353

355354
} // namespace nativeapi

0 commit comments

Comments
 (0)