You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/blog/posts/smooth-scrolling.md
+18-7Lines changed: 18 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,27 +31,38 @@ It isn't granular enough to know where the pointer is *within* a cell.
31
31
32
32
Until recently terminal apps couldn't do any better.
33
33
More granular mouse reporting is possible in the terminal; write the required escape sequence and mouse coordinates are reported in pixels rather than cells.
34
+
34
35
So why haven't TUIs been using this?
35
36
36
-
The problem is that we can't translate between pixel coordinates and cell coordinates without first knowing how many pixels are in a cell.
37
-
And in order to know that, we need to know the width and height of the terminal in *pixels*.
38
-
Unfortunately, that standard way to get the terminal size reports just cells.
37
+
The problem is that pixel coordinates are pretty much useless in TUIs unless we have some way of translating between pixel and cell coordinates.
38
+
Without that, we can never know which cell the user clicked on.
39
+
40
+
It's a trivial calculation, but we are missing a vital piece of information; the size of the terminal window in pixels.
41
+
If we had that, we could divide the pixel dimensions by the cell dimensions to calculate the pixels per cell.
42
+
Divide the pixel coordinates by *pixels per cell* and we have cell coordinates.
43
+
44
+
But the terminal reports its size in cells, and *not* pixels.
45
+
So we can't use granular mouse coordinates.
46
+
47
+
!!! question "What did people use pixel coordinate for?"
48
+
49
+
This does make we wonder what pixel reporting was ever used for in terminals.
50
+
Ping me on Discord if you know!
51
+
39
52
40
-
At least they didn't before[this extension](https://gist.github.com/rockorager/e695fb2924d36b2bcf1fff4a3704bd83) which reports the size of the terminal in cell *and* pixel coordinates.
53
+
At least we couldn't until[this recent extension](https://gist.github.com/rockorager/e695fb2924d36b2bcf1fff4a3704bd83) which reports the size of the terminal in cell *and* pixel coordinates.
41
54
Once we have both the mouse coordinates in pixels and the dimensions of the terminal in pixels, we can implement much smoother scrolling.
42
55
43
56
Let's see how this looks.
44
57
45
-
On the right we have smooth scrolling enabled, on the left is the default non-smooth scrolling:
58
+
On the left we have the default scrolling, on the right, Textual is using granular mouse coordinates.
|||
51
64
52
65
Notice how much smoother the motion of the table is, now that it tracks the mouse cursor more accurately.
53
-
If you move the scrollbar quickly, you may not notice the difference.
54
-
But if you move slowly like you are searching for something, it is a respectable quality of life improvement.
55
66
56
67
If you have one of the terminals which support this feature[^2], and at least [Textual](https://github.com/textualize/textual/) 2.0.0 you will be able to see this in action.
0 commit comments