Skip to content

Commit

Permalink
Snap cursor and window to canvas bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
newsch committed May 29, 2019
1 parent c3e666d commit 991d04f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/fe_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,30 @@ int master_handler(State *state, WINDOW *canvas_win, WINDOW *status_win) {
call_mode(state->current_mode, NEW_KEY, state);
}

// Check if view and/or cursor is out of bounds and snap back
{
View *v = state->view;
const int max_x = v->canvas->num_cols - 1;
const int max_y = v->canvas->num_rows - 1;
if (v->x > max_x) {
v->x = max_x;
logd("Snapped view to max width\n");
}
if (v->y > max_y) {
v->y = max_y;
logd("Snapped view to max height\n");
}
Cursor *c = state->cursor;
if (c->x + v->x > max_x) {
c->x = max_x - v->x;
logd("Snapped cursor to max x\n");
}
if (c->y + v->y > max_y) {
c->y = max_y - v->y;
logd("Snapped cursor to max y\n");
}
}

// Move UI cursor to the right place
wmove(canvas_win, cursor_y_to_canvas(state->cursor),
cursor_x_to_canvas(state->cursor));
Expand Down

0 comments on commit 991d04f

Please sign in to comment.