Skip to content

Commit

Permalink
Merge pull request #2221 from Dworv/text-shrink
Browse files Browse the repository at this point in the history
Text editor shrinking to content
  • Loading branch information
hecrj authored Feb 7, 2024
2 parents 5630feb + 4d7356e commit 111c8bf
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -118,6 +119,7 @@ Many thanks to...
- @derezzedex
- @DoomDuck
- @dtzxporter
- @Dworv
- @fogarecious
- @GyulyVGC
- @hicaru
Expand Down
4 changes: 4 additions & 0 deletions core/src/renderer/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ impl text::Editor for () {
Size::ZERO
}

fn min_bounds(&self) -> Size {
Size::ZERO
}

fn update(
&mut self,
_new_bounds: Size,
Expand Down
4 changes: 4 additions & 0 deletions core/src/text/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions examples/editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl Application for Editor {
column![
controls,
text_editor(&self.content)
.height(Length::Fill)
.on_action(Message::ActionPerformed)
.highlight::<Highlighter>(
highlighter::Settings {
Expand Down
6 changes: 6 additions & 0 deletions graphics/src/text/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 24 additions & 2 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`].
///
Expand Down Expand Up @@ -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()),
Expand All @@ -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(
Expand Down

0 comments on commit 111c8bf

Please sign in to comment.