Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ add_library(${PLUGIN_ADDON_NAME} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/QWebEngineView/qwebengineview_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/QWebEngineView/nwebengineview.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/QWebEngineSettings/qwebenginesettings_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/QWebEnginePage/qwebenginepage_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/QWebChannel/qwebchannel_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/QWebChannel/nwebchannel.hpp"
)

AddQtWebSupport(${PLUGIN_ADDON_NAME})
Expand Down
17 changes: 17 additions & 0 deletions src/cpp/QWebChannel/nwebchannel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <nodegui/core/NodeWidget/nodewidget.h>

#include <QtWebChannel/QWebChannel>

#include "nodegui/core/Events/eventwidget.h"
#include "nodegui/core/Events/eventwidget_macro.h"

class DLL_EXPORT NWebChannel : public QWebChannel, public EventWidget {
Q_OBJECT
EVENTWIDGET_IMPLEMENTATIONS(QWebChannel)
public:
using QWebChannel::QWebChannel;

void connectSignalsToEventEmitter() { }
};
74 changes: 74 additions & 0 deletions src/cpp/QWebChannel/qwebchannel_wrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "qwebchannel_wrap.h"

#include <QDebug>
#include <QObject>
#include <QWebChannel>

#include "nodegui/Extras/Utils/nutils.h"
#include "nodegui/QtCore/QObject/qobject_wrap.h"

Napi::FunctionReference QWebChannelWrap::constructor;

Napi::Object QWebChannelWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QWebChannel";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("registerObject", &QWebChannelWrap::registerObject),
InstanceMethod("deregisterObject", &QWebChannelWrap::deregisterObject),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QWebChannelWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}

QWebChannelWrap::QWebChannelWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QWebChannelWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

if (info[0].IsExternal()) {
this->instance = info[0].As<Napi::External<NWebChannel>>().Data();
} else if (info.Length() == 1) {
Napi::Object parentObject = info[0].As<Napi::Object>();
QObjectWrap* parentWrap =
Napi::ObjectWrap<QObjectWrap>::Unwrap(parentObject);
this->instance = new NWebChannel(parentWrap->getInternalInstance());
} else if (info.Length() == 0) {
this->instance = new NWebChannel();
} else {
Napi::TypeError::New(env, "Incorrect initialization of QWebChannelWrap")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}

QWebChannelWrap::~QWebChannelWrap() { extrautils::safeDelete(this->instance); }

NWebChannel* QWebChannelWrap::getInternalInstance() { return this->instance; }

Napi::Value QWebChannelWrap::registerObject(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

Napi::String napiName = info[0].As<Napi::String>();
std::string name = napiName.Utf8Value();

Napi::Object napiObject = info[1].As<Napi::Object>();
QObjectWrap* objectWrap = Napi::ObjectWrap<QObjectWrap>::Unwrap(napiObject);

this->instance->registerObject(QString::fromUtf8(name.c_str()),
objectWrap->getInternalInstance());
return env.Null();
}

Napi::Value QWebChannelWrap::deregisterObject(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

Napi::Object napiObject = info[0].As<Napi::Object>();
QObjectWrap* objectWrap = Napi::ObjectWrap<QObjectWrap>::Unwrap(napiObject);

this->instance->deregisterObject(objectWrap->getInternalInstance());
return env.Null();
}
25 changes: 25 additions & 0 deletions src/cpp/QWebChannel/qwebchannel_wrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <napi.h>
#include <nodegui/Extras/Export/export.h>

#include <QPointer>

#include "QtCore/QObject/qobject_macro.h"
#include "nwebchannel.hpp"

class DLL_EXPORT QWebChannelWrap : public Napi::ObjectWrap<QWebChannelWrap> {
QOBJECT_WRAPPED_METHODS_DECLARATION
private:
QPointer<NWebChannel> instance;

public:
static Napi::FunctionReference constructor;
static Napi::Object init(Napi::Env env, Napi::Object exports);
QWebChannelWrap(const Napi::CallbackInfo& info);
~QWebChannelWrap();
NWebChannel* getInternalInstance();
// Wrapped methods
Napi::Value registerObject(const Napi::CallbackInfo& info);
Napi::Value deregisterObject(const Napi::CallbackInfo& info);
};
69 changes: 69 additions & 0 deletions src/cpp/QWebEnginePage/qwebenginepage_wrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "qwebenginepage_wrap.h"

#include "Extras/Utils/nutils.h"
#include "src/cpp/QWebChannel/qwebchannel_wrap.h"

Napi::FunctionReference QWebEnginePageWrap::constructor;

Napi::Object QWebEnginePageWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QWebEnginePage";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("setWebChannel", &QWebEnginePageWrap::setWebChannel),
InstanceMethod("webChannel", &QWebEnginePageWrap::webChannel),
InstanceMethod("runJavaScript", &QWebEnginePageWrap::runJavaScript),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QWebEnginePageWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}

QWebEnginePageWrap::QWebEnginePageWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QWebEnginePageWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info[0].IsExternal()) {
this->instance = info[0].As<Napi::External<QWebEnginePage>>().Data();
} else {
Napi::TypeError::New(env, "Incorrect initialization of QWebEnginePageWrap")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}

QWebEnginePage* QWebEnginePageWrap::getInternalInstance() {
return this->instance;
}

Napi::Value QWebEnginePageWrap::runJavaScript(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

Napi::String napiScript = info[0].As<Napi::String>();
std::string script = napiScript.Utf8Value();

this->instance->runJavaScript(QString::fromUtf8(script.c_str()));
return env.Null();
}

Napi::Value QWebEnginePageWrap::setWebChannel(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

Napi::Object channel = info[0].As<Napi::Object>();
QWebChannelWrap* channelWrap =
Napi::ObjectWrap<QWebChannelWrap>::Unwrap(channel);

this->instance->setWebChannel(channelWrap->getInternalInstance());
return env.Null();
}

Napi::Value QWebEnginePageWrap::webChannel(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

QWebChannel* webChannel = this->instance->webChannel();
return QWebChannelWrap::constructor.New(
{Napi::External<QWebChannel>::New(env, webChannel)});
}
22 changes: 22 additions & 0 deletions src/cpp/QWebEnginePage/qwebenginepage_wrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <napi.h>
#include <nodegui/core/Component/component_macro.h>
#include <QWebEnginePage>

class QWebEnginePageWrap : public Napi::ObjectWrap<QWebEnginePageWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION

private:
QWebEnginePage* instance;

public:
static Napi::FunctionReference constructor;
static Napi::Object init(Napi::Env env, Napi::Object exports);
QWebEnginePageWrap(const Napi::CallbackInfo& info);
QWebEnginePage* getInternalInstance();
// Wrapped methods
Napi::Value runJavaScript(const Napi::CallbackInfo& info);
Napi::Value setWebChannel(const Napi::CallbackInfo& info);
Napi::Value webChannel(const Napi::CallbackInfo& info);
};
10 changes: 10 additions & 0 deletions src/cpp/QWebEngineView/qwebengineview_wrap.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "qwebengineview_wrap.h"

#include "src/cpp/QWebEngineSettings/qwebenginesettings_wrap.h"
#include "src/cpp/QWebEnginePage/qwebenginepage_wrap.h"
#include <QDebug>
#include <QWidget>
#include "nodegui/Extras/Utils/nutils.h"
Expand All @@ -14,6 +15,7 @@ Napi::Object QWebEngineViewWrap::init(Napi::Env env, Napi::Object exports) {
Napi::Function func =
DefineClass(env, CLASSNAME,
{InstanceMethod("settings", &QWebEngineViewWrap::settings),
InstanceMethod("page", &QWebEngineViewWrap::page),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QWebEngineViewWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
Expand Down Expand Up @@ -56,4 +58,12 @@ Napi::Value QWebEngineViewWrap::settings(const Napi::CallbackInfo& info) {
QWebEngineSettings* settings = this->instance->settings();
return QWebEngineSettingsWrap::constructor.New(
{Napi::External<QWebEngineSettings>::New(env, settings)});
}

Napi::Value QWebEngineViewWrap::page(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QWebEnginePage* page = this->instance->page();
return QWebEnginePageWrap::constructor.New(
{Napi::External<QWebEnginePage>::New(env, page)});
}
1 change: 1 addition & 0 deletions src/cpp/QWebEngineView/qwebengineview_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ class QWebEngineViewWrap : public Napi::ObjectWrap<QWebEngineViewWrap> {
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value settings(const Napi::CallbackInfo& info);
Napi::Value page(const Napi::CallbackInfo& info);
};
4 changes: 4 additions & 0 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include <napi.h>

#include "src/cpp/QWebEngineSettings/qwebenginesettings_wrap.h"
#include "src/cpp/QWebEnginePage/qwebenginepage_wrap.h"
#include "src/cpp/QWebEngineView/qwebengineview_wrap.h"
#include "src/cpp/QWebChannel/qwebchannel_wrap.h"

Napi::Object Main(Napi::Env env, Napi::Object exports) {
QWebEngineViewWrap::init(env, exports);
QWebEngineSettingsWrap::init(env, exports);
QWebEnginePageWrap::init(env, exports);
QWebChannelWrap::init(env, exports);
return exports;
}

Expand Down
12 changes: 10 additions & 2 deletions src/demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QWebEngineView } from "./index";
import { QWebEngineView, QWebChannel } from "./index";
// import {
// QMainWindow,
// QWidget,
Expand All @@ -11,12 +11,20 @@ import { QWebEngineView } from "./index";
const webview = new QWebEngineView();
webview.setInlineStyle("align-self:'stretch';");
webview.load("http://google.com");
webview.addEventListener("urlChanged", url => {
webview.addEventListener("urlChanged", (url) => {
console.log("changed to", url);
});
webview.addEventListener("selectionChanged", () => {
console.log("selection", webview.property("selectedText").toString());
});
webview.addEventListener("loadFinished", () => {
const js = `alert('nodeui');`;
const page = webview.page();
page.runJavaScript(js);
});

const channel = new QWebChannel();
webview.page().setWebChannel(channel);

webview.show();
(global as any).wv = webview;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import "../build/Release/nodegui_plugin_webview_init.node";

export { QWebEngineView, QWebEngineViewSignals } from "./lib/QWebEngineView";
export { QWebChannel } from "./lib/QWebChannel";
export {
QWebEngineSettings,
UnknownUrlSchemePolicy
Expand Down
32 changes: 32 additions & 0 deletions src/lib/QWebChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
QObject,
Component,
NativeElement,
checkIfNativeElement,
} from "@nodegui/nodegui";
import addon from "./utils/addon";

export class QWebChannel extends Component {
native: NativeElement;
constructor(arg?: NativeElement | QObject) {
super();
if (checkIfNativeElement(arg)) {
this.native = arg as NativeElement;
} else if (arg instanceof QObject) {
this.native = new addon.QWebChannel(arg.native);
} else if (!arg) {
this.native = new addon.QWebChannel(new QObject().native);
} else {
throw new Error(
"QWebChannel cannot be initialised this way. Use webengineview.settings()"
);
}
}
registerObject(name: string, obj: QObject): void {
this.native.registerObject(name, obj.native);
}

deregisterObject(obj: QObject): void {
this.native.deregisterObject(obj.native);
}
}
30 changes: 30 additions & 0 deletions src/lib/QWebEnginePage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
Component,
NativeElement,
checkIfNativeElement,
} from "@nodegui/nodegui";
import { QWebChannel } from "./QWebChannel";

export class QWebEnginePage extends Component {
native: NativeElement;
constructor(native: NativeElement) {
super();
if (checkIfNativeElement(native)) {
this.native = native;
} else {
throw new Error(
"QWebEnginePage cannot be initialised this way. Use webengineview.settings()"
);
}
}
runJavaScript(script: string): void {
this.native.runJavaScript(script);
}
setWebChannel(channel: QWebChannel): void {
this.native.setWebChannel(channel.native);
}

webChannel(): QWebChannel {
return new QWebChannel(this.native.webChannel());
}
}
5 changes: 5 additions & 0 deletions src/lib/QWebEngineView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@nodegui/nodegui";
import addon from "./utils/addon";
import { QWebEngineSettings } from "./QWebEngineSettings";
import { QWebEnginePage } from "./QWebEnginePage";

export interface QWebEngineViewSignals extends QWidgetSignals {
loadFinished: (ok: boolean) => void;
Expand Down Expand Up @@ -38,4 +39,8 @@ export class QWebEngineView extends NodeWidget<QWebEngineViewSignals> {
settings(): QWebEngineSettings {
return new QWebEngineSettings(this.native.settings());
}

page(): QWebEnginePage {
return new QWebEnginePage(this.native.page());
}
}