From bec6aa2255bd945e80d1e6f6b31b4e899177cab9 Mon Sep 17 00:00:00 2001 From: Dworv YT Date: Mon, 29 Jan 2024 14:41:12 -0800 Subject: [PATCH] feat: text-editor can shrink to content --- widget/src/text_editor.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 354abceb28..9189104d76 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -139,6 +139,19 @@ where self.style = style.into(); self } + + /// Choose whether or not to shrink the size of the editor to its contents. + pub fn shrink_to_content(mut self, shrink: bool) -> Self { + if shrink { + self.width = Length::Shrink; + self.height = Length::Shrink; + } else { + self.width = Length::Fill; + self.height = Length::Fill; + } + + self + } } /// The content of a [`TextEditor`]. @@ -358,7 +371,13 @@ where state.highlighter.borrow_mut().deref_mut(), ); - layout::Node::new(limits.max()) + if self.height == Length::Fill { + layout::Node::new(limits.max()) + } else { + let lines_height = self.line_height.to_absolute(self.text_size.unwrap_or(renderer.default_size())).0 * internal.editor.line_count() as f32; + let height = lines_height + self.padding.top + self.padding.bottom; + layout::Node::new(limits.max_height(height).max()) + } } fn on_event(