Skip to content

Commit

Permalink
add alignment option to Buffer::set_rich_text
Browse files Browse the repository at this point in the history
  • Loading branch information
UkoeHB authored and jackpot51 committed Sep 1, 2024
1 parent c65f299 commit 0935f54
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
24 changes: 18 additions & 6 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -714,6 +714,7 @@ impl Buffer {
/// ],
/// attrs,
/// Shaping::Advanced,
/// None,
/// );
/// ```
pub fn set_rich_text<'r, 's, I>(
Expand All @@ -722,6 +723,7 @@ impl Buffer {
spans: I,
default_attrs: Attrs,
shaping: Shaping,
alignment: Option<Align>,
) where
I: IntoIterator<Item = (&'s str, Attrs<'r>)>,
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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`]
Expand Down

0 comments on commit 0935f54

Please sign in to comment.