From 2c038d197729e790296a45e267c3adf3f11b94d3 Mon Sep 17 00:00:00 2001 From: Patrick Hilhorst Date: Mon, 17 Jun 2019 20:09:53 +0200 Subject: [PATCH 1/4] AModule::getScrollDir: move `dir` inside the only scope it is relevant --- src/AModule.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/AModule.cpp b/src/AModule.cpp index c8976623..c35d012d 100644 --- a/src/AModule.cpp +++ b/src/AModule.cpp @@ -53,16 +53,16 @@ bool AModule::handleToggle(GdkEventButton* const& e) { } AModule::SCROLL_DIR AModule::getScrollDir(GdkEventScroll* e) { - SCROLL_DIR dir{SCROLL_DIR::NONE}; if (e->direction == GDK_SCROLL_UP) { - dir = SCROLL_DIR::UP; + return SCROLL_DIR::UP; } else if (e->direction == GDK_SCROLL_DOWN) { - dir = SCROLL_DIR::DOWN; + return SCROLL_DIR::DOWN; } else if (e->direction == GDK_SCROLL_LEFT) { - dir = SCROLL_DIR::LEFT; + return SCROLL_DIR::LEFT; } else if (e->direction == GDK_SCROLL_RIGHT) { - dir = SCROLL_DIR::RIGHT; + return SCROLL_DIR::RIGHT; } else if (e->direction == GDK_SCROLL_SMOOTH) { + SCROLL_DIR dir{SCROLL_DIR::NONE}; gdouble delta_x, delta_y; gdk_event_get_scroll_deltas(reinterpret_cast(e), &delta_x, &delta_y); distance_scrolled_y_ += delta_y; @@ -88,8 +88,8 @@ AModule::SCROLL_DIR AModule::getScrollDir(GdkEventScroll* e) { } else if (dir == SCROLL_DIR::LEFT || dir == SCROLL_DIR::RIGHT) { distance_scrolled_x_ = 0; } + return dir; } - return dir; } bool AModule::handleScroll(GdkEventScroll* e) { From 7c85aec8e0606133b8d0d8331be970ed3e199858 Mon Sep 17 00:00:00 2001 From: Patrick Hilhorst Date: Mon, 17 Jun 2019 20:29:37 +0200 Subject: [PATCH 2/4] AModule::getScrollDir: get deltas in a more C++ way --- src/AModule.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/AModule.cpp b/src/AModule.cpp index c35d012d..2d5257bc 100644 --- a/src/AModule.cpp +++ b/src/AModule.cpp @@ -63,10 +63,9 @@ AModule::SCROLL_DIR AModule::getScrollDir(GdkEventScroll* e) { return SCROLL_DIR::RIGHT; } else if (e->direction == GDK_SCROLL_SMOOTH) { SCROLL_DIR dir{SCROLL_DIR::NONE}; - gdouble delta_x, delta_y; - gdk_event_get_scroll_deltas(reinterpret_cast(e), &delta_x, &delta_y); - distance_scrolled_y_ += delta_y; - distance_scrolled_x_ += delta_x; + + distance_scrolled_y_ += e->delta_y; + distance_scrolled_x_ += e->delta_x; gdouble threshold = 0; if (config_["smooth-scrolling-threshold"].isNumeric()) { From 86d6668ed457037d6fd23a114a88825de5eafd8a Mon Sep 17 00:00:00 2001 From: Patrick Hilhorst Date: Mon, 17 Jun 2019 20:37:37 +0200 Subject: [PATCH 3/4] AModule::getScrollDir: convert if-else chain into switch statement --- src/AModule.cpp | 64 ++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/AModule.cpp b/src/AModule.cpp index 2d5257bc..394f3547 100644 --- a/src/AModule.cpp +++ b/src/AModule.cpp @@ -53,41 +53,41 @@ bool AModule::handleToggle(GdkEventButton* const& e) { } AModule::SCROLL_DIR AModule::getScrollDir(GdkEventScroll* e) { - if (e->direction == GDK_SCROLL_UP) { - return SCROLL_DIR::UP; - } else if (e->direction == GDK_SCROLL_DOWN) { - return SCROLL_DIR::DOWN; - } else if (e->direction == GDK_SCROLL_LEFT) { - return SCROLL_DIR::LEFT; - } else if (e->direction == GDK_SCROLL_RIGHT) { - return SCROLL_DIR::RIGHT; - } else if (e->direction == GDK_SCROLL_SMOOTH) { - SCROLL_DIR dir{SCROLL_DIR::NONE}; + switch (e -> direction) { + case GDK_SCROLL_UP: return SCROLL_DIR::UP; + case GDK_SCROLL_DOWN: return SCROLL_DIR::DOWN; + case GDK_SCROLL_LEFT: return SCROLL_DIR::LEFT; + case GDK_SCROLL_RIGHT: return SCROLL_DIR::RIGHT; + case GDK_SCROLL_SMOOTH: { + SCROLL_DIR dir{SCROLL_DIR::NONE}; - distance_scrolled_y_ += e->delta_y; - distance_scrolled_x_ += e->delta_x; + distance_scrolled_y_ += e->delta_y; + distance_scrolled_x_ += e->delta_x; - gdouble threshold = 0; - if (config_["smooth-scrolling-threshold"].isNumeric()) { - threshold = config_["smooth-scrolling-threshold"].asDouble(); + gdouble threshold = 0; + if (config_["smooth-scrolling-threshold"].isNumeric()) { + threshold = config_["smooth-scrolling-threshold"].asDouble(); + } + + if (distance_scrolled_y_ < -threshold) { + dir = SCROLL_DIR::UP; + } else if (distance_scrolled_y_ > threshold) { + dir = SCROLL_DIR::DOWN; + } else if (distance_scrolled_x_ > threshold) { + dir = SCROLL_DIR::RIGHT; + } else if (distance_scrolled_x_ < -threshold) { + dir = SCROLL_DIR::LEFT; + } + + if (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::DOWN) { + distance_scrolled_y_ = 0; + } else if (dir == SCROLL_DIR::LEFT || dir == SCROLL_DIR::RIGHT) { + distance_scrolled_x_ = 0; + } + return dir; } - - if (distance_scrolled_y_ < -threshold) { - dir = SCROLL_DIR::UP; - } else if (distance_scrolled_y_ > threshold) { - dir = SCROLL_DIR::DOWN; - } else if (distance_scrolled_x_ > threshold) { - dir = SCROLL_DIR::RIGHT; - } else if (distance_scrolled_x_ < -threshold) { - dir = SCROLL_DIR::LEFT; - } - - if (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::DOWN) { - distance_scrolled_y_ = 0; - } else if (dir == SCROLL_DIR::LEFT || dir == SCROLL_DIR::RIGHT) { - distance_scrolled_x_ = 0; - } - return dir; + // Silence -Wreturn-type: + default: return SCROLL_DIR::NONE; } } From 12b30ca25f8575abf7c3f67c84e6a56ef7330661 Mon Sep 17 00:00:00 2001 From: Patrick Hilhorst Date: Mon, 17 Jun 2019 20:41:18 +0200 Subject: [PATCH 4/4] AModule::getScrollDir: convert reset if-else into switch --- src/AModule.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/AModule.cpp b/src/AModule.cpp index 394f3547..38e97850 100644 --- a/src/AModule.cpp +++ b/src/AModule.cpp @@ -79,11 +79,19 @@ AModule::SCROLL_DIR AModule::getScrollDir(GdkEventScroll* e) { dir = SCROLL_DIR::LEFT; } - if (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::DOWN) { - distance_scrolled_y_ = 0; - } else if (dir == SCROLL_DIR::LEFT || dir == SCROLL_DIR::RIGHT) { - distance_scrolled_x_ = 0; + switch (dir) { + case SCROLL_DIR::UP: + case SCROLL_DIR::DOWN: + distance_scrolled_y_ = 0; + break; + case SCROLL_DIR::LEFT: + case SCROLL_DIR::RIGHT: + distance_scrolled_x_ = 0; + break; + case SCROLL_DIR::NONE: + break; } + return dir; } // Silence -Wreturn-type: