diff --git a/hyprbars/barDeco.cpp b/hyprbars/barDeco.cpp index 2345933..9bb64a6 100644 --- a/hyprbars/barDeco.cpp +++ b/hyprbars/barDeco.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "globals.hpp" @@ -13,11 +14,18 @@ CHyprBar::CHyprBar(PHLWINDOW pWindow) : IHyprWindowDecoration(pWindow) { const auto PMONITOR = pWindow->m_pMonitor.lock(); PMONITOR->scheduledRecalc = true; + m_pTouchDownCallback = HyprlandAPI::registerCallbackDynamic( + PHANDLE, "touchDown", [&](void* self, SCallbackInfo& info, std::any param) { onTouchDown(info, std::any_cast(param)); }); + + m_pTouchUpCallback = + HyprlandAPI::registerCallbackDynamic(PHANDLE, "touchUp", [&](void* self, SCallbackInfo& info, std::any param) { onTouchUp(info, std::any_cast(param)); }); + + m_pTouchMoveCallback = HyprlandAPI::registerCallbackDynamic( + PHANDLE, "touchMove", [&](void* self, SCallbackInfo& info, std::any param) { onTouchMove(info, std::any_cast(param)); }); + m_pMouseButtonCallback = HyprlandAPI::registerCallbackDynamic( PHANDLE, "mouseButton", [&](void* self, SCallbackInfo& info, std::any param) { onMouseDown(info, std::any_cast(param)); }); - m_pTouchDownCallback = HyprlandAPI::registerCallbackDynamic(PHANDLE, "touchDown", [&](void* self, SCallbackInfo& info, std::any param) { onTouchDown(info); }); - m_pMouseMoveCallback = HyprlandAPI::registerCallbackDynamic(PHANDLE, "mouseMove", [&](void* self, SCallbackInfo& info, std::any param) { onMouseMove(std::any_cast(param)); }); @@ -29,6 +37,8 @@ CHyprBar::~CHyprBar() { damageEntire(); HyprlandAPI::unregisterCallback(PHANDLE, m_pMouseButtonCallback); HyprlandAPI::unregisterCallback(PHANDLE, m_pTouchDownCallback); + HyprlandAPI::unregisterCallback(PHANDLE, m_pTouchUpCallback); + HyprlandAPI::unregisterCallback(PHANDLE, m_pTouchMoveCallback); HyprlandAPI::unregisterCallback(PHANDLE, m_pMouseMoveCallback); std::erase(g_pGlobalState->bars, this); } @@ -59,16 +69,12 @@ std::string CHyprBar::getDisplayName() { void CHyprBar::onMouseDown(SCallbackInfo& info, IPointer::SButtonEvent e) { if (m_pWindow.lock() != g_pCompositor->m_pLastWindow.lock()) - return; + return; const auto PWINDOW = m_pWindow.lock(); const auto COORDS = cursorRelativeToBar(); - static auto* const PHEIGHT = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprbars:bar_height")->getDataStaticPtr(); - static auto* const PALIGNBUTTONS = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprbars:bar_buttons_alignment")->getDataStaticPtr(); - - const bool BUTTONSRIGHT = std::string{*PALIGNBUTTONS} != "left"; - + static auto* const PHEIGHT = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprbars:bar_height")->getDataStaticPtr(); if (!VECINRECT(COORDS, 0, 0, assignedBoxGlobal().w, **PHEIGHT - 1)) { if (m_bDraggingThis) { g_pKeybindManager->m_mDispatchers["mouse"]("0movewindow"); @@ -112,12 +118,69 @@ void CHyprBar::onMouseDown(SCallbackInfo& info, IPointer::SButtonEvent e) { m_bDragPending = true; } -void CHyprBar::onTouchDown(SCallbackInfo& info) { - const auto COORDS = cursorRelativeToBar(); - static auto* const PHEIGHT = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprbars:bar_height")->getDataStaticPtr(); - if (!VECINRECT(COORDS, 0, 0, assignedBoxGlobal().w, **PHEIGHT - 1)) +void CHyprBar::onTouchDown(SCallbackInfo& info, ITouch::SDownEvent e) { + if (m_pWindow.lock() != g_pCompositor->m_pLastWindow.lock()) return; + + const auto PWINDOW = m_pWindow.lock(); + const auto COORDS = cursorRelativeToBar(); + + static auto* const PHEIGHT = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprbars:bar_height")->getDataStaticPtr(); + if (!VECINRECT(COORDS, 0, 0, assignedBoxGlobal().w, **PHEIGHT - 1)) { + if (m_bDraggingThis) { + g_pCompositor->warpCursorTo(Vector2D(e.pos.x, e.pos.y)); + g_pInputManager->mouseMoveUnified(e.timeMs); + g_pKeybindManager->m_mDispatchers["mouse"]("0movewindow"); + Debug::log(LOG, "[hyprbars] Dragging ended on touchdown {:x}", (uintptr_t)PWINDOW.get()); + } + + m_bDraggingThis = false; + m_bDragPending = false; + m_bTouchEv = false; + return; + } + + g_pCompositor->warpCursorTo(Vector2D(e.pos.x, e.pos.y)); + + if (PWINDOW->m_bIsFloating) + g_pCompositor->changeWindowZOrder(PWINDOW, true); + + // do the button press + CHyprBar::doButtonPress(COORDS); + + m_bTouchEv = true; + m_bDragPending = true; +} + +void CHyprBar::onTouchUp(SCallbackInfo& info, ITouch::SUpEvent e) { + if (m_pWindow.lock() != g_pCompositor->m_pLastWindow.lock()) + return; + const auto PWINDOW = m_pWindow.lock(); + + if (m_bCancelledDown) + info.cancelled = true; + + m_bCancelledDown = false; + + if (m_bDraggingThis) { + g_pKeybindManager->m_mDispatchers["mouse"]("0movewindow"); + m_bDraggingThis = false; + + Debug::log(LOG, "[hyprbars] Dragging ended on touchup {:x}", (uintptr_t)PWINDOW.get()); + } + + m_bDragPending = false; + m_bTouchEv = false; +} + +void CHyprBar::onTouchMove(SCallbackInfo& info, ITouch::SMotionEvent e) { + if (m_bDragPending && m_bTouchEv) { + g_pInputManager->mouseMoveUnified(e.timeMs); + g_pKeybindManager->m_mDispatchers["mouse"]("1movewindow"); + m_bDraggingThis = true; + return; + } } void CHyprBar::doButtonPress(Vector2D coords) { @@ -145,13 +208,11 @@ void CHyprBar::doButtonPress(Vector2D coords) { } void CHyprBar::onMouseMove(Vector2D coords) { - if (m_bDragPending) { + if (m_bDragPending && !m_bTouchEv) { m_bDragPending = false; g_pKeybindManager->m_mDispatchers["mouse"]("1movewindow"); m_bDraggingThis = true; - Debug::log(LOG, "[hyprbars] Dragging initiated on {:x}", (uintptr_t)m_pWindow.lock().get()); - return; } } diff --git a/hyprbars/barDeco.hpp b/hyprbars/barDeco.hpp index e84c089..cebf6ba 100644 --- a/hyprbars/barDeco.hpp +++ b/hyprbars/barDeco.hpp @@ -9,6 +9,11 @@ #include #include "globals.hpp" +#define private public +#include +#include +#undef private + class CHyprBar : public IHyprWindowDecoration { public: CHyprBar(PHLWINDOW); @@ -62,18 +67,23 @@ class CHyprBar : public IHyprWindowDecoration { void renderBarButtons(const Vector2D& bufferSize, const float scale); void renderBarButtonsText(CBox* barBox, const float scale, const float a); void onMouseDown(SCallbackInfo& info, IPointer::SButtonEvent e); - void onTouchDown(SCallbackInfo& info); + void onTouchDown(SCallbackInfo& info, ITouch::SDownEvent e); + void onTouchUp(SCallbackInfo& info, ITouch::SUpEvent e); + void onTouchMove(SCallbackInfo& info, ITouch::SMotionEvent e); void doButtonPress(Vector2D coords); void onMouseMove(Vector2D coords); CBox assignedBoxGlobal(); - SP m_pMouseButtonCallback; SP m_pTouchDownCallback; + SP m_pTouchUpCallback; + SP m_pTouchMoveCallback; + SP m_pMouseButtonCallback; SP m_pMouseMoveCallback; std::string m_szLastTitle; bool m_bDraggingThis = false; + bool m_bTouchEv = false; bool m_bDragPending = false; bool m_bCancelledDown = false;