From 3a465782e115288e7d9e31b489f381ad59701a8e Mon Sep 17 00:00:00 2001
From: Michail Vourlakos <mvourlakos@gmail.com>
Date: Mon, 15 Jul 2019 19:15:53 +0300
Subject: [PATCH] wayland:improve latte windows id discovering

---
 app/view/positioner.cpp                   | 12 ++++++----
 app/view/screenedgeghostwindow.cpp        | 12 ++++++----
 app/view/settings/primaryconfigview.cpp   | 12 ++++++----
 app/view/settings/secondaryconfigview.cpp | 12 ++++++----
 app/wm/abstractwindowinterface.h          |  2 ++
 app/wm/waylandinterface.cpp               | 28 +++++++++++++++++------
 6 files changed, 51 insertions(+), 27 deletions(-)

diff --git a/app/view/positioner.cpp b/app/view/positioner.cpp
index 9fbcdfb6b..420318bfe 100644
--- a/app/view/positioner.cpp
+++ b/app/view/positioner.cpp
@@ -59,6 +59,13 @@ Positioner::Positioner(Latte::View *parent)
     if (m_corona) {
         if (KWindowSystem::isPlatformX11()) {
             m_corona->wm()->registerIgnoredWindow(m_view->winId());
+        } else {
+            connect(m_corona->wm(), &WindowSystem::AbstractWindowInterface::latteWindowAdded, this, [&]() {
+                if (m_waylandWindowId.isNull()) {
+                    m_waylandWindowId = m_corona->wm()->winIdFor("latte-dock", m_view->geometry());
+                    m_corona->wm()->registerIgnoredWindow(m_waylandWindowId);
+                }
+            });
         }
 
         m_screenSyncTimer.setInterval(qMax(m_corona->universalSettings()->screenTrackerInterval() - 500, 1000));
@@ -317,11 +324,6 @@ void Positioner::syncGeometry()
 
     qDebug() << "syncGeometry() called...";
 
-    if (KWindowSystem::isPlatformWayland() && m_waylandWindowId.isNull()) {
-        m_waylandWindowId = m_corona->wm()->winIdFor("latte-dock", m_view->geometry());
-        m_corona->wm()->registerIgnoredWindow(m_waylandWindowId);
-    }
-
     //! before updating the positioning and geometry of the dock
     //! we make sure that the dock is at the correct screen
     if (m_view->screen() != m_screenToFollow) {
diff --git a/app/view/screenedgeghostwindow.cpp b/app/view/screenedgeghostwindow.cpp
index 04fd4c07e..9cdfc5e2a 100644
--- a/app/view/screenedgeghostwindow.cpp
+++ b/app/view/screenedgeghostwindow.cpp
@@ -126,6 +126,13 @@ ScreenEdgeGhostWindow::ScreenEdgeGhostWindow(Latte::View *view) :
 
     if (KWindowSystem::isPlatformX11()) {
         m_corona->wm()->registerIgnoredWindow(winId());
+    } else {
+        connect(m_corona->wm(), &WindowSystem::AbstractWindowInterface::latteWindowAdded, this, [&]() {
+            if (m_waylandWindowId.isNull()) {
+                m_waylandWindowId = m_corona->wm()->winIdFor("latte-dock", geometry());
+                m_corona->wm()->registerIgnoredWindow(m_waylandWindowId);
+            }
+        });
     }
 
     setScreen(m_latteView->screen());
@@ -171,11 +178,6 @@ KWayland::Client::PlasmaShellSurface *ScreenEdgeGhostWindow::surface()
 
 void ScreenEdgeGhostWindow::updateGeometry()
 {
-    if (KWindowSystem::isPlatformWayland() && m_waylandWindowId.isNull()) {
-        m_waylandWindowId = m_corona->wm()->winIdFor("latte-dock", geometry());
-        m_corona->wm()->registerIgnoredWindow(m_waylandWindowId);
-    }
-
     QRect newGeometry;
     int thickness;
     if (KWindowSystem::compositingActive()) {
diff --git a/app/view/settings/primaryconfigview.cpp b/app/view/settings/primaryconfigview.cpp
index fdede6cd5..54d5ead8e 100644
--- a/app/view/settings/primaryconfigview.cpp
+++ b/app/view/settings/primaryconfigview.cpp
@@ -64,6 +64,13 @@ PrimaryConfigView::PrimaryConfigView(Plasma::Containment *containment, Latte::Vi
 
     if (KWindowSystem::isPlatformX11()) {
         m_corona->wm()->registerIgnoredWindow(winId());
+    } else {
+        connect(m_corona->wm(), &WindowSystem::AbstractWindowInterface::latteWindowAdded, this, [&]() {
+            if (m_waylandWindowId.isNull()) {
+                m_waylandWindowId = m_corona->wm()->winIdFor("latte-dock", geometry());
+                m_corona->wm()->registerIgnoredWindow(m_waylandWindowId);
+            }
+        });
     }
 
     setScreen(m_latteView->screen());
@@ -250,11 +257,6 @@ void PrimaryConfigView::syncGeometry()
         return;
     }
 
-    if (KWindowSystem::isPlatformWayland() && m_waylandWindowId.isNull()) {
-        m_waylandWindowId = m_corona->wm()->winIdFor("latte-dock", geometry());
-        m_corona->wm()->registerIgnoredWindow(m_waylandWindowId);
-    }
-
     const QSize size(rootObject()->width(), rootObject()->height());
     setMaximumSize(size);
     setMinimumSize(size);
diff --git a/app/view/settings/secondaryconfigview.cpp b/app/view/settings/secondaryconfigview.cpp
index b792b948d..2e716ecd5 100644
--- a/app/view/settings/secondaryconfigview.cpp
+++ b/app/view/settings/secondaryconfigview.cpp
@@ -58,6 +58,13 @@ SecondaryConfigView::SecondaryConfigView(Latte::View *view, QWindow *parent)
 
     if (KWindowSystem::isPlatformX11()) {
         m_corona->wm()->registerIgnoredWindow(winId());
+    } else {
+        connect(m_corona->wm(), &WindowSystem::AbstractWindowInterface::latteWindowAdded, this, [&]() {
+            if (m_waylandWindowId.isNull()) {
+                m_waylandWindowId = m_corona->wm()->winIdFor("latte-dock", geometry());
+                m_corona->wm()->registerIgnoredWindow(m_waylandWindowId);
+            }
+        });
     }
 
     setResizeMode(QQuickView::SizeViewToRootObject);
@@ -179,11 +186,6 @@ void SecondaryConfigView::syncGeometry()
         return;
     }
 
-    if (KWindowSystem::isPlatformWayland() && m_waylandWindowId.isNull()) {
-        m_waylandWindowId = m_corona->wm()->winIdFor("latte-dock", geometry());
-        m_corona->wm()->registerIgnoredWindow(m_waylandWindowId);
-    }
-
     const QSize size(rootObject()->width(), rootObject()->height());
     setMaximumSize(size);
     setMinimumSize(size);
diff --git a/app/wm/abstractwindowinterface.h b/app/wm/abstractwindowinterface.h
index ecb2c383a..9abf1753e 100644
--- a/app/wm/abstractwindowinterface.h
+++ b/app/wm/abstractwindowinterface.h
@@ -138,6 +138,8 @@ signals:
     void currentDesktopChanged();
     void currentActivityChanged();
 
+    void latteWindowAdded();
+
 protected:
     QString m_currentDesktop;
     QString m_currentActivity;
diff --git a/app/wm/waylandinterface.cpp b/app/wm/waylandinterface.cpp
index 642ec5ece..ea77b5c40 100644
--- a/app/wm/waylandinterface.cpp
+++ b/app/wm/waylandinterface.cpp
@@ -64,6 +64,8 @@ public:
                  | Qt::NoDropShadowWindowHint
                  | Qt::WindowDoesNotAcceptFocus);
 
+        connect(m_waylandInterface, &WindowSystem::AbstractWindowInterface::latteWindowAdded, this, &GhostWindow::identifyWinId);
+
         setupWaylandIntegration();
         show();
     }
@@ -78,10 +80,7 @@ public:
             return;
         }
 
-        if (m_winId.isNull()) {
-            m_winId = m_waylandInterface->winIdFor("latte-dock", geometry());
-            m_waylandInterface->registerIgnoredWindow(m_winId);
-        }
+        m_validGeometry = rect;
 
         setMinimumSize(rect.size());
         setMaximumSize(rect.size());
@@ -112,6 +111,17 @@ public:
 
     KWayland::Client::PlasmaShellSurface *m_shellSurface{nullptr};
     WindowSystem::WaylandInterface *m_waylandInterface{nullptr};
+
+    //! geometry() function under wayland does not return nice results
+    QRect m_validGeometry;
+
+public slots:
+    void identifyWinId() {
+        if (m_winId.isNull()) {
+            m_winId = m_waylandInterface->winIdFor("latte-dock", m_validGeometry);
+            m_waylandInterface->registerIgnoredWindow(m_winId);
+        }
+    }
 };
 
 namespace WindowSystem {
@@ -453,7 +463,7 @@ AppData WaylandInterface::appDataFor(WindowId wid) const
     auto window = windowFor(wid);
 
     const AppData &data = appDataFromUrl(windowUrlFromMetadata(window->appId(),
-                    window->pid(), rulesConfig));
+                                                               window->pid(), rulesConfig));
 
     return data;
 }
@@ -584,14 +594,14 @@ void WaylandInterface::requestToggleMaximized(WindowId wid) const
 {
     auto w = windowFor(wid);
 
-    if (w && isValidWindow(w)) {   
+    if (w && isValidWindow(w)) {
 #if KF5_VERSION_MINOR >= 52
         if (!m_currentDesktop.isEmpty()) {
             w->requestEnterVirtualDesktop(m_currentDesktop);
         }
 #endif
         w->requestToggleMaximized();
-    }   
+    }
 }
 
 bool WaylandInterface::isPlasmaDesktop(const KWayland::Client::PlasmaWindow *w) const
@@ -671,6 +681,10 @@ void WaylandInterface::windowCreatedProxy(KWayland::Client::PlasmaWindow *w)
     });
 
     emit windowAdded(w->internalId());
+
+    if (w->appId() == "latte-dock") {
+        emit latteWindowAdded();
+    }
 }
 
 }