From 87e03b6723e71b9017800bd6d45a42aa4f8349c7 Mon Sep 17 00:00:00 2001 From: reionwong Date: Sat, 4 Sep 2021 11:28:06 +0800 Subject: [PATCH] Add smart hide --- src/activity.cpp | 29 +++++++++++++++++++++++++++++ src/activity.h | 4 ++++ src/docksettings.h | 3 ++- src/mainwindow.cpp | 41 ++++++++++++++++++++++++++++++++++++----- src/mainwindow.h | 2 ++ 5 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/activity.cpp b/src/activity.cpp index 6420095..0aa3097 100644 --- a/src/activity.cpp +++ b/src/activity.cpp @@ -18,6 +18,7 @@ */ #include "activity.h" +#include "docksettings.h" #include #include @@ -34,6 +35,7 @@ Activity *Activity::self() Activity::Activity(QObject *parent) : QObject(parent) + , m_existsWindowMaximized(false) { onActiveWindowChanged(); @@ -42,6 +44,11 @@ Activity::Activity(QObject *parent) this, &Activity::onActiveWindowChanged); } +bool Activity::existsWindowMaximized() const +{ + return m_existsWindowMaximized; +} + bool Activity::launchPad() const { return m_launchPad; @@ -54,11 +61,33 @@ void Activity::onActiveWindowChanged() NET::WM2WindowClass); bool launchPad = info.windowClassClass() == "cutefish-launcher"; + if (m_launchPad != launchPad) { m_launchPad = launchPad; emit launchPadChanged(); } + if (DockSettings::self()->visibility() == DockSettings::IntellHide) { + bool existsWindowMaximized = false; + + for (WId wid : KWindowSystem::windows()) { + KWindowInfo i(wid, NET::WMState); + + if (i.isMinimized()) + continue; + + if (i.hasState(NET::MaxVert) || i.hasState(NET::MaxHoriz)) { + existsWindowMaximized = true; + break; + } + } + + if (m_existsWindowMaximized != existsWindowMaximized) { + m_existsWindowMaximized = existsWindowMaximized; + emit existsWindowMaximizedChanged(); + } + } + m_pid = info.pid(); m_windowClass = info.windowClassClass().toLower(); } diff --git a/src/activity.h b/src/activity.h index 0da591d..4b94f12 100644 --- a/src/activity.h +++ b/src/activity.h @@ -26,11 +26,13 @@ class Activity : public QObject { Q_OBJECT Q_PROPERTY(bool launchPad READ launchPad NOTIFY launchPadChanged) + Q_PROPERTY(bool existsWindowMaximized READ existsWindowMaximized NOTIFY existsWindowMaximizedChanged) public: static Activity *self(); explicit Activity(QObject *parent = nullptr); + bool existsWindowMaximized() const; bool launchPad() const; private slots: @@ -38,11 +40,13 @@ private slots: signals: void launchPadChanged(); + void existsWindowMaximizedChanged(); private: QString m_windowClass; quint32 m_pid; + bool m_existsWindowMaximized; bool m_launchPad; }; diff --git a/src/docksettings.h b/src/docksettings.h index 0577fd6..a671d8b 100644 --- a/src/docksettings.h +++ b/src/docksettings.h @@ -42,7 +42,8 @@ public: enum Visibility { AlwaysShow = 0, // AutoHide, - AlwaysHide + AlwaysHide, + IntellHide }; Q_ENUMS(Visibility) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6c3aa05..fca6f7a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -75,10 +75,11 @@ MainWindow::MainWindow(QQuickView *parent) m_hideTimer->setSingleShot(true); m_hideTimer->setInterval(500); - connect(m_hideTimer, &QTimer::timeout, this, [=] { setVisible(false); }); + connect(m_hideTimer, &QTimer::timeout, this, &MainWindow::onHideTimeout); // When the current window changes. connect(m_activity, &Activity::launchPadChanged, this, &MainWindow::onVisibilityChanged); + connect(m_activity, &Activity::existsWindowMaximizedChanged, this, &MainWindow::onVisibilityChanged); // Screen change. connect(qGuiApp, &QGuiApplication::primaryScreenChanged, this, &MainWindow::onPrimaryScreenChanged); @@ -263,8 +264,8 @@ void MainWindow::createFakeWindow() connect(m_fakeWindow, &FakeWindow::containsMouseChanged, this, [=](bool contains) { switch (m_settings->visibility()) { - case DockSettings::AlwaysHide: { - + case DockSettings::AlwaysHide: + case DockSettings::IntellHide:{ if (contains) { m_hideTimer->stop(); @@ -285,8 +286,6 @@ void MainWindow::createFakeWindow() break; } }); - - connect(m_fakeWindow, &FakeWindow::dragEntered, this, [&] {}); } } @@ -330,6 +329,14 @@ void MainWindow::onPositionChanged() updateViewStruts(); } + if (m_settings->visibility() == DockSettings::IntellHide) { + setVisible(false); + initSlideWindow(); + setVisible(true); + setGeometry(windowRect()); + updateViewStruts(); + } + emit positionChanged(); } @@ -361,6 +368,20 @@ void MainWindow::onVisibilityChanged() if (m_activity->launchPad()) return; + if (m_settings->visibility() == DockSettings::IntellHide) { + clearViewStruts(); + setGeometry(windowRect()); + + if (m_activity->existsWindowMaximized() && !m_hideBlocked) { + setVisible(false); + } else { + setVisible(true); + } + + if (!m_fakeWindow) + createFakeWindow(); + } + // Always hide if (m_settings->visibility() == DockSettings::AlwaysHide) { clearViewStruts(); @@ -373,6 +394,16 @@ void MainWindow::onVisibilityChanged() } } +void MainWindow::onHideTimeout() +{ + if (m_settings->visibility() == DockSettings::IntellHide + && !m_activity->existsWindowMaximized()) { + return; + } + + setVisible(false); +} + bool MainWindow::eventFilter(QObject *obj, QEvent *e) { switch (e->type()) { diff --git a/src/mainwindow.h b/src/mainwindow.h index 4b30a43..238325a 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -70,6 +70,8 @@ private slots: void onIconSizeChanged(); void onVisibilityChanged(); + void onHideTimeout(); + protected: bool eventFilter(QObject *obj, QEvent *e) override;