From: Max Kellermann Date: Fri, 9 Oct 2009 20:28:45 +0000 (+0200) Subject: list_window: moved code to list_window_fetch_cursor() X-Git-Tag: release-0.16~151 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=e5130b418b222185d1b61ab99c61e91ebbf11bc4;p=ncmpc.git list_window: moved code to list_window_fetch_cursor() Merged code from screen_play.c and list_window_scroll_up(), list_window_scroll_down(). --- diff --git a/src/list_window.c b/src/list_window.c index e11e75a..15ca07a 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -160,6 +160,40 @@ list_window_move_cursor(struct list_window *lw, unsigned n) } } +void +list_window_fetch_cursor(struct list_window *lw, unsigned length) +{ + if (lw->selected < lw->start + options.scroll_offset) { + if (lw->start > 0) + lw->selected = lw->start + options.scroll_offset; + if (lw->range_selection) { + if (lw->selected > lw->range_base) { + lw->selected_end = lw->selected; + lw->selected_start = lw->range_base; + } else { + lw->selected_start = lw->selected; + } + } else { + lw->selected_start = lw->selected; + lw->selected_end = lw->selected; + } + } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) { + if (lw->start + lw->rows < length) + lw->selected = lw->start + lw->rows - 1 - options.scroll_offset; + if (lw->range_selection) { + if (lw->selected < lw->range_base) { + lw->selected_start = lw->selected; + lw->selected_end = lw->range_base; + } else { + lw->selected_end = lw->selected; + } + } else { + lw->selected_start = lw->selected; + lw->selected_end = lw->selected; + } + } +} + static void list_window_next(struct list_window *lw, unsigned length) { @@ -252,27 +286,15 @@ list_window_previous_page(struct list_window *lw) } static void -list_window_scroll_up(struct list_window *lw, unsigned n) +list_window_scroll_up(struct list_window *lw, unsigned length, unsigned n) { if (lw->start > 0) { if (n > lw->start) lw->start = 0; else lw->start -= n; - if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) { - lw->selected = lw->start + lw->rows - 1 - options.scroll_offset; - if (lw->range_selection) { - if (lw->selected < lw->range_base) { - lw->selected_start = lw->selected; - lw->selected_end = lw->range_base; - } else { - lw->selected_end = lw->selected; - } - } else { - lw->selected_start = lw->selected; - lw->selected_end = lw->selected; - } - } + + list_window_fetch_cursor(lw, length); } } @@ -285,20 +307,8 @@ list_window_scroll_down(struct list_window *lw, unsigned length, unsigned n) lw->start = length - lw->rows; else lw->start += n; - if (lw->selected < lw->start + options.scroll_offset) { - lw->selected = lw->start + options.scroll_offset; - if (lw->range_selection) { - if (lw->selected > lw->range_base) { - lw->selected_end = lw->selected; - lw->selected_start = lw->range_base; - } else { - lw->selected_start = lw->selected; - } - } else { - lw->selected_start = lw->selected; - lw->selected_end = lw->selected; - } - } + + list_window_fetch_cursor(lw, length); } } @@ -577,13 +587,13 @@ list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd) } break; case CMD_LIST_SCROLL_UP_LINE: - list_window_scroll_up(lw, 1); + list_window_scroll_up(lw, rows, 1); break; case CMD_LIST_SCROLL_DOWN_LINE: list_window_scroll_down(lw, rows, 1); break; case CMD_LIST_SCROLL_UP_HALF: - list_window_scroll_up(lw, (lw->rows - 1) / 2); + list_window_scroll_up(lw, rows, (lw->rows - 1) / 2); break; case CMD_LIST_SCROLL_DOWN_HALF: list_window_scroll_down(lw, rows, (lw->rows - 1) / 2); diff --git a/src/list_window.h b/src/list_window.h index 7c6a2a7..d0ba6c1 100644 --- a/src/list_window.h +++ b/src/list_window.h @@ -106,6 +106,13 @@ list_window_set_cursor(struct list_window *lw, unsigned i); void list_window_move_cursor(struct list_window *lw, unsigned n); +/** + * Ensures that the cursor is visible on the screen, i.e. it is not + * outside the current scrolling range. + */ +void +list_window_fetch_cursor(struct list_window *lw, unsigned length); + /* find a string in a list window */ bool list_window_find(struct list_window *lw, diff --git a/src/screen_play.c b/src/screen_play.c index dd3fccd..7d079bd 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -224,27 +224,7 @@ center_playing_item(struct mpdclient *c, bool center_cursor) } /* make sure the cursor is in the window */ - if (lw->selected < lw->start + options.scroll_offset) { - if (lw->start > 0) - lw->selected = lw->start + options.scroll_offset; - if (lw->range_selection) { - lw->selected_start = lw->range_base; - lw->selected_end = lw->selected; - } else { - lw->selected_start = lw->selected; - lw->selected_end = lw->selected; - } - } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) { - if (lw->start + lw->rows < length) - lw->selected = lw->start + lw->rows - 1 - options.scroll_offset; - if (lw->range_selection) { - lw->selected_start = lw->selected; - lw->selected_end = lw->range_base; - } else { - lw->selected_start = lw->selected; - lw->selected_end = lw->selected; - } - } + list_window_fetch_cursor(lw, length); } #ifndef NCMPC_MINI