From 333fbc8d2fd5c2fbc15d4c068f6d8257b001ed2d Mon Sep 17 00:00:00 2001 From: reionwong Date: Mon, 21 Jun 2021 02:52:26 +0800 Subject: [PATCH] Support always hidden --- src/docksettings.cpp | 5 +- src/docksettings.h | 2 +- src/fakewindow.cpp | 4 +- src/mainwindow.cpp | 120 ++++++++++++++++++++++++++++++++++----- src/mainwindow.h | 9 ++- src/xwindowinterface.cpp | 5 ++ src/xwindowinterface.h | 1 + 7 files changed, 125 insertions(+), 21 deletions(-) diff --git a/src/docksettings.cpp b/src/docksettings.cpp index 5eb0fa6..3455cd6 100644 --- a/src/docksettings.cpp +++ b/src/docksettings.cpp @@ -42,7 +42,7 @@ DockSettings::DockSettings(QObject *parent) , m_edgeMargins(10) , m_roundedWindowEnabled(true) , m_direction(Left) - , m_visibility(AlwaysVisible) + , m_visibility(AlwaysShow) , m_settings(new QSettings(QSettings::UserScope, "cutefishos", "dock")) , m_fileWatcher(new QFileSystemWatcher(this)) { @@ -51,7 +51,7 @@ DockSettings::DockSettings(QObject *parent) if (!m_settings->contains("Direction")) m_settings->setValue("Direction", Bottom); if (!m_settings->contains("Visibility")) - m_settings->setValue("Visibility", AlwaysVisible); + m_settings->setValue("Visibility", AlwaysShow); if (!m_settings->contains("RoundedWindow")) m_settings->setValue("RoundedWindow", true); @@ -59,6 +59,7 @@ DockSettings::DockSettings(QObject *parent) m_iconSize = m_settings->value("IconSize").toInt(); m_direction = static_cast(m_settings->value("Direction").toInt()); + m_visibility = static_cast(m_settings->value("Visibility").toInt()); m_roundedWindowEnabled = m_settings->value("RoundedWindow").toBool(); m_fileWatcher->addPath(m_settings->fileName()); diff --git a/src/docksettings.h b/src/docksettings.h index 64cb327..1af4d80 100644 --- a/src/docksettings.h +++ b/src/docksettings.h @@ -41,7 +41,7 @@ public: Q_ENUMS(Direction) enum Visibility { - AlwaysVisible = 0, + AlwaysShow = 0, AutoHide, AlwaysHide }; diff --git a/src/fakewindow.cpp b/src/fakewindow.cpp index e502b21..0341179 100644 --- a/src/fakewindow.cpp +++ b/src/fakewindow.cpp @@ -38,7 +38,7 @@ FakeWindow::FakeWindow(QQuickView *parent) , m_delayedContainsMouse(false) , m_containsMouse(false) { - setColor(Qt::red); + setColor(Qt::transparent); setDefaultAlphaBuffer(true); setFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | @@ -100,7 +100,7 @@ void FakeWindow::setContainsMouse(bool contains) void FakeWindow::updateGeometry() { - int length = 10; + int length = 5; const QRect screenRect = qApp->primaryScreen()->geometry(); QRect newRect; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 2efc9ed..2c99296 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,7 +1,7 @@ /* * Copyright (C) 2021 CutefishOS Team. * - * Author: rekols + * Author: Reion Wong * * 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 @@ -39,6 +39,8 @@ MainWindow::MainWindow(QQuickView *parent) , m_appModel(new ApplicationModel) , m_fakeWindow(nullptr) , m_trashManager(new TrashManager) + , m_showTimer(new QTimer(this)) + , m_hideTimer(new QTimer(this)) { setDefaultAlphaBuffer(false); setColor(Qt::transparent); @@ -56,7 +58,6 @@ MainWindow::MainWindow(QQuickView *parent) setSource(QUrl(QStringLiteral("qrc:/qml/main.qml"))); setResizeMode(QQuickView::SizeRootObjectToView); setScreen(qApp->primaryScreen()); - setVisible(true); initSlideWindow(); resizeWindow(); @@ -64,13 +65,28 @@ MainWindow::MainWindow(QQuickView *parent) connect(qApp->primaryScreen(), &QScreen::virtualGeometryChanged, this, &MainWindow::resizeWindow); connect(qApp->primaryScreen(), &QScreen::geometryChanged, this, &MainWindow::resizeWindow); - connect(qApp->primaryScreen(), &QScreen::orientationChanged, this, [=] (Qt::ScreenOrientation orientation) { - Q_UNUSED(orientation) + m_showTimer->setSingleShot(true); + m_showTimer->setInterval(300); + connect(m_showTimer, &QTimer::timeout, this, [=] { + setVisible(true); + + if (m_fakeWindow) { + m_fakeWindow->setVisible(false); + } + }); + + m_hideTimer->setSingleShot(true); + m_hideTimer->setInterval(1000); + connect(m_hideTimer, &QTimer::timeout, this, [=] { + setVisible(false); + + if (m_fakeWindow) { + m_fakeWindow->setVisible(true); + } }); connect(m_appModel, &ApplicationModel::countChanged, this, &MainWindow::resizeWindow); - connect(m_settings, &DockSettings::directionChanged, this, &MainWindow::onPositionChanged); connect(m_settings, &DockSettings::iconSizeChanged, this, &MainWindow::onIconSizeChanged); connect(m_settings, &DockSettings::visibilityChanged, this, &MainWindow::onVisibilityChanged); @@ -130,7 +146,9 @@ QRect MainWindow::windowRect() const void MainWindow::resizeWindow() { setGeometry(windowRect()); - updateViewStruts(); + + if (m_settings->visibility() == DockSettings::AlwaysShow) + updateViewStruts(); emit resizingFished(); } @@ -154,23 +172,45 @@ void MainWindow::updateViewStruts() XWindowInterface::instance()->setViewStruts(this, m_settings->direction(), geometry()); } +void MainWindow::clearViewStruts() +{ + XWindowInterface::instance()->clearViewStruts(this); +} + void MainWindow::createFakeWindow() { if (!m_fakeWindow) { + installEventFilter(this); + m_fakeWindow = new FakeWindow; connect(m_fakeWindow, &FakeWindow::containsMouseChanged, this, [=](bool contains) { + switch (m_settings->visibility()) { + case DockSettings::AlwaysHide: { + m_showTimer->stop(); + if (contains) { + m_showTimer->start(); + } else { + m_hideTimer->start(); + } + + break; + } + default: + break; + } }); connect(m_fakeWindow, &FakeWindow::dragEntered, this, [&] {}); - } } void MainWindow::deleteFakeWindow() { if (m_fakeWindow) { + removeEventFilter(this); + disconnect(m_fakeWindow); m_fakeWindow->deleteLater(); m_fakeWindow = nullptr; } @@ -178,12 +218,20 @@ void MainWindow::deleteFakeWindow() void MainWindow::onPositionChanged() { - setVisible(false); - initSlideWindow(); - setVisible(true); + if (m_settings->visibility() == DockSettings::AlwaysHide) { + setVisible(false); + initSlideWindow(); + setGeometry(windowRect()); + clearViewStruts(); + } - setGeometry(windowRect()); - updateViewStruts(); + if (m_settings->visibility() == DockSettings::AlwaysShow) { + setVisible(false); + initSlideWindow(); + setVisible(true); + setGeometry(windowRect()); + updateViewStruts(); + } emit positionChanged(); } @@ -198,8 +246,50 @@ void MainWindow::onIconSizeChanged() void MainWindow::onVisibilityChanged() { - if (m_settings->visibility() == DockSettings::AlwaysVisible) - return; + // Always show + if (m_settings->visibility() == DockSettings::AlwaysShow) { + setGeometry(windowRect()); + setVisible(true); + updateViewStruts(); - createFakeWindow(); + // Delete fakewindow + if (m_fakeWindow) { + deleteFakeWindow(); + } + } + + // Always hide + if (m_settings->visibility() == DockSettings::AlwaysHide) { + clearViewStruts(); + setGeometry(windowRect()); + setVisible(false); + + // Create + if (!m_fakeWindow) + createFakeWindow(); + } +} + +bool MainWindow::eventFilter(QObject *obj, QEvent *e) +{ + switch (e->type()) { + case QEvent::Enter: + m_hideTimer->stop(); + break; + case QEvent::Leave: + m_hideTimer->start(); + break; + case QEvent::DragEnter: + case QEvent::DragMove: + m_hideTimer->stop(); + break; + case QEvent::DragLeave: + case QEvent::Drop: + m_hideTimer->stop(); + break; + default: + break; + } + + return QQuickView::eventFilter(obj, e); } diff --git a/src/mainwindow.h b/src/mainwindow.h index a3af6db..a2e9c87 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -1,7 +1,7 @@ /* * Copyright (C) 2021 CutefishOS Team. * - * Author: rekols + * Author: Reion Wong * * 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 @@ -46,6 +46,7 @@ private: void resizeWindow(); void initSlideWindow(); void updateViewStruts(); + void clearViewStruts(); void createFakeWindow(); void deleteFakeWindow(); @@ -55,11 +56,17 @@ private slots: void onIconSizeChanged(); void onVisibilityChanged(); +protected: + bool eventFilter(QObject *obj, QEvent *e) override; + private: DockSettings *m_settings; ApplicationModel *m_appModel; FakeWindow *m_fakeWindow; TrashManager *m_trashManager; + + QTimer *m_showTimer; + QTimer *m_hideTimer; }; #endif // MAINWINDOW_H diff --git a/src/xwindowinterface.cpp b/src/xwindowinterface.cpp index d494e8e..8b2bdbf 100644 --- a/src/xwindowinterface.cpp +++ b/src/xwindowinterface.cpp @@ -186,6 +186,11 @@ void XWindowInterface::setViewStruts(QWindow *view, DockSettings::Direction dire ); } +void XWindowInterface::clearViewStruts(QWindow *view) +{ + KWindowSystem::setExtendedStrut(view->winId(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); +} + void XWindowInterface::startInitWindows() { for (auto wid : KWindowSystem::self()->windows()) { diff --git a/src/xwindowinterface.h b/src/xwindowinterface.h index a1cbdf9..be3110f 100644 --- a/src/xwindowinterface.h +++ b/src/xwindowinterface.h @@ -48,6 +48,7 @@ public: bool isAcceptableWindow(quint64 wid); void setViewStruts(QWindow *view, DockSettings::Direction direction, const QRect &rect); + void clearViewStruts(QWindow *view); void startInitWindows();