Handle scroll in TextBuffer
This commit is contained in:
parent
c114fe19f3
commit
3e04ffdfa4
4 changed files with 129 additions and 61 deletions
109
src/buffer.rs
109
src/buffer.rs
|
|
@ -1,4 +1,7 @@
|
|||
use std::time::Instant;
|
||||
use std::{
|
||||
cmp,
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
use crate::{FontLayoutLine, FontLineIndex, FontMatches, FontShapeLine};
|
||||
|
||||
|
|
@ -9,6 +12,8 @@ pub enum TextAction {
|
|||
Down,
|
||||
Backspace,
|
||||
Delete,
|
||||
PageUp,
|
||||
PageDown,
|
||||
Insert(char),
|
||||
}
|
||||
|
||||
|
|
@ -30,13 +35,23 @@ pub struct TextBuffer<'a> {
|
|||
shape_lines: Vec<FontShapeLine<'a>>,
|
||||
layout_lines: Vec<FontLayoutLine<'a>>,
|
||||
font_size: i32,
|
||||
line_width: i32,
|
||||
line_height: i32,
|
||||
width: i32,
|
||||
height: i32,
|
||||
pub cursor: TextCursor,
|
||||
pub redraw: bool,
|
||||
pub scroll: i32,
|
||||
}
|
||||
|
||||
impl<'a> TextBuffer<'a> {
|
||||
pub fn new(font_matches: &'a FontMatches<'a>, text: &str, font_size: i32, line_width: i32) -> Self {
|
||||
pub fn new(
|
||||
font_matches: &'a FontMatches<'a>,
|
||||
text: &str,
|
||||
font_size: i32,
|
||||
line_height: i32,
|
||||
width: i32,
|
||||
height: i32,
|
||||
) -> Self {
|
||||
let mut text_lines: Vec<String> = text.lines().map(String::from).collect();
|
||||
if text_lines.is_empty() {
|
||||
text_lines.push(String::new());
|
||||
|
|
@ -47,9 +62,12 @@ impl<'a> TextBuffer<'a> {
|
|||
shape_lines: Vec::new(),
|
||||
layout_lines: Vec::new(),
|
||||
font_size,
|
||||
line_width,
|
||||
line_height,
|
||||
width,
|
||||
height,
|
||||
cursor: TextCursor::default(),
|
||||
redraw: false,
|
||||
scroll: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -71,6 +89,21 @@ impl<'a> TextBuffer<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn shape_until_scroll(&mut self) {
|
||||
let lines = self.height / self.line_height;
|
||||
|
||||
let scroll_end = self.scroll + lines;
|
||||
self.shape_until(scroll_end);
|
||||
|
||||
self.scroll = cmp::max(
|
||||
0,
|
||||
cmp::min(
|
||||
self.layout_lines().len() as i32 - (lines - 1),
|
||||
self.scroll,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn reshape_line(&mut self, line_i: FontLineIndex) {
|
||||
let instant = Instant::now();
|
||||
|
||||
|
|
@ -97,7 +130,7 @@ impl<'a> TextBuffer<'a> {
|
|||
let layout_i = self.layout_lines.len();
|
||||
line.layout(
|
||||
self.font_size,
|
||||
self.line_width,
|
||||
self.width,
|
||||
&mut self.layout_lines,
|
||||
layout_i,
|
||||
);
|
||||
|
|
@ -131,7 +164,7 @@ impl<'a> TextBuffer<'a> {
|
|||
let shape_line = &self.shape_lines[line_i.get()];
|
||||
shape_line.layout(
|
||||
self.font_size,
|
||||
self.line_width,
|
||||
self.width,
|
||||
&mut self.layout_lines,
|
||||
insert_i,
|
||||
);
|
||||
|
|
@ -150,18 +183,40 @@ impl<'a> TextBuffer<'a> {
|
|||
self.font_size
|
||||
}
|
||||
|
||||
pub fn set_font_size(&mut self, font_size: i32) {
|
||||
self.font_size = font_size;
|
||||
self.relayout();
|
||||
pub fn line_height(&self) -> i32 {
|
||||
self.line_height
|
||||
}
|
||||
|
||||
pub fn line_width(&self) -> i32 {
|
||||
self.line_width
|
||||
pub fn set_font_metrics(&mut self, font_size: i32, line_height: i32) {
|
||||
if font_size != self.font_size {
|
||||
self.font_size = font_size;
|
||||
self.relayout();
|
||||
}
|
||||
|
||||
if line_height != self.line_height {
|
||||
self.line_height = line_height;
|
||||
self.shape_until_scroll();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_line_width(&mut self, line_width: i32) {
|
||||
self.line_width = line_width;
|
||||
self.relayout();
|
||||
pub fn width(&self) -> i32 {
|
||||
self.width
|
||||
}
|
||||
|
||||
pub fn height(&self) -> i32 {
|
||||
self.height
|
||||
}
|
||||
|
||||
pub fn set_size(&mut self, width: i32, height: i32) {
|
||||
if width != self.width {
|
||||
self.width = width;
|
||||
self.relayout();
|
||||
}
|
||||
|
||||
if height != self.height {
|
||||
self.height = height;
|
||||
self.shape_until_scroll();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn layout_lines(&self) -> &[FontLayoutLine] {
|
||||
|
|
@ -184,7 +239,7 @@ impl<'a> TextBuffer<'a> {
|
|||
self.cursor.glyph -= 1;
|
||||
self.redraw = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
TextAction::Right => {
|
||||
let line = &self.layout_lines[self.cursor.line];
|
||||
if self.cursor.glyph > line.glyphs.len() {
|
||||
|
|
@ -195,19 +250,19 @@ impl<'a> TextBuffer<'a> {
|
|||
self.cursor.glyph += 1;
|
||||
self.redraw = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
TextAction::Up => {
|
||||
if self.cursor.line > 0 {
|
||||
self.cursor.line -= 1;
|
||||
self.redraw = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
TextAction::Down => {
|
||||
if self.cursor.line + 1 < self.layout_lines.len() {
|
||||
self.cursor.line += 1;
|
||||
self.redraw = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
TextAction::Backspace => {
|
||||
let line = &self.layout_lines[self.cursor.line];
|
||||
if self.cursor.glyph > line.glyphs.len() {
|
||||
|
|
@ -221,7 +276,7 @@ impl<'a> TextBuffer<'a> {
|
|||
text_line.remove(glyph.start);
|
||||
self.reshape_line(line.line_i);
|
||||
}
|
||||
}
|
||||
},
|
||||
TextAction::Delete => {
|
||||
let line = &self.layout_lines[self.cursor.line];
|
||||
if self.cursor.glyph < line.glyphs.len() {
|
||||
|
|
@ -230,7 +285,19 @@ impl<'a> TextBuffer<'a> {
|
|||
text_line.remove(glyph.start);
|
||||
self.reshape_line(line.line_i);
|
||||
}
|
||||
}
|
||||
},
|
||||
TextAction::PageUp => {
|
||||
let lines = self.height / self.line_height;
|
||||
self.scroll -= lines;
|
||||
self.redraw = true;
|
||||
self.shape_until_scroll();
|
||||
},
|
||||
TextAction::PageDown => {
|
||||
let lines = self.height / self.line_height;
|
||||
self.scroll += lines;
|
||||
self.redraw = true;
|
||||
self.shape_until_scroll();
|
||||
},
|
||||
TextAction::Insert(character) => {
|
||||
let line = &self.layout_lines[self.cursor.line];
|
||||
if self.cursor.glyph >= line.glyphs.len() {
|
||||
|
|
@ -255,7 +322,7 @@ impl<'a> TextBuffer<'a> {
|
|||
self.cursor.glyph += 1;
|
||||
self.reshape_line(line.line_i);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue