diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 76b0460e..82e2986e 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -246,29 +246,32 @@ SMonitor* CCompositor::getMonitorFromID(const int& id) { SMonitor* CCompositor::getMonitorFromCursor() { const auto COORDS = Vector2D(m_sWLRCursor->x, m_sWLRCursor->y); - const auto OUTPUT = wlr_output_layout_output_at(m_sWLROutputLayout, COORDS.x, COORDS.y); - if (!OUTPUT) { - Debug::log(WARN, "getMonitorFromCursor: cursor outside monitors??"); - return &m_lMonitors.front(); - } - - for (auto& m : m_lMonitors) { - if (m.output == OUTPUT) - return &m; - } - - Debug::log(LOG, "Monitor not in list??"); - - return &m_lMonitors.front(); + return getMonitorFromVector(COORDS); } SMonitor* CCompositor::getMonitorFromVector(const Vector2D& point) { const auto OUTPUT = wlr_output_layout_output_at(m_sWLROutputLayout, point.x, point.y); if (!OUTPUT) { - Debug::log(WARN, "getMonitorFromVector: vector outside monitors? Returning front"); - return &m_lMonitors.front(); + float bestDistance = 0.f; + SMonitor* pBestMon = nullptr; + + for (auto& m : m_lMonitors) { + float dist = vecToRectDistanceSquared(point, m.vecPosition, m.vecPosition + m.vecSize); + + if (dist < bestDistance || !pBestMon) { + bestDistance = dist; + pBestMon = &m; + } + } + + if (!pBestMon) { // ????? + Debug::log(WARN, "getMonitorFromVector no close mon???"); + return &m_lMonitors.front(); + } + + return pBestMon; } return getMonitorFromOutput(OUTPUT); diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index ebc8c213..440bdc26 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -140,4 +140,10 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { } return result; +} + +float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2) { + const float DX = std::max((double)0, std::max(p1.x - vec.x, vec.x - p2.x)); + const float DY = std::max((double)0, std::max(p1.y - vec.y, vec.y - p2.y)); + return DX * DX + DY * DY; } \ No newline at end of file diff --git a/src/helpers/MiscFunctions.hpp b/src/helpers/MiscFunctions.hpp index ff7cc70a..eed3670d 100644 --- a/src/helpers/MiscFunctions.hpp +++ b/src/helpers/MiscFunctions.hpp @@ -10,5 +10,6 @@ std::string removeBeginEndSpacesTabs(std::string); bool isNumber(const std::string&); bool isDirection(const std::string&); int getWorkspaceIDFromString(const std::string&, std::string&); +float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2); float getPlusMinusKeywordResult(std::string in, float relative); \ No newline at end of file diff --git a/src/layout/DwindleLayout.cpp b/src/layout/DwindleLayout.cpp index 0f77aac0..eef98622 100644 --- a/src/layout/DwindleLayout.cpp +++ b/src/layout/DwindleLayout.cpp @@ -184,9 +184,14 @@ void CHyprDwindleLayout::onWindowCreated(CWindow* pWindow) { SDwindleNodeData* OPENINGON; const auto MONFROMCURSOR = g_pCompositor->getMonitorFromCursor(); - if (PMONITOR->ID == MONFROMCURSOR->ID && PNODE->workspaceID == PMONITOR->activeWorkspace) + if (PMONITOR->ID == MONFROMCURSOR->ID && PNODE->workspaceID == PMONITOR->activeWorkspace) { OPENINGON = getNodeFromWindow(g_pCompositor->vectorToWindowTiled(g_pInputManager->getMouseCoordsInternal())); - else + + // happens on reserved area + if (!OPENINGON && g_pCompositor->getWindowsOnWorkspace(PNODE->workspaceID) > 0) + OPENINGON = getFirstNodeOnWorkspace(PMONITOR->activeWorkspace); + + } else OPENINGON = getFirstNodeOnWorkspace(PMONITOR->activeWorkspace); Debug::log(LOG, "OPENINGON: %x, Workspace: %i, Monitor: %i", OPENINGON, PNODE->workspaceID, PMONITOR->ID); @@ -373,6 +378,10 @@ void CHyprDwindleLayout::changeWindowFloatingMode(CWindow* pWindow) { const auto PNODE = getNodeFromWindow(pWindow); if (!PNODE) { + const auto PNEWMON = g_pCompositor->getMonitorFromVector(pWindow->m_vRealPosition.vec() + pWindow->m_vRealSize.vec() / 2.f); + pWindow->m_iMonitorID = PNEWMON->ID; + pWindow->m_iWorkspaceID = PNEWMON->activeWorkspace; + // save real pos cuz the func applies the default 5,5 mid const auto PSAVEDPOS = pWindow->m_vRealPosition.vec(); const auto PSAVEDSIZE = pWindow->m_vRealSize.vec();