Move all scroll handling to buffer
This commit is contained in:
parent
a21225c9a0
commit
2f6a9d33d1
3 changed files with 27 additions and 16 deletions
|
|
@ -39,11 +39,11 @@ where
|
||||||
Renderer: renderer::Renderer,
|
Renderer: renderer::Renderer,
|
||||||
{
|
{
|
||||||
fn width(&self) -> Length {
|
fn width(&self) -> Length {
|
||||||
Length::Shrink
|
Length::Fill
|
||||||
}
|
}
|
||||||
|
|
||||||
fn height(&self) -> Length {
|
fn height(&self) -> Length {
|
||||||
Length::Shrink
|
Length::Fill
|
||||||
}
|
}
|
||||||
|
|
||||||
fn layout(
|
fn layout(
|
||||||
|
|
@ -95,7 +95,7 @@ where
|
||||||
for (line_i, line) in buffer
|
for (line_i, line) in buffer
|
||||||
.layout_lines()
|
.layout_lines()
|
||||||
.iter()
|
.iter()
|
||||||
.skip(buffer.scroll as usize)
|
.skip(buffer.scroll() as usize)
|
||||||
.take(buffer.lines() as usize)
|
.take(buffer.lines() as usize)
|
||||||
.enumerate()
|
.enumerate()
|
||||||
{
|
{
|
||||||
|
|
@ -104,7 +104,7 @@ where
|
||||||
start_line_opt = Some(end_line);
|
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() {
|
if buffer.cursor.glyph >= line.glyphs.len() {
|
||||||
let x = match line.glyphs.last() {
|
let x = match line.glyphs.last() {
|
||||||
Some(glyph) => glyph.x + glyph.w,
|
Some(glyph) => glyph.x + glyph.w,
|
||||||
|
|
|
||||||
|
|
@ -97,9 +97,6 @@ fn main() {
|
||||||
let mut mouse_left = false;
|
let mut mouse_left = false;
|
||||||
let mut rehit = false;
|
let mut rehit = false;
|
||||||
loop {
|
loop {
|
||||||
//TODO: do not use this
|
|
||||||
buffer.shape_until_scroll();
|
|
||||||
|
|
||||||
let font_size = buffer.font_size();
|
let font_size = buffer.font_size();
|
||||||
let line_height = buffer.line_height();
|
let line_height = buffer.line_height();
|
||||||
|
|
||||||
|
|
@ -112,7 +109,7 @@ fn main() {
|
||||||
for (line_i, line) in buffer
|
for (line_i, line) in buffer
|
||||||
.layout_lines()
|
.layout_lines()
|
||||||
.iter()
|
.iter()
|
||||||
.skip(buffer.scroll as usize)
|
.skip(buffer.scroll() as usize)
|
||||||
.enumerate()
|
.enumerate()
|
||||||
{
|
{
|
||||||
if line_y >= window.height() as i32 {
|
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
|
||||||
&& mouse_y < line_y - font_size + line_height
|
&& 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();
|
let mut new_cursor_glyph = line.glyphs.len();
|
||||||
for (glyph_i, glyph) in line.glyphs.iter().enumerate() {
|
for (glyph_i, glyph) in line.glyphs.iter().enumerate() {
|
||||||
if mouse_x >= line_x + glyph.x as i32
|
if mouse_x >= line_x + glyph.x as i32
|
||||||
|
|
@ -162,7 +159,7 @@ fn main() {
|
||||||
for (line_i, line) in buffer
|
for (line_i, line) in buffer
|
||||||
.layout_lines()
|
.layout_lines()
|
||||||
.iter()
|
.iter()
|
||||||
.skip(buffer.scroll as usize)
|
.skip(buffer.scroll() as usize)
|
||||||
.enumerate()
|
.enumerate()
|
||||||
{
|
{
|
||||||
if line_y >= window.height() as i32 {
|
if line_y >= window.height() as i32 {
|
||||||
|
|
@ -174,7 +171,7 @@ fn main() {
|
||||||
start_line_opt = Some(end_line);
|
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() {
|
if buffer.cursor.glyph >= line.glyphs.len() {
|
||||||
let x = match line.glyphs.last() {
|
let x = match line.glyphs.last() {
|
||||||
Some(glyph) => glyph.x + glyph.w,
|
Some(glyph) => glyph.x + glyph.w,
|
||||||
|
|
@ -315,8 +312,7 @@ fn main() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
EventOption::Scroll(event) => {
|
EventOption::Scroll(event) => {
|
||||||
buffer.scroll -= event.y * 3;
|
buffer.action(TextAction::Scroll(-event.y * 3));
|
||||||
buffer.redraw = true;
|
|
||||||
}
|
}
|
||||||
EventOption::Quit(_) => return,
|
EventOption::Quit(_) => return,
|
||||||
_ => (),
|
_ => (),
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ pub enum TextAction {
|
||||||
PageUp,
|
PageUp,
|
||||||
PageDown,
|
PageDown,
|
||||||
Insert(char),
|
Insert(char),
|
||||||
|
Scroll(i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Eq, PartialEq)]
|
#[derive(Default, Eq, PartialEq)]
|
||||||
|
|
@ -38,9 +39,9 @@ pub struct TextBuffer<'a> {
|
||||||
line_height: i32,
|
line_height: i32,
|
||||||
width: i32,
|
width: i32,
|
||||||
height: i32,
|
height: i32,
|
||||||
|
scroll: i32,
|
||||||
pub cursor: TextCursor,
|
pub cursor: TextCursor,
|
||||||
pub redraw: bool,
|
pub redraw: bool,
|
||||||
pub scroll: i32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TextBuffer<'a> {
|
impl<'a> TextBuffer<'a> {
|
||||||
|
|
@ -56,7 +57,7 @@ impl<'a> TextBuffer<'a> {
|
||||||
if text_lines.is_empty() {
|
if text_lines.is_empty() {
|
||||||
text_lines.push(String::new());
|
text_lines.push(String::new());
|
||||||
}
|
}
|
||||||
Self {
|
let mut buffer = Self {
|
||||||
font_matches,
|
font_matches,
|
||||||
text_lines,
|
text_lines,
|
||||||
shape_lines: Vec::new(),
|
shape_lines: Vec::new(),
|
||||||
|
|
@ -68,7 +69,9 @@ impl<'a> TextBuffer<'a> {
|
||||||
cursor: TextCursor::default(),
|
cursor: TextCursor::default(),
|
||||||
redraw: false,
|
redraw: false,
|
||||||
scroll: 0,
|
scroll: 0,
|
||||||
}
|
};
|
||||||
|
buffer.shape_until_scroll();
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shape_until(&mut self, lines: i32) {
|
pub fn shape_until(&mut self, lines: i32) {
|
||||||
|
|
@ -191,6 +194,7 @@ impl<'a> TextBuffer<'a> {
|
||||||
if font_size != self.font_size {
|
if font_size != self.font_size {
|
||||||
self.font_size = font_size;
|
self.font_size = font_size;
|
||||||
self.relayout();
|
self.relayout();
|
||||||
|
self.shape_until_scroll();
|
||||||
}
|
}
|
||||||
|
|
||||||
if line_height != self.line_height {
|
if line_height != self.line_height {
|
||||||
|
|
@ -207,6 +211,10 @@ impl<'a> TextBuffer<'a> {
|
||||||
self.height
|
self.height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn scroll(&self) -> i32 {
|
||||||
|
self.scroll
|
||||||
|
}
|
||||||
|
|
||||||
pub fn lines(&self) -> i32 {
|
pub fn lines(&self) -> i32 {
|
||||||
self.height / self.line_height
|
self.height / self.line_height
|
||||||
}
|
}
|
||||||
|
|
@ -215,6 +223,7 @@ impl<'a> TextBuffer<'a> {
|
||||||
if width != self.width {
|
if width != self.width {
|
||||||
self.width = width;
|
self.width = width;
|
||||||
self.relayout();
|
self.relayout();
|
||||||
|
self.shape_until_scroll();
|
||||||
}
|
}
|
||||||
|
|
||||||
if height != self.height {
|
if height != self.height {
|
||||||
|
|
@ -316,6 +325,7 @@ impl<'a> TextBuffer<'a> {
|
||||||
self.shape_until_scroll();
|
self.shape_until_scroll();
|
||||||
},
|
},
|
||||||
TextAction::Insert(character) => {
|
TextAction::Insert(character) => {
|
||||||
|
//TODO: handle Enter
|
||||||
let line = &self.layout_lines[self.cursor.line];
|
let line = &self.layout_lines[self.cursor.line];
|
||||||
if self.cursor.glyph >= line.glyphs.len() {
|
if self.cursor.glyph >= line.glyphs.len() {
|
||||||
match line.glyphs.last() {
|
match line.glyphs.last() {
|
||||||
|
|
@ -340,6 +350,11 @@ impl<'a> TextBuffer<'a> {
|
||||||
self.reshape_line(line.line_i);
|
self.reshape_line(line.line_i);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
TextAction::Scroll(lines) => {
|
||||||
|
self.scroll += lines;
|
||||||
|
self.redraw = true;
|
||||||
|
self.shape_until_scroll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue