FluentUI/src/FluApp.cpp

95 lines
2.5 KiB
C++
Raw Normal View History

2023-05-22 16:17:51 +08:00
#include "FluApp.h"
2023-02-26 23:47:07 +08:00
#include <QQmlEngine>
#include <QGuiApplication>
#include <QQmlContext>
2023-02-27 18:46:39 +08:00
#include <QQuickItem>
2023-02-27 23:04:52 +08:00
#include <QTimer>
2023-03-30 17:16:57 +08:00
#include <QUuid>
2023-04-27 09:38:57 +08:00
#include <QFontDatabase>
2023-03-02 12:20:16 +08:00
#include <QClipboard>
2023-04-27 17:29:39 +08:00
2023-09-13 15:11:22 +08:00
FluApp::FluApp(QObject *parent):QObject{parent}{
2023-11-01 21:06:56 +08:00
vsync(true);
2023-11-23 19:58:54 +08:00
useSystemAppBar(false);
2023-04-27 17:29:39 +08:00
}
FluApp::~FluApp(){
2023-02-27 18:46:39 +08:00
}
2023-10-17 15:17:05 +08:00
void FluApp::init(QObject *application){
this->_application = application;
2023-12-05 18:16:06 +08:00
QJSEngine * jsEngine = qjsEngine(_application);
std::string jsFunction = R"( (function () { console.log("FluentUI");}) )";
QJSValue function = jsEngine->evaluate(QString::fromStdString(jsFunction));
jsEngine->globalObject().setProperty("__fluentui",function);
2023-02-26 23:47:07 +08:00
}
void FluApp::run(){
2023-02-27 23:04:52 +08:00
navigate(initialRoute());
2023-02-26 23:47:07 +08:00
}
2023-03-13 21:18:51 +08:00
void FluApp::navigate(const QString& route,const QJsonObject& argument,FluRegister* fluRegister){
2023-02-26 23:47:07 +08:00
if(!routes().contains(route)){
2023-06-25 23:17:44 +08:00
qCritical()<<"No route found "<<route;
2023-02-26 23:47:07 +08:00
return;
}
2023-09-27 15:18:10 +08:00
QQmlEngine *engine = qmlEngine(_application);
2023-04-11 23:12:31 +08:00
QQmlComponent component(engine, routes().value(route).toString());
2023-06-18 13:56:30 +08:00
if (component.isError()) {
2023-06-25 23:17:44 +08:00
qCritical() << component.errors();
2023-06-18 13:56:30 +08:00
return;
}
2023-04-11 23:12:31 +08:00
QVariantMap properties;
2023-08-11 22:47:36 +08:00
properties.insert("_route",route);
2023-03-13 21:18:51 +08:00
if(fluRegister){
2023-08-11 22:47:36 +08:00
properties.insert("_pageRegister",QVariant::fromValue(fluRegister));
2023-03-13 21:18:51 +08:00
}
2023-04-11 23:12:31 +08:00
properties.insert("argument",argument);
2023-09-27 15:18:10 +08:00
QQuickWindow *win=nullptr;
for (const auto& pair : _windows.toStdMap()) {
QString r = pair.second->property("_route").toString();
2023-05-10 10:32:37 +08:00
if(r == route){
2023-09-27 15:18:10 +08:00
win = pair.second;
2023-05-10 10:32:37 +08:00
break;
}
2023-05-10 10:32:37 +08:00
}
2023-09-27 15:18:10 +08:00
if(win){
int launchMode = win->property("launchMode").toInt();
2023-05-10 10:32:37 +08:00
if(launchMode == 1){
2023-09-27 15:18:10 +08:00
win->setProperty("argument",argument);
win->show();
win->raise();
win->requestActivate();
2023-05-10 10:32:37 +08:00
return;
}else if(launchMode == 2){
2023-09-27 15:18:10 +08:00
win->close();
}
}
2023-09-27 15:18:10 +08:00
win = qobject_cast<QQuickWindow*>(component.createWithInitialProperties(properties));
2023-04-11 23:12:31 +08:00
if(fluRegister){
2023-09-27 15:18:10 +08:00
fluRegister->to(win);
2023-03-05 23:39:13 +08:00
}
2023-09-27 15:18:10 +08:00
win->setColor(QColor(Qt::transparent));
2023-02-27 18:46:39 +08:00
}
2023-03-02 12:20:16 +08:00
2023-09-27 15:18:10 +08:00
void FluApp::exit(int retCode){
for (const auto& pair : _windows.toStdMap()) {
2023-10-19 22:52:36 +08:00
pair.second->close();
2023-09-27 15:18:10 +08:00
removeWindow(pair.second);
2023-03-02 12:20:16 +08:00
}
2023-09-27 15:18:10 +08:00
qApp->exit(retCode);
2023-03-02 12:20:16 +08:00
}
2023-09-27 15:18:10 +08:00
void FluApp::addWindow(QQuickWindow* window){
_windows.insert(window->winId(),window);
2023-04-19 09:41:08 +08:00
}
2023-06-28 02:28:34 +08:00
2023-09-27 15:18:10 +08:00
void FluApp::removeWindow(QQuickWindow* window){
2023-06-28 13:13:39 +08:00
if(window){
2023-09-27 15:18:10 +08:00
_windows.remove(window->winId());
2023-06-28 13:13:39 +08:00
window->deleteLater();
window = nullptr;
}
2023-06-28 02:28:34 +08:00
}