Skip to content

Commit

Permalink
Merge pull request #117 from NuiCpp/feat/user-events
Browse files Browse the repository at this point in the history
Feat/user events
  • Loading branch information
5cript authored Jul 15, 2024
2 parents 82700a2 + 6536fdc commit f8a8c69
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions cmake/backend/emscripten.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ function(nui_add_emscripten_target)
${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_CMAKE_OPTIONS}
"-DNUI_NPM=${NUI_NPM}"
"-DNUI_NODE=${NUI_NODE}"
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
-DNUI_INLINE_EXTRACTOR_TARGET_FILE=$<TARGET_FILE:inline-parser>
-DNUI_INLINE_INJECTOR_TARGET_FILE=$<TARGET_FILE:inline-injector>
-DNUI_MODULE_BUILD_DIR=${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}
Expand Down
13 changes: 9 additions & 4 deletions cmake/frontend/emscripten.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function(nui_prepare_emscripten_target)
cmake_parse_arguments(
NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS
"NO_INLINE;NO_INLINE_INJECT"
"NO_INLINE;NO_INLINE_INJECT;LEAN_INDEX_HTML"
"TARGET;PREJS;STATIC;UNPACKED_MODE"
"EMSCRIPTEN_LINK_OPTIONS;EMSCRIPTEN_COMPILE_OPTIONS;PARCEL_ARGS;NPM_INSTALL_ARGS"
${ARGN}
Expand All @@ -23,11 +23,16 @@ function(nui_prepare_emscripten_target)
set(NUI_DEFER_INLINE_SCRIPTS_TAG "defer")
endif()

set(NUI_LEAN_HTML "nolean")
if (NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_LEAN_INDEX_HTML)
set(NUI_LEAN_HTML "lean")
endif()

set(INLINER_COMMAND "")
if (NOT NO_INLINE)
if (NOT NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_NO_INLINE)
nui_enable_inline(TARGET ${NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_TARGET} RELATIVE_TO ${CMAKE_CURRENT_SOURCE_DIR})
if (NOT NO_INLINE_INJECT)
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})
if (NOT NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_NO_INLINE_INJECT)
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})
endif()
endif()

Expand Down
12 changes: 12 additions & 0 deletions nui/include/nui/event_system/listen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,16 @@ namespace Nui
{
return listen(eventContext, obs, std::function(std::move(onEvent)));
}

template <typename ValueT, typename FunctionT>
void listen(std::shared_ptr<Observed<ValueT>> const& obs, FunctionT onEvent)
{
return listen(globalEventContext, obs, std::function(std::move(onEvent)));
}

template <typename ValueT, typename FunctionT>
void listen(Observed<ValueT> const& obs, FunctionT onEvent)
{
return listen(globalEventContext, obs, std::function(std::move(onEvent)));
}
}
3 changes: 2 additions & 1 deletion nui/include/nui/event_system/observed_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <deque>
#include <string>
#include <cassert>
#include <set>

namespace Nui
{
Expand Down Expand Up @@ -1286,7 +1287,7 @@ namespace Nui
: observed_{&observed}
{}

inline T value() const
inline T const& value() const
{
return observed_->value();
}
Expand Down
2 changes: 1 addition & 1 deletion nui/test/nui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ add_library(nui-frontend-mocked STATIC ${NUI_FRONTEND_SOURCES})

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

find_package(Boost REQUIRED)
find_package(Boost CONFIG REQUIRED)

target_link_libraries(nui-frontend-mocked PUBLIC
libcpppre
Expand Down
22 changes: 16 additions & 6 deletions tools/inline_injector/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ std::string readFile(const std::filesystem::path& path)

int main(int argc, char** argv)
{
if (argc != 5)
if (argc != 6)
{
std::cout
<< "Expected 4 argument: <index.html> <import_scripts> <import_styles> <import_scripts_defer>, but got "
<< argc - 1 << "\n";
std::cout << "Expected 4 argument: <index.html> <import_scripts> <import_styles> <import_scripts_defer> "
"<lean_html>, but got "
<< argc - 1 << "\n";
return 1;
}

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

std::string indexHtml;
try
Expand All @@ -57,8 +58,17 @@ int main(int argc, char** argv)
relativeImportScriptsFile.generic_string() + "\";\n\t</script>\n";
const std::string importStylesHtml =
"\t<style>\n\t\t@import \"" + relativeImportStylesFile.generic_string() + "\";\n\t</style>\n";
const std::string importBinIndexHtml =
"\t<script type=\"module\" defer>\n\t\timport \"" + binIndex.generic_string() + "\";\n\t</script>\n";

std::string importBinIndexHtml;
if (!leanHtml)
{
importBinIndexHtml =
"\t<script type=\"module\" defer>\n\t\timport \"" + binIndex.generic_string() + "\";\n\t</script>\n";
}
else
{
importBinIndexHtml = "\t<script type=\"module\" defer src=\"" + binIndex.generic_string() + "\"></script>\n";
}

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

0 comments on commit f8a8c69

Please sign in to comment.