Skip to content

Commit 3b6e02b

Browse files
authored
feat(ui/webview): expose WebView::load_html (#32)
Signed-off-by: Tony Gorez <[email protected]>
1 parent c20ad2f commit 3b6e02b

File tree

8 files changed

+221
-68
lines changed

8 files changed

+221
-68
lines changed

cmake/example.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function(add_example)
2727
add_custom_target(${EXAMPLE_NAME}_build
2828
COMMAND "${CMAKE_COMMAND}"
2929
--build "${EXAMPLE_BINARY_DIR}"
30+
WORKING_DIRECTORY "${EXAMPLE_BINARY_DIR}"
3031
COMMENT "Building ${EXAMPLE_NAME} example"
3132
)
3233
add_dependencies(${EXAMPLE_NAME}_build ${EXAMPLE_NAME}_configure)
@@ -36,16 +37,19 @@ function(add_example)
3637
if(EXAMPLE_TYPE STREQUAL "desktop")
3738
add_custom_target(${EXAMPLE_NAME}_run
3839
COMMAND "${EXAMPLE_BINARY_DIR}/${EXAMPLE_APP_NAME}.app/Contents/MacOS/${EXAMPLE_APP_NAME}"
40+
WORKING_DIRECTORY "${EXAMPLE_BINARY_DIR}"
3941
COMMENT "Running ${EXAMPLE_NAME} example (bundle)")
4042
else()
4143
add_custom_target(${EXAMPLE_NAME}_run
4244
COMMAND "${EXAMPLE_BINARY_DIR}/${EXAMPLE_APP_NAME}" --foo bar
45+
WORKING_DIRECTORY "${EXAMPLE_BINARY_DIR}"
4346
COMMENT "Running ${EXAMPLE_NAME} example (executable)")
4447
endif()
4548
add_dependencies(${EXAMPLE_NAME}_run ${EXAMPLE_NAME}_build)
4649
elseif(WIN32)
4750
add_custom_target(${EXAMPLE_NAME}_run
4851
COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_NAME}/Debug/${EXAMPLE_APP_NAME}.exe"
52+
WORKING_DIRECTORY "${EXAMPLE_BINARY_DIR}"
4953
COMMENT "Running ${EXAMPLE_NAME} example (Windows)")
5054
add_dependencies(${EXAMPLE_NAME}_run ${EXAMPLE_NAME}_build)
5155
endif()

cmake/native.cmake

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function(native_add_app)
2-
cmake_parse_arguments(NATIVE "" "TARGET;PLATFORM" "" ${ARGN})
2+
cmake_parse_arguments(NATIVE "" "TARGET;PLATFORM" "ASSETS" ${ARGN})
33

44
if(APPLE)
55
_native_add_app_apple(${ARGN})
@@ -14,6 +14,27 @@ function(native_add_app)
1414
"NATIVE_${NATIVE_PLATFORM_UPPER}=1")
1515
endfunction()
1616

17+
function(native_add_assets)
18+
cmake_parse_arguments(NATIVE "" "TARGET" "ASSETS" ${ARGN})
19+
20+
if(NOT NATIVE_TARGET)
21+
message(FATAL_ERROR "You must specify a target")
22+
endif()
23+
24+
if(NOT NATIVE_ASSETS)
25+
message(FATAL_ERROR "You must specify assets")
26+
endif()
27+
28+
foreach(asset IN LISTS NATIVE_ASSETS)
29+
add_custom_command(
30+
TARGET ${NATIVE_TARGET}
31+
POST_BUILD
32+
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${asset} ${CMAKE_CURRENT_BINARY_DIR}/assets/${asset}
33+
COMMENT "Copying asset: ${asset} \n"
34+
)
35+
endforeach()
36+
endfunction()
37+
1738
# Function to set profile properties for the app, including code signing identity
1839
function(native_set_profile)
1940
if(APPLE)

example/hello_world/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ native_add_app(
1313
PLATFORM desktop
1414
SOURCES hello_world.cc)
1515

16+
native_add_assets(
17+
TARGET hello_world_app
18+
ASSETS index.html style.css)
19+
1620
native_set_profile(
1721
TARGET hello_world_app
1822
NAME "example_hello_world"

example/hello_world/hello_world.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#endif
66

77
#include <exception>
8+
#include <filesystem>
89
#include <iostream>
910

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

2021
#ifdef _WIN32
21-
webview.load_url("https://www.sourcemeta.com");
22+
webview.load_html("index.html");
2223
window.add(webview);
2324
#endif
2425

example/hello_world/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Native Framework</title>
8+
<link rel="stylesheet" href="https://native.assets/style.css">
9+
</head>
10+
11+
<body>
12+
<h1>Welcome to Native</h1>
13+
<p>This is a simple example to demonstrate loading HTML with CSS styling.</p>
14+
</body>
15+
16+
</html>

example/hello_world/style.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
margin: 0;
4+
padding: 0;
5+
display: flex;
6+
flex-direction: column;
7+
align-items: center;
8+
justify-content: center;
9+
height: 100vh;
10+
background-color: #f0f0f0;
11+
}
12+
13+
h1 {
14+
color: #333;
15+
}
16+
17+
p {
18+
color: #666;
19+
}

src/ui/webview/include/sourcemeta/native/webview.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ class WebView {
1818

1919
// Core functionality
2020
auto attach_to(sourcemeta::native::Window &window) -> void;
21+
22+
// Load content
2123
auto load_url(const std::string &url) -> void;
22-
// auto load_html(const std::string& html) -> void;
24+
auto load_html(const std::string &html_path) -> void;
2325

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

3234
private:
33-
using Internal = void *;
34-
Internal internal_;
35+
class Internal;
36+
Internal *internal_;
3537
};
3638

3739
} // namespace sourcemeta::native

0 commit comments

Comments
 (0)