Add alignment parameter to set_text (#419)

* add alignment parameter to set_text

* Fix doc comment
This commit is contained in:
Erik McClure 2025-09-07 11:40:42 -07:00 committed by GitHub
parent 750e1a4dd1
commit 3c1f6c9e8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 30 additions and 11 deletions

View file

@ -271,7 +271,7 @@ impl Buffer {
/// Will panic if `metrics.line_height` is zero.
pub fn new(font_system: &mut FontSystem, metrics: Metrics) -> Self {
let mut buffer = Self::new_empty(metrics);
buffer.set_text(font_system, "", &Attrs::new(), Shaping::Advanced);
buffer.set_text(font_system, "", &Attrs::new(), Shaping::Advanced, None);
buffer
}
@ -679,6 +679,7 @@ impl Buffer {
text: &str,
attrs: &Attrs,
shaping: Shaping,
alignment: Option<Align>,
) {
self.lines.clear();
for (range, ending) in LineIter::new(text) {
@ -697,6 +698,13 @@ impl Buffer {
shaping,
));
}
if alignment.is_some() {
self.lines.iter_mut().for_each(|line| {
line.set_align(alignment);
});
}
self.scroll = Scroll::default();
self.shape_until_scroll(font_system, false);
}
@ -1424,8 +1432,15 @@ impl BorrowedWithFontSystem<'_, Buffer> {
}
/// Set text of buffer, using provided attributes for each line by default
pub fn set_text(&mut self, text: &str, attrs: &Attrs, shaping: Shaping) {
self.inner.set_text(self.font_system, text, attrs, shaping);
pub fn set_text(
&mut self,
text: &str,
attrs: &Attrs,
shaping: Shaping,
alignment: Option<Align>,
) {
self.inner
.set_text(self.font_system, text, attrs, shaping, alignment);
}
/// Set text of buffer, using an iterator of styled spans (pairs of text and attributes)