chore: fix input auto-grow height

This commit is contained in:
2025-05-19 07:25:39 +07:00
parent 71fbd97bad
commit ba42bafc3a
3 changed files with 57 additions and 43 deletions

View File

@@ -220,6 +220,7 @@ pub struct InputState {
pub(super) rows: usize,
pub(super) min_rows: usize,
pub(super) max_rows: Option<usize>,
pub(super) auto_grow: bool,
pub(super) pattern: Option<regex::Regex>,
pub(super) validate: Validate,
pub(crate) scroll_handle: ScrollHandle,
@@ -284,6 +285,7 @@ impl InputState {
rows: 3,
min_rows: 3,
max_rows: None,
auto_grow: false,
height: None,
last_layout: None,
last_bounds: None,
@@ -489,6 +491,12 @@ impl InputState {
self
}
/// Set the auto-grow mode for the multi-line Textarea.
pub fn auto_grow(mut self) -> Self {
self.auto_grow = true;
self
}
/// Set the text of the input field.
///
/// And the selection_range will be reset to 0..0.
@@ -1010,25 +1018,6 @@ impl InputState {
});
}
fn check_to_auto_grow(&mut self, _: &mut Window, cx: &mut Context<Self>) {
if !self.is_multi_line() {
return;
}
let Some(max_rows) = self.max_rows else {
return;
};
let changed_rows = ((self.scroll_size.height - self.input_bounds.size.height)
/ self.last_line_height) as isize;
self.rows = (self.rows as isize + changed_rows)
.clamp(self.min_rows as isize, max_rows as isize)
.max(0) as usize;
cx.notify();
}
pub(super) fn clean(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.replace_text("", window, cx);
}
@@ -1591,6 +1580,10 @@ impl EntityInputHandler for InputState {
self.marked_range = None;
}
/// Replace text in range.
///
/// - If the new text is invalid, it will not be replaced.
/// - If `range_utf16` is not provided, the current selected range will be used.
fn replace_text_in_range(
&mut self,
range_utf16: Option<Range<usize>>,
@@ -1628,7 +1621,6 @@ impl EntityInputHandler for InputState {
self.update_preferred_x_offset(cx);
self.update_scroll_offset(None, cx);
self.check_to_auto_grow(window, cx);
cx.emit(InputEvent::Change(self.unmask_value()));
cx.notify();