From: Max Kellermann Date: Wed, 17 Sep 2008 10:20:42 +0000 (+0200) Subject: list_window: added list_window_center() X-Git-Tag: v0.12_alpha1~271 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=fd7abf4f81a69014b0e1e967c7965996a11295b0;p=ncmpc.git list_window: added list_window_center() Merge code which used to be duplicated in the screens "play", "help", "lyrics". --- diff --git a/src/list_window.c b/src/list_window.c index f26088d..ad671f0 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -81,6 +81,24 @@ list_window_check_selected(struct list_window *lw, unsigned length) lw->selected = length - 1; } +void +list_window_center(struct list_window *lw, unsigned rows, unsigned n) +{ + if (n > lw->rows / 2) + lw->start = n - lw->rows / 2; + else + lw->start = 0; + + if (lw->start + lw->rows > rows) { + if (lw->rows < rows) + lw->start = rows - lw->rows; + else + lw->start = 0; + } + + lw->repaint = lw->clear = 1; +} + void list_window_set_selected(struct list_window *lw, unsigned n) { diff --git a/src/list_window.h b/src/list_window.h index ab02f4b..3a9fba4 100644 --- a/src/list_window.h +++ b/src/list_window.h @@ -56,6 +56,8 @@ int list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd); int list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd); +void +list_window_center(struct list_window *lw, unsigned rows, unsigned n); /* select functions */ void list_window_set_selected(struct list_window *lw, unsigned n); diff --git a/src/screen_help.c b/src/screen_help.c index 8506081..363b440 100644 --- a/src/screen_help.c +++ b/src/screen_help.c @@ -248,16 +248,7 @@ help_cmd(screen_t *screen, mpd_unused mpdclient_t *c, command_t cmd) lw, help_text_rows, cmd, list_callback, NULL)) { /* center the row */ - if (lw->selected > lw->rows / 2) - lw->start = lw->selected - lw->rows / 2; - else - lw->start = 0; - if (lw->start + lw->rows > (unsigned)help_text_rows) { - if (lw->rows < (unsigned)help_text_rows) - lw->start = help_text_rows - lw->rows; - else - lw->start = 0; - } + list_window_center(lw, help_text_rows, lw->selected); return 1; } diff --git a/src/screen_lyrics.c b/src/screen_lyrics.c index 5356548..6853fec 100644 --- a/src/screen_lyrics.c +++ b/src/screen_lyrics.c @@ -292,8 +292,6 @@ lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) if (list_window_scroll_cmd(lw, current.lines->len, cmd)) return 1; - lw->repaint=1; - switch(cmd) { case CMD_SELECT: /* XXX */ @@ -329,13 +327,7 @@ lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) lw, lyrics_text_rows, cmd, list_callback, NULL)) { /* center the row */ - lw->start = lw->selected - (lw->rows / 2); - if (lw->start + lw->rows > (unsigned)lyrics_text_rows) { - if (lw->rows < (unsigned)lyrics_text_rows) - lw->start = lyrics_text_rows - lw->rows; - else - lw->start = 0; - } + list_window_center(lw, current.lines->len, lw->selected); return 1; } diff --git a/src/screen_play.c b/src/screen_play.c index 6b4de78..0426176 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -106,20 +106,15 @@ center_playing_item(mpdclient_t *c) /* try to center the song that are playing */ idx = playlist_get_index(c, c->song); D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,idx); - idx -= (lw->rows / 2); - if (idx + (int)lw->rows > (int)length) - idx = length - lw->rows; if (idx < 0) - idx = 0; - lw->start = idx; + return 0; + + list_window_center(lw, length, idx); /* make sure the cursor is in the window */ lw->selected = lw->start+offset; list_window_check_selected(lw, length); - lw->clear = 1; - lw->repaint = 1; - return 0; }