FluentUI/src/FluApp.cpp

147 lines
4.0 KiB
C++
Raw Normal View History

2023-04-27 09:38:57 +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-11 23:12:31 +08:00
#include "FluTheme.h"
2023-03-02 12:20:16 +08:00
#include "Def.h"
2023-02-26 23:47:07 +08:00
2023-04-11 23:12:31 +08:00
#ifdef Q_OS_WIN
2023-04-22 19:25:10 +08:00
#pragma comment(lib, "Dwmapi.lib")
#pragma comment(lib, "User32.lib")
2023-04-11 23:12:31 +08:00
#include <dwmapi.h>
#include <Windows.h>
#include <windowsx.h>
static bool isCompositionEnabled()
{
BOOL composition_enabled = FALSE;
bool success = ::DwmIsCompositionEnabled(&composition_enabled) == S_OK;
return composition_enabled && success;
}
#endif
2023-02-26 23:47:07 +08:00
2023-04-27 09:38:57 +08:00
FluApp* FluApp::fluApp = nullptr;
FluTheme* FluApp::fluTheme = nullptr;
FluColors* FluApp::flutColors = nullptr;
2023-02-26 23:47:07 +08:00
2023-04-27 09:38:57 +08:00
void FluApp::setFluApp(FluApp* val){
FluApp::fluApp = val;
}
void FluApp::setFluTheme(FluTheme* val){
FluApp::fluTheme = val;
}
void FluApp::setFluColors(FluColors* val){
FluApp::flutColors = val;
2023-02-26 23:47:07 +08:00
}
2023-02-27 18:46:39 +08:00
FluApp::FluApp(QObject *parent)
: QObject{parent}
{
2023-04-27 09:38:57 +08:00
QFontDatabase::addApplicationFont(":/FluentUI/Font/Segoe_Fluent_Icons.ttf");
2023-02-27 18:46:39 +08:00
}
2023-04-11 23:12:31 +08:00
void FluApp::init(QQuickWindow *window){
2023-03-05 23:39:13 +08:00
this->appWindow = window;
2023-04-27 09:38:57 +08:00
QQmlEngine *engine = qmlEngine(appWindow);
QQmlComponent component(engine, ":/FluentUI/Controls/FluSingleton.qml");
component.create();
nativeEvent = new NativeEventFilter();
qApp->installNativeEventFilter(nativeEvent);
2023-02-26 23:47:07 +08:00
}
void FluApp::run(){
2023-04-11 23:12:31 +08:00
#ifdef Q_OS_WIN
if(!isCompositionEnabled()){
2023-04-27 09:38:57 +08:00
fluTheme->frameless(false);
}
2023-04-11 23:12:31 +08:00
#endif
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)){
qErrnoWarning("没有找到当前路由");
return;
}
2023-04-11 23:12:31 +08:00
QQmlEngine *engine = qmlEngine(appWindow);
QQmlComponent component(engine, routes().value(route).toString());
QVariantMap properties;
properties.insert("route",route);
2023-03-13 21:18:51 +08:00
if(fluRegister){
2023-04-11 23:12:31 +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);
QQuickWindow *view = qobject_cast<QQuickWindow*>(component.createWithInitialProperties(properties));
int launchMode = view->property("launchMode").toInt();
if(launchMode==1){
for (auto& pair : wnds) {
QString r = pair->property("route").toString();
if(r == route){
2023-04-25 23:58:25 +08:00
pair->setProperty("argument",argument);
pair->raise();
pair->requestActivate();
2023-04-14 17:07:54 +08:00
view->deleteLater();
return;
}
}
}else if(launchMode==2){
for (auto& pair : wnds) {
QString r = pair->property("route").toString();
if(r == route){
pair->close();
break;
}
}
}
2023-04-27 09:38:57 +08:00
if(fluTheme->frameless()){
2023-04-12 00:15:38 +08:00
view->setFlag(Qt::FramelessWindowHint,true);
}
2023-04-11 23:12:31 +08:00
wnds.insert(view->winId(),view);
if(fluRegister){
fluRegister->to(view);
2023-03-05 23:39:13 +08:00
}
2023-03-02 23:58:50 +08:00
view->setColor(QColor(Qt::transparent));
2023-04-11 23:12:31 +08:00
view->show();
2023-02-27 18:46:39 +08:00
}
2023-03-02 12:20:16 +08:00
2023-03-02 18:21:43 +08:00
QJsonArray FluApp::awesomelist(const QString& keyword)
2023-03-02 12:20:16 +08:00
{
QJsonArray arr;
QMetaEnum enumType = Fluent_Awesome::staticMetaObject.enumerator(Fluent_Awesome::staticMetaObject.indexOfEnumerator("Fluent_AwesomeType"));
for(int i=0; i < enumType.keyCount(); ++i){
QString name = enumType.key(i);
int icon = enumType.value(i);
2023-03-02 18:21:43 +08:00
if(keyword.isEmpty()){
QJsonObject obj;
obj.insert("name",name);
obj.insert("icon",icon);
arr.append(obj);
}else{
2023-03-11 00:29:06 +08:00
if(name.contains(keyword)){
2023-03-02 18:21:43 +08:00
QJsonObject obj;
obj.insert("name",name);
obj.insert("icon",icon);
arr.append(obj);
}
}
2023-03-02 12:20:16 +08:00
}
return arr;
}
void FluApp::clipText(const QString& text){
QGuiApplication::clipboard()->setText(text);
}
2023-03-30 17:16:57 +08:00
QString FluApp::uuid(){
return QUuid::createUuid().toString();
}
2023-04-19 09:41:08 +08:00
void FluApp::closeApp(){
qApp->exit(0);
}