Skip to content

Commit 897a440

Browse files
committed
New C++ Example - Virtual File System
1 parent 1ce6841 commit 897a440

File tree

8 files changed

+665
-0
lines changed

8 files changed

+665
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# WebUI C++ Example
2+
3+
# == 1. VARIABLES =============================================================
4+
5+
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
6+
PROJECT_DIR := $(dir $(MAKEFILE_PATH))/../../../
7+
TARGET := $(firstword $(MAKECMDGOALS))
8+
LIB_DIR := $(PROJECT_DIR)/dist
9+
ifeq ($(TARGET), debug)
10+
LIB_DIR := $(LIB_DIR)/debug
11+
endif
12+
INCLUDE_DIR := $(PROJECT_DIR)/include
13+
WEBUI_LIB_NAME = webui-2
14+
ifeq ($(WEBUI_USE_TLS), 1)
15+
WEBUI_LIB_NAME = webui-2-secure
16+
endif
17+
18+
# ARGS
19+
# Set a compiler when running on Linux via `make CC=g++` / `make CC=clang`
20+
CC = g++
21+
# Build the WebUI library if running via `make BUILD_LIB=true`
22+
BUILD_LIB ?=
23+
24+
# BUILD FLAGS
25+
STATIC_BUILD_FLAGS = main.cpp -I"$(INCLUDE_DIR)" -L"$(LIB_DIR)"
26+
DYN_BUILD_FLAGS = main.cpp -I"$(INCLUDE_DIR)" -L"$(LIB_DIR)"
27+
28+
# Platform conditions
29+
ifeq ($(OS),Windows_NT)
30+
# Windows
31+
PLATFORM := windows
32+
SHELL := CMD
33+
STATIC_BUILD_FLAGS += -l$(WEBUI_LIB_NAME)-static -lws2_32 -Wall -luser32 -static
34+
COPY_LIB_CMD := @copy "$(LIB_DIR)\$(WEBUI_LIB_NAME).dll" "$(WEBUI_LIB_NAME).dll"
35+
DYN_BUILD_FLAGS += "$(WEBUI_LIB_NAME).dll" -lws2_32 -Wall -luser32
36+
STATIC_OUT := main.exe
37+
DYN_OUT := main-dyn.exe
38+
LWS2_OPT := -lws2_32 -lole32
39+
STRIP_OPT := --strip-all
40+
CONSOLE_APP := -Wl,-subsystem=console
41+
GUI_APP := -Wl,-subsystem=windows
42+
else
43+
STATIC_BUILD_FLAGS += -l$(WEBUI_LIB_NAME)-static -lpthread -lm -ldl
44+
DYN_BUILD_FLAGS += -l$(WEBUI_LIB_NAME) -lpthread -lm -ldl
45+
STATIC_OUT := main
46+
DYN_OUT := main-dyn
47+
ifeq ($(shell uname),Darwin)
48+
# MacOS
49+
PLATFORM := macos
50+
CC = clang
51+
COPY_LIB_CMD := @cp "$(LIB_DIR)/lib$(WEBUI_LIB_NAME).dylib" "lib$(WEBUI_LIB_NAME).dylib"
52+
WKWEBKIT_LINK_FLAGS := -framework Cocoa -framework WebKit
53+
else
54+
# Linux
55+
PLATFORM := linux
56+
COPY_LIB_CMD := @cp "$(LIB_DIR)/lib$(WEBUI_LIB_NAME).so" "lib$(WEBUI_LIB_NAME).so"
57+
STRIP_OPT := --strip-all
58+
ifeq ($(CC),clang)
59+
LLVM_OPT := llvm-
60+
endif
61+
endif
62+
endif
63+
64+
# == 2.TARGETS ================================================================
65+
66+
all: release
67+
68+
debug: --validate-args
69+
ifeq ($(BUILD_LIB),true)
70+
@cd "$(PROJECT_DIR)" && $(MAKE) debug
71+
endif
72+
# Static with Debug info
73+
ifneq ($(WEBUI_USE_TLS), 1)
74+
@echo "Build C++ Example ($(CC) debug static)..."
75+
@$(CC) -g $(CONSOLE_APP) $(STATIC_BUILD_FLAGS) $(LWS2_OPT) $(WKWEBKIT_LINK_FLAGS) -o $(STATIC_OUT)
76+
endif
77+
# Dynamic with Debug info
78+
@echo "Build C++ Example ($(CC) debug dynamic)..."
79+
$(COPY_LIB_CMD)
80+
@$(CC) -g $(CONSOLE_APP) $(DYN_BUILD_FLAGS) $(LWS2_OPT) $(WKWEBKIT_LINK_FLAGS) -o $(DYN_OUT)
81+
# Clean
82+
ifeq ($(PLATFORM),windows)
83+
@- del *.o >nul 2>&1
84+
else
85+
@- rm -f *.o
86+
@- rm -rf *.dSYM # macOS
87+
endif
88+
@echo "Done."
89+
90+
release: --validate-args
91+
ifeq ($(BUILD_LIB),true)
92+
@cd "$(PROJECT_DIR)" && $(MAKE)
93+
endif
94+
# Static Release
95+
ifneq ($(WEBUI_USE_TLS), 1)
96+
@echo "Build C++ Example ($(CC) release static)..."
97+
@$(CC) -Os $(GUI_APP) $(STATIC_BUILD_FLAGS) $(LWS2_OPT) $(WKWEBKIT_LINK_FLAGS) -o $(STATIC_OUT)
98+
@$(LLVM_OPT)strip $(STRIP_OPT) $(STATIC_OUT)
99+
endif
100+
# Dynamic Release
101+
@echo "Build C++ Example ($(CC) release dynamic)..."
102+
$(COPY_LIB_CMD)
103+
@$(CC) $(GUI_APP) $(DYN_BUILD_FLAGS) $(LWS2_OPT) $(WKWEBKIT_LINK_FLAGS) -o $(DYN_OUT)
104+
@$(LLVM_OPT)strip $(STRIP_OPT) $(DYN_OUT)
105+
# Clean
106+
ifeq ($(PLATFORM),windows)
107+
@- del *.o >nul 2>&1
108+
else
109+
@- rm -f *.o
110+
@- rm -rf *.dSYM # macOS
111+
endif
112+
@echo "Done."
113+
114+
clean: --clean-$(PLATFORM)
115+
116+
# INTERNAL TARGETS
117+
118+
--validate-args:
119+
ifneq ($(filter $(CC),g++ clang aarch64-linux-gnu-g++ arm-linux-gnueabihf-g++ musl-g++),$(CC))
120+
$(error Invalid compiler specified: `$(CC)`)
121+
endif
122+
123+
--clean-linux: --clean-unix
124+
125+
--clean-macos: --clean-unix
126+
127+
--clean-unix:
128+
- rm -f *.o
129+
- rm -f *.a
130+
- rm -f *.so
131+
- rm -f *.dylib
132+
- rm -rf *.dSYM
133+
134+
--clean-windows:
135+
- del *.o >nul 2>&1
136+
- del *.dll >nul 2>&1
137+
- del *.a >nul 2>&1
138+
- del *.exe >nul 2>&1
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# WebUI C++ Example
2+
# Windows - Microsoft Visual C++
3+
4+
SHELL = CMD
5+
LIB_DIR = ../../../dist
6+
INCLUDE_DIR = ../../../include
7+
WEBUI_LIB_NAME = webui-2
8+
!IF "$(WEBUI_USE_TLS)" == "1"
9+
WEBUI_LIB_NAME = webui-2-secure
10+
!ENDIF
11+
12+
# Build the WebUI library if running `nmake BUILD_LIB=true`
13+
BUILD_LIB =
14+
15+
all: release
16+
17+
debug:
18+
!IF "$(BUILD_LIB)" == "true"
19+
@cd "$(LIB_DIR)" && cd .. && $(MAKE) debug
20+
!ENDIF
21+
# Static with Debug info
22+
!IF "$(WEBUI_USE_TLS)" != "1"
23+
@echo Build C++ Example (Debug Static)...
24+
@cl /Zi /EHsc /std:c++17 main.cpp /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)/debug" /SUBSYSTEM:CONSOLE $(WEBUI_LIB_NAME)-static.lib user32.lib Advapi32.lib Shell32.lib Ole32.lib /OUT:main.exe 1>NUL 2>&1
25+
!ENDIF
26+
# Dynamic with Debug info
27+
@echo Build C++ Example (Debug Dynamic)...
28+
@cl /Zi /EHsc /std:c++17 main.cpp /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)/debug" /SUBSYSTEM:CONSOLE $(WEBUI_LIB_NAME)-static.lib user32.lib Advapi32.lib Shell32.lib Ole32.lib /OUT:main-dyn.exe 1>NUL 2>&1
29+
# Clean
30+
@- del *.exp >nul 2>&1
31+
@- del *.ilk >nul 2>&1
32+
@- del *.lib >nul 2>&1
33+
@- del *.obj >nul 2>&1
34+
@- del *.pdb >nul 2>&1
35+
@echo Done.
36+
37+
release:
38+
!IF "$(BUILD_LIB)" == "true"
39+
@cd "$(LIB_DIR)" && cd .. && $(MAKE)
40+
!ENDIF
41+
# Static Release
42+
!IF "$(WEBUI_USE_TLS)" != "1"
43+
@echo Build C++ Example (Release Static)...
44+
@cl /EHsc /std:c++17 main.cpp /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)" /SUBSYSTEM:WINDOWS $(WEBUI_LIB_NAME)-static.lib user32.lib Advapi32.lib Shell32.lib Ole32.lib /OUT:main.exe 1>NUL 2>&1
45+
!ENDIF
46+
# Dynamic Release
47+
@echo Build C++ Example (Release Dynamic)...
48+
@cl /EHsc /std:c++17 main.cpp /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)" /SUBSYSTEM:WINDOWS $(WEBUI_LIB_NAME).lib user32.lib Advapi32.lib Shell32.lib Ole32.lib /OUT:main-dyn.exe 1>NUL 2>&1
49+
# Clean
50+
@- del *.exp >nul 2>&1
51+
@- del *.ilk >nul 2>&1
52+
@- del *.lib >nul 2>&1
53+
@- del *.obj >nul 2>&1
54+
@- del *.pdb >nul 2>&1
55+
@echo Done.
56+
57+
clean:
58+
- del *.obj >nul 2>&1
59+
- del *.ilk >nul 2>&1
60+
- del *.pdb >nul 2>&1
61+
- del *.exp >nul 2>&1
62+
- del *.exe >nul 2>&1
63+
- del *.lib >nul 2>&1
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Virtual File System Example C++
2+
3+
#include "webui.hpp"
4+
#include "vfs.h"
5+
6+
void exit_app(webui::window::event* e) {
7+
webui::exit();
8+
}
9+
10+
int main() {
11+
12+
// Create new windows
13+
webui::window my_window;
14+
15+
// Bind HTML element IDs with a C functions
16+
my_window.bind("Exit", exit_app);
17+
18+
// VSF (Virtual File System) C++ Example
19+
//
20+
// 1. Run Python script to generate header file of a folder
21+
// python vfs.py "/path/to/folder" "vfs.h"
22+
//
23+
// 2. Include header file in your C++ project
24+
// #include "vfs.h"
25+
//
26+
// 3. use vfs in your custom files handler `webui::window::set_file_handler()`
27+
// MyWindow.set_file_handler(vfs);
28+
29+
// Set a custom files handler
30+
my_window.set_file_handler(vfs);
31+
32+
// Show a new window
33+
// webui_show_browser(MyWindow, "index.html", Chrome);
34+
my_window.show("index.html");
35+
36+
// Wait until all windows get closed
37+
webui::wait();
38+
39+
// Free all memory resources (Optional)
40+
webui::clean();
41+
42+
return 0;
43+
}
44+
45+
#if defined(_MSC_VER)
46+
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow) { return main(); }
47+
#endif
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>WebUI - Virtual File System C++</title>
6+
<style>
7+
body {
8+
font-family: 'Arial', sans-serif;
9+
color: white;
10+
background: linear-gradient(to right, #507d91, #1c596f, #022737);
11+
text-align: center;
12+
font-size: 18px;
13+
}
14+
button,
15+
input {
16+
padding: 10px;
17+
border-radius: 3px;
18+
border: 1px solid #ccc;
19+
box-shadow: 0 3px 5px rgba(0, 0, 0, 0.1);
20+
transition: 0.2s;
21+
}
22+
button {
23+
background: #3498db;
24+
color: #fff;
25+
cursor: pointer;
26+
font-size: 16px;
27+
}
28+
h1 {
29+
text-shadow: -7px 10px 7px rgb(67 57 57 / 76%);
30+
}
31+
button:hover {
32+
background: #c9913d;
33+
}
34+
input:focus {
35+
outline: none;
36+
border-color: #3498db;
37+
}
38+
39+
a:link {
40+
color: #fd5723;
41+
}
42+
a:active {
43+
color: #fd5723;
44+
}
45+
a:visited {
46+
color: #fd5723;
47+
}
48+
a:hover {
49+
color: #f0bcac;
50+
}
51+
</style>
52+
</head>
53+
<body>
54+
<h3 id="title">Virtual File System C++ Example</h3>
55+
<br />
56+
<img src="svg/webui.svg">
57+
<br />
58+
<p>
59+
This file is embedded in this C++ application.<br />
60+
</p>
61+
<h4><a href="sub">/sub</a></h4>
62+
<br />
63+
<button id="Exit">Exit</button>
64+
</body>
65+
66+
<!-- Connect this window to the background app -->
67+
<script src="/webui.js"></script>
68+
69+
<script>
70+
71+
// JavaScript Example
72+
/*
73+
document.addEventListener('DOMContentLoaded', function() {
74+
// DOM is loaded. Check if `webui` object is available
75+
if (typeof webui !== 'undefined') {
76+
// Set events callback
77+
webui.setEventCallback((e) => {
78+
if (e == webui.event.CONNECTED) {
79+
// Connection to the backend is established
80+
console.log('Connected.');
81+
webuiTest();
82+
} else if (e == webui.event.DISCONNECTED) {
83+
// Connection to the backend is lost
84+
console.log('Disconnected.');
85+
}
86+
});
87+
} else {
88+
// The virtual file `webui.js` is not included
89+
alert('Please add webui.js to your HTML.');
90+
}
91+
});
92+
93+
function webuiTest() {
94+
// Call a backend function
95+
if (webui.isConnected()) {
96+
97+
// When you bind a func in the backend
98+
// webui will create the `func` object
99+
// in three places:
100+
//
101+
// Global : func(...)
102+
// Property : webui.func(...)
103+
// Method : webui.call('func', ...)
104+
//
105+
// [!] Note: Objects creation fails when
106+
// a similar object already exist.
107+
108+
const foo = 'Hello';
109+
const bar = 123456;
110+
111+
// Calling as global object
112+
myBackendFunction(foo, bar).then((response) => {
113+
// Do something with `response`
114+
});
115+
116+
// Calling as property of `webui.` object
117+
webui.myBackendFunction(foo, bar).then((response) => {
118+
// Do something with `response`
119+
});
120+
121+
// Calling using the method `webui.call()`
122+
webui.call('myBackendFunction', foo, bar).then((response) => {
123+
// Do something with `response`
124+
});
125+
126+
// Using await
127+
// const response = await myBackendFunction(foo, bar);
128+
// const response = await webui.myBackendFunction(foo, bar);
129+
// const response = await webui.call('myBackendFunction', foo, bar);
130+
}
131+
}
132+
*/
133+
134+
</script>
135+
</html>

0 commit comments

Comments
 (0)