Removed alignment from Buffer, added alignment per Bufferline to editor-libcosmic

This commit is contained in:
Hojjat 2023-02-23 14:23:56 -07:00
parent 4e7e1cc79e
commit d313713f44
4 changed files with 25 additions and 34 deletions

View file

@ -205,7 +205,7 @@ impl Application for Window {
} }
Message::AlignmentChanged(align) => { Message::AlignmentChanged(align) => {
let mut editor = self.editor.lock().unwrap(); let mut editor = self.editor.lock().unwrap();
editor.buffer_mut().set_align(align); update_alignment(&mut *editor, align);
} }
Message::ThemeChanged(theme) => { Message::ThemeChanged(theme) => {
self.theme = match 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)); 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);
}
}

View file

@ -10,9 +10,7 @@ use unicode_segmentation::UnicodeSegmentation;
#[cfg(feature = "swash")] #[cfg(feature = "swash")]
use crate::Color; use crate::Color;
use crate::{ use crate::{Attrs, AttrsList, BufferLine, FontSystem, LayoutGlyph, LayoutLine, ShapeLine, Wrap};
Align, Attrs, AttrsList, BufferLine, FontSystem, LayoutGlyph, LayoutLine, ShapeLine, Wrap,
};
/// Current cursor location /// Current cursor location
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)] #[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 /// True if a redraw is requires. Set to false after processing
redraw: bool, redraw: bool,
wrap: Wrap, wrap: Wrap,
align: Option<Align>,
} }
impl<'a> Buffer<'a> { impl<'a> Buffer<'a> {
@ -320,7 +317,6 @@ impl<'a> Buffer<'a> {
scroll: 0, scroll: 0,
redraw: false, redraw: false,
wrap: Wrap::Word, wrap: Wrap::Word,
align: None,
}; };
buffer.set_text("", Attrs::new()); buffer.set_text("", Attrs::new());
buffer buffer
@ -338,7 +334,6 @@ impl<'a> Buffer<'a> {
self.metrics.font_size, self.metrics.font_size,
self.width, self.width,
self.wrap, self.wrap,
self.align,
); );
} }
} }
@ -369,7 +364,6 @@ impl<'a> Buffer<'a> {
self.metrics.font_size, self.metrics.font_size,
self.width, self.width,
self.wrap, self.wrap,
self.align,
); );
total_layout += layout.len() as i32; total_layout += layout.len() as i32;
} }
@ -403,7 +397,6 @@ impl<'a> Buffer<'a> {
self.metrics.font_size, self.metrics.font_size,
self.width, self.width,
self.wrap, self.wrap,
self.align,
); );
if line_i == cursor.line { if line_i == cursor.line {
let layout_cursor = self.layout_cursor(&cursor); let layout_cursor = self.layout_cursor(&cursor);
@ -489,7 +482,6 @@ impl<'a> Buffer<'a> {
self.metrics.font_size, self.metrics.font_size,
self.width, self.width,
self.wrap, self.wrap,
self.align,
)) ))
} }
@ -522,20 +514,6 @@ impl<'a> Buffer<'a> {
} }
} }
/// Get the current [`Align`]
pub fn align(&self) -> Option<Align> {
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) /// Get the current buffer dimensions (width, height)
pub fn size(&self) -> (i32, i32) { pub fn size(&self) -> (i32, i32) {
(self.width, self.height) (self.width, self.height)

View file

@ -89,7 +89,7 @@ impl BufferLine {
pub fn set_wrap(&mut self, wrap: Wrap) -> bool { pub fn set_wrap(&mut self, wrap: Wrap) -> bool {
if wrap != self.wrap { if wrap != self.wrap {
self.wrap = wrap; self.wrap = wrap;
self.reset(); self.reset_layout();
true true
} else { } else {
false false
@ -108,7 +108,7 @@ impl BufferLine {
pub fn set_align(&mut self, align: Align) -> bool { pub fn set_align(&mut self, align: Align) -> bool {
if Some(align) != self.align { if Some(align) != self.align {
self.align = Some(align); self.align = Some(align);
self.reset(); self.reset_layout();
true true
} else { } else {
false false
@ -186,11 +186,10 @@ impl BufferLine {
font_size: i32, font_size: i32,
width: i32, width: i32,
wrap: Wrap, wrap: Wrap,
align: Option<Align>,
) -> &[LayoutLine] { ) -> &[LayoutLine] {
if self.layout_opt.is_none() { if self.layout_opt.is_none() {
self.wrap = wrap; self.wrap = wrap;
self.align = align; let align = self.align;
let shape = self.shape(font_system); let shape = self.shape(font_system);
let layout = shape.layout(font_size, width, wrap, align); let layout = shape.layout(font_size, width, wrap, align);
self.layout_opt = Some(layout); self.layout_opt = Some(layout);

View file

@ -605,10 +605,18 @@ impl ShapeLine {
font_size: i32, font_size: i32,
line_width: i32, line_width: i32,
wrap: Wrap, wrap: Wrap,
align: Align, align: Option<Align>,
) -> Vec<LayoutLine> { ) -> Vec<LayoutLine> {
let mut layout_lines = Vec::with_capacity(1); 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 <CR>) // This is used to create a visual line for empty lines (e.g. lines with only a <CR>)
let mut push_line = true; let mut push_line = true;
@ -693,7 +701,7 @@ impl ShapeLine {
if previous_word.blank { if previous_word.blank {
number_of_blanks -= 1; number_of_blanks -= 1;
prev_word_width = 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 { if let Some(width) = prev_word_width {
@ -769,7 +777,7 @@ impl ShapeLine {
if previous_word.blank { if previous_word.blank {
number_of_blanks -= 1; number_of_blanks -= 1;
prev_word_width = 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 { if let Some(width) = prev_word_width {
@ -949,9 +957,8 @@ impl ShapeLine {
} }
} }
} }
} else } else {
/* LTR */ /* LTR */
{
if align != Align::Justified { if align != Align::Justified {
x += alignment_correction; x += alignment_correction;
} }