Skip to content

Commit d2b297a

Browse files
committed
Refactor example code for improved readability
Reformats and refactors example C and C++ files to use more concise and readable code style, primarily by reducing line breaks and simplifying multi-line statements. Also updates .clang-format to remove 'Language: ObjC' and applies similar formatting improvements across source files for consistency.
1 parent 7b72873 commit d2b297a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+752
-1311
lines changed

.clang-format

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ BasedOnStyle: Chromium
77
# 'int>>' if the file already contains at least one such instance.)
88
Standard: Cpp11
99
SortIncludes: true
10-
---
11-
Language: ObjC
1210
ColumnLimit: 100

examples/application_c_example/main.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ void on_application_event(const native_application_event_t* event) {
1010
printf("Application started event received\n");
1111
break;
1212
case NATIVE_APPLICATION_EVENT_EXITING:
13-
printf("Application exiting event received with exit code: %d\n",
14-
event->exit_code);
13+
printf("Application exiting event received with exit code: %d\n", event->exit_code);
1514
break;
1615
case NATIVE_APPLICATION_EVENT_ACTIVATED:
1716
printf("Application activated event received\n");
@@ -39,12 +38,10 @@ int main() {
3938
}
4039

4140
printf("Application instance obtained successfully\n");
42-
printf("Single instance: %s\n",
43-
native_application_is_single_instance(app) ? "Yes" : "No");
41+
printf("Single instance: %s\n", native_application_is_single_instance(app) ? "Yes" : "No");
4442

4543
// Add event listener
46-
size_t listener_id =
47-
native_application_add_event_listener(app, on_application_event);
44+
size_t listener_id = native_application_add_event_listener(app, on_application_event);
4845
if (listener_id == 0) {
4946
fprintf(stderr, "Failed to add event listener\n");
5047
return 1;
@@ -57,8 +54,7 @@ int main() {
5754
return 1;
5855
}
5956

60-
native_window_options_set_title(window_options,
61-
"Application C Example Window");
57+
native_window_options_set_title(window_options, "Application C Example Window");
6258
native_window_options_set_size(window_options, 400.0, 300.0);
6359

6460
native_window_t window = native_window_manager_create(window_options);

examples/application_example/main.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ int main() {
1212
auto& app = Application::GetInstance();
1313

1414
std::cout << "Application initialized automatically" << std::endl;
15-
std::cout << "Single instance: " << (app.IsSingleInstance() ? "Yes" : "No")
16-
<< std::endl;
15+
std::cout << "Single instance: " << (app.IsSingleInstance() ? "Yes" : "No") << std::endl;
1716

1817
// Add event listeners
19-
auto started_listener = app.AddListener<ApplicationStartedEvent>(
20-
[](const ApplicationStartedEvent& event) {
18+
auto started_listener =
19+
app.AddListener<ApplicationStartedEvent>([](const ApplicationStartedEvent& event) {
2120
std::cout << "Application started event received" << std::endl;
2221
});
2322

@@ -26,13 +25,13 @@ int main() {
2625
std::cout << "Application quit requested event received" << std::endl;
2726
});
2827

29-
auto activated_listener = app.AddListener<ApplicationActivatedEvent>(
30-
[](const ApplicationActivatedEvent& event) {
28+
auto activated_listener =
29+
app.AddListener<ApplicationActivatedEvent>([](const ApplicationActivatedEvent& event) {
3130
std::cout << "Application activated event received" << std::endl;
3231
});
3332

34-
auto deactivated_listener = app.AddListener<ApplicationDeactivatedEvent>(
35-
[](const ApplicationDeactivatedEvent& event) {
33+
auto deactivated_listener =
34+
app.AddListener<ApplicationDeactivatedEvent>([](const ApplicationDeactivatedEvent& event) {
3635
std::cout << "Application deactivated event received" << std::endl;
3736
});
3837

examples/display_c_example/main.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ int main() {
3636
printf(" Size: %.0f x %.0f\n", size.width, size.height);
3737

3838
native_rectangle_t work_area = native_display_get_work_area(display);
39-
printf(" Work Area: (%.0f, %.0f) %.0f x %.0f\n", work_area.x,
40-
work_area.y, work_area.width, work_area.height);
39+
printf(" Work Area: (%.0f, %.0f) %.0f x %.0f\n", work_area.x, work_area.y, work_area.width,
40+
work_area.height);
4141

4242
double scale_factor = native_display_get_scale_factor(display);
4343
printf(" Scale Factor: %.2f\n", scale_factor);
@@ -47,8 +47,7 @@ int main() {
4747

4848
// Display orientation
4949
printf(" Orientation: ");
50-
native_display_orientation_t orientation =
51-
native_display_get_orientation(display);
50+
native_display_orientation_t orientation = native_display_get_orientation(display);
5251
switch (orientation) {
5352
case NATIVE_DISPLAY_ORIENTATION_PORTRAIT:
5453
printf("Portrait (0°)");

examples/display_example/main.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ std::string orientationToString(DisplayOrientation orientation) {
2828

2929
int main() {
3030
try {
31-
std::cout << "=== Native API C++ Display Example ===" << std::endl
32-
<< std::endl;
31+
std::cout << "=== Native API C++ Display Example ===" << std::endl << std::endl;
3332

3433
DisplayManager& displayManager = DisplayManager::GetInstance();
3534

3635
// Test getting all displays
3736
std::vector<Display> displays = displayManager.GetAll();
3837

3938
if (!displays.empty()) {
40-
std::cout << "Found " << displays.size() << " display(s):" << std::endl
41-
<< std::endl;
39+
std::cout << "Found " << displays.size() << " display(s):" << std::endl << std::endl;
4240

4341
for (size_t i = 0; i < displays.size(); i++) {
4442
const Display& display = displays[i];
@@ -53,39 +51,34 @@ int main() {
5351

5452
// Position
5553
Point position = display.GetPosition();
56-
std::cout << " Position: (" << (int)position.x << ", "
57-
<< (int)position.y << ")" << std::endl;
54+
std::cout << " Position: (" << (int)position.x << ", " << (int)position.y << ")"
55+
<< std::endl;
5856

5957
// Size
6058
Size size = display.GetSize();
61-
std::cout << " Size: " << (int)size.width << " x " << (int)size.height
62-
<< std::endl;
59+
std::cout << " Size: " << (int)size.width << " x " << (int)size.height << std::endl;
6360

6461
// Work Area
6562
Rectangle workArea = display.GetWorkArea();
66-
std::cout << " Work Area: (" << (int)workArea.x << ", "
67-
<< (int)workArea.y << ") " << (int)workArea.width << " x "
68-
<< (int)workArea.height << std::endl;
63+
std::cout << " Work Area: (" << (int)workArea.x << ", " << (int)workArea.y << ") "
64+
<< (int)workArea.width << " x " << (int)workArea.height << std::endl;
6965

7066
// Scale Factor
7167
std::cout << " Scale Factor: " << std::fixed << std::setprecision(2)
7268
<< display.GetScaleFactor() << std::endl;
7369

7470
// Primary
75-
std::cout << " Primary: " << (display.IsPrimary() ? "Yes" : "No")
76-
<< std::endl;
71+
std::cout << " Primary: " << (display.IsPrimary() ? "Yes" : "No") << std::endl;
7772

7873
// Orientation
79-
std::cout << " Orientation: "
80-
<< orientationToString(display.GetOrientation()) << std::endl;
74+
std::cout << " Orientation: " << orientationToString(display.GetOrientation())
75+
<< std::endl;
8176

8277
// Refresh Rate
83-
std::cout << " Refresh Rate: " << display.GetRefreshRate() << " Hz"
84-
<< std::endl;
78+
std::cout << " Refresh Rate: " << display.GetRefreshRate() << " Hz" << std::endl;
8579

8680
// Bit Depth
87-
std::cout << " Bit Depth: " << display.GetBitDepth() << " bits"
88-
<< std::endl;
81+
std::cout << " Bit Depth: " << display.GetBitDepth() << " bits" << std::endl;
8982

9083
std::cout << std::endl;
9184
}
@@ -99,14 +92,13 @@ int main() {
9992
std::cout << "Primary display: " << primary.GetName() << std::endl;
10093

10194
Size size = primary.GetSize();
102-
std::cout << "Size: " << (int)size.width << " x " << (int)size.height
103-
<< std::endl;
95+
std::cout << "Size: " << (int)size.width << " x " << (int)size.height << std::endl;
10496

10597
// Test getting cursor position
10698
std::cout << std::endl << "=== Cursor Position ===" << std::endl;
10799
Point cursorPos = displayManager.GetCursorPosition();
108-
std::cout << "Cursor position: (" << (int)cursorPos.x << ", "
109-
<< (int)cursorPos.y << ")" << std::endl;
100+
std::cout << "Cursor position: (" << (int)cursorPos.x << ", " << (int)cursorPos.y << ")"
101+
<< std::endl;
110102

111103
} catch (const std::exception& e) {
112104
std::cerr << "Error: " << e.what() << std::endl;

examples/id_allocator_example/main.cpp

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,32 @@ int main() {
2828

2929
std::cout << "Window ID: 0x" << std::hex << window_id << std::dec
3030
<< " (Type: " << IdAllocator::GetType(window_id)
31-
<< ", Sequence: " << IdAllocator::GetSequence(window_id) << ")"
32-
<< std::endl;
31+
<< ", Sequence: " << IdAllocator::GetSequence(window_id) << ")" << std::endl;
3332

3433
std::cout << "Menu ID: 0x" << std::hex << menu_id << std::dec
3534
<< " (Type: " << IdAllocator::GetType(menu_id)
36-
<< ", Sequence: " << IdAllocator::GetSequence(menu_id) << ")"
37-
<< std::endl;
35+
<< ", Sequence: " << IdAllocator::GetSequence(menu_id) << ")" << std::endl;
3836

3937
std::cout << "Tray ID: 0x" << std::hex << tray_id << std::dec
4038
<< " (Type: " << IdAllocator::GetType(tray_id)
41-
<< ", Sequence: " << IdAllocator::GetSequence(tray_id) << ")"
42-
<< std::endl;
39+
<< ", Sequence: " << IdAllocator::GetSequence(tray_id) << ")" << std::endl;
4340

4441
// Example 2: TryAllocate with error checking
4542
std::cout << "\n2. TryAllocate (safer allocation):" << std::endl;
4643

4744
auto maybe_id = IdAllocator::TryAllocate<MenuItem>();
4845
if (maybe_id != IdAllocator::kInvalidId) {
49-
std::cout << "MenuItem ID allocated successfully: 0x" << std::hex
50-
<< maybe_id << std::dec << std::endl;
46+
std::cout << "MenuItem ID allocated successfully: 0x" << std::hex << maybe_id << std::dec
47+
<< std::endl;
5148
} else {
5249
std::cout << "MenuItem ID allocation failed" << std::endl;
5350
}
5451

5552
// Example 3: ID validation and decomposition
5653
std::cout << "\n3. ID Validation and Decomposition:" << std::endl;
5754

58-
std::cout << "Is window_id valid? "
59-
<< (IdAllocator::IsValid(window_id) ? "Yes" : "No") << std::endl;
55+
std::cout << "Is window_id valid? " << (IdAllocator::IsValid(window_id) ? "Yes" : "No")
56+
<< std::endl;
6057

6158
auto decomposed = IdAllocator::Decompose(window_id);
6259
std::cout << "Window ID decomposed - Type: " << decomposed.first
@@ -70,8 +67,7 @@ int main() {
7067

7168
auto new_window_id = IdAllocator::Allocate<Window>();
7269
std::cout << "New Window ID: 0x" << std::hex << new_window_id << std::dec
73-
<< " (Sequence: " << IdAllocator::GetSequence(new_window_id) << ")"
74-
<< std::endl;
70+
<< " (Sequence: " << IdAllocator::GetSequence(new_window_id) << ")" << std::endl;
7571

7672
std::cout << "Current Window counter (after allocation): "
7773
<< IdAllocator::GetCurrentCount<Window>() << std::endl;
@@ -86,10 +82,8 @@ int main() {
8682

8783
std::cout << "Allocated " << window_ids.size() << " Window IDs:" << std::endl;
8884
for (size_t i = 0; i < window_ids.size(); ++i) {
89-
std::cout << " ID " << (i + 1) << ": 0x" << std::hex << window_ids[i]
90-
<< std::dec
91-
<< " (Sequence: " << IdAllocator::GetSequence(window_ids[i])
92-
<< ")" << std::endl;
85+
std::cout << " ID " << (i + 1) << ": 0x" << std::hex << window_ids[i] << std::dec
86+
<< " (Sequence: " << IdAllocator::GetSequence(window_ids[i]) << ")" << std::endl;
9387
}
9488

9589
// Example 6: Thread safety demonstration
@@ -125,51 +119,41 @@ int main() {
125119
}
126120

127121
auto end_time = std::chrono::high_resolution_clock::now();
128-
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
129-
end_time - start_time);
122+
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
130123

131-
std::cout << "Allocated " << thread_ids.size() << " Display IDs from "
132-
<< num_threads << " threads in " << duration.count()
133-
<< " microseconds" << std::endl;
124+
std::cout << "Allocated " << thread_ids.size() << " Display IDs from " << num_threads
125+
<< " threads in " << duration.count() << " microseconds" << std::endl;
134126

135127
// Verify all IDs are unique
136128
std::sort(thread_ids.begin(), thread_ids.end());
137129
auto it = std::unique(thread_ids.begin(), thread_ids.end());
138-
std::cout << "All IDs are unique: " << (it == thread_ids.end() ? "Yes" : "No")
139-
<< std::endl;
130+
std::cout << "All IDs are unique: " << (it == thread_ids.end() ? "Yes" : "No") << std::endl;
140131

141132
// Example 7: Different object types
142133
std::cout << "\n7. Different Object Types:" << std::endl;
143134

144135
auto display_id = IdAllocator::Allocate<Display>();
145136

146137
std::cout << "Display ID: 0x" << std::hex << display_id << std::dec
147-
<< " (Type: " << IdAllocator::GetType(display_id) << ")"
148-
<< std::endl;
138+
<< " (Type: " << IdAllocator::GetType(display_id) << ")" << std::endl;
149139

150140
// Example 8: Reset functionality (for testing)
151141
std::cout << "\n8. Reset Functionality:" << std::endl;
152142

153-
std::cout << "Menu counter before reset: "
154-
<< IdAllocator::GetCurrentCount<Menu>() << std::endl;
143+
std::cout << "Menu counter before reset: " << IdAllocator::GetCurrentCount<Menu>() << std::endl;
155144
IdAllocator::Reset<Menu>();
156-
std::cout << "Menu counter after reset: "
157-
<< IdAllocator::GetCurrentCount<Menu>() << std::endl;
145+
std::cout << "Menu counter after reset: " << IdAllocator::GetCurrentCount<Menu>() << std::endl;
158146

159147
auto new_menu_id = IdAllocator::Allocate<Menu>();
160-
std::cout << "New Menu ID after reset: 0x" << std::hex << new_menu_id
161-
<< std::dec
162-
<< " (Sequence: " << IdAllocator::GetSequence(new_menu_id) << ")"
163-
<< std::endl;
148+
std::cout << "New Menu ID after reset: 0x" << std::hex << new_menu_id << std::dec
149+
<< " (Sequence: " << IdAllocator::GetSequence(new_menu_id) << ")" << std::endl;
164150

165151
// Example 9: Independent types after reset
166152
std::cout << "\n9. Independent Types After Reset:" << std::endl;
167153

168154
auto window_after_reset = IdAllocator::Allocate<Window>();
169-
std::cout << "Window ID after Menu reset: 0x" << std::hex
170-
<< window_after_reset << std::dec
171-
<< " (Sequence: " << IdAllocator::GetSequence(window_after_reset)
172-
<< ")" << std::endl;
155+
std::cout << "Window ID after Menu reset: 0x" << std::hex << window_after_reset << std::dec
156+
<< " (Sequence: " << IdAllocator::GetSequence(window_after_reset) << ")" << std::endl;
173157
std::cout << "Window counter was not affected by Menu reset" << std::endl;
174158

175159
std::cout << "\nExample completed successfully!" << std::endl;

examples/keyboard_example/main.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ void on_key_released(int keycode, void* user_data) {
3535

3636
// Callback for modifier keys changed events
3737
void on_modifier_keys_changed(uint32_t modifier_keys, void* user_data) {
38-
std::cout << "Modifier keys changed: 0x" << std::hex << modifier_keys
39-
<< std::dec;
38+
std::cout << "Modifier keys changed: 0x" << std::hex << modifier_keys << std::dec;
4039

4140
if (modifier_keys & NATIVE_MODIFIER_KEY_SHIFT)
4241
std::cout << " SHIFT";
@@ -75,9 +74,8 @@ int main() {
7574
std::cout << "Keyboard monitor created successfully\n";
7675

7776
// Set up callbacks
78-
if (!native_keyboard_monitor_set_callbacks(
79-
g_monitor, on_key_pressed, on_key_released, on_modifier_keys_changed,
80-
nullptr)) {
77+
if (!native_keyboard_monitor_set_callbacks(g_monitor, on_key_pressed, on_key_released,
78+
on_modifier_keys_changed, nullptr)) {
8179
std::cout << "Failed to set callbacks\n";
8280
native_keyboard_monitor_destroy(g_monitor);
8381
return 1;
@@ -94,10 +92,8 @@ int main() {
9492
if (native_keyboard_monitor_is_monitoring(g_monitor)) {
9593
std::cout << "Keyboard monitoring is now active\n";
9694
std::cout << "\nThis example demonstrates the KeyboardMonitor C API:\n";
97-
std::cout
98-
<< "• native_keyboard_monitor_create() - Creates a monitor instance\n";
99-
std::cout
100-
<< "• native_keyboard_monitor_set_callbacks() - Sets event callbacks\n";
95+
std::cout << "• native_keyboard_monitor_create() - Creates a monitor instance\n";
96+
std::cout << "• native_keyboard_monitor_set_callbacks() - Sets event callbacks\n";
10197
std::cout << "• native_keyboard_monitor_start() - Starts monitoring\n";
10298
std::cout << "• native_keyboard_monitor_is_monitoring() - Checks status\n";
10399
std::cout << "• native_keyboard_monitor_stop() - Stops monitoring\n";

0 commit comments

Comments
 (0)