add alignment option to Buffer::set_rich_text

This commit is contained in:
koe 2024-08-31 01:11:04 -05:00 committed by Jeremy Soller
parent c65f299e87
commit 0935f549ee
2 changed files with 19 additions and 7 deletions

View file

@ -103,7 +103,7 @@ fn set_buffer_text<'a>(buffer: &mut BorrowedWithFontSystem<'a, Buffer>) {
),
];
buffer.set_rich_text(spans.iter().copied(), attrs, Shaping::Advanced);
buffer.set_rich_text(spans.iter().copied(), attrs, Shaping::Advanced, None);
}
fn main() {

View file

@ -6,9 +6,9 @@ use core::{cmp, fmt};
use unicode_segmentation::UnicodeSegmentation;
use crate::{
Affinity, Attrs, AttrsList, BidiParagraphs, BorrowedWithFontSystem, BufferLine, Color, Cursor,
FontSystem, LayoutCursor, LayoutGlyph, LayoutLine, LineEnding, LineIter, Motion, Scroll,
ShapeBuffer, ShapeLine, Shaping, Wrap,
Affinity, Align, Attrs, AttrsList, BidiParagraphs, BorrowedWithFontSystem, BufferLine, Color,
Cursor, FontSystem, LayoutCursor, LayoutGlyph, LayoutLine, LineEnding, LineIter, Motion,
Scroll, ShapeBuffer, ShapeLine, Shaping, Wrap,
};
/// A line of visible text for rendering
@ -714,6 +714,7 @@ impl Buffer {
/// ],
/// attrs,
/// Shaping::Advanced,
/// None,
/// );
/// ```
pub fn set_rich_text<'r, 's, I>(
@ -722,6 +723,7 @@ impl Buffer {
spans: I,
default_attrs: Attrs,
shaping: Shaping,
alignment: Option<Align>,
) where
I: IntoIterator<Item = (&'s str, Attrs<'r>)>,
{
@ -842,6 +844,10 @@ impl Buffer {
// Discard excess lines now that we have reused as much of the existing allocations as possible.
self.lines.truncate(line_count);
self.lines.iter_mut().for_each(|line| {
line.set_align(alignment);
});
self.scroll = Scroll::default();
self.shape_until_scroll(font_system, false);
@ -1440,14 +1446,20 @@ impl<'a> BorrowedWithFontSystem<'a, Buffer> {
/// ],
/// attrs,
/// Shaping::Advanced,
/// None,
/// );
/// ```
pub fn set_rich_text<'r, 's, I>(&mut self, spans: I, default_attrs: Attrs, shaping: Shaping)
where
pub fn set_rich_text<'r, 's, I>(
&mut self,
spans: I,
default_attrs: Attrs,
shaping: Shaping,
alignment: Option<Align>,
) where
I: IntoIterator<Item = (&'s str, Attrs<'r>)>,
{
self.inner
.set_rich_text(self.font_system, spans, default_attrs, shaping);
.set_rich_text(self.font_system, spans, default_attrs, shaping, alignment);
}
/// Apply a [`Motion`] to a [`Cursor`]