The most basic implementation of touch support for hyprbars

This commit is contained in:
caffeine01 2024-12-09 23:07:12 +00:00
parent 2530394e67
commit 0bedd0abe5
2 changed files with 39 additions and 0 deletions

View file

@ -16,6 +16,8 @@ CHyprBar::CHyprBar(PHLWINDOW pWindow) : IHyprWindowDecoration(pWindow) {
m_pMouseButtonCallback = HyprlandAPI::registerCallbackDynamic(
PHANDLE, "mouseButton", [&](void* self, SCallbackInfo& info, std::any param) { onMouseDown(info, std::any_cast<IPointer::SButtonEvent>(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<Vector2D>(param)); });
@ -26,6 +28,7 @@ CHyprBar::CHyprBar(PHLWINDOW pWindow) : IHyprWindowDecoration(pWindow) {
CHyprBar::~CHyprBar() {
damageEntire();
HyprlandAPI::unregisterCallback(PHANDLE, m_pMouseButtonCallback);
HyprlandAPI::unregisterCallback(PHANDLE, m_pTouchDownCallback);
HyprlandAPI::unregisterCallback(PHANDLE, m_pMouseMoveCallback);
std::erase(g_pGlobalState->bars, this);
}
@ -126,6 +129,39 @@ void CHyprBar::onMouseDown(SCallbackInfo& info, IPointer::SButtonEvent e) {
m_bDragPending = true;
}
void CHyprBar::onTouchDown(SCallbackInfo& info) {
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();
static auto* const PBARBUTTONPADDING = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprbars:bar_button_padding")->getDataStaticPtr();
static auto* const PBARPADDING = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprbars:bar_padding")->getDataStaticPtr();
static auto* const PALIGNBUTTONS = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprbars:bar_buttons_alignment")->getDataStaticPtr();
const bool BUTTONSRIGHT = std::string{*PALIGNBUTTONS} != "left";
if (!VECINRECT(COORDS, 0, 0, assignedBoxGlobal().w, **PHEIGHT - 1))
return;
float offset = **PBARPADDING;
for (auto& b : g_pGlobalState->buttons) {
const auto BARBUF = Vector2D{(int)assignedBoxGlobal().w, **PHEIGHT};
Vector2D currentPos = Vector2D{(BUTTONSRIGHT ? BARBUF.x - **PBARBUTTONPADDING - b.size - offset : offset), (BARBUF.y - b.size) / 2.0}.floor();
if (VECINRECT(COORDS, currentPos.x, currentPos.y, currentPos.x + b.size + **PBARBUTTONPADDING, currentPos.y + b.size)) {
// hit on close
g_pKeybindManager->m_mDispatchers["exec"](b.cmd);
return;
}
offset += **PBARBUTTONPADDING + b.size;
}
}
void CHyprBar::onMouseMove(Vector2D coords) {
if (m_bDragPending) {
m_bDragPending = false;

View file

@ -5,6 +5,7 @@
#include <hyprland/src/render/decorations/IHyprWindowDecoration.hpp>
#include <hyprland/src/render/OpenGL.hpp>
#include <hyprland/src/devices/IPointer.hpp>
#include <hyprland/src/devices/ITouch.hpp>
#include "globals.hpp"
class CHyprBar : public IHyprWindowDecoration {
@ -60,10 +61,12 @@ 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 onMouseMove(Vector2D coords);
CBox assignedBoxGlobal();
SP<HOOK_CALLBACK_FN> m_pMouseButtonCallback;
SP<HOOK_CALLBACK_FN> m_pTouchDownCallback;
SP<HOOK_CALLBACK_FN> m_pMouseMoveCallback;
std::string m_szLastTitle;