@@ -160,26 +160,35 @@ int master_handler(State *state, WINDOW *canvas_win, WINDOW *status_win) {
160
160
// shift view down/up/left/right
161
161
// renaming for ease of use
162
162
const int h = getmaxy (canvas_win ) - 2 ; // height of visible canvas
163
- const int w = getmaxx (canvas_win ) - 2 ; // width
163
+ const int w = getmaxx (canvas_win ) - 2 ; // width of visible canvas
164
164
const int vy = state -> view -> y ;
165
165
const int vx = state -> view -> x ;
166
- const int ch = state -> view -> canvas -> num_rows ;
167
- const int cw = state -> view -> canvas -> num_cols ;
166
+ const int ch = state -> view -> canvas -> num_rows ; // height of total canvas
167
+ const int cw = state -> view -> canvas -> num_cols ; // width of total canvas
168
168
int new_vy = vy ;
169
169
int new_vx = vx ;
170
170
// calc shift based on key
171
171
switch (c ) {
172
172
case KEY_NPAGE : // down
173
- new_vy = min (vy + h , ch - h );
173
+ // b/c canvas snaps to the top left corner, we need to be more careful
174
+ // when moving down or right. Move to either:
175
+ // another window height
176
+ // | or the end of the visible canvas
177
+ // | | (when the canvas is smaller than the window, this
178
+ // v v is less than zero, so get the max of the two)
179
+ new_vy = min (vy + h , max (ch - h , 0 ));
174
180
break ;
175
181
case KEY_PPAGE : // up
176
182
new_vy = max (0 , vy - h );
177
183
break ;
178
- case KEY_SRIGHT :
179
- new_vx = min (vx + w , cw - w );
184
+ case KEY_SRIGHT : // right
185
+ // similar situation to moving down - jump to another window width or
186
+ // the end of the canvas, and check for negative values
187
+ new_vx = min (vx + w , max (cw - w , 0 ));
180
188
break ;
181
- case KEY_SLEFT :
189
+ case KEY_SLEFT : // left
182
190
new_vx = max (0 , vx - w );
191
+ break ;
183
192
}
184
193
// shift view
185
194
state -> view -> y = new_vy ;
0 commit comments