add alert

This commit is contained in:
王劲鹏 2021-04-22 11:43:48 +08:00 committed by osborn
parent 7bcb711dfe
commit 70cf4acd5b
11 changed files with 166 additions and 0 deletions

View File

@ -5,6 +5,7 @@
#include "DoricDemoBridge.h"
#include "DoricPanel.h"
#include "utils/DoricMouseAreaBridge.h"
#include "utils/DoricDialogOnAcceptedBridge.h"
#include "utils/DoricUtils.h"
DoricDemoBridge::DoricDemoBridge(QObject *parent) : QObject(parent) {}
@ -69,4 +70,6 @@ void DoricDemoBridge::navigate(QVariant route) {
auto context = view->engine()->rootContext();
DoricMouseAreaBridge *mouseAreaBridge = new DoricMouseAreaBridge();
context->setContextProperty("mouseAreaBridge", mouseAreaBridge);
DoricDialogOnAcceptedBridge *dialogOnAcceptedBridge = new DoricDialogOnAcceptedBridge();
context->setContextProperty("dialogOnAcceptedBridge", dialogOnAcceptedBridge);
}

View File

@ -47,6 +47,7 @@ SOURCES += \
shader/DoricViewNode.cpp \
utils/DoricConstant.cpp \
utils/DoricContextHolder.cpp \
utils/DoricDialogOnAcceptedBridge.cpp \
utils/DoricLayouts.cpp \
utils/DoricMouseAreaBridge.cpp \
widget/flex/FlexLayout.cpp \
@ -97,6 +98,7 @@ HEADERS += \
engine/DoricNativeJSE.h \
engine/DoricNativeLog.h \
engine/DoricNativeRequire.h \
engine/DoricPromise.h \
engine/DoricTimerExtension.h \
engine/native/NativeExecutor.h \
engine/v8/JSValueHelper.h \
@ -117,6 +119,7 @@ HEADERS += \
utils/DoricConstant.h \
utils/DoricContextHolder.h \
utils/DoricCountDownLatch.h \
utils/DoricDialogOnAcceptedBridge.h \
utils/DoricLayouts.h \
utils/DoricMouseAreaBridge.h \
utils/DoricObjectFactory.h \

View File

@ -0,0 +1,20 @@
#ifndef DORICPROMISE_H
#define DORICPROMISE_H
#include <QDebug>
#include "DoricContext.h"
#include "utils/DoricConstant.h"
class DoricPromise {
public:
static void resolve(DoricContext *context, QString callbackId) {
QVariantList params;
params.append(context->getContextId());
params.append(callbackId);
context->getDriver()->invokeDoricMethod(DoricConstant::DORIC_BRIDGE_RESOLVE,
params);
}
};
#endif // DORICPROMISE_H

View File

@ -1,4 +1,5 @@
#include "DoricModalPlugin.h"
#include "engine/DoricPromise.h"
#include "shader/DoricRootNode.h"
#include "utils/DoricLayouts.h"
@ -70,3 +71,56 @@ void DoricModalPlugin::toast(QString jsValueString, QString callbackId) {
},
DoricThreadMode::UI);
}
void DoricModalPlugin::alert(QString jsValueString, QString callbackId) {
getContext()->getDriver()->asyncCall(
[this, jsValueString, callbackId] {
QJsonDocument document =
QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
QJsonValue titleVal = jsValue["title"];
QJsonValue msgVal = jsValue["msg"];
QJsonValue okBtn = jsValue["okLabel"];
QQmlComponent component(getContext()->getQmlEngine());
const QUrl url(QStringLiteral("qrc:/doric/qml/alert.qml"));
component.loadUrl(url);
if (component.isError()) {
qCritical() << component.errorString();
}
QQuickWindow *window = qobject_cast<QQuickWindow *>(component.create());
window->setProperty("pointer", QString::number((qint64)window));
window->setProperty("plugin", QString::number((qint64)this));
window->setProperty("callbackId", callbackId);
QQuickWindow *parentWindow =
getContext()->getRootNode()->getRootView()->window();
std::function setX = [window, parentWindow]() {
window->setProperty("x",
(parentWindow->width() - window->width()) / 2.f +
parentWindow->x());
};
std::function setY = [window, parentWindow]() {
window->setProperty("y",
(parentWindow->height() - window->height()) / 2 +
parentWindow->y());
};
// init set x
setX();
// init set y
setY();
// update x
connect(window, &QQuickWindow::widthChanged, setX);
// update y
connect(window, &QQuickWindow::heightChanged, setY);
},
DoricThreadMode::UI);
}
void DoricModalPlugin::onAccept(QString callbackId) {
DoricPromise::resolve(getContext(), callbackId);
}

View File

@ -9,6 +9,10 @@ public:
using DoricNativePlugin::DoricNativePlugin;
Q_INVOKABLE void toast(QString jsValueString, QString callbackId);
Q_INVOKABLE void alert(QString jsValueString, QString callbackId);
void onAccept(QString callbackId);
};
#endif // DORICMODALPLUGIN_H

View File

@ -26,6 +26,7 @@
<file alias="scroller.qml">resources/scroller.qml</file>
<file alias="toast.qml">resources/toast.qml</file>
<file alias="alert.qml">resources/alert.qml</file>
<file alias="util.mjs">resources/util.mjs</file>
<file alias="gravity.mjs">resources/gravity.mjs</file>

View File

@ -0,0 +1,38 @@
import QtQuick 2.12
import QtQuick.Controls 2.12
Window {
id: window
flags: flags | Qt.WindowStaysOnTopHint | Qt.Tool | Qt.FramelessWindowHint
visible: true
modality: Qt.ApplicationModal
property var pointer
property var plugin
property var callbackId
Dialog {
id: dialog
title: "Title"
standardButtons: Dialog.Ok
modal: true
onAccepted: {
dialogOnAcceptedBridge.onClick(pointer, plugin, callbackId)
}
onWidthChanged: {
window.width = implicitWidth
}
onHeightChanged: {
window.height = implicitHeight
}
}
Component.onCompleted: {
dialog.open()
dialog.standardButton(Dialog.Ok).text = qsTrId("OkLabel")
}
}

View File

@ -28,6 +28,8 @@ const QString DoricConstant::TEMPLATE_CONTEXT_DESTROY =
const QString DoricConstant::GLOBAL_DORIC = "doric";
const QString DoricConstant::DORIC_CONTEXT_INVOKE = "jsCallEntityMethod";
const QString DoricConstant::DORIC_TIMER_CALLBACK = "jsCallbackTimer";
const QString DoricConstant::DORIC_BRIDGE_RESOLVE = "jsCallResolve";
const QString DoricConstant::DORIC_BRIDGE_REJECT = "jsCallReject";
const QString DoricConstant::DORIC_ENTITY_RESPONSE = "__response__";
const QString DoricConstant::DORIC_ENTITY_INIT = "__init__";

View File

@ -25,6 +25,8 @@ public:
static const QString GLOBAL_DORIC;
static const QString DORIC_CONTEXT_INVOKE;
static const QString DORIC_TIMER_CALLBACK;
static const QString DORIC_BRIDGE_RESOLVE;
static const QString DORIC_BRIDGE_REJECT;
static const QString DORIC_ENTITY_RESPONSE;
static const QString DORIC_ENTITY_CREATE;

View File

@ -0,0 +1,23 @@
#include "DoricDialogOnAcceptedBridge.h"
#include "plugin/DoricModalPlugin.h"
#include <QQuickWindow>
DoricDialogOnAcceptedBridge::DoricDialogOnAcceptedBridge(QObject *parent)
: QObject(parent) {}
void DoricDialogOnAcceptedBridge::onClick(QString windowPointer,
QString pluginPointer,
QString callbackId) {
{
QObject *object = (QObject *)(windowPointer.toULongLong());
QQuickWindow *window = dynamic_cast<QQuickWindow *>(object);
window->deleteLater();
}
{
QObject *object = (QObject *)(pluginPointer.toULongLong());
DoricModalPlugin *modalPlugin = dynamic_cast<DoricModalPlugin *>(object);
modalPlugin->onAccept(callbackId);
}
}

View File

@ -0,0 +1,16 @@
#ifndef DORICDIALOGONACCEPTEDBRIDGE_H
#define DORICDIALOGONACCEPTEDBRIDGE_H
#include <QObject>
class DoricDialogOnAcceptedBridge : public QObject {
Q_OBJECT
public:
explicit DoricDialogOnAcceptedBridge(QObject *parent = nullptr);
Q_INVOKABLE
void onClick(QString windowPointer, QString pluginPointer,
QString callbackId);
};
#endif // DORICDIALOGONACCEPTEDBRIDGE_H