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:
parent
a5427ae662
commit
482a31fa19
2 changed files with 18 additions and 11 deletions
|
|
@ -10,6 +10,7 @@ use crate::theme::ActiveTheme as _;
|
||||||
use super::TextInput;
|
use super::TextInput;
|
||||||
|
|
||||||
const RIGHT_MARGIN: Pixels = px(5.);
|
const RIGHT_MARGIN: Pixels = px(5.);
|
||||||
|
const BOTTOM_MARGIN: Pixels = px(20.);
|
||||||
const CURSOR_INSET: Pixels = px(0.5);
|
const CURSOR_INSET: Pixels = px(0.5);
|
||||||
|
|
||||||
pub(super) struct TextElement {
|
pub(super) struct TextElement {
|
||||||
|
|
@ -103,15 +104,16 @@ impl TextElement {
|
||||||
} else {
|
} else {
|
||||||
scroll_offset.x
|
scroll_offset.x
|
||||||
};
|
};
|
||||||
scroll_offset.y = if scroll_offset.y + cursor_pos.y > (bounds.size.height) {
|
scroll_offset.y =
|
||||||
// cursor is out of bottom
|
if scroll_offset.y + cursor_pos.y > (bounds.size.height - BOTTOM_MARGIN) {
|
||||||
bounds.size.height - cursor_pos.y
|
// cursor is out of bottom
|
||||||
} else if scroll_offset.y + cursor_pos.y < px(0.) {
|
bounds.size.height - BOTTOM_MARGIN - cursor_pos.y
|
||||||
// cursor is out of top
|
} else if scroll_offset.y + cursor_pos.y < px(0.) {
|
||||||
scroll_offset.y - cursor_pos.y
|
// cursor is out of top
|
||||||
} else {
|
scroll_offset.y - cursor_pos.y
|
||||||
scroll_offset.y
|
} else {
|
||||||
};
|
scroll_offset.y
|
||||||
|
};
|
||||||
|
|
||||||
if input.selection_reversed {
|
if input.selection_reversed {
|
||||||
if scroll_offset.x + cursor_start.x < px(0.) {
|
if scroll_offset.x + cursor_start.x < px(0.) {
|
||||||
|
|
|
||||||
|
|
@ -704,10 +704,15 @@ impl TextInput {
|
||||||
|
|
||||||
fn enter(&mut self, _: &Enter, cx: &mut ViewContext<Self>) {
|
fn enter(&mut self, _: &Enter, cx: &mut ViewContext<Self>) {
|
||||||
if self.is_multi_line() {
|
if self.is_multi_line() {
|
||||||
|
let is_eof = self.selected_range.end == self.text.len();
|
||||||
self.replace_text_in_range(None, "\n", cx);
|
self.replace_text_in_range(None, "\n", cx);
|
||||||
|
|
||||||
// Move cursor to the start of the next line
|
// Move cursor to the start of the next line
|
||||||
// TODO: To be test this line is valid
|
let mut new_offset = self.next_boundary(self.cursor_offset()) - 1;
|
||||||
self.move_to(self.next_boundary(self.cursor_offset()) - 1, cx);
|
if is_eof {
|
||||||
|
new_offset += 1;
|
||||||
|
}
|
||||||
|
self.move_to(new_offset, cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
cx.emit(InputEvent::PressEnter);
|
cx.emit(InputEvent::PressEnter);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue