diff --git a/examples/editor-libcosmic/src/main.rs b/examples/editor-libcosmic/src/main.rs index f90bd52..0bc178d 100644 --- a/examples/editor-libcosmic/src/main.rs +++ b/examples/editor-libcosmic/src/main.rs @@ -205,7 +205,7 @@ impl Application for Window { } Message::AlignmentChanged(align) => { let mut editor = self.editor.lock().unwrap(); - editor.buffer_mut().set_align(align); + update_alignment(&mut *editor, align); } Message::ThemeChanged(theme) => { self.theme = match theme { @@ -324,3 +324,10 @@ fn update_attrs<'a, T: Edit<'a>>(editor: &mut T, attrs: Attrs<'a>) { line.set_attrs_list(AttrsList::new(attrs)); }); } + +fn update_alignment<'a, T: Edit<'a>>(editor: &mut T, align: Align) { + let current_line = editor.cursor().line; + if let Some(line) = editor.buffer_mut().lines.get_mut(current_line) { + line.set_align(align); + } +} diff --git a/src/buffer.rs b/src/buffer.rs index 81f3997..7bd6554 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -10,9 +10,7 @@ use unicode_segmentation::UnicodeSegmentation; #[cfg(feature = "swash")] use crate::Color; -use crate::{ - Align, Attrs, AttrsList, BufferLine, FontSystem, LayoutGlyph, LayoutLine, ShapeLine, Wrap, -}; +use crate::{Attrs, AttrsList, BufferLine, FontSystem, LayoutGlyph, LayoutLine, ShapeLine, Wrap}; /// Current cursor location #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)] @@ -303,7 +301,6 @@ pub struct Buffer<'a> { /// True if a redraw is requires. Set to false after processing redraw: bool, wrap: Wrap, - align: Option, } impl<'a> Buffer<'a> { @@ -320,7 +317,6 @@ impl<'a> Buffer<'a> { scroll: 0, redraw: false, wrap: Wrap::Word, - align: None, }; buffer.set_text("", Attrs::new()); buffer @@ -338,7 +334,6 @@ impl<'a> Buffer<'a> { self.metrics.font_size, self.width, self.wrap, - self.align, ); } } @@ -369,7 +364,6 @@ impl<'a> Buffer<'a> { self.metrics.font_size, self.width, self.wrap, - self.align, ); total_layout += layout.len() as i32; } @@ -403,7 +397,6 @@ impl<'a> Buffer<'a> { self.metrics.font_size, self.width, self.wrap, - self.align, ); if line_i == cursor.line { let layout_cursor = self.layout_cursor(&cursor); @@ -489,7 +482,6 @@ impl<'a> Buffer<'a> { self.metrics.font_size, self.width, self.wrap, - self.align, )) } @@ -522,20 +514,6 @@ impl<'a> Buffer<'a> { } } - /// Get the current [`Align`] - pub fn align(&self) -> Option { - self.align - } - - /// Set the current [`Wrap`] - pub fn set_align(&mut self, align: Align) { - if Some(align) != self.align { - self.align = Some(align); - self.relayout(); - self.shape_until_scroll(); - } - } - /// Get the current buffer dimensions (width, height) pub fn size(&self) -> (i32, i32) { (self.width, self.height) diff --git a/src/buffer_line.rs b/src/buffer_line.rs index 2a5ff3b..57acdac 100644 --- a/src/buffer_line.rs +++ b/src/buffer_line.rs @@ -89,7 +89,7 @@ impl BufferLine { pub fn set_wrap(&mut self, wrap: Wrap) -> bool { if wrap != self.wrap { self.wrap = wrap; - self.reset(); + self.reset_layout(); true } else { false @@ -108,7 +108,7 @@ impl BufferLine { pub fn set_align(&mut self, align: Align) -> bool { if Some(align) != self.align { self.align = Some(align); - self.reset(); + self.reset_layout(); true } else { false @@ -186,11 +186,10 @@ impl BufferLine { font_size: i32, width: i32, wrap: Wrap, - align: Option, ) -> &[LayoutLine] { if self.layout_opt.is_none() { self.wrap = wrap; - self.align = align; + let align = self.align; let shape = self.shape(font_system); let layout = shape.layout(font_size, width, wrap, align); self.layout_opt = Some(layout); diff --git a/src/shape.rs b/src/shape.rs index 03370fc..41e60f5 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -605,10 +605,18 @@ impl ShapeLine { font_size: i32, line_width: i32, wrap: Wrap, - align: Align, + align: Option, ) -> Vec { let mut layout_lines = Vec::with_capacity(1); + let align = align.unwrap_or({ + if self.rtl { + Align::Right + } else { + Align::Left + } + }); + // This is used to create a visual line for empty lines (e.g. lines with only a ) let mut push_line = true; @@ -693,7 +701,7 @@ impl ShapeLine { if previous_word.blank { number_of_blanks -= 1; prev_word_width = - Some(previous_word.x_advance * font_size as f32) + Some(previous_word.x_advance * font_size as f32); } } if let Some(width) = prev_word_width { @@ -769,7 +777,7 @@ impl ShapeLine { if previous_word.blank { number_of_blanks -= 1; prev_word_width = - Some(previous_word.x_advance * font_size as f32) + Some(previous_word.x_advance * font_size as f32); } } if let Some(width) = prev_word_width { @@ -949,9 +957,8 @@ impl ShapeLine { } } } - } else - /* LTR */ - { + } else { + /* LTR */ if align != Align::Justified { x += alignment_correction; }