diff --git a/src/ui/window.c b/src/ui/window.c index 9bce0f7b..5dc81391 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -710,17 +710,21 @@ win_page_down(ProfWin* window, int scroll_size) // Scrolled down after reaching the bottom of the page if ((*page_start > total_rows - page_space || (*page_start == page_space && *page_start >= total_rows)) && window->type == WIN_CHAT) { int bf_size = buffer_size(window->layout->buffer); - if (bf_size > 0) { + if (bf_size > 0 && *scroll_state != WIN_SCROLL_REACHED_BOTTOM) { + // How many lines are left until end of the screen + int current_offset = total_rows - *page_start; GDateTime* now = g_date_time_new_now_local(); ProfBuffEntry* last_entry = buffer_get_entry(window->layout->buffer, bf_size - 1); auto_gchar gchar* start = g_date_time_format_iso8601(last_entry->time); auto_gchar gchar* end_date = g_date_time_format_iso8601(now); - if (*scroll_state != WIN_SCROLL_REACHED_BOTTOM && !chatwin_db_history((ProfChatWin*)window, start, end_date, FALSE)) { + db_history_result_t db_response = chatwin_db_history((ProfChatWin*)window, start, end_date, FALSE); + if (db_response == DB_RESPONSE_EMPTY) *scroll_state = WIN_SCROLL_REACHED_BOTTOM; - } - int offset = last_entry->y_end_pos - 1; - *page_start = offset - page_space + scroll_size; + // similar to page_up (see explanation there) + // Get offset of the message that was the latest prior to loading messages from the DB + int original_pos = last_entry->y_end_pos; + *page_start = original_pos - current_offset; g_date_time_unref(now); } }