From 5191892950b6487d4d2e993c9b6170e556ec7505 Mon Sep 17 00:00:00 2001 From: Simon Edwards Date: Thu, 12 May 2022 20:49:17 +0200 Subject: [PATCH] Less caching in `QMainWindow` --- .../QtWidgets/QMainWindow/qmainwindow_wrap.h | 1 + .../QMainWindow/qmainwindow_wrap.cpp | 48 ++++++++++--- src/lib/QtWidgets/QMainWindow.ts | 69 ++++++++++--------- 3 files changed, 75 insertions(+), 43 deletions(-) diff --git a/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h b/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h index 2fbdb6eee..8cf3560c1 100644 --- a/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h @@ -22,6 +22,7 @@ class DLL_EXPORT QMainWindowWrap : public Napi::ObjectWrap { // class constructor static Napi::FunctionReference constructor; // wrapped methods + Napi::Value centralWidget(const Napi::CallbackInfo& info); Napi::Value setCentralWidget(const Napi::CallbackInfo& info); Napi::Value takeCentralWidget(const Napi::CallbackInfo& info); Napi::Value setMenuBar(const Napi::CallbackInfo& info); diff --git a/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp b/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp index a5639273a..a97159ea8 100644 --- a/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp @@ -16,15 +16,15 @@ Napi::Object QMainWindowWrap::init(Napi::Env env, Napi::Object exports) { Napi::Function func = DefineClass( env, CLASSNAME, {InstanceMethod("setCentralWidget", &QMainWindowWrap::setCentralWidget), + InstanceMethod("centralWidget", &QMainWindowWrap::centralWidget), InstanceMethod("takeCentralWidget", &QMainWindowWrap::takeCentralWidget), InstanceMethod("setMenuBar", &QMainWindowWrap::setMenuBar), + InstanceMethod("menuBar", &QMainWindowWrap::menuBar), InstanceMethod("setMenuWidget", &QMainWindowWrap::setMenuWidget), InstanceMethod("center", &QMainWindowWrap::center), InstanceMethod("setStatusBar", &QMainWindowWrap::setStatusBar), InstanceMethod("statusBar", &QMainWindowWrap::statusBar), - QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QMainWindowWrap) - - }); + QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QMainWindowWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); QOBJECT_REGISTER_WRAPPER(QMainWindow, QMainWindowWrap); @@ -67,15 +67,32 @@ Napi::Value QMainWindowWrap::setCentralWidget(const Napi::CallbackInfo& info) { Napi::Object widgetObject = info[0].As(); NodeWidgetWrap* centralWidget = Napi::ObjectWrap::Unwrap(widgetObject); - this->instance->setCentralWidget(centralWidget->getInternalInstance()); + if (centralWidget != nullptr) { + this->instance->setCentralWidget(centralWidget->getInternalInstance()); + } else { + this->instance->setCentralWidget(nullptr); + } return env.Null(); } +Napi::Value QMainWindowWrap::centralWidget(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + QWidget* widget = this->instance->centralWidget(); + if (widget) { + return WrapperCache::instance.getWrapper(env, widget); + } else { + return env.Null(); + } +} + Napi::Value QMainWindowWrap::takeCentralWidget(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); - this->instance->takeCentralWidget(); - // We will not return the value here since we are doing it in js side anyway - return env.Null(); + QWidget* widget = this->instance->takeCentralWidget(); + if (widget) { + return WrapperCache::instance.getWrapper(env, widget); + } else { + return env.Null(); + } } Napi::Value QMainWindowWrap::setMenuBar(const Napi::CallbackInfo& info) { @@ -129,6 +146,19 @@ Napi::Value QMainWindowWrap::setStatusBar(const Napi::CallbackInfo& info) { Napi::Value QMainWindowWrap::statusBar(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); QStatusBar* statusBar = this->instance->statusBar(); - - return QStatusBarWrap::fromQStatusBar(env, statusBar); + if (statusBar) { + return WrapperCache::instance.getWrapper(env, statusBar); + } else { + return env.Null(); + } +} + +Napi::Value QMainWindowWrap::menuBar(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + QMenuBar* menuBar = this->instance->menuBar(); + if (menuBar) { + return WrapperCache::instance.getWrapper(env, menuBar); + } else { + return env.Null(); + } } diff --git a/src/lib/QtWidgets/QMainWindow.ts b/src/lib/QtWidgets/QMainWindow.ts index 8cd3abce1..54a6b2ef5 100644 --- a/src/lib/QtWidgets/QMainWindow.ts +++ b/src/lib/QtWidgets/QMainWindow.ts @@ -4,6 +4,8 @@ import { QLayout } from './QLayout'; import { QMenuBar } from './QMenuBar'; import { QStatusBar } from './QStatusBar'; import { NativeElement } from '../core/Component'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -32,55 +34,58 @@ QMainWindow needs to have a central widget set before other widgets can be added Once a central widget is set you can add children/layout to the central widget. */ export class QMainWindow extends QWidget { - public centralWidget?: QWidget | null; - private _menuBar?: QMenuBar; - private _statusBar?: QStatusBar | null; - // TODO - constructor(parent?: QWidget) { + constructor(arg?: QWidget | NativeElement) { let native: NativeElement; - if (parent) { + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QMainWindow(parent.native); } else { native = new addon.QMainWindow(); } super(native); - - this.setLayout = (parentLayout: QLayout): void => { - if (this.centralWidget) { - this.centralWidget.setLayout(parentLayout); - } else { - this.native.setLayout(parentLayout.native); - } - }; + } + setLayout(parentLayout: QLayout): void { + const centralWidget = this.centralWidget(); + if (centralWidget) { + centralWidget.setLayout(parentLayout); + } else { + super.setLayout(parentLayout); + } } setCentralWidget(widget: QWidget): void { this.native.setCentralWidget(widget.native); - this.centralWidget = widget; - this.centralWidget.setFlexNodeSizeControlled(true); + const centralWidget = this.centralWidget(); + if (centralWidget) { + centralWidget.setFlexNodeSizeControlled(true); + } + } + centralWidget(): QWidget { + return wrapperCache.getWrapper(this.native.centralWidget()) as QWidget; } takeCentralWidget(): QWidget | null { - const centralWidget = this.centralWidget; + const centralWidget = this.centralWidget(); this.centralWidget = null; if (centralWidget) { centralWidget.setFlexNodeSizeControlled(false); - this.native.takeCentralWidget(); - return centralWidget; + return wrapperCache.getWrapper(this.native.takeCentralWidget()) as QWidget; } return null; } setMenuBar(menuBar: QMenuBar): void { this.native.setMenuBar(menuBar.native); - this._menuBar = menuBar; } menuBar(): QMenuBar | undefined { - return this._menuBar; + return wrapperCache.getWrapper(this.native.menuBar()) as QMenuBar; } setMenuWidget(menuWidget: QWidget): void { this.native.setMenuWidget(menuWidget.native); } layout(): QLayout | undefined { - if (this.centralWidget) { - return this.centralWidget.layout(); + const centralWidget = this.centralWidget(); + if (centralWidget) { + return centralWidget.layout(); } return super.layout(); } @@ -94,24 +99,20 @@ export class QMainWindow extends QWidget { * @param statusBar The status bar. */ setStatusBar(statusBar: QStatusBar): void { - this.native.setStatusBar(statusBar.native); - this._statusBar = statusBar; - } - - /** - * Removes the status bar from the main window. - */ - removeStatusBar(): void { - this.native.setStatusBar(null); - this._statusBar = null; + if (statusBar != null) { + this.native.setStatusBar(statusBar.native); + } else { + this.native.setStatusBar(null); + } } /** * Returns the status bar for the main window. */ statusBar(): QStatusBar { - return new QStatusBar(this.native.statusBar()); + return wrapperCache.getWrapper(this.native.statusBar()) as QStatusBar; } } +wrapperCache.registerWrapper('QMainWindowWrap', QMainWindow); export type QMainWindowSignals = QWidgetSignals;