diff options
author | 2024-02-07 22:00:53 +0100 | |
---|---|---|
committer | 2024-02-07 22:00:53 +0100 | |
commit | 111c8bfa7965cad8fe7253c8601e620b8582eaaf (patch) | |
tree | 549936d3630b06eeb6cc3349a38f0bc5bcf92226 /widget | |
parent | 5630febf963ffcdd9eb1e0e8581a65a08d501467 (diff) | |
parent | 4d7356e5e44821402df0531943681a1f3f17d385 (diff) | |
download | iced-111c8bfa7965cad8fe7253c8601e620b8582eaaf.tar.gz iced-111c8bfa7965cad8fe7253c8601e620b8582eaaf.tar.bz2 iced-111c8bfa7965cad8fe7253c8601e620b8582eaaf.zip |
Merge pull request #2221 from Dworv/text-shrink
Text editor shrinking to content
Diffstat (limited to 'widget')
-rw-r--r-- | widget/src/text_editor.rs | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 8d431991..cbcab1eb 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -64,7 +64,7 @@ where text_size: None, line_height: LineHeight::default(), width: Length::Fill, - height: Length::Fill, + height: Length::Shrink, padding: Padding::new(5.0), style: Default::default(), on_edit: None, @@ -83,6 +83,12 @@ where Theme: StyleSheet, Renderer: text::Renderer, { + /// Sets the height of the [`TextEditor`]. + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); + self + } + /// Sets the message that should be produced when some action is performed in /// the [`TextEditor`]. /// @@ -352,6 +358,8 @@ where state.highlighter_settings = self.highlighter_settings.clone(); } + let limits = limits.height(self.height); + internal.editor.update( limits.shrink(self.padding).max(), self.font.unwrap_or_else(|| renderer.default_font()), @@ -360,7 +368,21 @@ where state.highlighter.borrow_mut().deref_mut(), ); - layout::Node::new(limits.max()) + match self.height { + Length::Fill | Length::FillPortion(_) | Length::Fixed(_) => { + layout::Node::new(limits.max()) + } + Length::Shrink => { + let min_bounds = internal.editor.min_bounds(); + + layout::Node::new( + limits + .height(min_bounds.height) + .max() + .expand(Size::new(0.0, self.padding.vertical())), + ) + } + } } fn on_event( |