From 482a31fa190bef6caae3dd127ae8b768c6dfce9a Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 31 Dec 2024 19:19:14 +0800 Subject: [PATCH] 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. --- crates/ui/src/input/element.rs | 20 +++++++++++--------- crates/ui/src/input/input.rs | 9 +++++++-- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/crates/ui/src/input/element.rs b/crates/ui/src/input/element.rs index 5cf77ee1..bd37ac0c 100644 --- a/crates/ui/src/input/element.rs +++ b/crates/ui/src/input/element.rs @@ -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.) { diff --git a/crates/ui/src/input/input.rs b/crates/ui/src/input/input.rs index 31f1c26d..39673a70 100644 --- a/crates/ui/src/input/input.rs +++ b/crates/ui/src/input/input.rs @@ -704,10 +704,15 @@ impl TextInput { fn enter(&mut self, _: &Enter, cx: &mut ViewContext) { 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);