feat(src/windows): 开始菜单适配多屏任务栏

This commit is contained in:
youdiansaodongxi 2024-04-07 09:38:21 +08:00 committed by iaom
parent 598758c28d
commit 7a52fc5d45
4 changed files with 126 additions and 25 deletions

View File

@ -71,6 +71,7 @@ Item {
Component.onCompleted: mainWindow.changeWindowBlurRegion(x, y, width, height, radius)
onRadiusChanged: mainWindow.changeWindowBlurRegion(x, y, width, height, radius)
onWidthChanged: mainWindow.changeWindowBlurRegion(x, y, width, height, radius)
onHeightChanged: mainWindow.changeWindowBlurRegion(x, y, width, height, radius)
ParallelAnimation {
id: enterFullScreenAnimation

View File

@ -142,19 +142,19 @@ void UkuiMenuApplication::execCommand(Command command)
switch (command) {
case Active: {
if (m_mainWindow) {
m_mainWindow->setVisible(!m_mainWindow->isVisible());
m_mainWindow->activeMenuWindow(!m_mainWindow->isVisible());
}
break;
}
case Show: {
if (m_mainWindow) {
m_mainWindow->setVisible(true);
m_mainWindow->activeMenuWindow(true);
}
break;
}
case Quit: {
if (m_mainWindow) {
m_mainWindow->setVisible(false);
m_mainWindow->activeMenuWindow(false);
m_engine->quit();
}
QCoreApplication::quit();
@ -162,7 +162,7 @@ void UkuiMenuApplication::execCommand(Command command)
}
case Hide: {
if (m_mainWindow) {
m_mainWindow->setVisible(false);
m_mainWindow->activeMenuWindow(false);
}
break;
}

View File

@ -23,7 +23,6 @@
#include <window-helper.h>
#include <QGuiApplication>
#include <QQuickItem>
#include <QGSettings>
#include <QX11Info>
#include <QQmlContext>
#include <QPoint>
@ -31,6 +30,7 @@
#include <KWindowEffects>
#include <QPainterPath>
#include <QTimer>
#include <QCursor>
#define UKUI_PANEL_SETTING "org.ukui.panel.settings"
#define UKUI_PANEL_POSITION_KEY "panelposition"
@ -212,19 +212,28 @@ const int WindowGeometryHelper::getPanelPos()
}
//======MenuWindow======//
MenuWindow::MenuWindow(QWindow *parent) : QQuickView(parent), m_geometryHelper(new WindowGeometryHelper(this))
MenuWindow::MenuWindow(QWindow *parent) : QQuickView(parent)
{
init();
}
MenuWindow::MenuWindow(QQmlEngine *engine, QWindow *parent)
: QQuickView(engine, parent), m_geometryHelper(new WindowGeometryHelper(this))
: QQuickView(engine, parent)
{
init();
}
void MenuWindow::init()
{
initPanelSetting();
connect(MenuSetting::instance(), &MenuSetting::changed, this, [this] (const QString& key) {
if (key == MENU_WIDTH || key == MENU_HEIGHT) {
updateGeometry();
}
});
setScreen(QGuiApplication::primaryScreen());
setTitle(QCoreApplication::applicationName());
setResizeMode(SizeRootObjectToView);
setColor("transparent");
@ -237,13 +246,6 @@ void MenuWindow::init()
// 访问窗口api
rootContext()->setContextProperty("mainWindow", this);
connect(m_geometryHelper, &WindowGeometryHelper::geometryChanged, this, [this] {
QEvent event(QEvent::Move);
QCoreApplication::sendEvent(this, &event);
Q_EMIT panelPosChanged();
Q_EMIT normalRectChanged();
});
rootContext()->setContextProperty("isLiteMode", GlobalSetting::instance()->get(GlobalSetting::IsLiteMode));
connect(GlobalSetting::instance(), &GlobalSetting::styleChanged, this , [this] (const GlobalSetting::Key& key) {
if (key == GlobalSetting::EffectEnabled) {
@ -260,9 +262,34 @@ void MenuWindow::init()
ContextMenuManager::instance()->setMainWindow(this);
}
void MenuWindow::initPanelSetting()
{
if (!MenuSetting::instance()->get(FOLLOW_UKUI_PANEL).toBool()) {
return;
}
const QByteArray id(UKUI_PANEL_SETTING);
if (QGSettings::isSchemaInstalled(id)) {
m_setting = new QGSettings(id, QByteArray(), this);
QStringList keys = m_setting->keys();
if (keys.contains(UKUI_PANEL_POSITION_KEY)) {
m_panelPos = m_setting->get(UKUI_PANEL_POSITION_KEY).toInt();
}
connect(m_setting, &QGSettings::changed, this, [this] (const QString& key) {
if (key == UKUI_PANEL_POSITION_KEY) {
m_panelPos = m_setting->get(UKUI_PANEL_POSITION_KEY).toInt();
updateGeometry();
}
});
}
}
void MenuWindow::updateGeometry()
{
UkuiQuick::WindowProxy::setWindowGeometry(this, m_geometryHelper->fullScreenGeometry());
updateCurrentScreenGeometry();
UkuiQuick::WindowProxy::setWindowGeometry(this, m_fullScreenGeometry);
setMinimumSize(geometry().size());
setMaximumSize(geometry().size());
@ -320,11 +347,6 @@ void MenuWindow::focusOutEvent(QFocusEvent *event)
bool MenuWindow::event(QEvent *event)
{
if (event->type() == QEvent::Move) {
updateGeometry();
return true;
}
if (event->type() == QEvent::Show) {
KWindowSystem::setType(winId(), NET::SystemWindow);
updateGeometry();
@ -350,7 +372,7 @@ double MenuWindow::transparency() const
int MenuWindow::panelPos() const
{
return m_geometryHelper->getPanelPos();
return m_panelPos;
}
bool MenuWindow::editMode() const
@ -371,17 +393,85 @@ void MenuWindow::setEditMode(bool mode)
void MenuWindow::updateGeometryOfMask()
{
if (m_isFullScreen) {
m_maskGeometry = QRect(0, 0, m_geometryHelper->fullScreenGeometry().width(), m_geometryHelper->fullScreenGeometry().height());
m_maskGeometry = QRect(0, 0, m_fullScreenGeometry.width(), m_fullScreenGeometry.height());
} else {
m_maskGeometry = m_geometryHelper->normalGeometry();
m_maskGeometry = m_normalGeometry;
}
setMask(m_maskGeometry);
}
void MenuWindow::updateCurrentScreenGeometry()
{
if (!screen()) {
if (!QGuiApplication::primaryScreen()) {
return;
}
setScreen(QGuiApplication::primaryScreen());
}
QRect normalMaskRect, fullRect = screen()->availableGeometry();
int width = MenuSetting::instance()->get(MENU_WIDTH).toInt();
int height = MenuSetting::instance()->get(MENU_HEIGHT).toInt();
int margin = MenuSetting::instance()->get(MENU_MARGIN).toInt();
//上: 1, 下: 0, 左: 2, 右: 3
switch (m_panelPos) {
default:
case 0: {
QSize normalSize(qMin<int>(fullRect.width() - margin*2, width), qMin<int>(fullRect.height() - margin*2, height));
normalMaskRect.setTopLeft({margin, fullRect.height() - normalSize.height() - margin});
normalMaskRect.setSize(normalSize);
break;
}
case 1: {
normalMaskRect.setTopLeft({margin, margin});
normalMaskRect.setSize({qMin<int>(fullRect.width() - margin*2, width), qMin<int>(fullRect.height() - margin*2, height)});
break;
}
case 2: {
normalMaskRect.setTopLeft({margin, margin});
normalMaskRect.setSize({qMin<int>(fullRect.width() - margin*2, width), qMin<int>(fullRect.height() - margin*2, height)});
break;
}
case 3: {
QSize normalSize(qMin<int>(fullRect.width() - margin*2, width), qMin<int>(fullRect.height() - margin*2, height));
normalMaskRect.setTopLeft({fullRect.width() - normalSize.width() - margin, margin});
normalMaskRect.setSize(normalSize);
break;
}
}
m_normalGeometry = normalMaskRect;
m_fullScreenGeometry = fullRect;
Q_EMIT panelPosChanged();
Q_EMIT normalRectChanged();
}
QRect MenuWindow::normalRect() const
{
return m_geometryHelper->normalGeometry();
return m_normalGeometry;
}
void MenuWindow::activeMenuWindow(bool active)
{
if (active == isVisible()) {
return;
}
if (active) {
QScreen* currrentScreen = nullptr;
for (const auto& screen : QGuiApplication::screens()) {
if (screen->geometry().contains(QCursor::pos())) {
currrentScreen = screen;
break;
}
}
this->setScreen(currrentScreen ? currrentScreen : QGuiApplication::primaryScreen());
}
setVisible(active);
}
} // UkuiMenu

View File

@ -24,6 +24,7 @@
#include <QScreen>
#include <QQuickView>
#include <QQuickWindow>
#include <QGSettings>
namespace UkuiMenu {
@ -96,10 +97,13 @@ public:
int panelPos() const;
QRect normalRect() const;
void activeMenuWindow(bool active);
Q_INVOKABLE void changeWindowBlurRegion(qreal x, qreal y, qreal w, qreal h, qreal radius = 8);
Q_INVOKABLE void exitFullScreen();
Q_SIGNALS:
void geometryChanged();
void effectEnabledChanged();
void transparencyChanged();
void fullScreenChanged();
@ -116,14 +120,20 @@ protected:
private:
void init();
void initPanelSetting();
void updateGeometry();
void updateGeometryOfMask();
void updateCurrentScreenGeometry();
private:
// 任务栏位置与屏幕:上: 1, 下: 0, 左: 2, 右: 3, 如果为其他值,则说明任务栏不存在
int m_panelPos{4};
bool m_editMode {false};
bool m_isFullScreen{false};
WindowGeometryHelper *m_geometryHelper{nullptr};
QRect m_maskGeometry = QRect(0,0,0,0);
QRect m_normalGeometry = QRect(0,0,0,0);
QRect m_fullScreenGeometry = QRect(0,0,0,0);
QGSettings *m_setting {nullptr};
};
} // UkuiMenu