Skip to content
This repository was archived by the owner on Apr 5, 2022. It is now read-only.

Commit 7250f97

Browse files
authored
Merge pull request #2 from Soundux/rewrite
Rewrite
2 parents 4fe7947 + 59debfd commit 7250f97

40 files changed

+1838
-1254
lines changed

.clang-tidy

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,4 @@ Checks: "*,\
5050
-cppcoreguidelines-pro-type-union-access,\
5151
-readability-static-accessed-through-instance,\
5252
-cppcoreguidelines-special-member-functions,\
53-
-readability-isolate-declaration,\
54-
-google-default-arguments"
53+
-readability-isolate-declaration"

.github/workflows/build_linux.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
on:
22
push:
3-
branches: [master]
43
paths-ignore:
54
- "**/README.md"
65
- "**/build_windows.yml"
76
pull_request:
8-
branches: [master]
9-
7+
108
name: Build on Linux
119
jobs:
1210
build-linux:

.github/workflows/build_windows.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
on:
22
push:
3-
branches: [master]
43
paths-ignore:
54
- "**/README.md"
65
- "**/build_linux.yml"
76
pull_request:
8-
branches: [master]
97

108
name: Build on Windows
119
jobs:

CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
cmake_minimum_required(VERSION 3.1)
2-
project(webview VERSION 0.1 DESCRIPTION "The WebView component of Soundux")
2+
project(webview VERSION 0.2 DESCRIPTION "A cross-platform C++ webview library")
33

44
file(GLOB src
5-
"src/*.cpp"
6-
"src/*/*.cpp"
7-
"src/*/*/*.cpp"
5+
"webview/src/*.cpp"
6+
"webview/src/*/*.cpp"
7+
"webview/src/*/*/*.cpp"
88
)
99

1010
add_library(webview STATIC ${src})
@@ -26,17 +26,17 @@ if (WIN32)
2626
target_link_libraries(webview INTERFACE Version.lib Shlwapi.lib ${CMAKE_BINARY_DIR}/packages/Microsoft.Web.WebView2/build/native/x64/WebView2LoaderStatic.lib ${CMAKE_BINARY_DIR}/packages/Microsoft.Windows.ImplementationLibrary/build/native/Microsoft.Windows.ImplementationLibrary.targets)
2727
target_include_directories(webview SYSTEM PUBLIC ${CMAKE_BINARY_DIR}/packages/Microsoft.Web.WebView2/build/native/include ${CMAKE_BINARY_DIR}/packages/Microsoft.Windows.ImplementationLibrary/include)
2828
elseif(UNIX)
29-
target_compile_options(webview INTERFACE -Wall -Wextra -Werror -pedantic -Wno-unused-lambda-capture)
29+
target_compile_options(webview PRIVATE -Wall -Wextra -Werror -pedantic -Wno-unused-lambda-capture)
3030

3131
find_package(PkgConfig REQUIRED)
3232
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
3333
pkg_check_modules(WEBKIT2 REQUIRED webkit2gtk-4.0)
3434

35-
target_link_libraries(webview INTERFACE ${GTK3_LIBRARIES} ${WEBKIT2_LIBRARIES})
35+
target_link_libraries(webview INTERFACE ${GTK3_LIBRARIES} ${WEBKIT2_LIBRARIES} pthread)
3636
target_include_directories(webview SYSTEM PUBLIC ${GTK3_INCLUDE_DIRS} ${WEBKIT2_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR})
3737
endif()
3838

39-
target_include_directories(webview SYSTEM PUBLIC "src/")
39+
target_include_directories(webview SYSTEM PUBLIC "webview/include/")
4040
target_include_directories(webview SYSTEM PUBLIC "lib/json/single_include/nlohmann")
4141

4242
target_compile_features(webview PUBLIC cxx_std_17)
@@ -45,4 +45,4 @@ set_target_properties(webview PROPERTIES CMAKE_CXX_EXTENSIONS Off)
4545
set_target_properties(webview PROPERTIES CMAKE_CXX_STANDARD_REQUIRED On)
4646

4747
set_target_properties(webview PROPERTIES VERSION ${PROJECT_VERSION})
48-
set_target_properties(webview PROPERTIES PROJECT_NAME ${PROJECT_NAME})
48+
set_target_properties(webview PROPERTIES PROJECT_NAME ${PROJECT_NAME})

README.md

Lines changed: 241 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ A cross-platform C++17 library that allows you to create a simple webview.
2424
- Linux
2525
- (Runtime & Build) webkit2gtk
2626
27+
## Example
28+
```cpp
29+
#include <webview.hpp>
30+
31+
int main()
32+
{
33+
Webview::Window webview("webview", 800, 900);
34+
webview.expose(Webview::Function("addTen", [](int num) {
35+
return num + 10;
36+
}));
37+
38+
webview.run();
39+
return 0;
40+
}
41+
```
42+
43+
For more examples see [`examples`](https://github.com/Soundux/webviewpp/tree/master/examples)
44+
2745
## Embedding
2846
webviewpp supports embedding of all required files.
2947
To embed your files you have to use the [embed-helper](https://github.com/Soundux/webviewpp/tree/master/embed-helper).
@@ -38,14 +56,234 @@ Usage:
3856
./embed_helper <path to folder containing all the required files>
3957
```
4058
- Add the parent folder of the `embedded` folder to your include directories
41-
- Change `navigate` calls to
59+
- Change `setUrl` calls to
4260
- `embedded://<filepath>` on Linux
4361
- `file:///embedded/<filepath>` on Windows
4462

4563
> For an example see [examples/embedded](https://github.com/Soundux/webviewpp/tree/master/examples/embedded)
4664

4765
## Documentation
48-
The documentation was moved to the [main header file](https://github.com/Soundux/webviewpp/blob/master/src/webview/webview.hpp#L200)
66+
### Window::hide
67+
68+
``` cpp
69+
void hide();
70+
```
71+
72+
> Hides the window
73+
74+
-----
75+
76+
### Window::show
77+
78+
``` cpp
79+
void show();
80+
```
81+
82+
> Shows the window
83+
84+
-----
85+
86+
### Window::isHidden
87+
88+
``` cpp
89+
bool isHidden();
90+
```
91+
92+
**Returns:**
93+
> Whether or the window is hidden
94+
95+
-----
96+
97+
### Window::setSize
98+
99+
``` cpp
100+
void setSize(std::size_t, std::size_t);
101+
```
102+
103+
> Sets the window size
104+
105+
-----
106+
107+
### Window::getSize
108+
109+
``` cpp
110+
std::pair<std::size_t, std::size_t> getSize();
111+
```
112+
113+
**Returns:**
114+
> The width and height in form of an `std::pair`
115+
116+
-----
117+
118+
### Window::getTitle
119+
120+
``` cpp
121+
std::string getTitle();
122+
```
123+
124+
**Returns:**
125+
> The title of the window
126+
127+
-----
128+
129+
### Window::setTitle
130+
131+
``` cpp
132+
void setTitle(std::string);
133+
```
134+
135+
> Sets the window title
136+
137+
-----
138+
139+
### Window::run
140+
141+
``` cpp
142+
void run();
143+
```
144+
145+
> Runs the mainloop
146+
147+
**Remarks:**
148+
> Is blocking
149+
150+
-----
151+
152+
### Window::exit
153+
154+
``` cpp
155+
void exit();
156+
```
157+
158+
> Closes the webview
159+
160+
-----
161+
162+
### Window::getUrl
163+
164+
``` cpp
165+
std::string getUrl();
166+
```
167+
168+
**Returns:**
169+
> The current url
170+
171+
-----
172+
173+
### Window::setUrl
174+
175+
``` cpp
176+
void setUrl(std::string);
177+
```
178+
179+
> Navigates to the given url
180+
181+
-----
182+
183+
### Window::enableContextMenu
184+
185+
``` cpp
186+
void enableContextMenu(bool);
187+
```
188+
189+
> Enables the context menu
190+
191+
-----
192+
193+
### Window::enableDevTools
194+
195+
``` cpp
196+
void enableDevTools(bool);
197+
```
198+
199+
> Enables the developer tools
200+
201+
-----
202+
203+
### Window::expose
204+
205+
``` cpp
206+
void expose(Webview::Function const&);
207+
```
208+
209+
> Exposes the given function
210+
211+
**Remarks:**
212+
> If the given Function is an `AsyncFunction` it will be run in a new thread
213+
214+
-----
215+
216+
### Window::callFunction
217+
218+
``` cpp
219+
template <typename T>
220+
std::future<T> callFunction(Webview::JavaScriptFunction&& function);
221+
```
222+
223+
> Calls the given javascript function
224+
225+
**Returns:**
226+
> The result of the javascript function call as `T`
227+
228+
**Preconditions**
229+
> `T` must be serializable by nlohmann::json
230+
231+
**Remarks:**
232+
> You should never call `.get()` on the returned future in a **non async** context as it will freeze the webview
233+
234+
-----
235+
236+
### Window::runCode
237+
238+
``` cpp
239+
void runCode(std::string const&);
240+
```
241+
242+
> Runs the given javascript code
243+
244+
-----
245+
246+
### Window::injectCode
247+
248+
``` cpp
249+
void injectCode(std::string const&);
250+
```
251+
252+
> Makes the given javascript code run on document load
253+
254+
-----
255+
256+
### Window::setCloseCallback
257+
258+
``` cpp
259+
void setCloseCallback(std::function<bool ()>);
260+
```
261+
262+
> Sets the close-callback to the given callback
263+
264+
**Remarks:**
265+
> If the callback returns `true` the webview will not close
266+
267+
-----
268+
269+
### Window::setNavigateCallback
270+
271+
``` cpp
272+
void setNavigateCallback(std::function<void (const std::string &)>);
273+
```
274+
275+
> Sets the navigate-callback to the given callback
276+
277+
-----
278+
279+
### Window::setResizeCallback
280+
281+
``` cpp
282+
void setResizeCallback(std::function<void (std::size_t, std::size_t)>);
283+
```
284+
285+
> Sets the resize-callback to the given callback
49286
287+
-----
50288

51-
> Note: This work was originally based on the work of [MichaelKim](https://github.com/MichaelKim/webview)
289+
> This work was originally based on the work of [MichaelKim](https://github.com/MichaelKim/webview)

embed-helper/main.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ int main(int argc, char **args)
2020
std::ofstream output("embedded/webview_base.hpp");
2121
output << "#pragma once" << std::endl
2222
<< "#include <map>" << std::endl
23-
<< "#include <utility>" << std::endl
2423
<< "#include <string>" << std::endl
25-
<< "inline std::map<const std::string, std::pair<std::size_t, unsigned char*>> embedded_files;";
24+
<< "#include <core/resource.hpp>" << std::endl
25+
<< "namespace Webview {" << std::endl
26+
<< "namespace Embedded {" << std::endl
27+
<< "inline std::map <const std::string, Resource> files;" << std::endl
28+
<< "}" << std::endl
29+
<< "}" << std::endl;
2630
output.close();
2731
std::vector<std::string> filesToInclude;
2832

@@ -43,6 +47,7 @@ int main(int argc, char **args)
4347
std::ofstream fileStream("embedded/" + file.path().filename().string() + ".hpp");
4448
fileStream << "#pragma once" << std::endl
4549
<< "#include \"webview_base.hpp\"" << std::endl
50+
<< "namespace Webview::Embedded {" << std::endl
4651
<< "inline unsigned char embed_file_" << fileFunc << "[] = {";
4752

4853
std::ifstream fileDataStream(file.path(), std::ios::binary);
@@ -58,10 +63,11 @@ int main(int argc, char **args)
5863
}
5964
fileStream << "};";
6065

61-
fileStream << "inline auto webview_embed_file_" << fileFunc << " = []() -> bool { embedded_files.insert({\""
66+
fileStream << "inline auto webview_embed_file_" << fileFunc << " = []() -> bool { files.insert({\""
6267
<< file.path().filename().string() << "\", {" << std::dec << buffer.size() << ","
6368
<< "embed_file_" << fileFunc << "}});"
64-
<< "return true; }();";
69+
<< "return true; }();" << std::endl
70+
<< "}";
6571
fileStream.close();
6672
}
6773

examples/advanced/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
project(webview-example VERSION 0.1)
3+
4+
add_executable(webview-example "main.cpp")
5+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../ ${CMAKE_CURRENT_SOURCE_DIR}/../../example_build EXCLUDE_FROM_ALL)
6+
target_link_libraries(webview-example webview)
7+
8+
target_compile_features(webview-example PUBLIC cxx_std_17)
9+
set_target_properties(webview-example PROPERTIES CMAKE_CXX_STANDARD 17)
10+
set_target_properties(webview-example PROPERTIES CMAKE_CXX_EXTENSIONS Off)
11+
set_target_properties(webview-example PROPERTIES CMAKE_CXX_STANDARD_REQUIRED On)
12+
set_target_properties(webview-example PROPERTIES VERSION ${PROJECT_VERSION})
13+
set_target_properties(webview-example PROPERTIES PROJECT_NAME ${PROJECT_NAME})

0 commit comments

Comments
 (0)