diff options
author | 2024-01-29 14:41:12 -0800 | |
---|---|---|
committer | 2024-02-07 21:28:45 +0100 | |
commit | 8b492a9b443993f1db2d3df31a29bc68738d73c9 (patch) | |
tree | 8c56f1c9b80ff86a2296d0d42c20bb63db245e43 /widget/src | |
parent | 5630febf963ffcdd9eb1e0e8581a65a08d501467 (diff) | |
download | iced-8b492a9b443993f1db2d3df31a29bc68738d73c9.tar.gz iced-8b492a9b443993f1db2d3df31a29bc68738d73c9.tar.bz2 iced-8b492a9b443993f1db2d3df31a29bc68738d73c9.zip |
feat: text-editor can shrink to content
Diffstat (limited to 'widget/src')
-rw-r--r-- | widget/src/text_editor.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 8d431991..33793c92 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -139,6 +139,17 @@ 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.height = Length::Shrink; + } else { + self.height = Length::Fill; + } + + self + } } /// The content of a [`TextEditor`]. @@ -360,7 +371,17 @@ 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( |