Skip to content

Commit

Permalink
Merge pull request #136 from horriblename/refactor/drag-and-complete-…
Browse files Browse the repository at this point in the history
…gestures

refactor: extract common function
  • Loading branch information
horriblename authored Jun 8, 2024
2 parents aff5c4d + 51775c1 commit 03e752c
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/GestureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ GestureManager::~GestureManager() {
wl_event_source_remove(this->long_press_timer);
}

bool GestureManager::handleCompletedGesture(const CompletedGesture& gev) {
bool GestureManager::handleCompletedGesture(const CompletedGestureEvent& gev) {
return this->handleGestureBind(gev.to_string(), false);
}

bool GestureManager::handleDragGesture(const DragGesture& gev) {
bool GestureManager::handleDragGesture(const DragGestureEvent& gev) {
static auto const WORKSPACE_SWIPE_FINGERS =
(Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:workspace_swipe_fingers")
->getDataStaticPtr();
Expand Down Expand Up @@ -224,7 +224,7 @@ void GestureManager::dragGestureUpdate(const wf::touch::gesture_event_t& ev) {
}
}

void GestureManager::handleDragGestureEnd(const DragGesture& gev) {
void GestureManager::handleDragGestureEnd(const DragGestureEvent& gev) {
Debug::log(LOG, "[hyprgrass] Drag gesture ended: {}", gev.to_string());
switch (gev.type) {
case DragGestureType::SWIPE:
Expand Down
6 changes: 3 additions & 3 deletions src/GestureManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class GestureManager : public IGestureManager {

protected:
SMonitorArea getMonitorArea() const override;
bool handleCompletedGesture(const CompletedGesture& gev) override;
bool handleCompletedGesture(const CompletedGestureEvent& gev) override;
void handleCancelledGesture() override;

private:
Expand All @@ -59,9 +59,9 @@ class GestureManager : public IGestureManager {
Vector2D pixelPositionToPercentagePosition(wf::touch::point_t) const;
bool handleWorkspaceSwipe(const GestureDirection direction);

bool handleDragGesture(const DragGesture& gev) override;
bool handleDragGesture(const DragGestureEvent& gev) override;
void dragGestureUpdate(const wf::touch::gesture_event_t&) override;
void handleDragGestureEnd(const DragGesture& gev) override;
void handleDragGestureEnd(const DragGestureEvent& gev) override;

void updateLongPressTimer(uint32_t current_time, uint32_t delay) override;
void stopLongPressTimer() override;
Expand Down
2 changes: 1 addition & 1 deletion src/gestures/CompletedGesture.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "CompletedGesture.hpp"

std::string CompletedGesture::to_string() const {
std::string CompletedGestureEvent::to_string() const {
switch (type) {
case CompletedGestureType::EDGE_SWIPE:
return "edge:" + stringifyDirection(this->edge_origin) + ":" + stringifyDirection(this->direction);
Expand Down
2 changes: 1 addition & 1 deletion src/gestures/CompletedGesture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ enum class CompletedGestureType {
// PINCH,
};

struct CompletedGesture {
struct CompletedGestureEvent {
CompletedGestureType type;
GestureDirection direction;
int finger_count;
Expand Down
2 changes: 1 addition & 1 deletion src/gestures/DragGesture.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "DragGesture.hpp"

std::string DragGesture::to_string() const {
std::string DragGestureEvent::to_string() const {
switch (type) {
case DragGestureType::LONG_PRESS:
return "longpress:" + std::to_string(finger_count);
Expand Down
2 changes: 1 addition & 1 deletion src/gestures/DragGesture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ enum class DragGestureType {
EDGE_SWIPE,
};

struct DragGesture {
struct DragGestureEvent {
DragGestureType type;
GestureDirection direction;
int finger_count;
Expand Down
79 changes: 43 additions & 36 deletions src/gestures/Gestures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void IGestureManager::cancelTouchEventsOnAllWindows() {
this->sendCancelEventsToWindows();
}
}
bool IGestureManager::emitCompletedGesture(const CompletedGesture& gev) {
bool IGestureManager::emitCompletedGesture(const CompletedGestureEvent& gev) {
bool handled = this->handleCompletedGesture(gev);
if (handled) {
this->stopLongPressTimer();
Expand All @@ -37,7 +37,7 @@ bool IGestureManager::emitCompletedGesture(const CompletedGesture& gev) {
return handled;
}

bool IGestureManager::emitDragGesture(const DragGesture& gev) {
bool IGestureManager::emitDragGesture(const DragGestureEvent& gev) {
bool handled = this->handleDragGesture(gev);
if (handled) {
this->stopLongPressTimer();
Expand All @@ -46,6 +46,16 @@ bool IGestureManager::emitDragGesture(const DragGesture& gev) {
return handled;
}

bool IGestureManager::emitDragGestureEnd(const DragGestureEvent& gev) {
if (this->activeDragGesture.has_value() && this->activeDragGesture->type == gev.type) {

this->handleDragGestureEnd(gev);
this->activeDragGesture = std::nullopt;
return true;
}
return false;
}

// @return whether or not to inhibit further actions
bool IGestureManager::onTouchDown(const wf::touch::gesture_event_t& ev) {
// NOTE @m_sGestureState is used in gesture-completed callbacks
Expand Down Expand Up @@ -125,8 +135,8 @@ void IGestureManager::addMultiFingerGesture(const float* sensitivity, const int6
if (this->activeDragGesture.has_value()) {
return;
}
const auto gesture = DragGesture{DragGestureType::SWIPE, swipe_ptr->target_direction,
static_cast<int>(this->m_sGestureState.fingers.size())};
const auto gesture = DragGestureEvent{DragGestureType::SWIPE, swipe_ptr->target_direction,
static_cast<int>(this->m_sGestureState.fingers.size())};

this->activeDragGesture = this->emitDragGesture(gesture) ? std::optional(gesture) : std::nullopt;
});
Expand All @@ -140,15 +150,13 @@ void IGestureManager::addMultiFingerGesture(const float* sensitivity, const int6
swipe_actions.emplace_back(std::move(swipe_liftoff));

auto ack = [swipe_ptr, this]() {
if (this->activeDragGesture.has_value() && this->activeDragGesture->type == DragGestureType::SWIPE) {
const auto gesture =
DragGesture{DragGestureType::SWIPE, 0, static_cast<int>(this->m_sGestureState.fingers.size())};

this->handleDragGestureEnd(gesture);
this->activeDragGesture = std::nullopt;
const auto drag =
DragGestureEvent{DragGestureType::SWIPE, 0, static_cast<int>(this->m_sGestureState.fingers.size())};
if (this->emitDragGestureEnd(drag)) {
return;
} else {
const auto gesture = CompletedGesture{CompletedGestureType::SWIPE, swipe_ptr->target_direction,
static_cast<int>(this->m_sGestureState.fingers.size())};
const auto gesture = CompletedGestureEvent{CompletedGestureType::SWIPE, swipe_ptr->target_direction,
static_cast<int>(this->m_sGestureState.fingers.size())};

this->emitCompletedGesture(gesture);
}
Expand All @@ -166,7 +174,7 @@ void IGestureManager::addMultiFingerTap(const float* sensitivity, const int64_t*

auto ack = [this]() {
const auto gesture =
CompletedGesture{CompletedGestureType::TAP, 0, static_cast<int>(this->m_sGestureState.fingers.size())};
CompletedGestureEvent{CompletedGestureType::TAP, 0, static_cast<int>(this->m_sGestureState.fingers.size())};
if (this->emitCompletedGesture(gesture)) {
this->cancelTouchEventsOnAllWindows();
}
Expand All @@ -185,8 +193,8 @@ void IGestureManager::addLongPress(const float* sensitivity, const int64_t* dela
if (this->activeDragGesture.has_value()) {
return;
}
const auto gesture =
DragGesture{DragGestureType::LONG_PRESS, 0, static_cast<int>(this->m_sGestureState.fingers.size())};
const auto gesture = DragGestureEvent{DragGestureType::LONG_PRESS, 0,
static_cast<int>(this->m_sGestureState.fingers.size())};

if (this->emitDragGesture(gesture)) {
this->activeDragGesture = std::optional(gesture);
Expand All @@ -201,15 +209,13 @@ void IGestureManager::addLongPress(const float* sensitivity, const int64_t* dela
long_press_actions.emplace_back(std::move(touch_up_or_down));

auto ack = [this]() {
if (this->activeDragGesture.has_value() && this->activeDragGesture->type == DragGestureType::LONG_PRESS) {
const auto gesture =
DragGesture{DragGestureType::LONG_PRESS, 0, static_cast<int>(this->m_sGestureState.fingers.size())};

this->handleDragGestureEnd(gesture);
this->activeDragGesture = std::nullopt;
const auto drag =
DragGestureEvent{DragGestureType::LONG_PRESS, 0, static_cast<int>(this->m_sGestureState.fingers.size())};
if (this->emitDragGestureEnd(drag)) {
return;
} else {
const auto gesture = CompletedGesture{CompletedGestureType::LONG_PRESS, 0,
static_cast<int>(this->m_sGestureState.fingers.size())};
const auto gesture = CompletedGestureEvent{CompletedGestureType::LONG_PRESS, 0,
static_cast<int>(this->m_sGestureState.fingers.size())};

this->emitCompletedGesture(gesture);
};
Expand All @@ -232,7 +238,7 @@ void IGestureManager::addEdgeSwipeGesture(const float* sensitivity, const int64_
return;
}
auto direction = edge_ptr->target_direction;
auto gesture = DragGesture{DragGestureType::EDGE_SWIPE, direction, edge_ptr->finger_count, origin_edges};
auto gesture = DragGestureEvent{DragGestureType::EDGE_SWIPE, direction, edge_ptr->finger_count, origin_edges};
if (this->emitDragGesture(gesture)) {
this->activeDragGesture = std::optional(gesture);
this->cancelTouchEventsOnAllWindows();
Expand All @@ -254,25 +260,26 @@ void IGestureManager::addEdgeSwipeGesture(const float* sensitivity, const int64_

auto ack = [edge_ptr, this]() {
auto origin_edges = find_swipe_edges(m_sGestureState.get_center().origin);
auto direction = edge_ptr->target_direction;
auto dragEvent = DragGestureEvent{
.type = DragGestureType::EDGE_SWIPE,
.direction = direction,
.finger_count = edge_ptr->finger_count,
.edge_origin = origin_edges,
};

if (this->activeDragGesture.has_value() && this->activeDragGesture->type == DragGestureType::EDGE_SWIPE) {
const auto gesture = DragGesture{.type = DragGestureType::EDGE_SWIPE,
.direction = edge_ptr->target_direction,
.finger_count = edge_ptr->finger_count,
.edge_origin = origin_edges};

this->handleDragGestureEnd(gesture);
this->activeDragGesture = std::nullopt;
if (this->emitDragGestureEnd(dragEvent)) {
return;
}

if (origin_edges == 0) {
return;
}
auto direction = edge_ptr->target_direction;
auto gesture =
CompletedGesture{CompletedGestureType::EDGE_SWIPE, direction, edge_ptr->finger_count, origin_edges};
this->emitCompletedGesture(gesture);

auto event =
CompletedGestureEvent{CompletedGestureType::EDGE_SWIPE, direction, edge_ptr->finger_count, origin_edges};

this->emitCompletedGesture(event);
};
auto cancel = [this]() { this->handleCancelledGesture(); };

Expand Down
15 changes: 8 additions & 7 deletions src/gestures/Gestures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class IGestureManager {
void addLongPress(const float* sensitivity, const int64_t* delay);
void addEdgeSwipeGesture(const float* sensitivity, const int64_t* timeout);

std::optional<DragGesture> getActiveDragGesture() const {
std::optional<DragGestureEvent> getActiveDragGesture() const {
return activeDragGesture;
}

Expand All @@ -60,16 +60,16 @@ class IGestureManager {
virtual SMonitorArea getMonitorArea() const = 0;

// handles gesture events and returns whether or not the event is used.
virtual bool handleCompletedGesture(const CompletedGesture& gev) = 0;
virtual bool handleCompletedGesture(const CompletedGestureEvent& gev) = 0;

// called at the start of drag evetns and returns whether or not the event is used.
virtual bool handleDragGesture(const DragGesture& gev) = 0;
virtual bool handleDragGesture(const DragGestureEvent& gev) = 0;

// called on every touch event while a drag gesture is active
virtual void dragGestureUpdate(const wf::touch::gesture_event_t&) = 0;

// called at the end of a drag event
virtual void handleDragGestureEnd(const DragGesture& gev) = 0;
virtual void handleDragGestureEnd(const DragGestureEvent& gev) = 0;

// this function should cleanup after drag gestures
virtual void handleCancelledGesture() = 0;
Expand All @@ -79,14 +79,15 @@ class IGestureManager {

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

// this function is called when needed to send "cancel touch" events to
// client windows/surfaces
virtual void sendCancelEventsToWindows() = 0;

bool emitCompletedGesture(const CompletedGesture& gev);
bool emitDragGesture(const DragGesture& gev);
bool emitCompletedGesture(const CompletedGestureEvent& gev);
bool emitDragGesture(const DragGestureEvent& gev);
bool emitDragGestureEnd(const DragGestureEvent& gev);

void updateGestures(const wf::touch::gesture_event_t&);
void cancelTouchEventsOnAllWindows();
Expand Down
6 changes: 3 additions & 3 deletions src/gestures/test/MockGestureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

#define CONFIG_SENSITIVITY 1.0

bool CMockGestureManager::handleCompletedGesture(const CompletedGesture& gev) {
bool CMockGestureManager::handleCompletedGesture(const CompletedGestureEvent& gev) {
std::cout << "gesture triggered: " << gev.to_string() << "\n";
this->triggered = true;
return true;
}

bool CMockGestureManager::handleDragGesture(const DragGesture& gev) {
bool CMockGestureManager::handleDragGesture(const DragGestureEvent& gev) {
std::cout << "drag started: " << gev.to_string() << "\n";
return this->handlesDragEvents;
}
Expand All @@ -20,7 +20,7 @@ void CMockGestureManager::dragGestureUpdate(const wf::touch::gesture_event_t& ge
std::cout << "drag update" << std::endl;
}

void CMockGestureManager::handleDragGestureEnd(const DragGesture& gev) {
void CMockGestureManager::handleDragGestureEnd(const DragGestureEvent& gev) {
std::cout << "drag end: " << gev.to_string() << "\n";
this->dragEnded = true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/gestures/test/MockGestureManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class CMockGestureManager final : public IGestureManager {
return {pos->x, pos->y};
}

bool handleCompletedGesture(const CompletedGesture& gev) override;
bool handleDragGesture(const DragGesture& gev) override;
bool handleCompletedGesture(const CompletedGestureEvent& gev) override;
bool handleDragGesture(const DragGestureEvent& gev) override;
void dragGestureUpdate(const wf::touch::gesture_event_t&) override;
void handleDragGestureEnd(const DragGesture& gev) override;
void handleDragGestureEnd(const DragGestureEvent& gev) override;
void handleCancelledGesture() override;

void updateLongPressTimer(uint32_t current_time, uint32_t delay) override {}
Expand Down
16 changes: 15 additions & 1 deletion src/gestures/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct ExpectResult {
float progress = 0.0;

// which of the m_vGestures to use
// usually the first (0)
// usually the first (0) in tests
int gesture_index = 0;
};

Expand Down Expand Up @@ -336,3 +336,17 @@ TEST_CASE("Edge Swipe Drag: begin") {
const ExpectResult expected_result = {ExpectResultType::DRAG_TRIGGERED, 1.0};
ProcessEvents(gm, expected_result, events);
}

TEST_CASE("Edge Swipe Drag: emits drag end event") {
auto gm = CMockGestureManager::newDragHandler();
gm.addEdgeSwipeGesture(&SENSITIVITY, &LONG_PRESS_DELAY);

const std::vector<TouchEvent> events{
{wf::touch::EVENT_TYPE_TOUCH_DOWN, 100, 0, {5, 300}}, {wf::touch::EVENT_TYPE_MOTION, 150, 0, {250, 300}},
{wf::touch::EVENT_TYPE_MOTION, 200, 0, {455, 300}}, {wf::touch::EVENT_TYPE_MOTION, 250, 0, {600, 300}},
{wf::touch::EVENT_TYPE_TOUCH_UP, 300, 0, {700, 400}},
};

const ExpectResult expected_result = {ExpectResultType::DRAG_ENDED, 1.0};
ProcessEvents(gm, expected_result, events);
}

0 comments on commit 03e752c

Please sign in to comment.