Fix wrapped lines not being considered when measuring TextEditor
This commit is contained in:
parent
8b492a9b44
commit
2ba73b0faf
5 changed files with 38 additions and 22 deletions
|
|
@ -150,6 +150,10 @@ impl text::Editor for () {
|
||||||
Size::ZERO
|
Size::ZERO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn min_bounds(&self) -> Size {
|
||||||
|
Size::ZERO
|
||||||
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
_new_bounds: Size,
|
_new_bounds: Size,
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,10 @@ pub trait Editor: Sized + Default {
|
||||||
/// Returns the current boundaries of the [`Editor`].
|
/// Returns the current boundaries of the [`Editor`].
|
||||||
fn bounds(&self) -> Size;
|
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.
|
/// Updates the [`Editor`] with some new attributes.
|
||||||
fn update(
|
fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
|
||||||
|
|
@ -191,6 +191,7 @@ impl Application for Editor {
|
||||||
column![
|
column![
|
||||||
controls,
|
controls,
|
||||||
text_editor(&self.content)
|
text_editor(&self.content)
|
||||||
|
.height(Length::Fill)
|
||||||
.on_action(Message::ActionPerformed)
|
.on_action(Message::ActionPerformed)
|
||||||
.highlight::<Highlighter>(
|
.highlight::<Highlighter>(
|
||||||
highlighter::Settings {
|
highlighter::Settings {
|
||||||
|
|
|
||||||
|
|
@ -470,6 +470,12 @@ impl editor::Editor for Editor {
|
||||||
self.internal().bounds
|
self.internal().bounds
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn min_bounds(&self) -> Size {
|
||||||
|
let internal = self.internal();
|
||||||
|
|
||||||
|
text::measure(internal.editor.buffer())
|
||||||
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
new_bounds: Size,
|
new_bounds: Size,
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ where
|
||||||
text_size: None,
|
text_size: None,
|
||||||
line_height: LineHeight::default(),
|
line_height: LineHeight::default(),
|
||||||
width: Length::Fill,
|
width: Length::Fill,
|
||||||
height: Length::Fill,
|
height: Length::Shrink,
|
||||||
padding: Padding::new(5.0),
|
padding: Padding::new(5.0),
|
||||||
style: Default::default(),
|
style: Default::default(),
|
||||||
on_edit: None,
|
on_edit: None,
|
||||||
|
|
@ -83,6 +83,12 @@ where
|
||||||
Theme: StyleSheet,
|
Theme: StyleSheet,
|
||||||
Renderer: text::Renderer,
|
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
|
/// Sets the message that should be produced when some action is performed in
|
||||||
/// the [`TextEditor`].
|
/// the [`TextEditor`].
|
||||||
///
|
///
|
||||||
|
|
@ -139,17 +145,6 @@ where
|
||||||
self.style = style.into();
|
self.style = style.into();
|
||||||
self
|
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`].
|
/// The content of a [`TextEditor`].
|
||||||
|
|
@ -363,6 +358,8 @@ where
|
||||||
state.highlighter_settings = self.highlighter_settings.clone();
|
state.highlighter_settings = self.highlighter_settings.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let limits = limits.height(self.height);
|
||||||
|
|
||||||
internal.editor.update(
|
internal.editor.update(
|
||||||
limits.shrink(self.padding).max(),
|
limits.shrink(self.padding).max(),
|
||||||
self.font.unwrap_or_else(|| renderer.default_font()),
|
self.font.unwrap_or_else(|| renderer.default_font()),
|
||||||
|
|
@ -371,16 +368,20 @@ where
|
||||||
state.highlighter.borrow_mut().deref_mut(),
|
state.highlighter.borrow_mut().deref_mut(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if self.height == Length::Fill {
|
match self.height {
|
||||||
layout::Node::new(limits.max())
|
Length::Fill | Length::FillPortion(_) | Length::Fixed(_) => {
|
||||||
} else {
|
layout::Node::new(limits.max())
|
||||||
let lines_height = self
|
}
|
||||||
.line_height
|
Length::Shrink => {
|
||||||
.to_absolute(self.text_size.unwrap_or(renderer.default_size()))
|
let min_bounds = internal.editor.min_bounds();
|
||||||
.0
|
|
||||||
* internal.editor.line_count() as f32;
|
layout::Node::new(
|
||||||
let height = lines_height + self.padding.top + self.padding.bottom;
|
limits
|
||||||
layout::Node::new(limits.max_height(height).max())
|
.height(min_bounds.height)
|
||||||
|
.max()
|
||||||
|
.expand(Size::new(0.0, self.padding.vertical())),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue