From 11182b36822c56678f8457a911956a0741613ca0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 29 Nov 2016 10:00:49 +0530 Subject: [PATCH] Implement wheel scrolling --- kitty/char_grid.py | 3 ++- kitty/config.py | 1 + kitty/kitty.conf | 3 +++ kitty/tabs.py | 4 ++-- kitty/window.py | 34 ++++++++++++++++++++++++---------- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/kitty/char_grid.py b/kitty/char_grid.py index 9b20afeeb..88344b531 100644 --- a/kitty/char_grid.py +++ b/kitty/char_grid.py @@ -228,7 +228,8 @@ class CharGrid: self.screen.mark_as_dirty() def scroll(self, amt, upwards=True): - amt = {'line': 1, 'page': self.screen.lines - 1, 'full': self.screen.historybuf.count}[amt] + if not isinstance(amt, int): + amt = {'line': 1, 'page': self.screen.lines - 1, 'full': self.screen.historybuf.count}[amt] if not upwards: amt *= -1 y = max(0, min(self.scrolled_by + amt, self.screen.historybuf.count)) diff --git a/kitty/config.py b/kitty/config.py index b149b7ebe..e3056de28 100644 --- a/kitty/config.py +++ b/kitty/config.py @@ -45,6 +45,7 @@ type_map = { 'cursor_opacity': to_opacity, 'repaint_delay': int, 'window_border_width': float, + 'wheel_scroll_multiplier': float, } for name in 'foreground background cursor active_border_color inactive_border_color selection_foreground selection_background'.split(): diff --git a/kitty/kitty.conf b/kitty/kitty.conf index 37b65b3ae..7621a52bf 100644 --- a/kitty/kitty.conf +++ b/kitty/kitty.conf @@ -30,6 +30,9 @@ font_size 11.0 # Number of lines of history to keep in memory for scrolling back scrollback_lines 2000 +# Wheel scroll multiplier (modify the amount scrolled by the mouse wheel) +wheel_scroll_multiplier 5.0 + # Delay (in milliseconds) between screen updates. Decreasing it, increases fps # at the cost of more CPU usage. The default value yields ~50fps which is more # that sufficient for most uses. diff --git a/kitty/tabs.py b/kitty/tabs.py index 8c9c81dea..9f58e0ce2 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -332,12 +332,12 @@ class TabManager(Thread): def on_mouse_move(self, window, xpos, ypos): w = self.window_for_pos(*glfw.glfwGetCursorPos(window)) if w is not None: - w.on_mouse_move(xpos, ypos) + w.on_mouse_move(window, xpos, ypos) def on_mouse_scroll(self, window, x, y): w = self.window_for_pos(*glfw.glfwGetCursorPos(window)) if w is not None: - w.on_mouse_scroll(x, y) + w.on_mouse_scroll(window, x, y) # GUI thread API {{{ def render(self): diff --git a/kitty/window.py b/kitty/window.py index ecbebfea2..fd0ab62c7 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -10,7 +10,7 @@ from functools import partial import glfw import glfw_constants from .char_grid import CharGrid -from .constants import wakeup, tab_manager, appname, WindowGeometry, queue_action +from .constants import wakeup, tab_manager, appname, WindowGeometry from .fast_data_types import ( BRACKETED_PASTE_START, BRACKETED_PASTE_END, Screen, read_bytes_dump, read_bytes ) @@ -124,12 +124,20 @@ class Window: self.paste_from_selection() return - def on_mouse_move(self, x, y): + def on_mouse_move(self, window, x, y): if self.char_grid.current_selection.in_progress: self.char_grid.update_drag(None, max(0, x - self.geometry.left), max(0, y - self.geometry.top)) - def on_mouse_scroll(self, x, y): - pass + def on_mouse_scroll(self, window, x, y): + ignore_mouse_mode = ( + glfw.glfwGetKey(window, glfw_constants.GLFW_KEY_LEFT_SHIFT) == glfw_constants.GLFW_PRESS or + glfw.glfwGetKey(window, glfw_constants.GLFW_KEY_RIGHT_SHIFT) == glfw_constants.GLFW_PRESS or + not self.screen.mouse_button_tracking_enabled()) + if ignore_mouse_mode: + s = int(round(y * self.opts.wheel_scroll_multiplier)) + if abs(s) > 0: + self.char_grid.scroll(abs(s), s > 0) + glfw.glfwPostEmptyEvent() # actions {{{ @@ -150,22 +158,28 @@ class Window: self.paste(text) def scroll_line_up(self): - queue_action(self.char_grid.scroll, 'line', True) + self.char_grid.scroll('line', True) + glfw.glfwPostEmptyEvent() def scroll_line_down(self): - queue_action(self.char_grid.scroll, 'line', False) + self.char_grid.scroll('line', False) + glfw.glfwPostEmptyEvent() def scroll_page_up(self): - queue_action(self.char_grid.scroll, 'page', True) + self.char_grid.scroll('page', True) + glfw.glfwPostEmptyEvent() def scroll_page_down(self): - queue_action(self.char_grid.scroll, 'page', False) + self.char_grid.scroll('page', False) + glfw.glfwPostEmptyEvent() def scroll_home(self): - queue_action(self.char_grid.scroll, 'full', True) + self.char_grid.scroll('full', True) + glfw.glfwPostEmptyEvent() def scroll_end(self): - queue_action(self.char_grid.scroll, 'full', False) + self.char_grid.scroll('full', False) + glfw.glfwPostEmptyEvent() # }}} def dump_commands(self, *a):