Make QWindow fire QEvents as events; add "window state" methods

This commit is contained in:
Simon Edwards 2022-02-08 22:19:40 +01:00
parent 2865212937
commit c877a2814e
4 changed files with 44 additions and 4 deletions

View File

@ -9,7 +9,8 @@
#include "QtCore/QObject/qobject_macro.h"
class DLL_EXPORT QWindowWrap : public Napi::ObjectWrap<QWindowWrap>,
public EventWidget {
public EventWidget,
public QObject {
QOBJECT_WRAPPED_METHODS_DECLARATION_WITH_EVENT_SOURCE(this)
// Note: We don't use EVENTWIDGET_IMPLEMENTATIONS() here because this class
// doesn't handle any QEvents.
@ -34,6 +35,7 @@ class DLL_EXPORT QWindowWrap : public Napi::ObjectWrap<QWindowWrap>,
static Napi::Object init(Napi::Env env, Napi::Object exports);
QWindowWrap(const Napi::CallbackInfo& info);
~QWindowWrap();
QWindow* getInternalInstance();
virtual void connectSignalsToEventEmitter();
@ -46,4 +48,9 @@ class DLL_EXPORT QWindowWrap : public Napi::ObjectWrap<QWindowWrap>,
Napi::Value showNormal(const Napi::CallbackInfo& info);
Napi::Value startSystemMove(const Napi::CallbackInfo& info);
Napi::Value startSystemResize(const Napi::CallbackInfo& info);
Napi::Value setWindowState(const Napi::CallbackInfo& info);
Napi::Value windowState(const Napi::CallbackInfo& info);
protected:
bool eventFilter(QObject* watched, QEvent* event) override;
};

View File

@ -18,6 +18,8 @@ Napi::Object QWindowWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("showNormal", &QWindowWrap::showNormal),
InstanceMethod("startSystemMove", &QWindowWrap::startSystemMove),
InstanceMethod("startSystemResize", &QWindowWrap::startSystemResize),
InstanceMethod("setWindowState", &QWindowWrap::setWindowState),
InstanceMethod("windowState", &QWindowWrap::windowState),
QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QWindowWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
@ -38,6 +40,12 @@ QWindowWrap::QWindowWrap(const Napi::CallbackInfo& info)
this->rawData = extrautils::configureQObject(this->getInternalInstance());
}
QWindowWrap::~QWindowWrap() {
if (!this->instance.isNull()) {
this->instance->removeEventFilter(this);
}
}
void QWindowWrap::connectSignalsToEventEmitter() {
QOBJECT_SIGNALS_ON_TARGET(this->instance.data());
@ -50,6 +58,12 @@ void QWindowWrap::connectSignalsToEventEmitter() {
this->emitOnNode.Call(
{Napi::String::New(env, "screenChanged"), instance});
});
this->instance->installEventFilter(this);
}
bool QWindowWrap::eventFilter(QObject* watched, QEvent* event) {
return this->EventWidget::event(event);
}
Napi::Value QWindowWrap::screen(const Napi::CallbackInfo& info) {
@ -98,3 +112,17 @@ Napi::Value QWindowWrap::startSystemResize(const Napi::CallbackInfo& info) {
bool result = this->instance->startSystemResize(static_cast<Qt::Edges>(edge));
return Napi::Boolean::New(env, result);
}
Napi::Value QWindowWrap::setWindowState(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Number state = info[0].As<Napi::Number>();
this->instance->setWindowState(
static_cast<Qt::WindowState>(state.Int32Value()));
return env.Null();
}
Napi::Value QWindowWrap::windowState(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int state = static_cast<int>(this->instance->windowState());
return Napi::Value::From(env, state);
}

View File

@ -3,7 +3,7 @@ import { checkIfNativeElement, registerNativeWrapFunction } from '../utils/helpe
import { NodeObject, QObjectSignals } from '../QtCore/QObject';
import { QScreen } from './QScreen';
import { wrapperCache } from '../core/WrapperCache';
import { Edge } from '../QtEnums';
import { Edge, WindowState } from '../QtEnums';
export class QWindow extends NodeObject<QWindowSignals> {
native: NativeElement;
@ -42,7 +42,9 @@ export class QWindow extends NodeObject<QWindowSignals> {
// TODO: void setX(int arg)
// TODO: void setY(int arg)
// TODO: void show()
setWindowState(state: WindowState): void {
return this.native.setWindowState(state);
}
showFullScreen(): void {
this.native.showFullScreen();
}
@ -61,6 +63,9 @@ export class QWindow extends NodeObject<QWindowSignals> {
startSystemResize(edges: Edge): boolean {
return this.native.startSystemResize(edges);
}
windowState(): WindowState {
return this.native.windowState();
}
}
export interface QWindowSignals extends QObjectSignals {

View File

@ -565,7 +565,7 @@ export abstract class NodeWidget<Signals extends QWidgetSignals> extends YogaWid
windowRole(): string {
return this.native.windowRole();
}
windowState(): number {
windowState(): WindowState {
return this.native.windowState();
}
windowTitle(): string {