mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-17 13:42:49 +00:00
Cleanup previous PR
- Only call into Python for scroll events when the wheel is mapped for the current modifiers and mode, using a bitmap of wheel mappings built in set_options(), and only when a step completes - Do not swallow the momentum of an unmapped gesture that follows a mapped one - Do not carry a partial step over to a different window or modifiers or across unmapped scrolling - Count wheel steps without relying on the mouse tracking mode semantics of scale_scroll() - Return the result of the mapped actions from on_mouse_event() - Ignore mouse_selection for the wheel, it would never be released - Add integration tests for scrolling with wheel mappings
This commit is contained in:
parent
0ae80efa10
commit
2d57c2201d
8 changed files with 324 additions and 123 deletions
|
|
@ -60,9 +60,9 @@ MOUSE_SELECTION_LINE_FROM_BEGIN: int
|
|||
MOUSE_SELECTION_LINE_FROM_POINT: int
|
||||
MOUSE_SELECTION_UPTO_SURROUNDING_WHITESPACE: int
|
||||
MOUSE_SELECTION_WORD_AND_LINE_FROM_POINT: int
|
||||
MOUSE_SELECTION_MOVE_END: int
|
||||
MOUSE_WHEEL_UP: int
|
||||
MOUSE_WHEEL_DOWN: int
|
||||
MOUSE_SELECTION_MOVE_END: int
|
||||
KITTY_VCS_REV: str
|
||||
NO_CLOSE_REQUESTED: int
|
||||
IMPERATIVE_CLOSE_REQUESTED: int
|
||||
|
|
|
|||
314
kitty/mouse.c
314
kitty/mouse.c
|
|
@ -204,7 +204,7 @@ set_currently_hovered_window(id_type window_id, int modifiers, bool focus_follow
|
|||
}
|
||||
|
||||
// Pseudo buttons so that the scroll wheel can be mapped with mouse_map. For
|
||||
// these, count is the number of wheel steps, which can be zero.
|
||||
// these, count is the number of wheel steps, which is always positive.
|
||||
#define MOUSE_WHEEL_UP (GLFW_MOUSE_BUTTON_LAST + 1)
|
||||
#define MOUSE_WHEEL_DOWN (GLFW_MOUSE_BUTTON_LAST + 2)
|
||||
|
||||
|
|
@ -231,7 +231,7 @@ dispatch_mouse_event(Window *w, int button, int count, int modifiers, bool grabb
|
|||
}
|
||||
if (OPT(debug_keyboard)) {
|
||||
const char *evname = "move";
|
||||
if (button > GLFW_MOUSE_BUTTON_LAST) evname = count ? "press" : "partial press";
|
||||
if (button > GLFW_MOUSE_BUTTON_LAST) evname = "press";
|
||||
else switch (count) {
|
||||
case -3: evname = "doubleclick"; break;
|
||||
case -2: evname = "click"; break;
|
||||
|
|
@ -1241,6 +1241,9 @@ typedef enum MouseSelectionType {
|
|||
|
||||
void
|
||||
mouse_selection(Window *w, int code, int button) {
|
||||
// A selection is ended by releasing the button that started it, which the
|
||||
// wheel pseudo buttons never do
|
||||
if (button < 0 || button > GLFW_MOUSE_BUTTON_LAST) return;
|
||||
zero_at_ptr(&w->drag_source.potential_drag);
|
||||
global_state.active_drag_in_window = w->id;
|
||||
global_state.active_drag_button = button;
|
||||
|
|
@ -1553,14 +1556,94 @@ scale_scroll(
|
|||
// Number of mouse_map wheel steps in a scroll event: one per click for classic
|
||||
// wheels, one per 120 units for high resolution wheels and one per
|
||||
// MAPPED_SCROLL_STEP_PIXELS logical pixels of finger travel for touchpads, so
|
||||
// that the step does not depend on the font size.
|
||||
#define MAPPED_SCROLL_STEP_PIXELS 100
|
||||
// that the step does not depend on the font size. Wheels count clicks, so only
|
||||
// the sign of wheel_scroll_multiplier applies to them. direction is set to the
|
||||
// sign of the scaled offset, so that partial steps have a direction too.
|
||||
#define MAPPED_SCROLL_STEP_PIXELS 100.
|
||||
static int
|
||||
mapped_scroll_steps(GLFWOffsetType offset_type, double offset, double *pending, int *last_dir) {
|
||||
// Wheels count clicks, so only the sign of wheel_scroll_multiplier applies
|
||||
if (offset_type != GLFW_SCROLL_OFFEST_HIGHRES) return scale_scroll(ANY_MODE, offset, offset_type, pending, MAPPED_SCROLL_STEP_PIXELS, last_dir);
|
||||
if (*pending * offset * OPT(touch_scroll_multiplier) < 0) *pending = 0; // direction reversed, start a fresh step
|
||||
return scale_scroll(NO_TRACKING, offset, offset_type, pending, MAPPED_SCROLL_STEP_PIXELS, last_dir);
|
||||
mapped_scroll_steps(GLFWOffsetType offset_type, double offset, double *pending, int *direction) {
|
||||
double step_size = 1;
|
||||
switch (offset_type) {
|
||||
case GLFW_SCROLL_OFFSET_LINES: offset *= copysign(1., OPT(wheel_scroll_multiplier)); break;
|
||||
case GLFW_SCROLL_OFFEST_V120:
|
||||
offset *= copysign(1., OPT(wheel_scroll_multiplier));
|
||||
step_size = 120;
|
||||
break;
|
||||
case GLFW_SCROLL_OFFEST_HIGHRES:
|
||||
offset *= OPT(touch_scroll_multiplier);
|
||||
step_size = MAPPED_SCROLL_STEP_PIXELS;
|
||||
break;
|
||||
}
|
||||
*direction = offset > 0 ? 1 : (offset < 0 ? -1 : 0);
|
||||
if (offset_type == GLFW_SCROLL_OFFSET_LINES) {
|
||||
// apparently on cocoa some mice generate really small offsets when
|
||||
// scrolling slowly, every event is at least one click
|
||||
*pending = 0;
|
||||
const int s = (int)round(offset);
|
||||
return s ? s : *direction;
|
||||
}
|
||||
if (*pending * offset < 0) *pending = 0; // direction reversed, start a fresh step
|
||||
*pending += offset;
|
||||
const int s = (int)(*pending / step_size);
|
||||
*pending -= s * step_size;
|
||||
return s;
|
||||
}
|
||||
|
||||
// Bit i of wheel_mappings[direction][grabbed] is set when the wheel is mapped
|
||||
// with the modifiers i, so that unmapped scrolling never calls into Python.
|
||||
#define WHEEL_MAPPING_MODS_MASK (GLFW_MOD_SHIFT | GLFW_MOD_ALT | GLFW_MOD_CONTROL | GLFW_MOD_SUPER | GLFW_MOD_HYPER | GLFW_MOD_META)
|
||||
static_assert(WHEEL_MAPPING_MODS_MASK < 64, "wheel mapping modifiers must fit in a uint64_t bitmap");
|
||||
|
||||
bool
|
||||
set_wheel_mappings(PyObject *py_opts, Options *opts) {
|
||||
memset(opts->wheel_mappings, 0, sizeof(opts->wheel_mappings));
|
||||
RAII_PyObject(mousemap, PyObject_GetAttrString(py_opts, "mousemap"));
|
||||
if (!mousemap) return false;
|
||||
if (!PyDict_Check(mousemap)) {
|
||||
PyErr_SetString(PyExc_TypeError, "mousemap must be a dict");
|
||||
return false;
|
||||
}
|
||||
PyObject *key, *value;
|
||||
Py_ssize_t pos = 0;
|
||||
while (PyDict_Next(mousemap, &pos, &key, &value)) {
|
||||
// keys are MouseEvent(button, mods, repeat_count, grabbed)
|
||||
if (!PyTuple_Check(key) || PyTuple_GET_SIZE(key) < 4) {
|
||||
PyErr_SetString(PyExc_TypeError, "mousemap keys must be MouseEvent tuples");
|
||||
return false;
|
||||
}
|
||||
const long button = PyLong_AsLong(PyTuple_GET_ITEM(key, 0)), mods = PyLong_AsLong(PyTuple_GET_ITEM(key, 1));
|
||||
if (PyErr_Occurred()) return false;
|
||||
// modifiers outside the mask are stripped from events, so such a mapping can never match
|
||||
if ((button != MOUSE_WHEEL_UP && button != MOUSE_WHEEL_DOWN) || (mods & ~WHEEL_MAPPING_MODS_MASK)) continue;
|
||||
const int grabbed = PyObject_IsTrue(PyTuple_GET_ITEM(key, 3));
|
||||
if (grabbed < 0) return false;
|
||||
opts->wheel_mappings[button == MOUSE_WHEEL_UP ? 0 : 1][grabbed] |= 1ull << mods;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fires the wheel_up/wheel_down mouse_map actions for vertical scrolling.
|
||||
// Returns true if the wheel is mapped for this event, in which case the event
|
||||
// is consumed even if it does not complete a step, so the screen does not
|
||||
// also scroll.
|
||||
static bool
|
||||
dispatch_mapped_scroll(OSWindow *osw, Window *w, const GLFWScrollEvent *ev) {
|
||||
const bool grabbed = w->render_data.screen->modes.mouse_tracking_mode != NO_TRACKING;
|
||||
const int mods = ev->keyboard_modifiers & WHEEL_MAPPING_MODS_MASK;
|
||||
// A partial step does not carry over to a different window or mapping
|
||||
double pending = osw->scroll.mapped_window_id == w->id && osw->scroll.mapped_modifiers == mods ? osw->scroll.mapped_pending_pixels_y : 0;
|
||||
int direction;
|
||||
// unscaled is in logical pixels for touchpads and equal to y_offset otherwise
|
||||
const int steps = mapped_scroll_steps(ev->offset_type, ev->unscaled.y, &pending, &direction);
|
||||
if (!direction || !(OPT(wheel_mappings)[direction > 0 ? 0 : 1][grabbed] & (1ull << mods))) {
|
||||
osw->scroll.mapped_pending_pixels_y = 0;
|
||||
return false;
|
||||
}
|
||||
osw->scroll.mapped_pending_pixels_y = pending;
|
||||
osw->scroll.mapped_window_id = w->id;
|
||||
osw->scroll.mapped_modifiers = mods;
|
||||
if (steps) dispatch_mouse_event(w, direction > 0 ? MOUSE_WHEEL_UP : MOUSE_WHEEL_DOWN, abs(steps), ev->keyboard_modifiers, grabbed);
|
||||
return true;
|
||||
}
|
||||
|
||||
static const char *
|
||||
|
|
@ -1593,6 +1676,98 @@ pixel_scroll_enabled_for_screen(const Screen *screen) {
|
|||
return OPT(pixel_scroll) && screen->linebuf == screen->main_linebuf;
|
||||
}
|
||||
|
||||
static id_type window_for_momentum_scroll = 0;
|
||||
static bool main_screen_for_momentum_scroll = false;
|
||||
|
||||
static void
|
||||
scroll_event_in_window(OSWindow *osw, Window *w, const GLFWScrollEvent *ev, unsigned cell_width, unsigned cell_height) {
|
||||
Screen *screen = w->render_data.screen;
|
||||
|
||||
switch (ev->momentum_type) {
|
||||
case GLFW_NO_MOMENTUM_DATA: break;
|
||||
case GLFW_MOMENTUM_PHASE_BEGAN:
|
||||
window_for_momentum_scroll = w->id;
|
||||
main_screen_for_momentum_scroll = screen->linebuf == screen->main_linebuf;
|
||||
break;
|
||||
case GLFW_MOMENTUM_PHASE_STATIONARY:
|
||||
case GLFW_MOMENTUM_PHASE_ACTIVE:
|
||||
if (window_for_momentum_scroll != w->id || main_screen_for_momentum_scroll != (screen->linebuf == screen->main_linebuf)) return;
|
||||
break;
|
||||
case GLFW_MOMENTUM_PHASE_ENDED:
|
||||
case GLFW_MOMENTUM_PHASE_CANCELED: window_for_momentum_scroll = 0; break;
|
||||
case GLFW_MOMENTUM_PHASE_MAY_BEGIN: break;
|
||||
}
|
||||
if (ev->momentum_type == GLFW_NO_MOMENTUM_DATA) {
|
||||
if (ev->y_offset != 0.0) osw->scroll.mapped_gesture = dispatch_mapped_scroll(osw, w, ev);
|
||||
// Horizontal only events continue a mapped gesture, but not one in a
|
||||
// different window or with different modifiers, so that its momentum
|
||||
// is not swallowed
|
||||
else if (osw->scroll.mapped_window_id != w->id || osw->scroll.mapped_modifiers != (ev->keyboard_modifiers & WHEEL_MAPPING_MODS_MASK))
|
||||
osw->scroll.mapped_gesture = false;
|
||||
if (osw->scroll.mapped_gesture) return;
|
||||
} else if (osw->scroll.mapped_gesture) {
|
||||
// Inertial scrolling after a mapped gesture neither fires the mapping nor scrolls
|
||||
osw->scroll.mapped_pending_pixels_y = 0;
|
||||
return;
|
||||
}
|
||||
finish_scroll_animation(screen);
|
||||
if (ev->y_offset != 0.0) {
|
||||
if (screen->modes.mouse_tracking_mode == NO_TRACKING && pixel_scroll_enabled_for_screen(screen) &&
|
||||
(ev->offset_type == GLFW_SCROLL_OFFEST_HIGHRES || ev->offset_type == GLFW_SCROLL_OFFEST_V120)) {
|
||||
double delta_pixels;
|
||||
if (ev->offset_type == GLFW_SCROLL_OFFEST_HIGHRES) {
|
||||
delta_pixels = ev->y_offset * OPT(touch_scroll_multiplier);
|
||||
} else {
|
||||
const double offset_lines = (ev->y_offset / 120.) * OPT(wheel_scroll_multiplier);
|
||||
delta_pixels = offset_lines * cell_height;
|
||||
}
|
||||
osw->scroll.pending_pixels_y = 0.0;
|
||||
if (screen_apply_pixel_scroll(screen, delta_pixels) && screen->selections.in_progress) update_drag(w);
|
||||
} else {
|
||||
int s = scale_scroll(
|
||||
screen->modes.mouse_tracking_mode,
|
||||
ev->y_offset,
|
||||
ev->offset_type,
|
||||
&osw->scroll.pending_pixels_y,
|
||||
cell_height,
|
||||
&osw->scroll.last_v120_dir_y);
|
||||
if (s) {
|
||||
bool upwards = s > 0;
|
||||
if (screen->modes.mouse_tracking_mode) {
|
||||
int sz = encode_mouse_scroll(w, upwards ? 4 : 5, ev->keyboard_modifiers);
|
||||
if (sz > 0) {
|
||||
mouse_event_buf[sz] = 0;
|
||||
for (s = abs(s); s > 0; s--) { write_escape_code_to_child(screen, ESC_CSI, mouse_event_buf); }
|
||||
}
|
||||
} else {
|
||||
if (screen->linebuf == screen->main_linebuf) {
|
||||
screen_history_scroll(screen, abs(s), upwards);
|
||||
if (screen->selections.in_progress) update_drag(w);
|
||||
} else fake_scroll(w, abs(s), upwards);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ev->x_offset != 0.0) {
|
||||
int s = scale_scroll(
|
||||
screen->modes.mouse_tracking_mode,
|
||||
ev->x_offset,
|
||||
ev->offset_type,
|
||||
&osw->scroll.pending_pixels_x,
|
||||
cell_width,
|
||||
&osw->scroll.last_v120_dir_x);
|
||||
if (s) {
|
||||
if (screen->modes.mouse_tracking_mode) {
|
||||
int sz = encode_mouse_scroll(w, s > 0 ? 6 : 7, ev->keyboard_modifiers);
|
||||
if (sz > 0) {
|
||||
mouse_event_buf[sz] = 0;
|
||||
for (s = abs(s); s > 0; s--) { write_escape_code_to_child(screen, ESC_CSI, mouse_event_buf); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
scroll_event(const GLFWScrollEvent *ev) {
|
||||
debug(
|
||||
|
|
@ -1602,8 +1777,6 @@ scroll_event(const GLFWScrollEvent *ev) {
|
|||
ev->y_offset,
|
||||
scroll_phase(ev->momentum_type),
|
||||
format_mods(ev->keyboard_modifiers));
|
||||
static id_type window_for_momentum_scroll = 0;
|
||||
static bool main_screen_for_momentum_scroll = false;
|
||||
// allow scroll events even if window is not currently focused (in
|
||||
// which case on some platforms such as macOS the mouse location is zeroed so
|
||||
// window_for_event() does not work).
|
||||
|
|
@ -1633,99 +1806,7 @@ scroll_event(const GLFWScrollEvent *ev) {
|
|||
w->mouse_pos.in_left_half_of_cell = in_left_half_of_cell;
|
||||
}
|
||||
}
|
||||
Screen *screen = w->render_data.screen;
|
||||
|
||||
switch (ev->momentum_type) {
|
||||
case GLFW_NO_MOMENTUM_DATA: break;
|
||||
case GLFW_MOMENTUM_PHASE_BEGAN:
|
||||
window_for_momentum_scroll = w->id;
|
||||
main_screen_for_momentum_scroll = screen->linebuf == screen->main_linebuf;
|
||||
break;
|
||||
case GLFW_MOMENTUM_PHASE_STATIONARY:
|
||||
case GLFW_MOMENTUM_PHASE_ACTIVE:
|
||||
if (window_for_momentum_scroll != w->id || main_screen_for_momentum_scroll != (screen->linebuf == screen->main_linebuf)) return;
|
||||
break;
|
||||
case GLFW_MOMENTUM_PHASE_ENDED:
|
||||
case GLFW_MOMENTUM_PHASE_CANCELED: window_for_momentum_scroll = 0; break;
|
||||
case GLFW_MOMENTUM_PHASE_MAY_BEGIN: break;
|
||||
}
|
||||
if (ev->momentum_type != GLFW_NO_MOMENTUM_DATA) {
|
||||
// Inertial scrolling after a mapped gesture neither fires the mapping nor scrolls
|
||||
if (osw->scroll.mapped_gesture) {
|
||||
osw->scroll.mapped_pending_pixels_y = 0;
|
||||
return;
|
||||
}
|
||||
} else if (ev->y_offset != 0.0) {
|
||||
// Events that do not complete a step are consumed too, when mapped, so
|
||||
// the screen does not also scroll. unscaled is in logical pixels for
|
||||
// touchpads and equal to y_offset otherwise.
|
||||
double pending = osw->scroll.mapped_pending_pixels_y;
|
||||
int last_dir = osw->scroll.mapped_last_v120_dir_y;
|
||||
int steps = mapped_scroll_steps(ev->offset_type, ev->unscaled.y, &pending, &last_dir);
|
||||
int button = (steps ? steps : pending) > 0 ? MOUSE_WHEEL_UP : MOUSE_WHEEL_DOWN;
|
||||
osw->scroll.mapped_gesture = dispatch_mouse_event(w, button, abs(steps), ev->keyboard_modifiers, screen->modes.mouse_tracking_mode != NO_TRACKING);
|
||||
if (osw->scroll.mapped_gesture) {
|
||||
osw->scroll.mapped_pending_pixels_y = pending;
|
||||
osw->scroll.mapped_last_v120_dir_y = last_dir;
|
||||
return;
|
||||
}
|
||||
}
|
||||
finish_scroll_animation(screen);
|
||||
if (ev->y_offset != 0.0) {
|
||||
if (screen->modes.mouse_tracking_mode == NO_TRACKING && pixel_scroll_enabled_for_screen(screen) &&
|
||||
(ev->offset_type == GLFW_SCROLL_OFFEST_HIGHRES || ev->offset_type == GLFW_SCROLL_OFFEST_V120)) {
|
||||
double delta_pixels;
|
||||
if (ev->offset_type == GLFW_SCROLL_OFFEST_HIGHRES) {
|
||||
delta_pixels = ev->y_offset * OPT(touch_scroll_multiplier);
|
||||
} else {
|
||||
const double offset_lines = (ev->y_offset / 120.) * OPT(wheel_scroll_multiplier);
|
||||
delta_pixels = offset_lines * global_state.callback_os_window->fonts_data->fcm.cell_height;
|
||||
}
|
||||
osw->scroll.pending_pixels_y = 0.0;
|
||||
if (screen_apply_pixel_scroll(screen, delta_pixels) && screen->selections.in_progress) update_drag(w);
|
||||
} else {
|
||||
int s = scale_scroll(
|
||||
screen->modes.mouse_tracking_mode,
|
||||
ev->y_offset,
|
||||
ev->offset_type,
|
||||
&osw->scroll.pending_pixels_y,
|
||||
global_state.callback_os_window->fonts_data->fcm.cell_height,
|
||||
&osw->scroll.last_v120_dir_y);
|
||||
if (s) {
|
||||
bool upwards = s > 0;
|
||||
if (screen->modes.mouse_tracking_mode) {
|
||||
int sz = encode_mouse_scroll(w, upwards ? 4 : 5, ev->keyboard_modifiers);
|
||||
if (sz > 0) {
|
||||
mouse_event_buf[sz] = 0;
|
||||
for (s = abs(s); s > 0; s--) { write_escape_code_to_child(screen, ESC_CSI, mouse_event_buf); }
|
||||
}
|
||||
} else {
|
||||
if (screen->linebuf == screen->main_linebuf) {
|
||||
screen_history_scroll(screen, abs(s), upwards);
|
||||
if (screen->selections.in_progress) update_drag(w);
|
||||
} else fake_scroll(w, abs(s), upwards);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ev->x_offset != 0.0) {
|
||||
int s = scale_scroll(
|
||||
screen->modes.mouse_tracking_mode,
|
||||
ev->x_offset,
|
||||
ev->offset_type,
|
||||
&osw->scroll.pending_pixels_x,
|
||||
global_state.callback_os_window->fonts_data->fcm.cell_width,
|
||||
&osw->scroll.last_v120_dir_x);
|
||||
if (s) {
|
||||
if (screen->modes.mouse_tracking_mode) {
|
||||
int sz = encode_mouse_scroll(w, s > 0 ? 6 : 7, ev->keyboard_modifiers);
|
||||
if (sz > 0) {
|
||||
mouse_event_buf[sz] = 0;
|
||||
for (s = abs(s); s > 0; s--) { write_escape_code_to_child(screen, ESC_CSI, mouse_event_buf); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
scroll_event_in_window(osw, w, ev, osw->fonts_data->fcm.cell_width, osw->fonts_data->fcm.cell_height);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
|
|
@ -1867,12 +1948,12 @@ test_mapped_scroll_steps(PyObject *self UNUSED, PyObject *args) {
|
|||
PyObject *values;
|
||||
if (!PyArg_ParseTuple(args, "iO!", &offset_type, &PyList_Type, &values)) return NULL;
|
||||
double pending = 0;
|
||||
int last_dir = 0;
|
||||
int direction;
|
||||
PyObject *ans = PyList_New(PyList_GET_SIZE(values));
|
||||
if (!ans) return NULL;
|
||||
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(values); i++) {
|
||||
const double v = PyFloat_AsDouble(PyList_GET_ITEM(values, i));
|
||||
PyObject *t = PyErr_Occurred() ? NULL : PyLong_FromLong(mapped_scroll_steps(offset_type, v, &pending, &last_dir));
|
||||
PyObject *t = PyErr_Occurred() ? NULL : PyLong_FromLong(mapped_scroll_steps(offset_type, v, &pending, &direction));
|
||||
if (!t) {
|
||||
Py_DECREF(ans);
|
||||
return NULL;
|
||||
|
|
@ -1882,12 +1963,33 @@ test_mapped_scroll_steps(PyObject *self UNUSED, PyObject *args) {
|
|||
return ans;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
send_mock_scroll_event_to_window(PyObject *self UNUSED, PyObject *args) {
|
||||
PyObject *capsule;
|
||||
GLFWScrollEvent ev = {0};
|
||||
int offset_type, momentum_type = GLFW_NO_MOMENTUM_DATA;
|
||||
if (!PyArg_ParseTuple(
|
||||
args, "O!iddi|i", &PyCapsule_Type, &capsule, &offset_type, &ev.x_offset, &ev.y_offset, &ev.keyboard_modifiers, &momentum_type))
|
||||
return NULL;
|
||||
Window *w = PyCapsule_GetPointer(capsule, "Window");
|
||||
if (!w) return NULL;
|
||||
// Scroll state is per OS window, mock windows share this one
|
||||
static OSWindow osw = {0};
|
||||
ev.offset_type = offset_type;
|
||||
ev.momentum_type = momentum_type;
|
||||
ev.unscaled.x = ev.x_offset;
|
||||
ev.unscaled.y = ev.y_offset;
|
||||
scroll_event_in_window(&osw, w, &ev, w->render_data.screen->cell_size.width, w->render_data.screen->cell_size.height);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyMethodDef module_methods[] = {
|
||||
{"send_mouse_event", (PyCFunction)(void (*)(void))(send_mouse_event), METH_VARARGS | METH_KEYWORDS, NULL},
|
||||
METHODB(test_encode_mouse, METH_VARARGS),
|
||||
METHODB(test_scale_scroll, METH_VARARGS),
|
||||
METHODB(test_mapped_scroll_steps, METH_VARARGS),
|
||||
METHODB(send_mock_mouse_event_to_window, METH_VARARGS),
|
||||
METHODB(send_mock_scroll_event_to_window, METH_VARARGS),
|
||||
METHODB(mock_mouse_selection, METH_VARARGS),
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1235,10 +1235,13 @@ mouse. :code:`wheel_up` and :code:`wheel_down` refer to scrolling the mouse
|
|||
wheel (or touchpad) vertically and only support the :code:`press` event type,
|
||||
which is triggered once per wheel step. A step is one click of a classic wheel,
|
||||
one notch of a high resolution wheel or 100 pixels of touchpad travel (scaled by
|
||||
:opt:`touch_scroll_multiplier`). Inertial (momentum) scrolling does not trigger
|
||||
them. Mapping them replaces the normal scroll behavior for that combination of
|
||||
modifiers and mode. For example, to change
|
||||
the font size with :kbd:`Ctrl` and the mouse wheel::
|
||||
:opt:`touch_scroll_multiplier`). The direction follows the scroll direction of
|
||||
kitty, so a negative :opt:`wheel_scroll_multiplier` or
|
||||
:opt:`touch_scroll_multiplier` swaps them. Inertial (momentum) scrolling does
|
||||
not trigger them. Mapping them replaces the normal scroll behavior for that
|
||||
combination of modifiers and mode. They cannot start a selection with
|
||||
:ac:`mouse_selection`. For example, to change the font size with :kbd:`Ctrl` and
|
||||
the mouse wheel::
|
||||
|
||||
mouse_map ctrl+wheel_up press ungrabbed,grabbed change_font_size all +2
|
||||
mouse_map ctrl+wheel_down press ungrabbed,grabbed change_font_size all -2
|
||||
|
|
|
|||
|
|
@ -1105,6 +1105,7 @@ PYWRAP1(set_options) {
|
|||
global_state.debug_rendering = debug_rendering ? true : false;
|
||||
global_state.debug_font_fallback = debug_font_fallback ? true : false;
|
||||
if (!convert_opts_from_python_opts(opts, &global_state.opts)) return NULL;
|
||||
if (!set_wheel_mappings(opts, &global_state.opts)) return NULL;
|
||||
push_modifier_remap_to_glfw();
|
||||
Py_XDECREF(global_state.options_object);
|
||||
global_state.options_object = Py_NewRef(opts);
|
||||
|
|
|
|||
|
|
@ -130,6 +130,9 @@ typedef struct Options {
|
|||
// all remapped source bits, used as a fast path.
|
||||
int modifier_remap[8];
|
||||
int modifier_remap_mask;
|
||||
// Bit i of wheel_mappings[0 for wheel_up, 1 for wheel_down][grabbed] is set
|
||||
// when mouse_map maps the wheel with the modifiers i
|
||||
uint64_t wheel_mappings[2][2];
|
||||
bool window_alert_on_bell;
|
||||
bool macos_dock_badge_on_bell;
|
||||
bool debug_keyboard;
|
||||
|
|
@ -599,8 +602,12 @@ typedef struct OSWindow {
|
|||
struct {
|
||||
double pending_pixels_x, pending_pixels_y;
|
||||
int last_v120_dir_x, last_v120_dir_y;
|
||||
// wheel_up/wheel_down mouse_map state: the partial step and the
|
||||
// window and modifiers it belongs to, and whether the current gesture
|
||||
// fired a mapping, so that its momentum is swallowed
|
||||
double mapped_pending_pixels_y;
|
||||
int mapped_last_v120_dir_y;
|
||||
id_type mapped_window_id;
|
||||
int mapped_modifiers;
|
||||
bool mapped_gesture;
|
||||
} scroll;
|
||||
unsigned shader_anim_event_registry;
|
||||
|
|
@ -802,6 +809,7 @@ bool update_ime_position_for_window(id_type window_id, bool force, int update_fo
|
|||
void update_ime_state_in_os_window(OSWindow *osw, Window *window, int update_focus);
|
||||
void set_ignore_os_keyboard_processing(bool enabled);
|
||||
void push_modifier_remap_to_glfw(void);
|
||||
bool set_wheel_mappings(PyObject *py_opts, Options *opts);
|
||||
void update_menu_bar_title(PyObject *title UNUSED);
|
||||
void change_live_resize_state(OSWindow *, bool);
|
||||
bool render_os_window(OSWindow *w, monotonic_t now, bool scan_for_animated_images);
|
||||
|
|
|
|||
|
|
@ -1424,19 +1424,18 @@ class Window:
|
|||
|
||||
def on_mouse_event(self, event: dict[str, Any]) -> bool:
|
||||
event['mods'] = event.get('mods', 0) & mod_mask
|
||||
is_wheel = event['button'] in (MOUSE_WHEEL_UP, MOUSE_WHEEL_DOWN)
|
||||
if is_wheel: # repeat_count is the number of wheel steps, possibly zero
|
||||
steps = 1
|
||||
if event['button'] in (MOUSE_WHEEL_UP, MOUSE_WHEEL_DOWN): # repeat_count is the number of wheel steps
|
||||
steps, event['repeat_count'] = event['repeat_count'], 1
|
||||
ev = MouseEvent(**event)
|
||||
self.current_mouse_event_button = ev.button
|
||||
action = get_options().mousemap.get(ev)
|
||||
if action is None:
|
||||
return False
|
||||
if is_wheel:
|
||||
for _ in range(steps):
|
||||
get_boss().combine(action, window_for_dispatch=self, dispatch_type='MouseEvent')
|
||||
return True
|
||||
return get_boss().combine(action, window_for_dispatch=self, dispatch_type='MouseEvent')
|
||||
handled = False
|
||||
for _step in range(steps):
|
||||
handled = get_boss().combine(action, window_for_dispatch=self, dispatch_type='MouseEvent') or handled
|
||||
return handled
|
||||
|
||||
def drag_thumbnails(self, label: str) -> tuple[tuple[bytes, int, int], ...]:
|
||||
# Render label as a single line of text, clipped to the width of this window
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ from pty import CHILD, STDIN_FILENO, STDOUT_FILENO, fork
|
|||
from unittest import TestCase
|
||||
|
||||
from kitty.config import finalize_keys, finalize_mouse_mappings
|
||||
from kitty.fast_data_types import TEXT_SIZE_CODE, Cursor, HistoryBuf, LineBuf, Screen, get_options, monotonic, set_options
|
||||
from kitty.fast_data_types import MOUSE_WHEEL_DOWN, MOUSE_WHEEL_UP, TEXT_SIZE_CODE, Cursor, HistoryBuf, LineBuf, Screen, get_options, monotonic, set_options
|
||||
from kitty.options.parse import merge_result_dicts
|
||||
from kitty.options.types import Options, defaults
|
||||
from kitty.rgb import to_color
|
||||
|
|
@ -175,14 +175,18 @@ class Callbacks:
|
|||
pass
|
||||
|
||||
def on_mouse_event(self, event):
|
||||
steps = 1
|
||||
if event['button'] in (MOUSE_WHEEL_UP, MOUSE_WHEEL_DOWN): # repeat_count is the number of wheel steps
|
||||
steps, event['repeat_count'] = event['repeat_count'], 1
|
||||
ev = MouseEvent(**event)
|
||||
opts = get_options()
|
||||
action_def = opts.mousemap.get(ev)
|
||||
if not action_def:
|
||||
return False
|
||||
self.current_mouse_button = ev.button
|
||||
for action in opts.alias_map.resolve_aliases(action_def, 'mouse_map'):
|
||||
getattr(self, action.func)(*action.args)
|
||||
for _ in range(steps):
|
||||
for action in opts.alias_map.resolve_aliases(action_def, 'mouse_map'):
|
||||
getattr(self, action.func)(*action.args)
|
||||
self.current_mouse_button = 0
|
||||
return True
|
||||
|
||||
|
|
|
|||
|
|
@ -12,9 +12,11 @@ from kitty.fast_data_types import (
|
|||
create_mock_window,
|
||||
mock_mouse_selection,
|
||||
send_mock_mouse_event_to_window,
|
||||
send_mock_scroll_event_to_window,
|
||||
set_options,
|
||||
)
|
||||
|
||||
from .base import BaseTest
|
||||
from .base import BaseTest, parse_bytes
|
||||
|
||||
|
||||
def send_mouse_event(
|
||||
|
|
@ -472,3 +474,85 @@ class TestMouse(BaseTest):
|
|||
# touch_scroll_multiplier scales touchpad steps and its sign flips them
|
||||
self.set_options({'touch_scroll_multiplier': -2.0})
|
||||
self.ae(steps(HIGHRES, [30.0, 30.0]), [0, -1])
|
||||
# Wheels only take the sign of wheel_scroll_multiplier and every click is a step
|
||||
self.set_options({'wheel_scroll_multiplier': -3.0})
|
||||
self.ae(steps(LINES, [1.0, 0.1]), [-1, -1])
|
||||
self.ae(steps(V120, [120.0, 240.0]), [-1, -2])
|
||||
|
||||
def test_mapped_scroll(self):
|
||||
from kitty.config import load_config
|
||||
|
||||
LINES, HIGHRES = 0, 2 # GLFWOffsetType
|
||||
MOMENTUM_BEGAN, MOMENTUM_ACTIVE = 1, 3 # GLFWMomentumType
|
||||
s = self.create_screen(lines=5, scrollback=50)
|
||||
errors = []
|
||||
opts = load_config(
|
||||
overrides=(
|
||||
'pixel_scroll no',
|
||||
'wheel_scroll_multiplier 1',
|
||||
'mouse_map ctrl+wheel_up press ungrabbed change_font_size all +1',
|
||||
'mouse_map ctrl+wheel_down press ungrabbed change_font_size all -1',
|
||||
'mouse_map alt+wheel_up press ungrabbed mouse_selection normal',
|
||||
),
|
||||
accumulate_bad_lines=errors,
|
||||
)
|
||||
self.ae(errors, [])
|
||||
set_options(opts)
|
||||
w = create_mock_window(s)
|
||||
for i in range(50):
|
||||
s.draw(f'{i}')
|
||||
s.carriage_return()
|
||||
s.linefeed()
|
||||
calls = []
|
||||
s.callbacks.change_font_size = lambda *a: calls.append(a[1])
|
||||
s.callbacks.mouse_selection = lambda code: mock_mouse_selection(w, s.callbacks.current_mouse_button, code)
|
||||
|
||||
def scroll(y, mods=0, offset_type=LINES, momentum=0, x=0.0):
|
||||
send_mock_scroll_event_to_window(w, offset_type, x, float(y), mods, momentum)
|
||||
|
||||
def ac(expected_calls, scrolled_by):
|
||||
self.ae(calls, expected_calls)
|
||||
self.ae(s.scrolled_by, scrolled_by)
|
||||
del calls[:]
|
||||
|
||||
# Unmapped scrolling still scrolls the screen
|
||||
scroll(2)
|
||||
ac([], 2)
|
||||
# Mapped scrolling fires the action once per step instead
|
||||
scroll(1, GLFW_MOD_CONTROL)
|
||||
ac(['+'], 2)
|
||||
scroll(3, GLFW_MOD_CONTROL)
|
||||
ac(['+'] * 3, 2)
|
||||
scroll(-1, GLFW_MOD_CONTROL)
|
||||
ac(['-'], 2)
|
||||
# Touchpad events that do not complete a step are consumed
|
||||
scroll(60, GLFW_MOD_CONTROL, HIGHRES)
|
||||
ac([], 2)
|
||||
scroll(40, GLFW_MOD_CONTROL, HIGHRES)
|
||||
ac(['+'], 2)
|
||||
# A partial step does not survive unmapped scrolling in between
|
||||
scroll(60, GLFW_MOD_CONTROL, HIGHRES)
|
||||
scroll(1)
|
||||
ac([], 3)
|
||||
scroll(40, GLFW_MOD_CONTROL, HIGHRES)
|
||||
ac([], 3)
|
||||
# Momentum after a mapped gesture neither fires the action nor scrolls
|
||||
scroll(100, GLFW_MOD_CONTROL, HIGHRES)
|
||||
scroll(200, GLFW_MOD_CONTROL, HIGHRES, MOMENTUM_BEGAN)
|
||||
scroll(200, GLFW_MOD_CONTROL, HIGHRES, MOMENTUM_ACTIVE)
|
||||
ac(['+'], 3)
|
||||
# A horizontal unmapped gesture ends the mapped gesture, so its momentum scrolls
|
||||
scroll(0, x=10, offset_type=HIGHRES)
|
||||
scroll(-40, 0, HIGHRES, MOMENTUM_BEGAN)
|
||||
ac([], 1)
|
||||
# Modifiers or modes without a mapping scroll normally
|
||||
scroll(1, GLFW_MOD_ALT | GLFW_MOD_CONTROL)
|
||||
ac([], 2)
|
||||
parse_bytes(s, b'\x1b[?1000h') # grab the mouse
|
||||
scroll(1, GLFW_MOD_CONTROL)
|
||||
ac([], 2)
|
||||
parse_bytes(s, b'\x1b[?1000l')
|
||||
# The wheel cannot start a selection, it never releases
|
||||
scroll(1, GLFW_MOD_ALT)
|
||||
self.assertFalse(s.has_selection())
|
||||
ac([], 2)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue