Fill in more screen and DPI support in QApplication

This commit is contained in:
Simon Edwards 2021-10-24 11:19:05 +02:00
parent fa52c674ef
commit 05c690dcd9
4 changed files with 113 additions and 22 deletions

View File

@ -5,6 +5,7 @@
#include "Extras/Export/export.h"
#include "QtCore/QObject/qobject_macro.h"
#include "QtGui/QScreen/qscreen_wrap.h"
#include "napi.h"
class DLL_EXPORT NApplication : public QApplication, public EventWidget {
@ -22,5 +23,33 @@ class DLL_EXPORT NApplication : public QApplication, public EventWidget {
Napi::HandleScope scope(env);
this->emitOnNode.Call({Napi::String::New(env, "focusWindowChanged")});
});
QObject::connect(
this, &QGuiApplication::primaryScreenChanged, [=](QScreen* screen) {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
auto instance = QScreenWrap::constructor.New(
{Napi::External<QScreen>::New(env, screen)});
this->emitOnNode.Call(
{Napi::String::New(env, "primaryScreenChanged"), instance});
});
QObject::connect(this, &QGuiApplication::screenAdded, [=](QScreen* screen) {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
auto instance = QScreenWrap::constructor.New(
{Napi::External<QScreen>::New(env, screen)});
this->emitOnNode.Call({Napi::String::New(env, "screenAdded"), instance});
});
QObject::connect(this, &QGuiApplication::screenRemoved,
[=](QScreen* screen) {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
auto instance = QScreenWrap::constructor.New(
{Napi::External<QScreen>::New(env, screen)});
this->emitOnNode.Call(
{Napi::String::New(env, "screenRemoved"), instance});
});
}
};

View File

@ -30,11 +30,14 @@ class DLL_EXPORT QApplicationWrap : public Napi::ObjectWrap<QApplicationWrap> {
Napi::Value quitOnLastWindowClosed(const Napi::CallbackInfo& info);
Napi::Value palette(const Napi::CallbackInfo& info);
Napi::Value setStyleSheet(const Napi::CallbackInfo& info);
Napi::Value devicePixelRatio(const Napi::CallbackInfo& info);
};
namespace StaticQApplicationWrapMethods {
DLL_EXPORT Napi::Value instance(const Napi::CallbackInfo& info);
DLL_EXPORT Napi::Value clipboard(const Napi::CallbackInfo& info);
DLL_EXPORT Napi::Value instance(const Napi::CallbackInfo& info);
DLL_EXPORT Napi::Value primaryScreen(const Napi::CallbackInfo& info);
DLL_EXPORT Napi::Value screens(const Napi::CallbackInfo& info);
DLL_EXPORT Napi::Value setStyle(const Napi::CallbackInfo& info);
DLL_EXPORT Napi::Value style(const Napi::CallbackInfo& info);
} // namespace StaticQApplicationWrapMethods

View File

@ -23,10 +23,14 @@ Napi::Object QApplicationWrap::init(Napi::Env env, Napi::Object exports) {
&QApplicationWrap::quitOnLastWindowClosed),
InstanceMethod("palette", &QApplicationWrap::palette),
InstanceMethod("setStyleSheet", &QApplicationWrap::setStyleSheet),
InstanceMethod("devicePixelRatio", &QApplicationWrap::devicePixelRatio),
StaticMethod("instance", &StaticQApplicationWrapMethods::instance),
StaticMethod("clipboard", &StaticQApplicationWrapMethods::clipboard),
StaticMethod("setStyle", &StaticQApplicationWrapMethods::setStyle),
StaticMethod("style", &StaticQApplicationWrapMethods::style),
StaticMethod("primaryScreen",
&StaticQApplicationWrapMethods::primaryScreen),
StaticMethod("screens", &StaticQApplicationWrapMethods::screens),
QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QApplicationWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
@ -164,3 +168,40 @@ Napi::Value QApplicationWrap::quitOnLastWindowClosed(
bool quit = this->instance->quitOnLastWindowClosed();
return Napi::Value::From(env, quit);
}
Napi::Value StaticQApplicationWrapMethods::primaryScreen(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto screen = QApplication::primaryScreen();
if (screen) {
return QScreenWrap::constructor.New(
{Napi::External<QScreen>::New(env, screen)});
} else {
return env.Null();
}
}
Napi::Value StaticQApplicationWrapMethods::screens(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto screens = QApplication::screens();
Napi::Array jsArray = Napi::Array::New(env, screens.size());
for (int i = 0; i < screens.size(); i++) {
QScreen* screen = screens[i];
auto instance = QScreenWrap::constructor.New(
{Napi::External<QScreen>::New(env, screen)});
jsArray[i] = instance;
}
return jsArray;
}
Napi::Value QApplicationWrap::devicePixelRatio(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
qreal result = this->instance->devicePixelRatio();
return Napi::Value::From(env, result);
}

View File

@ -7,6 +7,7 @@ import { QObjectSignals, NodeObject } from '../QtCore/QObject';
import { QPalette } from './QPalette';
import { StyleSheet } from '../core/Style/StyleSheet';
import memoizeOne from 'memoize-one';
import { QScreen } from './QScreen';
/**
@ -41,37 +42,51 @@ export class QApplication extends NodeObject<QApplicationSignals> {
this.setStyleSheet = memoizeOne(this.setStyleSheet);
}
static clipboard(): QClipboard {
return new QClipboard(addon.QApplication.clipboard());
devicePixelRatio(): number {
return this.native.devicePixelRatio();
}
exec(): number {
return this.native.exec();
}
exit(exitCode: number): number {
return this.native.exit(exitCode);
}
palette(): QPalette {
return new QPalette(this.native.palette());
}
processEvents(): void {
this.native.processEvents();
}
exec(): number {
return this.native.exec();
quit(): number {
return this.native.quit();
}
quitOnLastWindowClosed(): boolean {
return this.native.quitOnLastWindowClosed();
}
setQuitOnLastWindowClosed(quit: boolean): void {
this.native.setQuitOnLastWindowClosed(quit);
}
setStyleSheet(styleSheet: string): void {
const preparedSheet = StyleSheet.create(styleSheet);
this.native.setStyleSheet(preparedSheet);
}
static clipboard(): QClipboard {
return new QClipboard(addon.QApplication.clipboard());
}
static instance(): QApplication {
const nativeQApp = addon.QApplication.instance();
return new QApplication(nativeQApp);
}
quit(): number {
return this.native.quit();
static primaryScreen(): QScreen | null {
const screenNative = addon.QApplication.primaryScreen();
if (screenNative == null) {
return null;
}
return new QScreen(screenNative);
}
exit(exitCode: number): number {
return this.native.exit(exitCode);
}
setQuitOnLastWindowClosed(quit: boolean): void {
this.native.setQuitOnLastWindowClosed(quit);
}
quitOnLastWindowClosed(): boolean {
return this.native.quitOnLastWindowClosed();
}
palette(): QPalette {
return new QPalette(this.native.palette());
}
setStyleSheet(styleSheet: string): void {
const preparedSheet = StyleSheet.create(styleSheet);
this.native.setStyleSheet(preparedSheet);
static screens(): QScreen[] {
const screenNativeList = addon.QApplication.screens();
return screenNativeList.map((screenNative: any) => new QScreen(screenNative));
}
static setStyle(style: QStyle): void {
addon.QApplication.setStyle(style.native);
@ -83,4 +98,7 @@ export class QApplication extends NodeObject<QApplicationSignals> {
export interface QApplicationSignals extends QObjectSignals {
focusWindowChanged: () => void;
primaryScreenChanged: (screen: QScreen) => void;
screenAdded: (screen: QScreen) => void;
screenRemoved: (screen: QScreen) => void;
}