]> Sergey Matveev's repositories - nnn.git/commitdiff
Fix #257: fix the logic for determining which file was clicked.
authorBruce Hill <bruce@bruce-hill.com>
Thu, 9 May 2019 02:37:37 +0000 (19:37 -0700)
committerArun Prakash Jana <engineerarun@gmail.com>
Wed, 22 May 2019 03:14:19 +0000 (08:44 +0530)
src/nnn.c

index 4615db5035825df105ba76d8d07bccd22b969483..3d66b1ddb6a2d848466bd136b6c1277233b6693e 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
 #define LEN(x) (sizeof(x) / sizeof(*(x)))
 #undef MIN
 #define MIN(x, y) ((x) < (y) ? (x) : (y))
+#undef MAX
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
 #define ISODD(x) ((x) & 1)
 #define ISBLANK(x) ((x) == ' ' || (x) == '\t')
 #define TOUPPER(ch) \
@@ -3155,19 +3157,20 @@ nochange:
 
                        // Handle clicking on a file:
                        if (2 <= event.y && event.y < xlines - 2) {
-                               r = event.y - 2;
+                               // Get index of the first file listed on-screen:
+                               r = MAX(0, MIN(cur-((xlines-4)>>1), ndents-(xlines-4)));
+                               // Add the mouse click position to get the clicked file:
+                               r += event.y - 2;
 
                                if (r >= ndents)
                                        goto nochange;
 
-                               if (ndents > (xlines - 4) && cur >= ((xlines - 4) >> 1))
-                                       cur -= ((xlines - 4) >> 1) - r;
-                               else
-                                       cur = r;
+                               cur = r;
 
                                // Single click just selects, double click also opens
                                if (event.bstate != BUTTON1_DOUBLE_CLICKED)
                                        break;
+                               // fallthrough to select the file
                        } else
                                goto nochange; // fallthrough
                case SEL_NAV_IN: // fallthrough