Add scroll bar

This commit is contained in:
Jeremy Soller 2022-10-12 14:56:11 -06:00
parent eef1f2407b
commit bc97a1323a
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE

View file

@ -6,6 +6,7 @@ use std::{
time::Instant,
};
use text::{
FontLineIndex,
FontSystem,
TextAction,
TextCursor,
@ -185,11 +186,18 @@ fn main() {
window.set(bg_color);
let mut line_y = line_height;
let mut start_line_opt = None;
let mut end_line = FontLineIndex::new(0);
for (line_i, line) in buffer.layout_lines().iter().skip(scroll as usize).enumerate() {
if line_y >= window.height() as i32 {
break;
}
end_line = line.line_i;
if start_line_opt == None {
start_line_opt = Some(end_line);
}
if buffer.cursor.line == line_i + scroll as usize {
if buffer.cursor.glyph >= line.glyphs.len() {
let x = match line.glyphs.last() {
@ -225,6 +233,24 @@ fn main() {
line_y += line_height;
}
// Draw scrollbar
{
let start_line = start_line_opt.unwrap_or(end_line);
let lines = buffer.text_lines().len();
println!("{},{}/{}", start_line.get(), end_line.get(), lines);
let start_y = (start_line.get() * window.height() as usize) / lines;
let end_y = (end_line.get() * window.height() as usize) / lines;
if end_y > start_y {
window.rect(
window.width() as i32 - line_x as i32,
start_y as i32,
line_x as u32,
(end_y - start_y) as u32,
Color::rgba(0xFF, 0xFF, 0xFF, 0x40)
);
}
}
window.sync();
buffer.redraw = false;