Improve input handling and scrolling behavior for high-resolution trackpads.

- Added precise fractional mouse wheel delta handling with per-step command emission.
- Introduced scroll accumulators (`wheel_accum_y_`, `wheel_accum_x_`) for high-resolution trackpad input.
- Replaced hardcoded ESC delay with configurable `kEscDelayMs` constant in `TerminalFrontend`.
- Enabled mouse position reporting and reduced CPU usage during idle with optimized `timeout()` setting.
This commit is contained in:
2025-12-05 20:42:46 -08:00
parent 3f4c60d311
commit 12cc04d7e0
4 changed files with 38 additions and 20 deletions

View File

@@ -294,25 +294,34 @@ ImGuiInputHandler::ProcessSDLEvent(const SDL_Event &e)
bool produced = false;
switch (e.type) {
case SDL_MOUSEWHEEL: {
// Let ImGui handle mouse wheel when it wants to capture the mouse
// (e.g., when hovering the editor child window with scrollbars).
// This enables native vertical and horizontal scrolling behavior in GUI.
if (ImGui::GetIO().WantCaptureMouse)
return false;
// Otherwise, fallback to mapping vertical wheel to editor scroll commands.
int dy = e.wheel.y;
// High-resolution trackpads can deliver fractional wheel deltas. Accumulate
// precise values and emit one scroll step per whole unit.
float dy = 0.0f;
#if SDL_VERSION_ATLEAST(2,0,18)
dy = e.wheel.preciseY;
#else
dy = static_cast<float>(e.wheel.y);
#endif
#ifdef SDL_MOUSEWHEEL_FLIPPED
if (e.wheel.direction == SDL_MOUSEWHEEL_FLIPPED)
dy = -dy;
#endif
if (dy != 0) {
int repeat = dy > 0 ? dy : -dy;
CommandId id = dy > 0 ? CommandId::ScrollUp : CommandId::ScrollDown;
std::lock_guard<std::mutex> lk(mu_);
for (int i = 0; i < repeat; ++i) {
q_.push(MappedInput{true, id, std::string(), 0});
if (dy != 0.0f) {
wheel_accum_y_ += dy;
float abs_accum = wheel_accum_y_ >= 0.0f ? wheel_accum_y_ : -wheel_accum_y_;
int steps = static_cast<int>(abs_accum);
if (steps > 0) {
CommandId id = (wheel_accum_y_ > 0.0f) ? CommandId::ScrollUp : CommandId::ScrollDown;
std::lock_guard<std::mutex> lk(mu_);
for (int i = 0; i < steps; ++i) {
q_.push(MappedInput{true, id, std::string(), 0});
}
// remove the whole steps, keep fractional remainder
wheel_accum_y_ += (wheel_accum_y_ > 0.0f)
? -static_cast<float>(steps)
: static_cast<float>(steps);
return true; // consumed
}
return true; // consumed
}
return false;
}

View File

@@ -41,4 +41,9 @@ private:
bool suppress_text_input_once_ = false;
Editor *ed_ = nullptr; // attached editor for editor-owned uarg handling
// Accumulators for high-resolution (trackpad) scrolling. We emit one scroll
// command per whole step and keep the fractional remainder.
float wheel_accum_y_ = 0.0f;
float wheel_accum_x_ = 0.0f; // reserved for future horizontal scrolling
};

View File

@@ -42,13 +42,15 @@ TerminalFrontend::Init(Editor &ed)
meta(stdscr, TRUE);
// Make ESC key sequences resolve quickly so ESC+<key> works as meta
#ifdef set_escdelay
set_escdelay(50);
set_escdelay(TerminalFrontend::kEscDelayMs);
#endif
nodelay(stdscr, TRUE);
// Make getch() block briefly instead of busy-looping; reduces CPU when idle
// Equivalent to nodelay(FALSE) with a small timeout.
timeout(16); // ~16ms (about 60Hz)
curs_set(1);
// Enable mouse support if available
mouseinterval(0);
mousemask(ALL_MOUSE_EVENTS, nullptr);
mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, nullptr);
int r = 0, c = 0;
getmaxyx(stdscr, r, c);
@@ -94,9 +96,6 @@ TerminalFrontend::Step(Editor &ed, bool &running)
if (mi.hasCommand) {
Execute(ed, mi.id, mi.arg, mi.count);
}
} else {
// Avoid busy loop
usleep(1000);
}
if (ed.QuitRequested()) {

View File

@@ -16,6 +16,11 @@ public:
~TerminalFrontend() override = default;
// Configurable ESC key delay (ms) for ncurses' set_escdelay().
// Controls how long ncurses waits to distinguish ESC vs. meta sequences.
// Adjust if your terminal needs a different threshold.
static constexpr int kEscDelayMs = 50;
bool Init(Editor &ed) override;
void Step(Editor &ed, bool &running) override;