[Fix] Correct terminal auto-scroll

This commit is contained in:
Ritchie Cunningham 2025-10-04 21:37:40 +01:00
parent caa482a7a0
commit 8d9cd9a777

View File

@ -75,16 +75,6 @@ void Terminal::_on_ret_press(void) {
_history.push_back(_prompt + "> " + command); _history.push_back(_prompt + "> " + command);
_network->send(command); _network->send(command);
/* TODO: Ugly hack. Refactor to pass window height
* We need the window height to know if we should
* auto-scroll, but we don't have it here.
* Assume the height of 500 for now.
*/
int visible_lines = (500-5.0f)/20.0f; /* Subtract padding. */
if((int)_history.size()+1 > visible_lines) {
_scroll_offset = (_history.size()+1) - visible_lines+1;
}
} }
void Terminal::handle_input(SDL_Event* event, int window_x, int window_y, int window_gl_y) { void Terminal::handle_input(SDL_Event* event, int window_x, int window_y, int window_gl_y) {
@ -117,6 +107,15 @@ void Terminal::render(const RenderContext& context, int x, int y_screen, int y_g
float line_height = 20.0f; float line_height = 20.0f;
float padding = 5.0f; float padding = 5.0f;
/*
* Auto-scroll to bottom if the user has not manually scrolled up.
* Ensures input line is always visible after submitting a command.
*/
int visible_lines = (height-padding) / line_height;
int max_scroll_offset = (_history.size() + 1) - visible_lines;
if(max_scroll_offset < 0) max_scroll_offset = 0;
if(_scroll_offset >= max_scroll_offset - 1) _scroll_offset = max_scroll_offset;
context.ui_renderer->begin_text(); context.ui_renderer->begin_text();
/* Enable scissor test to clip rendering to the window content area. */ /* Enable scissor test to clip rendering to the window content area. */