Use the same logic for mouse wheel and (half) page up/down

Fixes #749
This commit is contained in:
Maxime Coste 2016-07-28 09:51:49 +01:00
parent a7005ec74b
commit d28e503150
3 changed files with 33 additions and 46 deletions

View File

@ -95,12 +95,12 @@ struct MouseHandler
case Key::Modifiers::MouseWheelDown:
m_dragging = false;
wheel(context, 3);
scroll_window(context, 3);
return true;
case Key::Modifiers::MouseWheelUp:
m_dragging = false;
wheel(context, -3);
scroll_window(context, -3);
return true;
default: return false;
@ -108,32 +108,6 @@ struct MouseHandler
}
private:
void wheel(Context& context, LineCount offset)
{
Window& window = context.window();
Buffer& buffer = context.buffer();
CharCoord win_pos = window.position();
CharCoord win_dim = window.dimensions();
const CharCoord max_offset{(win_dim.line - 1)/2, (win_dim.column - 1)/2};
const CharCoord scrolloff =
std::min(context.options()["scrolloff"].get<CharCoord>(), max_offset);
const LineCount line_count = buffer.line_count();
win_pos.line = clamp(win_pos.line + offset, 0_line, line_count-1);
SelectionList& selections = context.selections();
const ByteCoord cursor = selections.main().cursor();
auto clamp_line = [&](LineCount line) { return clamp(line, 0_line, line_count-1); };
auto min_coord = buffer.offset_coord(clamp_line(win_pos.line + scrolloff.line), win_pos.column);
auto max_coord = buffer.offset_coord(clamp_line(win_pos.line + win_dim.line - 1 - scrolloff.line), win_pos.column);
selections = SelectionList{buffer, clamp(cursor, min_coord, max_coord)};
window.set_position(win_pos);
}
bool m_dragging = false;
ByteCoord m_anchor;
@ -1459,4 +1433,31 @@ void hide_auto_info_ifn(const Context& context, bool hide)
context.client().info_hide();
}
void scroll_window(Context& context, LineCount offset)
{
Window& window = context.window();
Buffer& buffer = context.buffer();
CharCoord win_pos = window.position();
CharCoord win_dim = window.dimensions();
const CharCoord max_offset{(win_dim.line - 1)/2, (win_dim.column - 1)/2};
const CharCoord scrolloff =
std::min(context.options()["scrolloff"].get<CharCoord>(), max_offset);
const LineCount line_count = buffer.line_count();
win_pos.line = clamp(win_pos.line + offset, 0_line, line_count-1);
SelectionList& selections = context.selections();
const ByteCoord cursor = selections.main().cursor();
auto clamp_line = [&](LineCount line) { return clamp(line, 0_line, line_count-1); };
auto min_coord = buffer.offset_coord(clamp_line(win_pos.line + scrolloff.line), win_pos.column);
auto max_coord = buffer.offset_coord(clamp_line(win_pos.line + win_dim.line - 1 - scrolloff.line), win_pos.column);
selections = SelectionList{buffer, clamp(cursor, min_coord, max_coord)};
window.set_position(win_pos);
}
}

View File

@ -149,6 +149,8 @@ void on_next_key_with_autoinfo(const Context& context, KeymapMode keymap_mode, C
});
}
void scroll_window(Context& context, LineCount offset);
}
#endif // input_handler_hh_INCLUDED

View File

@ -1027,25 +1027,9 @@ template<Direction direction, bool half = false>
void scroll(Context& context, NormalParams)
{
Window& window = context.window();
Buffer& buffer = context.buffer();
CharCoord position = window.position();
LineCount cursor_line = 0;
const LineCount offset = (window.dimensions().line - 2) / (half ? 2 : 1);
if (direction == Backward)
{
position.line -= (window.dimensions().line - 2) / (half ? 2 : 1);
cursor_line = position.line;
}
else if (direction == Forward)
{
position.line += (window.dimensions().line - 2) / (half ? 2 : 1);
cursor_line = position.line + window.dimensions().line - 1;
}
auto cursor_pos = utf8::advance(buffer.iterator_at(position.line),
buffer.iterator_at(position.line+1),
position.column);
select_coord(buffer, cursor_pos.coord(), context.selections());
window.set_position(position);
scroll_window(context, direction == Direction::Forward ? offset : -offset);
}
template<Direction direction>