Skip to content

Commit

Permalink
feat(ui/webview): expose WebView::load_html (#32)
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Gorez <[email protected]>
  • Loading branch information
tony-go authored Nov 12, 2024
1 parent c20ad2f commit 3b6e02b
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 68 deletions.
4 changes: 4 additions & 0 deletions cmake/example.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function(add_example)
add_custom_target(${EXAMPLE_NAME}_build
COMMAND "${CMAKE_COMMAND}"
--build "${EXAMPLE_BINARY_DIR}"
WORKING_DIRECTORY "${EXAMPLE_BINARY_DIR}"
COMMENT "Building ${EXAMPLE_NAME} example"
)
add_dependencies(${EXAMPLE_NAME}_build ${EXAMPLE_NAME}_configure)
Expand All @@ -36,16 +37,19 @@ function(add_example)
if(EXAMPLE_TYPE STREQUAL "desktop")
add_custom_target(${EXAMPLE_NAME}_run
COMMAND "${EXAMPLE_BINARY_DIR}/${EXAMPLE_APP_NAME}.app/Contents/MacOS/${EXAMPLE_APP_NAME}"
WORKING_DIRECTORY "${EXAMPLE_BINARY_DIR}"
COMMENT "Running ${EXAMPLE_NAME} example (bundle)")
else()
add_custom_target(${EXAMPLE_NAME}_run
COMMAND "${EXAMPLE_BINARY_DIR}/${EXAMPLE_APP_NAME}" --foo bar
WORKING_DIRECTORY "${EXAMPLE_BINARY_DIR}"
COMMENT "Running ${EXAMPLE_NAME} example (executable)")
endif()
add_dependencies(${EXAMPLE_NAME}_run ${EXAMPLE_NAME}_build)
elseif(WIN32)
add_custom_target(${EXAMPLE_NAME}_run
COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_NAME}/Debug/${EXAMPLE_APP_NAME}.exe"
WORKING_DIRECTORY "${EXAMPLE_BINARY_DIR}"
COMMENT "Running ${EXAMPLE_NAME} example (Windows)")
add_dependencies(${EXAMPLE_NAME}_run ${EXAMPLE_NAME}_build)
endif()
Expand Down
23 changes: 22 additions & 1 deletion cmake/native.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function(native_add_app)
cmake_parse_arguments(NATIVE "" "TARGET;PLATFORM" "" ${ARGN})
cmake_parse_arguments(NATIVE "" "TARGET;PLATFORM" "ASSETS" ${ARGN})

if(APPLE)
_native_add_app_apple(${ARGN})
Expand All @@ -14,6 +14,27 @@ function(native_add_app)
"NATIVE_${NATIVE_PLATFORM_UPPER}=1")
endfunction()

function(native_add_assets)
cmake_parse_arguments(NATIVE "" "TARGET" "ASSETS" ${ARGN})

if(NOT NATIVE_TARGET)
message(FATAL_ERROR "You must specify a target")
endif()

if(NOT NATIVE_ASSETS)
message(FATAL_ERROR "You must specify assets")
endif()

foreach(asset IN LISTS NATIVE_ASSETS)
add_custom_command(
TARGET ${NATIVE_TARGET}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${asset} ${CMAKE_CURRENT_BINARY_DIR}/assets/${asset}
COMMENT "Copying asset: ${asset} \n"
)
endforeach()
endfunction()

# Function to set profile properties for the app, including code signing identity
function(native_set_profile)
if(APPLE)
Expand Down
4 changes: 4 additions & 0 deletions example/hello_world/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ native_add_app(
PLATFORM desktop
SOURCES hello_world.cc)

native_add_assets(
TARGET hello_world_app
ASSETS index.html style.css)

native_set_profile(
TARGET hello_world_app
NAME "example_hello_world"
Expand Down
3 changes: 2 additions & 1 deletion example/hello_world/hello_world.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif

#include <exception>
#include <filesystem>
#include <iostream>

class App : public sourcemeta::native::Application {
Expand All @@ -18,7 +19,7 @@ class App : public sourcemeta::native::Application {
window.show();

#ifdef _WIN32
webview.load_url("https://www.sourcemeta.com");
webview.load_html("index.html");
window.add(webview);
#endif

Expand Down
16 changes: 16 additions & 0 deletions example/hello_world/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Native Framework</title>
<link rel="stylesheet" href="https://native.assets/style.css">
</head>

<body>
<h1>Welcome to Native</h1>
<p>This is a simple example to demonstrate loading HTML with CSS styling.</p>
</body>

</html>
19 changes: 19 additions & 0 deletions example/hello_world/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
}

h1 {
color: #333;
}

p {
color: #666;
}
8 changes: 5 additions & 3 deletions src/ui/webview/include/sourcemeta/native/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ class WebView {

// Core functionality
auto attach_to(sourcemeta::native::Window &window) -> void;

// Load content
auto load_url(const std::string &url) -> void;
// auto load_html(const std::string& html) -> void;
auto load_html(const std::string &html_path) -> void;

// IPC messaging
// auto send_message(const std::string& channel, const std::string& message)
Expand All @@ -30,8 +32,8 @@ class WebView {
auto resize() -> void;

private:
using Internal = void *;
Internal internal_;
class Internal;
Internal *internal_;
};

} // namespace sourcemeta::native
Expand Down
Loading

0 comments on commit 3b6e02b

Please sign in to comment.