input: Fix TextArea enter to newline not work and scroll to visible on move cursor. (#523)

Close #494

- Fix enter to newline.
- Fix to ensure scroll on move cursor or enter.
This commit is contained in:
Jason Lee 2024-12-31 19:19:14 +08:00 committed by GitHub
parent a5427ae662
commit 482a31fa19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 11 deletions

View file

@ -10,6 +10,7 @@ use crate::theme::ActiveTheme as _;
use super::TextInput;
const RIGHT_MARGIN: Pixels = px(5.);
const BOTTOM_MARGIN: Pixels = px(20.);
const CURSOR_INSET: Pixels = px(0.5);
pub(super) struct TextElement {
@ -103,15 +104,16 @@ impl TextElement {
} else {
scroll_offset.x
};
scroll_offset.y = if scroll_offset.y + cursor_pos.y > (bounds.size.height) {
// cursor is out of bottom
bounds.size.height - cursor_pos.y
} else if scroll_offset.y + cursor_pos.y < px(0.) {
// cursor is out of top
scroll_offset.y - cursor_pos.y
} else {
scroll_offset.y
};
scroll_offset.y =
if scroll_offset.y + cursor_pos.y > (bounds.size.height - BOTTOM_MARGIN) {
// cursor is out of bottom
bounds.size.height - BOTTOM_MARGIN - cursor_pos.y
} else if scroll_offset.y + cursor_pos.y < px(0.) {
// cursor is out of top
scroll_offset.y - cursor_pos.y
} else {
scroll_offset.y
};
if input.selection_reversed {
if scroll_offset.x + cursor_start.x < px(0.) {

View file

@ -704,10 +704,15 @@ impl TextInput {
fn enter(&mut self, _: &Enter, cx: &mut ViewContext<Self>) {
if self.is_multi_line() {
let is_eof = self.selected_range.end == self.text.len();
self.replace_text_in_range(None, "\n", cx);
// Move cursor to the start of the next line
// TODO: To be test this line is valid
self.move_to(self.next_boundary(self.cursor_offset()) - 1, cx);
let mut new_offset = self.next_boundary(self.cursor_offset()) - 1;
if is_eof {
new_offset += 1;
}
self.move_to(new_offset, cx);
}
cx.emit(InputEvent::PressEnter);