Skip to content

Commit

Permalink
changed signature, adapted examples, updated tests and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomq committed Nov 6, 2021
1 parent 0940031 commit 4fd8be6
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 145 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def echoAndModify(value): # One input value in the function signature is require
print ("From front-end: " + value)
return (value + " appended")
nimview.addRequest("echoAndModify", echoAndModify)
nimview.add("echoAndModify", echoAndModify)
nimview.start("minimal_ui_sample/index.html")
```

Expand All @@ -103,7 +103,7 @@ The same in Nim:
## Minimal Nim example
```
import nimview
nimview.addRequest("echoAndModify", proc (value: string): string =
nimview.add("echoAndModify", proc (value: string): string =
echo "From front-end: " & value
result = "'" & value & "' modified by back-end")
nimview.start()
Expand Down Expand Up @@ -154,10 +154,10 @@ If you need Json, you need to parse this value in javascript manually, for examp
In case you want to use C++ - don't write your own C++ Json parser. Feel free to use https://github.com/nlohmann/json. You might re-use it in other code locations.

It is also possible to call front-end functions directly from back-end by using
`callFrontendJs`. You may trigger an `alert("Hello World!")` on the frontend by
`callJs`. You may trigger an `alert("Hello World!")` on the frontend by
using following Nim code:
```
nimview.callFrontendJs("alert", "Hello World!")
nimview.callJs("alert", "Hello World!")
```
in your back-end code. You may use as many parameters as you want.
This works directy for functions in the global js "window" namespace. If you need
Expand Down
2 changes: 1 addition & 1 deletion examples/android/app/src/main/nim/src/App.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ proc countDown() =
callFrontendJs("alert", "1")
sleep(1000)

addRequest("countDown", countDown)
add("countDown", countDown)

when not defined(just_core):
start()
2 changes: 1 addition & 1 deletion examples/c_cpp/src/c_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ char* echoAndModify(char* something) {
int main(int argc, char* argv[]) {
printf(" starting c code\n");
NimMain();
nimview_addRequest_cstring_rstr("echoAndModify", echoAndModify, free);
nimview_add_cstring_rstr("echoAndModify", echoAndModify, free);
#ifdef _DEBUG
nimview_startHttpServer("../dist/index.html", 8000, "localhost", 1);
#else
Expand Down
2 changes: 1 addition & 1 deletion examples/c_cpp/src/cpp_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ std::string echoAndModify(const std::string& something) {

int main(int argc, char* argv[]) {
nimview::nimMain();
nimview::addRequest<std::string>("echoAndModify", echoAndModify);
nimview::add<std::string>("echoAndModify", echoAndModify);
nimview::start("../dist/index.html", 8000, "localhost");
}
2 changes: 1 addition & 1 deletion examples/minimal/App.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ proc echoAndModify(value: string): string =
result = "'" & value & "' modified by minimal"

when isMainModule:
addRequest("echoAndModify", echoAndModify)
add("echoAndModify", echoAndModify)
start("dist/index.html")
4 changes: 2 additions & 2 deletions examples/minimal2/App.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ proc echoAndModify(value: string): string =
result = "'" & value & "' modified by minimal"

when isMainModule:
addRequest("callJsProgress", callJsProgress)
addRequest("echoAndModify", echoAndModify)
add("callJsProgress", callJsProgress)
add("echoAndModify", echoAndModify)
startDesktop("dist/index.html", debug=true)
2 changes: 1 addition & 1 deletion examples/python/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = nimview
version = 0.3.3
version = 0.4.0
author = Marco Mengelkoch
author_email = MMengelkoch@gmx.de
description = A lightwight cross platform UI library for Nim, C, C++ or Python. The main purpose is to simplify creation of Desktop applications based on a HTML/CSS/JS layer that is displayed with Webview.
Expand Down
10 changes: 5 additions & 5 deletions examples/svelte/src/App.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ proc appendSomething(value: string): string {.noSideEffect.} =
result = "'" & value & "' modified by svelte sample"

proc countDown() =
callFrontendJs("alert", "waiting 6 seconds")
callJs("alert", "waiting 6 seconds")
sleep(2000)
callFrontendJs("alert", "4")
callJs("alert", "4")
sleep(3000)
callFrontendJs("alert", "1")
callJs("alert", "1")
sleep(1000)

proc main() =
addRequest("appendSomething", appendSomething)
addRequest("countDown", countDown)
add("appendSomething", appendSomething)
add("countDown", countDown)
start()
## alternative fullscreen mode:
# start(run=false)
Expand Down
4 changes: 2 additions & 2 deletions examples/vue/src/App.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ proc countDown() =
sleep(1000)

proc main() =
addRequest("appendSomething", appendSomething)
addRequest("countDown", countDown)
add("appendSomething", appendSomething)
add("countDown", countDown)
let argv = os.commandLineParams()
for arg in argv:
readAndParseJsonCmdFile(arg)
Expand Down
4 changes: 3 additions & 1 deletion nimview.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "0.3.3"
version = "0.4.0"
author = "Marco Mengelkoch"
description = "Nim / Python / C library to run webview with HTML/JS as UI"
license = "MIT"
Expand Down Expand Up @@ -66,8 +66,10 @@ task test, "Run tests":
let baseDir = thisDir()
cd baseDir / "examples/c_cpp"
let nake = system.findExe("nake")
exec nake & " clean"
exec nake & " test"
cd baseDir / "examples/python"
exec nake & " clean"
exec nake & " test"
cd baseDir
exec "testament pattern \"tests/*.nim\""
Expand Down
62 changes: 39 additions & 23 deletions src/nimview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ namespace nimview {
}

template<typename T1, typename T2, typename T3, typename T4>
void addRequest(const std::string &request, const std::function<std::string(T1, T2, T3, T4)> &callback) {
void addt(const std::string &request, const std::function<std::string(T1, T2, T3, T4)> &callback) {
requestFunction lambda = [&, callback](int argc, char** argv) {
if (argc <= 4) {
throw std::runtime_error("Less than 4 arguments");
Expand All @@ -133,12 +133,12 @@ namespace nimview {
};
requestMap.insert(std::make_pair(request, lambda));
std::string signature = typeName<T1>() + ", " + typeName<T2>() + ", " + typeName<T3>() + ", " + typeName<T4>();
nimview_addRequest_argc_argv_rstr(const_cast<char*>(request.c_str()),
nimview_add_argc_argv_rstr(const_cast<char*>(request.c_str()),
findAndCall, free, const_cast<char*>(signature.c_str()));
}

template<typename T1, typename T2, typename T3>
void addRequest(const std::string &request, const std::function<std::string(T1, T2, T3)> &callback) {
void add(const std::string &request, const std::function<std::string(T1, T2, T3)> &callback) {
requestFunction lambda = [&, callback](int argc, char** argv) {
if (argc <= 3) {
throw std::runtime_error("Less than 3 arguments");
Expand All @@ -148,12 +148,12 @@ namespace nimview {
};
requestMap.insert(std::make_pair(request, lambda));
std::string signature = typeName<T1>() + ", " + typeName<T2>() + ", " + typeName<T3>();
nimview_addRequest_argc_argv_rstr(const_cast<char*>(request.c_str()),
nimview_add_argc_argv_rstr(const_cast<char*>(request.c_str()),
findAndCall, free, const_cast<char*>(signature.c_str()));
}

template<typename T1, typename T2>
void addRequest(const std::string &request, const std::function<std::string(T1, T2)> &callback) {
void add(const std::string &request, const std::function<std::string(T1, T2)> &callback) {
requestFunction lambda = [&, callback](int argc, char** argv) {
if (argc <= 2) {
throw std::runtime_error("Less than 2 arguments");
Expand All @@ -163,12 +163,12 @@ namespace nimview {
};
requestMap.insert(std::make_pair(request, lambda));
std::string signature = typeName<T1>() + ", " + typeName<T2>();
nimview_addRequest_argc_argv_rstr(const_cast<char*>(request.c_str()),
nimview_add_argc_argv_rstr(const_cast<char*>(request.c_str()),
findAndCall, free, const_cast<char*>(signature.c_str()));
}

template<typename T1>
void addRequest(const std::string &request, const std::function<std::string(T1)> &callback) {
void add(const std::string &request, const std::function<std::string(T1)> &callback) {
requestFunction lambda = [&, callback](int argc, char** argv) {
if (argc <= 1) {
throw std::runtime_error("Less than 1 argument");
Expand All @@ -178,16 +178,16 @@ namespace nimview {
};
requestMap.insert(std::make_pair(request, lambda));
std::string signature = typeName<T1>();
nimview_addRequest_argc_argv_rstr(const_cast<char*>(request.c_str()),
nimview_add_argc_argv_rstr(const_cast<char*>(request.c_str()),
findAndCall, free, const_cast<char*>(signature.c_str()));
}

void addRequest(const std::string &request, const std::function<std::string(void)> &callback) {
void add(const std::string &request, const std::function<std::string(void)> &callback) {
requestFunction lambda = [&, callback](int argc, char** argv) {
return strToNewCharPtr(callback());
};
requestMap.insert(std::make_pair(request, lambda));
nimview_addRequest_argc_argv_rstr(const_cast<char*>(request.c_str()), findAndCall, free, "void");
nimview_add_argc_argv_rstr(const_cast<char*>(request.c_str()), findAndCall, free, "void");
}

#ifndef JUST_CORE
Expand All @@ -199,7 +199,7 @@ namespace nimview {
nimMain();
::nimview_startHttpServer(const_cast<char*>(folder), port, const_cast<char*>(bindAddr), run);
};
void start(const char* folder, int port = 8000, const char* bindAddr = "localhost", const char* title = "nimview", int width = 640, int height = 480, bool resizable = true, bool run = true) {
void start(const char* folder, int port = 8000, const char* bindAddr = "localhost", const char* title = "nimview", int width = 640, int height = 480, bool resizable = true, bool debug = false, bool run = true) {
nimMain();
#ifdef _WIN32
bool runWithGui = true;
Expand All @@ -210,14 +210,18 @@ namespace nimview {
runWithGui = false;
#endif
if (runWithGui) {
nimview::startDesktop(folder, title, width, height, resizable, false, run);
nimview::startDesktop(folder, title, width, height, resizable, debug, run);
}
else {
nimview::startHttpServer(folder, port, bindAddr, run);
}
}
void init(const char* folder, int port = 8000, const char* bindAddr = "localhost", const char* title = "nimview", int width = 640, int height = 480, bool resizable = true, bool debug = false) {
start(folder, port, bindAddr, title, width, height, resizable, debug);
}
auto stopHttpServer = ::nimview_stopHttpServer;
auto stopDesktop = ::nimview_stopDesktop;
auto stop = ::nimview_stop;
#endif
char* dispatchRequest(char* request, char* value) {
nimMain();
Expand All @@ -233,17 +237,29 @@ namespace nimview {
void enableStorage(const std::string &fileName = "storage.js") {
::nimview_enableStorage(const_cast<char*>(fileName.c_str()));
}
void callFrontendJs(const std::string &functionName, const std::string &args) {
::nimview_callFrontendJs(const_cast<char*>(functionName.c_str()), const_cast<char*>(args.c_str()));
void callJs(const std::string &functionName, const std::string &args) {
::nimview_callJs(const_cast<char*>(functionName.c_str()), const_cast<char*>(args.c_str()));
}
auto callFrontendJs = callJs;
auto setCustomJsEval = ::nimview_setCustomJsEval;
auto addRequest_void = ::nimview_addRequest;
auto addRequest_rstr = ::nimview_addRequest_rstr;
auto addRequest_cstring = ::nimview_addRequest_cstring;
auto addRequest_cstring_rstr = ::nimview_addRequest_cstring_rstr;
auto addRequest_clonglong = ::nimview_addRequest_clonglong;
auto addRequest_clonglong_rstr = ::nimview_addRequest_clonglong_rstr;
auto addRequest_cdouble = ::nimview_addRequest_cdouble;
auto addRequest_cdouble_rstr = ::nimview_addRequest_cdouble_rstr;
auto addRequest_argc_argv_rstr = ::nimview_addRequest_argc_argv_rstr;
auto add_void = ::nimview_add;
auto add_rstr = ::nimview_add_rstr;
auto add_cstring = ::nimview_add_cstring;
auto add_cstring_rstr = ::nimview_add_cstring_rstr;
auto add_clonglong = ::nimview_add_clonglong;
auto add_clonglong_rstr = ::nimview_add_clonglong_rstr;
auto add_cdouble = ::nimview_add_cdouble;
auto add_cdouble_rstr = ::nimview_add_cdouble_rstr;
auto add_argc_argv_rstr = ::nimview_add_argc_argv_rstr;
// deprecated start
auto addRequest_void = ::nimview_add;
auto addRequest_rstr = ::nimview_add_rstr;
auto addRequest_cstring = ::nimview_add_cstring;
auto addRequest_cstring_rstr = ::nimview_add_cstring_rstr;
auto addRequest_clonglong = ::nimview_add_clonglong;
auto addRequest_clonglong_rstr = ::nimview_add_clonglong_rstr;
auto addRequest_cdouble = ::nimview_add_cdouble;
auto addRequest_cdouble_rstr = ::nimview_add_cdouble_rstr;
auto addRequest_argc_argv_rstr = ::nimview_add_argc_argv_rstr;
// deprecated end
}
Loading

0 comments on commit 4fd8be6

Please sign in to comment.