Skip to content

Commit

Permalink
fix: handle hold gestures
Browse files Browse the repository at this point in the history
  • Loading branch information
horriblename committed Dec 17, 2023
1 parent a92ea5e commit f9707f6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
40 changes: 26 additions & 14 deletions src/GestureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <hyprland/src/managers/KeybindManager.hpp>
#include <hyprland/src/managers/input/InputManager.hpp>
#include <memory>
#include <optional>

// constexpr double SWIPE_THRESHOLD = 30.;

Expand Down Expand Up @@ -65,18 +66,21 @@ void GestureManager::emulateSwipeUpdate(uint32_t time) {
}

bool GestureManager::handleCompletedGesture(const CompletedGesture& gev) {
if (this->dragGestureIsActive()) {
switch (gev.type) {
case CompletedGestureType::SWIPE:
// TODO: maybe check if active drag gesture is also swipe
this->emulateSwipeEnd(0, false);
return true;
case CompletedGestureType::HOLD_END:
// TODO:
return true;
default:
return false;
}
if (this->getActiveDragGesture().has_value()) {
switch (this->getActiveDragGesture()->type) {
case DragGestureType::SWIPE:
if (gev.type == CompletedGestureType::SWIPE) {
this->emulateSwipeEnd(0, false);
return true;
}
case DragGestureType::HOLD:
if (gev.type == CompletedGestureType::HOLD_END) {
// TODO:
return true;
}
};

return false;
}

return handleGestureBind(gev.to_string(), false);
Expand Down Expand Up @@ -132,11 +136,19 @@ bool handleGestureBind(std::string bind, bool pressed) {
}

void GestureManager::handleCancelledGesture() {
if (!this->dragGestureIsActive()) {
if (!this->getActiveDragGesture().has_value()) {
return;
}

this->emulateSwipeEnd(0, false);
switch (this->getActiveDragGesture()->type) {
case DragGestureType::SWIPE:
this->emulateSwipeEnd(0, false);
return;
case DragGestureType::HOLD:
// TODO:
// this->handleGestureBind("")
return;
}
}

void GestureManager::dragGestureUpdate(const wf::touch::gesture_event_t& ev) {
Expand Down
17 changes: 9 additions & 8 deletions src/gestures/Gestures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <functional>
#include <glm/glm.hpp>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <wayfire/touch/touch.hpp>
Expand Down Expand Up @@ -209,7 +210,7 @@ wf::touch::action_status_t OnCompleteAction::update_state(const wf::touch::gestu
void IGestureManager::updateGestures(const wf::touch::gesture_event_t& ev) {
if (m_sGestureState.fingers.size() == 1 && ev.type == wf::touch::EVENT_TYPE_TOUCH_DOWN) {
this->inhibitTouchEvents = false;
this->dragGestureActive = false;
this->activeDragGesture = std::nullopt;
}
for (const auto& gesture : m_vGestures) {
if (m_sGestureState.fingers.size() == 1 && ev.type == wf::touch::EVENT_TYPE_TOUCH_DOWN) {
Expand All @@ -236,7 +237,7 @@ bool IGestureManager::onTouchDown(const wf::touch::gesture_event_t& ev) {
this->m_sGestureState.update(ev);
this->updateGestures(ev);

if (this->dragGestureActive) {
if (this->activeDragGesture.has_value()) {
this->dragGestureUpdate(ev);
}

Expand All @@ -247,7 +248,7 @@ bool IGestureManager::onTouchUp(const wf::touch::gesture_event_t& ev) {
this->updateGestures(ev);
this->m_sGestureState.update(ev);

if (this->dragGestureActive) {
if (this->activeDragGesture.has_value()) {
this->dragGestureUpdate(ev);
}

Expand All @@ -258,7 +259,7 @@ bool IGestureManager::onTouchMove(const wf::touch::gesture_event_t& ev) {
this->updateGestures(ev);
this->m_sGestureState.update(ev);

if (this->dragGestureActive) {
if (this->activeDragGesture.has_value()) {
this->dragGestureUpdate(ev);
}

Expand Down Expand Up @@ -309,13 +310,13 @@ void IGestureManager::addMultiFingerGesture(const float* sensitivity, const int6
auto swipe_ptr = swipe.get();

auto swipe_and_emit = std::make_unique<OnCompleteAction>(std::move(swipe), [=, this]() {
if (this->dragGestureActive) {
if (this->activeDragGesture.has_value()) {
return;
}
const auto gesture = DragGesture{DragGestureType::SWIPE, swipe_ptr->target_direction,
static_cast<int>(this->m_sGestureState.fingers.size())};

this->dragGestureActive = this->handleDragGesture(gesture);
this->activeDragGesture = this->handleDragGesture(gesture) ? std::optional(gesture) : std::nullopt;
});

auto swipe_liftoff = std::make_unique<LiftoffAction>();
Expand Down Expand Up @@ -355,13 +356,13 @@ void IGestureManager::addMultiFingerTap(const float* sensitivity, const int64_t*
void IGestureManager::addLongPress(const float* sensitivity, const int64_t* delay) {
auto long_press_and_emit = std::make_unique<OnCompleteAction>(
std::make_unique<LongPress>(SWIPE_INCORRECT_DRAG_TOLERANCE, sensitivity, delay), [this]() {
if (this->dragGestureActive) {
if (this->activeDragGesture.has_value()) {
return;
}
const auto gesture =
DragGesture{DragGestureType::HOLD, 0, static_cast<int>(this->m_sGestureState.fingers.size())};

this->dragGestureActive = this->handleDragGesture(gesture);
this->activeDragGesture = this->handleDragGesture(gesture) ? std::optional(gesture) : std::nullopt;
});

auto touch_up_or_down = std::make_unique<TouchUpOrDownAction>();
Expand Down
6 changes: 3 additions & 3 deletions src/gestures/Gestures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ class IGestureManager {
void addLongPress(const float* sensitivity, const int64_t* delay);
void addEdgeSwipeGesture(const float* sensitivity, const int64_t* timeout);

bool dragGestureIsActive() const {
return dragGestureActive;
std::optional<DragGesture> getActiveDragGesture() const {
return activeDragGesture;
}

// indicates whether events should be blocked from forwarding to client
Expand Down Expand Up @@ -247,7 +247,7 @@ class IGestureManager {

private:
bool inhibitTouchEvents;
bool dragGestureActive;
std::optional<DragGesture> activeDragGesture;

// this function is called when needed to send "cancel touch" events to
// client windows/surfaces
Expand Down
2 changes: 1 addition & 1 deletion src/gestures/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void ProcessEvents(CMockGestureManager& gm, ExpectResult expect,
CHECK(gm.triggered);
break;
case ExpectResultType::DRAG_TRIGGERED:
CHECK(gm.dragGestureIsActive());
CHECK(gm.getActiveDragGesture().has_value());
break;
case ExpectResultType::CANCELLED:
CHECK(gm.cancelled);
Expand Down

0 comments on commit f9707f6

Please sign in to comment.