Skip to content

Commit

Permalink
Merge pull request #81 from NuiCpp/devel
Browse files Browse the repository at this point in the history
CMake and Resizeability Updates
  • Loading branch information
5cript authored Oct 13, 2023
2 parents 4b574e2 + a78ca10 commit f00abc9
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 30 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)

project(nui VERSION 0.1.0)
project(nui VERSION 0.9.0)

set(NUI_SOURCE_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "NUI source directory")

Expand Down
47 changes: 39 additions & 8 deletions cmake/backend/emscripten.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ else()
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/_deps/emscripten-src"
)
endif()

add_custom_target(
emscripten-setup
DEPENDS ${CMAKE_BINARY_DIR}/_deps/emscripten-src/upstream/emscripten/.emscripten
Expand All @@ -38,11 +39,35 @@ function(nui_add_emscripten_target)

cmake_parse_arguments(
NUI_ADD_EMSCRIPTEN_TARGET_ARGS
""
"DISABLE_BIN2HPP;DISABLE_PARCEL_ADAPTER"
"TARGET;PREJS;SOURCE_DIR"
"CMAKE_OPTIONS"
${ARGN}
)
)

if (NOT NUI_ADD_EMSCRIPTEN_TARGET_ARGS_DISABLE_BIN2HPP)
set(ENABLE_BIN2HPP ON)
else()
if (NUI_ADD_EMSCRIPTEN_TARGET_ARGS_DISABLE_BIN2HPP)
set(ENABLE_BIN2HPP OFF)
else()
if (${TARGET_TYPE} STREQUAL "INTERFACE_LIBRARY")
set(ENABLE_BIN2HPP OFF)
else()
set(ENABLE_BIN2HPP ON)
endif()
endif()
endif()

if (NOT NUI_ADD_EMSCRIPTEN_TARGET_ARGS_DISABLE_PARCEL_ADAPTER)
set(ENABLE_PARCEL_ADAPTER ON)
else()
if (NUI_ADD_EMSCRIPTEN_TARGET_ARGS_DISABLE_PARCEL_ADAPTER)
set(ENABLE_PARCEL_ADAPTER OFF)
else()
set(ENABLE_PARCEL_ADAPTER ON)
endif()
endif()

if (NOT NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET)
message(FATAL_ERROR "You must provide a target to create a frontend pendant of.")
Expand All @@ -63,11 +88,17 @@ function(nui_add_emscripten_target)
string(REPLACE "-" "_" targetNormalized ${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET})

message(STATUS "emcmake: ${EMCMAKE}")

if (${TARGET_TYPE} STREQUAL "INTERFACE_LIBRARY")
set(ENABLE_BIN2HPP "no")

if (ENABLE_PARCEL_ADAPTER)
set(BUILD_COMMAND BUILD_COMMAND $<TARGET_FILE:parcel-adapter> "${SOURCE_DIR}/package.json" "${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}/package.json" "${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}")
else()
set(BUILD_COMMAND BUILD_COMMAND cmake -E copy "${SOURCE_DIR}/package.json" "${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}/package.json")
endif()

if (ENABLE_BIN2HPP)
set(BIN2HPP_COMMAND COMMAND $<TARGET_FILE:bin2hpp> "on" "${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}/bin/index.html" "${CMAKE_BINARY_DIR}/include/index.hpp" index)
else()
set(ENABLE_BIN2HPP "yes")
set(BIN2HPP_COMMAND COMMAND cmake -E true)
endif()

include(ExternalProject)
Expand All @@ -80,11 +111,11 @@ function(nui_add_emscripten_target)
${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_CMAKE_OPTIONS}
"${SOURCE_DIR}"
# copy over package.json and fill parcel options that do not exist on it
BUILD_COMMAND $<TARGET_FILE:parcel-adapter> "${SOURCE_DIR}/package.json" "${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}/package.json" "${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}"
${BUILD_COMMAND}
# emscripten make
COMMAND cmake --build "${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}" --target ${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET} ${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}-parcel
# convert result to header file containing the page
COMMAND $<TARGET_FILE:bin2hpp> ${ENABLE_BIN2HPP} "${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}/bin/index.html" "${CMAKE_BINARY_DIR}/include/index.hpp" index
${BIN2HPP_COMMAND}
BINARY_DIR "${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}"
BUILD_ALWAYS 1
BUILD_BYPRODUCTS "${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}/bin/index.html"
Expand Down
16 changes: 9 additions & 7 deletions cmake/dependencies/fmt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ option(NUI_FETCH_FMT "Fetch fmt" ON)
set(NUI_FMT_GIT_REPOSITORY "https://github.com/fmtlib/fmt.git" CACHE STRING "fmt git repository")
set(NUI_FMT_GIT_TAG "5f774c0aed4af6494b412cd3883e0f09295bd79d" CACHE STRING "fmt git tag")

include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY ${NUI_FMT_GIT_REPOSITORY}
GIT_TAG ${NUI_FMT_GIT_TAG}
)
if (NUI_FETCH_FMT)
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY ${NUI_FMT_GIT_REPOSITORY}
GIT_TAG ${NUI_FMT_GIT_TAG}
)

FetchContent_MakeAvailable(fmt)
FetchContent_MakeAvailable(fmt)
endif()
10 changes: 5 additions & 5 deletions cmake/dependencies/webview.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ endif()

option(NUI_FETCH_WEBVIEW "Fetch webview" ON)
set(NUI_WEBVIEW_REPOSITORY "https://github.com/5cript/webview.git" CACHE STRING "Webview repository")
set(NUI_WEBVIEW_TAG "c3e57c164a0751e5adb2ea45e1b5c308c3dbe716" CACHE STRING "Webview tag")
set(NUI_WEBVIEW_TAG "749f9dfbe140f5fd0be1cd53abcec7f05496cbc3" CACHE STRING "Webview tag")

if (NUI_FETCH_WEBVIEW)
include(FetchContent)
Expand All @@ -21,8 +21,8 @@ if (NUI_FETCH_WEBVIEW)
add_library(webview INTERFACE)

add_custom_target(
webview-prep
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/include"
webview-prep
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/include"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_BINARY_DIR}/_deps/webview_raw-src/webview.h" "${CMAKE_BINARY_DIR}/include/webview.h"
COMMAND_EXPAND_LISTS
)
Expand Down Expand Up @@ -73,8 +73,8 @@ if (NUI_FETCH_WEBVIEW)

target_include_directories(webview INTERFACE "${CMAKE_BINARY_DIR}/_deps/webview_binary-src/build/native/include")
target_link_directories(
webview
INTERFACE
webview
INTERFACE
"${CMAKE_BINARY_DIR}/_deps/webview_binary-src/build/native/${WEBVIEW_DLL_SUBDIRECTORY}"
"${CMAKE_BINARY_DIR}/lib"
)
Expand Down
6 changes: 4 additions & 2 deletions cmake/options.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
option(NUI_USE_EXTERNAL_EMSCRIPTEN "Use external emscripten?" off)
option(NUI_NPM "set npm" "npm")

option(NUI_ENABLE_TESTS "Enable test target" off)
option(NUI_NPM "set npm" "npm")
option(NUI_BUILD_EXAMPLES "Build examples" off)
option(NUI_ENABLE_CLANG_TIDY "Enable clang-tidy" off)
option(NUI_ENABLE_CLANG_TIDY "Enable clang-tidy" off)

option(NUI_ENABLE_TOOLING_CONFIGURE "Enable patching and configuring of acorn and emscription? (default on)" on)
3 changes: 2 additions & 1 deletion nui/include/nui/frontend/extensions/make_resizeable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace Nui
enum class ResizeableEdge
{
Right,
Bottom
Bottom,
Top
};

void makeResizeable(Nui::val const& element, ResizeableEdge edge = ResizeableEdge::Right);
Expand Down
19 changes: 17 additions & 2 deletions nui/js/make_resizeable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const makeResizeable = (element: HTMLElement, borderSize: number, edge?: "right" | "bottom") => {
const makeResizeable = (element: HTMLElement, borderSize: number, edge?: "right" | "bottom" | "top") => {
edge = edge || "right";

let pos: number;
Expand All @@ -7,7 +7,7 @@ const makeResizeable = (element: HTMLElement, borderSize: number, edge?: "right"
return (x: number, y: number, w: number, h: number) => {
const dx = -(pos - x);
pos = x;
element.style.width = (w + dx) + "px";
element.style.width = (w + dx) + "px";
};
}
else if (edge === "bottom") {
Expand All @@ -17,6 +17,14 @@ const makeResizeable = (element: HTMLElement, borderSize: number, edge?: "right"
element.style.height = (h + dy) + "px";
};
}
else if (edge === "top") {
// Assuming that the previous elements auto resize
return (x: number, y: number, w: number, h: number) => {
const dy = (pos - y);
pos = y;
element.style.height = (h + dy) + "px";
}
}
else {
throw new Error("Invalid edge");
}
Expand Down Expand Up @@ -50,6 +58,13 @@ const makeResizeable = (element: HTMLElement, borderSize: number, edge?: "right"
document.addEventListener("mousemove", resize, false);
}
}
else if (edge == "top") {
const t = (e.target as HTMLElement).getBoundingClientRect().top;
if (e.y <= t + borderSize + 1 /*rounding tolerance*/) {
pos = e.y;
document.addEventListener("mousemove", resize, false);
}
}
}, false);

document.addEventListener("mouseup", () => {
Expand Down
16 changes: 12 additions & 4 deletions nui/src/nui/frontend/extensions/make_resizeable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ namespace Nui
{
void makeResizeable(Nui::val const& element, ResizeableEdge edge)
{
Nui::val::global("nui_lib")["makeResizeable"](
element,
Nui::val{5},
Nui::val{edge == ResizeableEdge::Right ? std::string{"right"} : std::string{"bottom"}});
const auto edgeString = [&edge]() -> std::string {
if (edge == ResizeableEdge::Right)
return "right";
else if (edge == ResizeableEdge::Bottom)
return "bottom";
else if (edge == ResizeableEdge::Top)
return "top";
else
return "right";
}();

Nui::val::global("nui_lib")["makeResizeable"](element, Nui::val{5}, Nui::val{edgeString});
}
}

0 comments on commit f00abc9

Please sign in to comment.