1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:27:35 +00:00

LibVT: Auto-scroll the terminal

TerminalWidget will now automatically scroll up or down when the user
drags the mouse out of its bounds while selecting text. This happens
at a fixed speed.
This commit is contained in:
Julian Offenhäuser 2020-12-03 22:18:28 +01:00 committed by Andreas Kling
parent 1fab67ea52
commit 6a83475ec5
2 changed files with 30 additions and 0 deletions

View file

@ -101,6 +101,7 @@ TerminalWidget::TerminalWidget(int ptm_fd, bool automatic_size_policy, RefPtr<Co
set_pty_master_fd(ptm_fd); set_pty_master_fd(ptm_fd);
m_cursor_blink_timer = add<Core::Timer>(); m_cursor_blink_timer = add<Core::Timer>();
m_visual_beep_timer = add<Core::Timer>(); m_visual_beep_timer = add<Core::Timer>();
m_auto_scroll_timer = add<Core::Timer>();
m_scrollbar = add<GUI::ScrollBar>(Orientation::Vertical); m_scrollbar = add<GUI::ScrollBar>(Orientation::Vertical);
m_scrollbar->set_relative_rect(0, 0, 16, 0); m_scrollbar->set_relative_rect(0, 0, 16, 0);
@ -118,6 +119,15 @@ TerminalWidget::TerminalWidget(int ptm_fd, bool automatic_size_policy, RefPtr<Co
update_cursor(); update_cursor();
}; };
m_auto_scroll_timer->set_interval(50);
m_auto_scroll_timer->on_timeout = [this] {
if (m_auto_scroll_direction != AutoScrollDirection::None) {
int scroll_amount = m_auto_scroll_direction == AutoScrollDirection::Up ? -1 : 1;
m_scrollbar->set_value(m_scrollbar->value() + scroll_amount);
}
};
m_auto_scroll_timer->start();
auto font_entry = m_config->read_entry("Text", "Font", "default"); auto font_entry = m_config->read_entry("Text", "Font", "default");
if (font_entry == "default") if (font_entry == "default")
set_font(Gfx::Font::default_fixed_width_font()); set_font(Gfx::Font::default_fixed_width_font());
@ -180,6 +190,7 @@ void TerminalWidget::set_logical_focus(bool focus)
m_cursor_blink_state = true; m_cursor_blink_state = true;
m_cursor_blink_timer->start(); m_cursor_blink_timer->start();
} }
m_auto_scroll_direction = AutoScrollDirection::None;
invalidate_cursor(); invalidate_cursor();
update(); update();
} }
@ -579,6 +590,7 @@ void TerminalWidget::mouseup_event(GUI::MouseEvent& event)
m_active_href_id = {}; m_active_href_id = {};
update(); update();
} }
m_auto_scroll_direction = AutoScrollDirection::None;
} }
} }
@ -662,6 +674,14 @@ void TerminalWidget::mousemove_event(GUI::MouseEvent& event)
return; return;
} }
auto adjusted_position = event.position().translated(-(frame_thickness() + m_inset), -(frame_thickness() + m_inset));
if (adjusted_position.y() < 0)
m_auto_scroll_direction = AutoScrollDirection::Up;
else if (adjusted_position.y() > m_terminal.rows() * m_line_height)
m_auto_scroll_direction = AutoScrollDirection::Down;
else
m_auto_scroll_direction = AutoScrollDirection::None;
auto old_selection_end = m_selection_end; auto old_selection_end = m_selection_end;
m_selection_end = position; m_selection_end = position;
if (old_selection_end != m_selection_end) if (old_selection_end != m_selection_end)
@ -681,6 +701,7 @@ void TerminalWidget::mousewheel_event(GUI::MouseEvent& event)
{ {
if (!is_scrollable()) if (!is_scrollable())
return; return;
m_auto_scroll_direction = AutoScrollDirection::None;
m_scrollbar->set_value(m_scrollbar->value() + event.wheel_delta() * scroll_length()); m_scrollbar->set_value(m_scrollbar->value() + event.wheel_delta() * scroll_length());
GUI::Frame::mousewheel_event(event); GUI::Frame::mousewheel_event(event);
} }

View file

@ -175,8 +175,17 @@ private:
int m_glyph_width { 0 }; int m_glyph_width { 0 };
enum class AutoScrollDirection {
None,
Up,
Down
};
AutoScrollDirection m_auto_scroll_direction { AutoScrollDirection::None };
RefPtr<Core::Timer> m_cursor_blink_timer; RefPtr<Core::Timer> m_cursor_blink_timer;
RefPtr<Core::Timer> m_visual_beep_timer; RefPtr<Core::Timer> m_visual_beep_timer;
RefPtr<Core::Timer> m_auto_scroll_timer;
RefPtr<Core::ConfigFile> m_config; RefPtr<Core::ConfigFile> m_config;
RefPtr<GUI::ScrollBar> m_scrollbar; RefPtr<GUI::ScrollBar> m_scrollbar;