diff --git a/qml/MenuMainWindow.qml b/qml/MenuMainWindow.qml new file mode 100644 index 0000000..c9b2ec6 --- /dev/null +++ b/qml/MenuMainWindow.qml @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2023, KylinSoft Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +import QtQuick 2.12 +import org.ukui.menu.core 1.0 + +import AppUI 1.0 as AppUI + +MenuMainWindow { + id: mainWindow + visible: true + + Component.onCompleted: { + console.log("MenuMainWindow Completed."); + } + + onIsFullScreenChanged: { + console.log("full screen", isFullScreen) + //loader.source = isFullScreen ? "qrc:/qml/FullScreenUI.qml" : "qrc:/qml/NormalUI.qml"; + } + + AppUI.NormalUI { + parent: mainWindow.contentItem + anchors.fill: parent + } +} diff --git a/qml/main.qml b/qml/main.qml index d383f54..f65dcd7 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, KylinSoft Co., Ltd. + * Copyright (C) 2023, KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,25 +17,11 @@ */ import QtQuick 2.12 -import org.ukui.menu.core 1.0 import AppUI 1.0 as AppUI -MenuMainWindow { - id: mainWindow - visible: true - - Component.onCompleted: { - console.log("MenuMainWindow Completed."); - } - - onIsFullScreenChanged: { - console.log("full screen", isFullScreen) - //loader.source = isFullScreen ? "qrc:/qml/FullScreenUI.qml" : "qrc:/qml/NormalUI.qml"; - } - +Item { AppUI.NormalUI { - parent: mainWindow.contentItem anchors.fill: parent } } diff --git a/qml/qml.qrc b/qml/qml.qrc index 51b51d4..4ae3cd6 100644 --- a/qml/qml.qrc +++ b/qml/qml.qrc @@ -1,6 +1,7 @@ main.qml + MenuMainWindow.qml org/ukui/menu/extension/qmldir org/ukui/menu/extension/UkuiMenuExtension.qml AppUI/qmldir diff --git a/src/ukui-menu-application.cpp b/src/ukui-menu-application.cpp index fdcd789..f1cba61 100644 --- a/src/ukui-menu-application.cpp +++ b/src/ukui-menu-application.cpp @@ -44,6 +44,7 @@ UkuiMenuApplication::UkuiMenuApplication(MenuMessageProcessor *processor) : QObj void UkuiMenuApplication::startUkuiMenu() { initQmlEngine(); + loadMenuUI(); } void UkuiMenuApplication::registerQmlTypes() @@ -65,28 +66,36 @@ void UkuiMenuApplication::registerQmlTypes() } void UkuiMenuApplication::initQmlEngine() +{ + m_engine = new QQmlEngine(this); + m_engine->addImportPath("qrc:/qml"); + m_engine->addImageProvider("appicon", new AppIconProvider()); + + QQmlContext *context = m_engine->rootContext(); + context->setContextProperty("colorHelper", ColorHelper::instance()); + context->setContextProperty("themePalette", ThemePalette::getInstance()); + context->setContextProperty("menuSetting", MenuSetting::instance()); + context->setContextProperty("modelManager", new ModelManager(this)); + context->setContextProperty("extensionManager", MenuExtension::instance()); + + // MenuMainWindow +// const QUrl url(QStringLiteral("qrc:/qml/MenuMainWindow.qml")); +// QQmlApplicationEngine *m_applicationEngine{nullptr}; +// m_applicationEngine = new QQmlApplicationEngine(this); +// QObject::connect(m_applicationEngine, &QQmlApplicationEngine::objectCreated, +// this, [url](QObject *obj, const QUrl &objUrl) { +// if (!obj && url == objUrl) +// QCoreApplication::exit(-1); +// }, Qt::QueuedConnection); +// +// m_applicationEngine->load(url); +} + +void UkuiMenuApplication::loadMenuUI() { const QUrl url(QStringLiteral("qrc:/qml/main.qml")); - m_applicationEngine = new QQmlApplicationEngine(this); - - m_applicationEngine->addImportPath("qrc:/qml"); - - // icon provider - m_applicationEngine->addImageProvider("appicon", new AppIconProvider()); - - m_applicationEngine->rootContext()->setContextProperty("colorHelper", ColorHelper::instance()); - m_applicationEngine->rootContext()->setContextProperty("themePalette", ThemePalette::getInstance()); - m_applicationEngine->rootContext()->setContextProperty("menuSetting", MenuSetting::instance()); - m_applicationEngine->rootContext()->setContextProperty("modelManager", new ModelManager(this)); - m_applicationEngine->rootContext()->setContextProperty("extensionManager", MenuExtension::instance()); - - QObject::connect(m_applicationEngine, &QQmlApplicationEngine::objectCreated, - this, [url](QObject *obj, const QUrl &objUrl) { - if (!obj && url == objUrl) - QCoreApplication::exit(-1); - }, Qt::QueuedConnection); - - m_applicationEngine->load(url); + m_mainWindow = new MenuWindow(m_engine, nullptr); + m_mainWindow->setSource(url); } void UkuiMenuApplication::initDbusService() @@ -105,22 +114,38 @@ void UkuiMenuApplication::initDbusService() void UkuiMenuApplication::execCommand(Command command) { switch (command) { - case Active: - qDebug() << "=Active=>>" << Active; + case Active: { + if (m_mainWindow) { + m_mainWindow->setVisible(!m_mainWindow->isVisible()); + } break; - case Show: - qDebug() << "=Show=>>" << Show; + } + case Show: { + if (m_mainWindow) { + m_mainWindow->setVisible(true); + } break; - case Quit: - qDebug() << "=Quit=>>" << Quit; - m_applicationEngine->quit(); + } + case Quit: { + if (m_mainWindow) { + m_mainWindow->setVisible(false); + m_engine->quit(); + } QCoreApplication::quit(); break; + } default: break; } } +UkuiMenuApplication::~UkuiMenuApplication() +{ + if (m_mainWindow) { + m_mainWindow->deleteLater(); + } +} + // == MenuMessageProcessor == // MenuMessageProcessor::MenuMessageProcessor() : QObject(nullptr) {} diff --git a/src/ukui-menu-application.h b/src/ukui-menu-application.h index ed037a5..d20c6fe 100644 --- a/src/ukui-menu-application.h +++ b/src/ukui-menu-application.h @@ -20,11 +20,12 @@ #define UKUI_MENU_UKUI_MENU_APPLICATION_H #include "menu-dbus-service.h" -#include +#include namespace UkuiMenu { class MenuMessageProcessor; +class MenuWindow; class UkuiMenuApplication : public QObject { @@ -36,6 +37,7 @@ public: Quit }; explicit UkuiMenuApplication(MenuMessageProcessor *processor); + ~UkuiMenuApplication() override; UkuiMenuApplication() = delete; UkuiMenuApplication(const UkuiMenuApplication& obj) = delete; UkuiMenuApplication(const UkuiMenuApplication&& obj) = delete; @@ -47,13 +49,15 @@ private: static void registerQmlTypes(); void startUkuiMenu(); void initQmlEngine(); + void loadMenuUI(); //注册dubs void initDbusService(); private: - QQmlApplicationEngine *m_applicationEngine{nullptr}; - MenuDbusService *m_menuDbusService = nullptr; + QQmlEngine *m_engine{nullptr}; + MenuWindow *m_mainWindow{nullptr}; + MenuDbusService *m_menuDbusService{nullptr}; }; class MenuMessageProcessor : public QObject diff --git a/src/windows/menu-main-window.cpp b/src/windows/menu-main-window.cpp index dca4e0c..3e3e4ed 100644 --- a/src/windows/menu-main-window.cpp +++ b/src/windows/menu-main-window.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -89,6 +90,7 @@ void WindowHelper::setWindowAttribute(QWindow *window) } } +//======MenuMainWindow======// MenuMainWindow::MenuMainWindow(QWindow *parent) : QQuickWindow(parent), m_geometryHelper(new WindowGeometryHelper(this)) { init(); @@ -314,4 +316,99 @@ const QRect &WindowGeometryHelper::normalGeometry() return m_normalGeometry; } +//======MenuWindow======// +MenuWindow::MenuWindow(QWindow *parent) : QQuickView(parent), m_geometryHelper(new WindowGeometryHelper(this)) +{ + init(); +} + +MenuWindow::MenuWindow(QQmlEngine *engine, QWindow *parent) + : QQuickView(engine, parent), m_geometryHelper(new WindowGeometryHelper(this)) +{ + init(); +} + +void MenuWindow::init() +{ + setTitle(QCoreApplication::applicationName()); + setResizeMode(SizeRootObjectToView); + setColor("transparent"); + + // TODO 使用窗管接口设置无边框 + setFlags(Qt::FramelessWindowHint); + WindowHelper::setWindowAttribute(this); + + // 访问窗口api + rootContext()->setContextProperty("mainWindow", this); + connect(this, &QQuickView::activeFocusItemChanged, this, &MenuWindow::onActiveFocusItemChanged); +} + +void MenuWindow::updateGeometry() +{ + QRect rect = m_isFullScreen ? m_geometryHelper->fullScreenGeometry() : m_geometryHelper->normalGeometry(); + if (rect == geometry()) { + return; + } + + WindowHelper::setWindowGeometry(this, rect); +} + +bool MenuWindow::isFullScreen() const +{ + return m_isFullScreen; +} + +void MenuWindow::setFullScreen(bool isFullScreen) +{ + if (m_isFullScreen == isFullScreen) { + return; + } + + m_isFullScreen = isFullScreen; + QEvent event(QEvent::Resize); + QCoreApplication::sendEvent(this, &event); + + Q_EMIT fullScreenChanged(); +} + +void MenuWindow::exposeEvent(QExposeEvent *event) +{ + QQuickWindow::exposeEvent(event); +} + +void MenuWindow::resizeEvent(QResizeEvent *event) +{ + updateGeometry(); + QQuickView::resizeEvent(event); +} + +void MenuWindow::showEvent(QShowEvent *event) +{ + QQuickWindow::showEvent(event); +} + +void MenuWindow::focusOutEvent(QFocusEvent *event) +{ + //setVisible(false); + QQuickWindow::focusOutEvent(event); +} + +bool MenuWindow::event(QEvent *event) +{ + if (event->type() == QEvent::Move) { + updateGeometry(); + return true; + } + return QQuickView::event(event); +} + +void MenuWindow::onActiveFocusItemChanged() +{ + if (activeFocusItem()) { + return; + } + + setVisible(false); +} + } // UkuiMenu diff --git a/src/windows/menu-main-window.h b/src/windows/menu-main-window.h index 57dc094..c45a72f 100644 --- a/src/windows/menu-main-window.h +++ b/src/windows/menu-main-window.h @@ -22,6 +22,7 @@ #include #include #include +#include #include namespace UkuiMenu { @@ -98,6 +99,40 @@ private: WindowGeometryHelper *m_geometryHelper{nullptr}; }; +class MenuWindow : public QQuickView +{ + Q_OBJECT + Q_PROPERTY(bool isFullScreen READ isFullScreen WRITE setFullScreen NOTIFY fullScreenChanged) + +public: + explicit MenuWindow(QWindow *parent = nullptr); + MenuWindow(QQmlEngine* engine, QWindow *parent); + + bool isFullScreen() const; + void setFullScreen(bool isFullScreen); + +Q_SIGNALS: + void fullScreenChanged(); + +private Q_SLOTS: + void onActiveFocusItemChanged(); + +protected: + void exposeEvent(QExposeEvent *event) override; + void resizeEvent(QResizeEvent *event) override; + void showEvent(QShowEvent *event) override; + void focusOutEvent(QFocusEvent *event) override; + bool event(QEvent *event) override; + +private: + void init(); + void updateGeometry(); + +private: + bool m_isFullScreen{false}; + WindowGeometryHelper *m_geometryHelper{nullptr}; +}; + } // UkuiMenu #endif //UKUI_MENU_MENU_MAIN_WINDOW_H