diff --git a/examples/editor-libcosmic/src/text_box.rs b/examples/editor-libcosmic/src/text_box.rs index e03a315..c2a864d 100644 --- a/examples/editor-libcosmic/src/text_box.rs +++ b/examples/editor-libcosmic/src/text_box.rs @@ -39,11 +39,11 @@ where Renderer: renderer::Renderer, { fn width(&self) -> Length { - Length::Shrink + Length::Fill } fn height(&self) -> Length { - Length::Shrink + Length::Fill } fn layout( @@ -95,7 +95,7 @@ where for (line_i, line) in buffer .layout_lines() .iter() - .skip(buffer.scroll as usize) + .skip(buffer.scroll() as usize) .take(buffer.lines() as usize) .enumerate() { @@ -104,7 +104,7 @@ where start_line_opt = Some(end_line); } - if buffer.cursor.line == line_i + buffer.scroll as usize { + if buffer.cursor.line == line_i + buffer.scroll() as usize { if buffer.cursor.glyph >= line.glyphs.len() { let x = match line.glyphs.last() { Some(glyph) => glyph.x + glyph.w, diff --git a/examples/editor-orbclient/src/main.rs b/examples/editor-orbclient/src/main.rs index 6cdf913..aa38c1c 100644 --- a/examples/editor-orbclient/src/main.rs +++ b/examples/editor-orbclient/src/main.rs @@ -97,9 +97,6 @@ fn main() { let mut mouse_left = false; let mut rehit = false; loop { - //TODO: do not use this - buffer.shape_until_scroll(); - let font_size = buffer.font_size(); let line_height = buffer.line_height(); @@ -112,7 +109,7 @@ fn main() { for (line_i, line) in buffer .layout_lines() .iter() - .skip(buffer.scroll as usize) + .skip(buffer.scroll() as usize) .enumerate() { if line_y >= window.height() as i32 { @@ -123,7 +120,7 @@ fn main() { && mouse_y >= line_y - font_size && mouse_y < line_y - font_size + line_height { - let new_cursor_line = line_i + buffer.scroll as usize; + let new_cursor_line = line_i + buffer.scroll() as usize; let mut new_cursor_glyph = line.glyphs.len(); for (glyph_i, glyph) in line.glyphs.iter().enumerate() { if mouse_x >= line_x + glyph.x as i32 @@ -162,7 +159,7 @@ fn main() { for (line_i, line) in buffer .layout_lines() .iter() - .skip(buffer.scroll as usize) + .skip(buffer.scroll() as usize) .enumerate() { if line_y >= window.height() as i32 { @@ -174,7 +171,7 @@ fn main() { start_line_opt = Some(end_line); } - if buffer.cursor.line == line_i + buffer.scroll as usize { + if buffer.cursor.line == line_i + buffer.scroll() as usize { if buffer.cursor.glyph >= line.glyphs.len() { let x = match line.glyphs.last() { Some(glyph) => glyph.x + glyph.w, @@ -315,8 +312,7 @@ fn main() { ); } EventOption::Scroll(event) => { - buffer.scroll -= event.y * 3; - buffer.redraw = true; + buffer.action(TextAction::Scroll(-event.y * 3)); } EventOption::Quit(_) => return, _ => (), diff --git a/src/buffer.rs b/src/buffer.rs index 6ce214f..14a63d5 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -15,6 +15,7 @@ pub enum TextAction { PageUp, PageDown, Insert(char), + Scroll(i32), } #[derive(Default, Eq, PartialEq)] @@ -38,9 +39,9 @@ pub struct TextBuffer<'a> { line_height: i32, width: i32, height: i32, + scroll: i32, pub cursor: TextCursor, pub redraw: bool, - pub scroll: i32, } impl<'a> TextBuffer<'a> { @@ -56,7 +57,7 @@ impl<'a> TextBuffer<'a> { if text_lines.is_empty() { text_lines.push(String::new()); } - Self { + let mut buffer = Self { font_matches, text_lines, shape_lines: Vec::new(), @@ -68,7 +69,9 @@ impl<'a> TextBuffer<'a> { cursor: TextCursor::default(), redraw: false, scroll: 0, - } + }; + buffer.shape_until_scroll(); + buffer } pub fn shape_until(&mut self, lines: i32) { @@ -191,6 +194,7 @@ impl<'a> TextBuffer<'a> { if font_size != self.font_size { self.font_size = font_size; self.relayout(); + self.shape_until_scroll(); } if line_height != self.line_height { @@ -207,6 +211,10 @@ impl<'a> TextBuffer<'a> { self.height } + pub fn scroll(&self) -> i32 { + self.scroll + } + pub fn lines(&self) -> i32 { self.height / self.line_height } @@ -215,6 +223,7 @@ impl<'a> TextBuffer<'a> { if width != self.width { self.width = width; self.relayout(); + self.shape_until_scroll(); } if height != self.height { @@ -316,6 +325,7 @@ impl<'a> TextBuffer<'a> { self.shape_until_scroll(); }, TextAction::Insert(character) => { + //TODO: handle Enter let line = &self.layout_lines[self.cursor.line]; if self.cursor.glyph >= line.glyphs.len() { match line.glyphs.last() { @@ -340,6 +350,11 @@ impl<'a> TextBuffer<'a> { self.reshape_line(line.line_i); } }, + TextAction::Scroll(lines) => { + self.scroll += lines; + self.redraw = true; + self.shape_until_scroll(); + } } } }