Return change flag in paragraph::Plain::update
This commit is contained in:
parent
9b710a9aa8
commit
f031b99196
5 changed files with 32 additions and 18 deletions
|
|
@ -79,11 +79,13 @@ impl<P: Paragraph> Plain<P> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates the plain [`Paragraph`] to match the given [`Text`], if needed.
|
/// Updates the plain [`Paragraph`] to match the given [`Text`], if needed.
|
||||||
pub fn update(&mut self, text: Text<&str, P::Font>) {
|
///
|
||||||
|
/// Returns true if the [`Paragraph`] changed.
|
||||||
|
pub fn update(&mut self, text: Text<&str, P::Font>) -> bool {
|
||||||
if self.content != text.content {
|
if self.content != text.content {
|
||||||
text.content.clone_into(&mut self.content);
|
text.content.clone_into(&mut self.content);
|
||||||
self.raw = P::with_text(text);
|
self.raw = P::with_text(text);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.raw.compare(Text {
|
match self.raw.compare(Text {
|
||||||
|
|
@ -97,12 +99,14 @@ impl<P: Paragraph> Plain<P> {
|
||||||
shaping: text.shaping,
|
shaping: text.shaping,
|
||||||
wrapping: text.wrapping,
|
wrapping: text.wrapping,
|
||||||
}) {
|
}) {
|
||||||
Difference::None => {}
|
Difference::None => false,
|
||||||
Difference::Bounds => {
|
Difference::Bounds => {
|
||||||
self.raw.resize(text.bounds);
|
self.raw.resize(text.bounds);
|
||||||
|
true
|
||||||
}
|
}
|
||||||
Difference::Shape => {
|
Difference::Shape => {
|
||||||
self.raw = P::with_text(text);
|
self.raw = P::with_text(text);
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -303,7 +303,7 @@ where
|
||||||
|
|
||||||
let State(paragraph) = state;
|
let State(paragraph) = state;
|
||||||
|
|
||||||
paragraph.update(text::Text {
|
let _ = paragraph.update(text::Text {
|
||||||
content,
|
content,
|
||||||
bounds,
|
bounds,
|
||||||
size,
|
size,
|
||||||
|
|
@ -340,25 +340,35 @@ pub fn draw<Renderer>(
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
|
let anchor = anchor(bounds, paragraph.align_x(), paragraph.align_y());
|
||||||
|
|
||||||
let x = match paragraph.align_x() {
|
renderer.fill_paragraph(
|
||||||
|
paragraph,
|
||||||
|
anchor,
|
||||||
|
appearance.color.unwrap_or(style.text_color),
|
||||||
|
*viewport,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the anchor position of a [`Text`] widget.
|
||||||
|
pub fn anchor(
|
||||||
|
bounds: Rectangle,
|
||||||
|
align_x: text::Alignment,
|
||||||
|
align_y: alignment::Vertical,
|
||||||
|
) -> Point {
|
||||||
|
let x = match align_x {
|
||||||
Alignment::Default | Alignment::Left | Alignment::Justified => bounds.x,
|
Alignment::Default | Alignment::Left | Alignment::Justified => bounds.x,
|
||||||
Alignment::Center => bounds.center_x(),
|
Alignment::Center => bounds.center_x(),
|
||||||
Alignment::Right => bounds.x + bounds.width,
|
Alignment::Right => bounds.x + bounds.width,
|
||||||
};
|
};
|
||||||
|
|
||||||
let y = match paragraph.align_y() {
|
let y = match align_y {
|
||||||
alignment::Vertical::Top => bounds.y,
|
alignment::Vertical::Top => bounds.y,
|
||||||
alignment::Vertical::Center => bounds.center_y(),
|
alignment::Vertical::Center => bounds.center_y(),
|
||||||
alignment::Vertical::Bottom => bounds.y + bounds.height,
|
alignment::Vertical::Bottom => bounds.y + bounds.height,
|
||||||
};
|
};
|
||||||
|
|
||||||
renderer.fill_paragraph(
|
Point::new(x, y)
|
||||||
paragraph,
|
|
||||||
Point::new(x, y),
|
|
||||||
appearance.color.unwrap_or(style.text_color),
|
|
||||||
*viewport,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Theme, Renderer> From<Text<'a, Theme, Renderer>>
|
impl<'a, Message, Theme, Renderer> From<Text<'a, Theme, Renderer>>
|
||||||
|
|
|
||||||
|
|
@ -421,7 +421,7 @@ where
|
||||||
shaping: *shaping,
|
shaping: *shaping,
|
||||||
wrapping: text::Wrapping::default(),
|
wrapping: text::Wrapping::default(),
|
||||||
},
|
},
|
||||||
bounds.center(),
|
bounds.position(),
|
||||||
style.icon_color,
|
style.icon_color,
|
||||||
*viewport,
|
*viewport,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -381,14 +381,14 @@ where
|
||||||
{
|
{
|
||||||
let label = option.to_string();
|
let label = option.to_string();
|
||||||
|
|
||||||
paragraph.update(Text {
|
let _ = paragraph.update(Text {
|
||||||
content: &label,
|
content: &label,
|
||||||
..option_text
|
..option_text
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(placeholder) = &self.placeholder {
|
if let Some(placeholder) = &self.placeholder {
|
||||||
state.placeholder.update(Text {
|
let _ = state.placeholder.update(Text {
|
||||||
content: placeholder,
|
content: placeholder,
|
||||||
..option_text
|
..option_text
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -325,12 +325,12 @@ where
|
||||||
wrapping: text::Wrapping::default(),
|
wrapping: text::Wrapping::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
state.placeholder.update(placeholder_text);
|
let _ = state.placeholder.update(placeholder_text);
|
||||||
|
|
||||||
let secure_value = self.is_secure.then(|| value.secure());
|
let secure_value = self.is_secure.then(|| value.secure());
|
||||||
let value = secure_value.as_ref().unwrap_or(value);
|
let value = secure_value.as_ref().unwrap_or(value);
|
||||||
|
|
||||||
state.value.update(Text {
|
let _ = state.value.update(Text {
|
||||||
content: &value.to_string(),
|
content: &value.to_string(),
|
||||||
..placeholder_text
|
..placeholder_text
|
||||||
});
|
});
|
||||||
|
|
@ -350,7 +350,7 @@ where
|
||||||
wrapping: text::Wrapping::default(),
|
wrapping: text::Wrapping::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
state.icon.update(icon_text);
|
let _ = state.icon.update(icon_text);
|
||||||
|
|
||||||
let icon_width = state.icon.min_width();
|
let icon_width = state.icon.min_width();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue