From 8b492a9b443993f1db2d3df31a29bc68738d73c9 Mon Sep 17 00:00:00 2001 From: Dworv YT Date: Mon, 29 Jan 2024 14:41:12 -0800 Subject: [PATCH 1/3] feat: text-editor can shrink to content --- widget/src/text_editor.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 8d4319911b..33793c92a5 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( From 2ba73b0faf04c21053f279e7c189b28ca718d853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 7 Feb 2024 21:48:28 +0100 Subject: [PATCH 2/3] Fix wrapped lines not being considered when measuring `TextEditor` --- core/src/renderer/null.rs | 4 ++++ core/src/text/editor.rs | 4 ++++ examples/editor/src/main.rs | 1 + graphics/src/text/editor.rs | 6 +++++ widget/src/text_editor.rs | 45 +++++++++++++++++++------------------ 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index 75a3c8b641..83688ff7c8 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -150,6 +150,10 @@ impl text::Editor for () { Size::ZERO } + fn min_bounds(&self) -> Size { + Size::ZERO + } + fn update( &mut self, _new_bounds: Size, diff --git a/core/src/text/editor.rs b/core/src/text/editor.rs index f3c6e3423f..fbf6069607 100644 --- a/core/src/text/editor.rs +++ b/core/src/text/editor.rs @@ -36,6 +36,10 @@ pub trait Editor: Sized + Default { /// Returns the current boundaries of the [`Editor`]. fn bounds(&self) -> Size; + /// Returns the minimum boundaries to fit the current contents of + /// the [`Editor`]. + fn min_bounds(&self) -> Size; + /// Updates the [`Editor`] with some new attributes. fn update( &mut self, diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index bf2aaaa38c..75b6626445 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -191,6 +191,7 @@ impl Application for Editor { column![ controls, text_editor(&self.content) + .height(Length::Fill) .on_action(Message::ActionPerformed) .highlight::( highlighter::Settings { diff --git a/graphics/src/text/editor.rs b/graphics/src/text/editor.rs index d5262ae820..c488a51c61 100644 --- a/graphics/src/text/editor.rs +++ b/graphics/src/text/editor.rs @@ -470,6 +470,12 @@ impl editor::Editor for Editor { self.internal().bounds } + fn min_bounds(&self) -> Size { + let internal = self.internal(); + + text::measure(internal.editor.buffer()) + } + fn update( &mut self, new_bounds: Size, diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 33793c92a5..cbcab1ebb4 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) -> Self { + self.height = height.into(); + self + } + /// Sets the message that should be produced when some action is performed in /// the [`TextEditor`]. /// @@ -139,17 +145,6 @@ 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`]. @@ -363,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()), @@ -371,16 +368,20 @@ where state.highlighter.borrow_mut().deref_mut(), ); - 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()) + 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())), + ) + } } } From 4d7356e5e44821402df0531943681a1f3f17d385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 7 Feb 2024 21:50:09 +0100 Subject: [PATCH 3/3] Update `CHANGELOG` --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb9afe476e..7304306897 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `text_shaping` method for `Tooltip`. [#2172](https://github.com/iced-rs/iced/pull/2172) - `interaction` method for `MouseArea`. [#2207](https://github.com/iced-rs/iced/pull/2207) - `hovered` styling for `Svg` widget. [#2163](https://github.com/iced-rs/iced/pull/2163) +- `height` method for `TextEditor`. [#2221](https://github.com/iced-rs/iced/pull/2221) - Customizable style for `TextEditor`. [#2159](https://github.com/iced-rs/iced/pull/2159) - Border width styling for `Toggler`. [#2219](https://github.com/iced-rs/iced/pull/2219) - `RawText` variant for `Primitive` in `iced_graphics`. [#2158](https://github.com/iced-rs/iced/pull/2158) @@ -118,6 +119,7 @@ Many thanks to... - @derezzedex - @DoomDuck - @dtzxporter +- @Dworv - @fogarecious - @GyulyVGC - @hicaru