Skip to content

Commit f8a8c69

Browse files
authored
Merge pull request #117 from NuiCpp/feat/user-events
Feat/user events
2 parents 82700a2 + 6536fdc commit f8a8c69

File tree

6 files changed

+41
-12
lines changed

6 files changed

+41
-12
lines changed

cmake/backend/emscripten.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ function(nui_add_emscripten_target)
134134
${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_CMAKE_OPTIONS}
135135
"-DNUI_NPM=${NUI_NPM}"
136136
"-DNUI_NODE=${NUI_NODE}"
137+
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
137138
-DNUI_INLINE_EXTRACTOR_TARGET_FILE=$<TARGET_FILE:inline-parser>
138139
-DNUI_INLINE_INJECTOR_TARGET_FILE=$<TARGET_FILE:inline-injector>
139140
-DNUI_MODULE_BUILD_DIR=${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}

cmake/frontend/emscripten.cmake

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function(nui_prepare_emscripten_target)
22
cmake_parse_arguments(
33
NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS
4-
"NO_INLINE;NO_INLINE_INJECT"
4+
"NO_INLINE;NO_INLINE_INJECT;LEAN_INDEX_HTML"
55
"TARGET;PREJS;STATIC;UNPACKED_MODE"
66
"EMSCRIPTEN_LINK_OPTIONS;EMSCRIPTEN_COMPILE_OPTIONS;PARCEL_ARGS;NPM_INSTALL_ARGS"
77
${ARGN}
@@ -23,11 +23,16 @@ function(nui_prepare_emscripten_target)
2323
set(NUI_DEFER_INLINE_SCRIPTS_TAG "defer")
2424
endif()
2525

26+
set(NUI_LEAN_HTML "nolean")
27+
if (NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_LEAN_INDEX_HTML)
28+
set(NUI_LEAN_HTML "lean")
29+
endif()
30+
2631
set(INLINER_COMMAND "")
27-
if (NOT NO_INLINE)
32+
if (NOT NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_NO_INLINE)
2833
nui_enable_inline(TARGET ${NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_TARGET} RELATIVE_TO ${CMAKE_CURRENT_SOURCE_DIR})
29-
if (NOT NO_INLINE_INJECT)
30-
set(INLINER_COMMAND COMMAND ${NUI_INLINE_INJECTOR_TARGET_FILE} "${CMAKE_BINARY_DIR}/static/index.html" "${CMAKE_BINARY_DIR}/nui-inline/inline_imports.js" "${CMAKE_BINARY_DIR}/nui-inline/inline_imports.css" ${NUI_DEFER_INLINE_SCRIPTS_TAG})
34+
if (NOT NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_NO_INLINE_INJECT)
35+
set(INLINER_COMMAND COMMAND ${NUI_INLINE_INJECTOR_TARGET_FILE} "${CMAKE_BINARY_DIR}/static/index.html" "${CMAKE_BINARY_DIR}/nui-inline/inline_imports.js" "${CMAKE_BINARY_DIR}/nui-inline/inline_imports.css" ${NUI_DEFER_INLINE_SCRIPTS_TAG} ${NUI_LEAN_HTML})
3136
endif()
3237
endif()
3338

nui/include/nui/event_system/listen.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,16 @@ namespace Nui
7272
{
7373
return listen(eventContext, obs, std::function(std::move(onEvent)));
7474
}
75+
76+
template <typename ValueT, typename FunctionT>
77+
void listen(std::shared_ptr<Observed<ValueT>> const& obs, FunctionT onEvent)
78+
{
79+
return listen(globalEventContext, obs, std::function(std::move(onEvent)));
80+
}
81+
82+
template <typename ValueT, typename FunctionT>
83+
void listen(Observed<ValueT> const& obs, FunctionT onEvent)
84+
{
85+
return listen(globalEventContext, obs, std::function(std::move(onEvent)));
86+
}
7587
}

nui/include/nui/event_system/observed_value.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <deque>
1616
#include <string>
1717
#include <cassert>
18+
#include <set>
1819

1920
namespace Nui
2021
{
@@ -1286,7 +1287,7 @@ namespace Nui
12861287
: observed_{&observed}
12871288
{}
12881289

1289-
inline T value() const
1290+
inline T const& value() const
12901291
{
12911292
return observed_->value();
12921293
}

nui/test/nui/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ add_library(nui-frontend-mocked STATIC ${NUI_FRONTEND_SOURCES})
55

66
target_include_directories(nui-frontend-mocked PUBLIC emscripten_mock ${CMAKE_CURRENT_LIST_DIR}/../../include)
77

8-
find_package(Boost REQUIRED)
8+
find_package(Boost CONFIG REQUIRED)
99

1010
target_link_libraries(nui-frontend-mocked PUBLIC
1111
libcpppre

tools/inline_injector/main.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ std::string readFile(const std::filesystem::path& path)
2121

2222
int main(int argc, char** argv)
2323
{
24-
if (argc != 5)
24+
if (argc != 6)
2525
{
26-
std::cout
27-
<< "Expected 4 argument: <index.html> <import_scripts> <import_styles> <import_scripts_defer>, but got "
28-
<< argc - 1 << "\n";
26+
std::cout << "Expected 4 argument: <index.html> <import_scripts> <import_styles> <import_scripts_defer> "
27+
"<lean_html>, but got "
28+
<< argc - 1 << "\n";
2929
return 1;
3030
}
3131

3232
const auto index = std::filesystem::path{argv[1]};
3333
const auto importScripts = std::filesystem::path{argv[2]};
3434
const auto importStyles = std::filesystem::path{argv[3]};
3535
const auto importScriptsDefer = std::string{argv[4]} == "defer" ? true : false;
36+
const auto leanHtml = std::string{argv[5]} == "lean" ? true : false;
3637

3738
std::string indexHtml;
3839
try
@@ -57,8 +58,17 @@ int main(int argc, char** argv)
5758
relativeImportScriptsFile.generic_string() + "\";\n\t</script>\n";
5859
const std::string importStylesHtml =
5960
"\t<style>\n\t\t@import \"" + relativeImportStylesFile.generic_string() + "\";\n\t</style>\n";
60-
const std::string importBinIndexHtml =
61-
"\t<script type=\"module\" defer>\n\t\timport \"" + binIndex.generic_string() + "\";\n\t</script>\n";
61+
62+
std::string importBinIndexHtml;
63+
if (!leanHtml)
64+
{
65+
importBinIndexHtml =
66+
"\t<script type=\"module\" defer>\n\t\timport \"" + binIndex.generic_string() + "\";\n\t</script>\n";
67+
}
68+
else
69+
{
70+
importBinIndexHtml = "\t<script type=\"module\" defer src=\"" + binIndex.generic_string() + "\"></script>\n";
71+
}
6272

6373
// find end of header </head> from behind in indexHtml:
6474
auto headEnd = indexHtml.rfind("</head>");

0 commit comments

Comments
 (0)