Update scroll behavior

This commit is contained in:
Mikhail Pogretskiy 2019-11-28 22:35:52 +07:00
parent b765fb4971
commit 89b4996fd7
3 changed files with 20 additions and 14 deletions

View File

@ -138,7 +138,7 @@ struct MouseHandler
return true; return true;
case Key::Modifiers::Scroll: case Key::Modifiers::Scroll:
scroll_window(context, static_cast<int32_t>(key.key), not m_dragging); scroll_window(context, static_cast<int32_t>(key.key), m_dragging);
return true; return true;
default: return false; default: return false;
@ -1722,31 +1722,33 @@ void hide_auto_info_ifn(const Context& context, bool hide)
context.client().info_hide(); context.client().info_hide();
} }
void scroll_window(Context& context, LineCount offset, bool adapt_cursor) void scroll_window(Context& context, LineCount offset, bool mouse_dragging)
{ {
Window& window = context.window(); Window& window = context.window();
Buffer& buffer = context.buffer(); Buffer& buffer = context.buffer();
const LineCount line_count = buffer.line_count();
DisplayCoord win_pos = window.position(); DisplayCoord win_pos = window.position();
DisplayCoord win_dim = window.dimensions(); DisplayCoord win_dim = window.dimensions();
if ((offset < 0 and win_pos.line == 0) or (offset > 0 and win_pos.line == line_count - 1))
return;
const DisplayCoord max_offset{(win_dim.line - 1)/2, (win_dim.column - 1)/2}; const DisplayCoord max_offset{(win_dim.line - 1)/2, (win_dim.column - 1)/2};
const DisplayCoord scrolloff = const DisplayCoord scrolloff =
std::min(context.options()["scrolloff"].get<DisplayCoord>(), max_offset); std::min(context.options()["scrolloff"].get<DisplayCoord>(), max_offset);
const LineCount line_count = buffer.line_count();
win_pos.line = clamp(win_pos.line + offset, 0_line, line_count-1); win_pos.line = clamp(win_pos.line + offset, 0_line, line_count-1);
window.set_position(win_pos);
if (not adapt_cursor) Selection& main_selection = context.selections().main();
return; const BufferCoord anchor = main_selection.anchor();
const BufferCoord cursor = main_selection.cursor();
SelectionList& selections = context.selections(); auto cursor_off = mouse_dragging ? win_pos.line - window.position().line : 0;
const BufferCoord cursor = selections.main().cursor();
auto line = clamp(cursor.line, win_pos.line + scrolloff.line, auto line = clamp(cursor.line + cursor_off, win_pos.line + scrolloff.line,
win_pos.line + win_dim.line - 1 - scrolloff.line); win_pos.line + win_dim.line - 1 - scrolloff.line);
line = clamp(line, 0_line, line_count-1); line = clamp(line, 0_line, buffer.line_count() - 1);
using std::min; using std::max; using std::min; using std::max;
// This is not exactly a clamp, and must be done in this order as // This is not exactly a clamp, and must be done in this order as
@ -1754,7 +1756,11 @@ void scroll_window(Context& context, LineCount offset, bool adapt_cursor)
auto col = min(max(cursor.column, buffer[line].byte_count_to(win_pos.column)), auto col = min(max(cursor.column, buffer[line].byte_count_to(win_pos.column)),
buffer[line].length()-1); buffer[line].length()-1);
selections = SelectionList{buffer, BufferCoord{line, col}}; BufferCoord new_cursor = { line, col };
BufferCoord new_anchor = (mouse_dragging or new_cursor == cursor) ? anchor : new_cursor;
window.set_position(win_pos);
main_selection = { new_anchor, new_cursor };
} }
} }

View File

@ -187,7 +187,7 @@ void on_next_key_with_autoinfo(const Context& context, StringView mode_name,
}); });
} }
void scroll_window(Context& context, LineCount offset, bool adapt_cursor = true); void scroll_window(Context& context, LineCount offset, bool mouse_dragging = false);
} }

View File

@ -376,10 +376,10 @@ void view_commands(Context& context, NormalParams params)
window.scroll(-std::max<ColumnCount>(1, count)); window.scroll(-std::max<ColumnCount>(1, count));
break; break;
case 'j': case 'j':
window.scroll( std::max<LineCount>(1, count)); scroll_window(context, std::max<LineCount>(1, count));
break; break;
case 'k': case 'k':
window.scroll(-std::max<LineCount>(1, count)); scroll_window(context, -std::max<LineCount>(1, count));
break; break;
case 'l': case 'l':
window.scroll( std::max<ColumnCount>(1, count)); window.scroll( std::max<ColumnCount>(1, count));