Skip to content

Commit

Permalink
feat: text-editor can shrink to content
Browse files Browse the repository at this point in the history
  • Loading branch information
kyteware committed Jan 29, 2024
1 parent 5540ac0 commit bec6aa2
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit bec6aa2

Please sign in to comment.