Skip to content

Commit a3a5de5

Browse files
committed
feat: Add TUIKLoader for dynamic UI loading from .tuik files
- Introduced TUIKLoader class to handle loading of UI components from JSON-like .tuik files. - Implemented loadUI and getWidget methods in TUIApp to utilize TUIKLoader. - Created example_2.cpp demonstrating dynamic UI loading and interaction. - Added simple_ui.tuik file defining a basic UI layout with input fields and buttons. - Enhanced TUIButton to allow setting text dynamically. - Updated TUIGroupBox and TUITextField to improve rendering and initialization. - Added debug logging throughout the loading process for better traceability.
1 parent 2bcb33a commit a3a5de5

15 files changed

Lines changed: 888 additions & 261 deletions

File tree

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"files.associations": {
33
"system_error": "cpp",
44
"functional": "cpp",
5-
"chrono": "cpp"
5+
"chrono": "cpp",
6+
"atomic": "cpp"
67
}
78
}

CMakeLists.txt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ endif()
2323
# FTXUI
2424
add_subdirectory(external/ftxui)
2525

26+
# nlohmann/json (header-only)
27+
include_directories(external/json)
28+
2629
# =============================================================================
2730
# PROJECT STRUCTURE
2831
# =============================================================================
@@ -83,11 +86,20 @@ set(TUIKIT_SOURCES
8386
src/widgets/TUITextArea.cpp
8487
src/widgets/TUITreeView.cpp
8588
src/widgets/Notification.cpp
89+
src/core/TUIKLoader.cpp
8690
# Add other source files as they are created
8791
)
8892

8993
set(EXAMPLE_SOURCES
90-
examples/main.cpp
94+
examples/example_1.cpp
95+
)
96+
97+
set(EXAMPLE_2_SOURCES
98+
examples/example_2.cpp
99+
)
100+
101+
set(FTXUI_TEST_SOURCES
102+
examples/ftxui_test.cpp
91103
)
92104

93105
# =============================================================================
@@ -98,15 +110,24 @@ add_library(tuikit ${TUIKIT_SOURCES})
98110
target_include_directories(tuikit PUBLIC
99111
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
100112
$<INSTALL_INTERFACE:include>
113+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/external/json>
101114
)
102115
target_link_libraries(tuikit PUBLIC ftxui::screen ftxui::dom ftxui::component)
103116

104117
# =============================================================================
105-
# EXAMPLE EXECUTABLE
118+
# EXAMPLE EXECUTABLE 1
106119
# =============================================================================
107120

108-
add_executable(example_usage ${EXAMPLE_SOURCES})
109-
target_link_libraries(example_usage PRIVATE tuikit)
121+
add_executable(example_1 ${EXAMPLE_SOURCES})
122+
target_link_libraries(example_1 PRIVATE tuikit)
123+
124+
# =============================================================================
125+
# EXAMPLE EXECUTABLE 2
126+
# =============================================================================
127+
128+
add_executable(example_2 ${EXAMPLE_2_SOURCES})
129+
target_link_libraries(example_2 PRIVATE tuikit)
130+
110131

111132

112133
# =============================================================================

debug_log.txt

Lines changed: 0 additions & 197 deletions
This file was deleted.

examples/example_2.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "tuikit.h"
2+
#include <iostream>
3+
#include <string>
4+
#include <fstream>
5+
6+
using namespace TUIKIT;
7+
8+
int main() {
9+
std::ofstream cerr_log("debug_log_example2.txt");
10+
std::streambuf* old_cerr_buf = std::cerr.rdbuf();
11+
std::cerr.rdbuf(cerr_log.rdbuf());
12+
13+
TUIApp app("TUIKIT UI Loader Example");
14+
15+
// Load UI from .tuik file
16+
auto main_ui_widget = app.loadUI("../examples/simple_ui.tuik");
17+
18+
if (!main_ui_widget) {
19+
std::cerr << "Failed to load UI from simple_ui.tuik" << std::endl;
20+
std::cout << "Failed to load UI from simple_ui.tuik" << std::endl;
21+
return 1;
22+
}
23+
24+
app.setMainWidget(main_ui_widget);
25+
26+
// Get named widgets
27+
auto name_input = std::dynamic_pointer_cast<TUITextField>(app.getWidget("name_input"));
28+
auto submit_button = std::dynamic_pointer_cast<TUIButton>(app.getWidget("submit_button"));
29+
auto output_label = std::dynamic_pointer_cast<TUILabel>(app.getWidget("output_label"));
30+
31+
if (name_input && submit_button && output_label) {
32+
connect(submit_button, [&] {
33+
output_label->setText("Hello, " + name_input->text() + "!");
34+
});
35+
} else {
36+
std::cerr << "Error: Could not find all required widgets by name." << std::endl;
37+
}
38+
39+
app.setOnExit([&] {
40+
std::cerr << "Application is exiting. Goodbye from example_2!" << std::endl;
41+
std::cerr.rdbuf(old_cerr_buf);
42+
});
43+
44+
int result = app.exec();
45+
46+
std::cerr.rdbuf(old_cerr_buf);
47+
48+
return result;
49+
50+
std::cerr.rdbuf(old_cerr_buf);
51+
52+
return result;
53+
}

examples/simple_ui.tuik

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"class": "TUIVBoxLayout",
3+
"name": "main_layout",
4+
"children": [
5+
{
6+
"class": "TUIGroupBox",
7+
"name": "input_group",
8+
"properties": {
9+
"title": "Input & Button Example"
10+
},
11+
"content": {
12+
"class": "TUIVBoxLayout",
13+
"children": [
14+
{
15+
"class": "TUILabel",
16+
"name": "info_label",
17+
"properties": {
18+
"text": "Enter your name:"
19+
}
20+
},
21+
{
22+
"class": "TUITextField",
23+
"name": "name_input",
24+
"properties": {
25+
"placeholder": "Your name..."
26+
}
27+
},
28+
{
29+
"class": "TUIButton",
30+
"name": "submit_button",
31+
"properties": {
32+
"text": "Submit",
33+
"icon": "Rocket"
34+
}
35+
}
36+
]
37+
}
38+
},
39+
{
40+
"class": "TUILabel",
41+
"name": "output_label",
42+
"properties": {
43+
"text": ""
44+
}
45+
}
46+
]
47+
}

include/tuikit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "tuikit/core/TUIIcons.h"
3333
#include "tuikit/widgets/Notification.h"
34+
#include "tuikit/core/TUIKLoader.h"
3435

3536

3637

0 commit comments

Comments
 (0)