Refactor of scroll and shaping

- Scroll is identified by line index and layout index, instead of just
  layout index
- Shaping has the option to prune, where caches outside of the scroll
  view are cleared
- Syntax editor no longer requires layout of all lines, only of lines
  inside scroll
- BufferLine has a metadata field that can be used by other abstractions
  to know when text was changed
This commit is contained in:
Jeremy Soller 2023-12-15 13:37:59 -07:00
parent e7261fc06e
commit d0b4b4635e
16 changed files with 213 additions and 159 deletions

View file

@ -131,12 +131,10 @@ where
fn layout(&self, _renderer: &Renderer, limits: &layout::Limits) -> layout::Node {
let limits = limits.width(Length::Fill).height(Length::Fill);
//TODO: allow lazy shape
let mut editor = self.editor.lock().unwrap();
editor
.borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.buffer_mut()
.shape_until(i32::max_value());
.shape_as_needed(true);
let mut layout_lines = 0;
for line in editor.buffer().lines.iter() {
@ -148,7 +146,6 @@ where
let height = layout_lines as f32 * editor.buffer().metrics().line_height;
let size = Size::new(limits.max().width, height);
log::info!("size {:?}", size);
layout::Node::new(limits.resolve(size))
}
@ -228,7 +225,7 @@ where
editor.buffer_mut().set_size(image_w as f32, image_h as f32);
// Shape and layout
editor.shape_as_needed();
editor.shape_as_needed(true);
// Draw to pixel buffer
let mut pixels = vec![0; image_w as usize * image_h as usize * 4];

View file

@ -88,7 +88,7 @@ fn main() {
let mut mouse_y = -1;
let mut mouse_left = false;
loop {
editor.shape_as_needed();
editor.shape_as_needed(true);
if editor.buffer().redraw() {
let instant = Instant::now();

View file

@ -16,7 +16,7 @@ fn redraw(
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
editor.shape_as_needed();
editor.shape_as_needed(true);
if editor.buffer().redraw() {
let instant = Instant::now();

View file

@ -1,6 +1,8 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic_text::{Action, Attrs, Buffer, Edit, Family, FontSystem, Metrics, Shaping, SwashCache};
use cosmic_text::{
Action, Attrs, Buffer, Edit, Family, FontSystem, Metrics, Scroll, Shaping, SwashCache,
};
use std::{collections::HashMap, env, fs, num::NonZeroU32, rc::Rc, slice};
use tiny_skia::{Color, Paint, PixmapMut, Rect, Transform};
use winit::{
@ -41,7 +43,7 @@ fn main() {
window: Rc<WinitWindow>,
context: softbuffer::Context<Rc<WinitWindow>>,
surface: softbuffer::Surface<Rc<WinitWindow>, Rc<WinitWindow>>,
scroll: i32,
scroll: Scroll,
}
let mut windows = HashMap::new();
for _ in 0..2 {
@ -54,7 +56,7 @@ fn main() {
window,
context,
surface,
scroll: 0,
scroll: Scroll::default(),
},
);
}
@ -102,7 +104,8 @@ fn main() {
// Set size, will relayout and shape until scroll if changed
buffer.set_size(width as f32, height as f32);
// Shape until scroll, ensures scroll is clamped
buffer.shape_until_scroll();
//TODO: ability to prune with multiple views?
buffer.shape_until_scroll(true);
// Update scroll after buffer clamps it
*scroll = buffer.scroll();
@ -145,16 +148,16 @@ fn main() {
if state == ElementState::Pressed {
match logical_key {
Key::Named(NamedKey::ArrowDown) => {
*scroll += 1;
scroll.layout += 1;
}
Key::Named(NamedKey::ArrowUp) => {
*scroll -= 1;
scroll.layout -= 1;
}
Key::Named(NamedKey::PageDown) => {
*scroll += buffer.visible_lines();
scroll.layout += buffer.visible_lines();
}
Key::Named(NamedKey::PageUp) => {
*scroll -= buffer.visible_lines();
scroll.layout -= buffer.visible_lines();
}
_ => {}
}

View file

@ -131,7 +131,7 @@ fn main() {
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
editor.shape_as_needed();
editor.shape_as_needed(true);
if editor.buffer().redraw() {
let instant = Instant::now();

View file

@ -39,7 +39,7 @@ fn main() {
buffer.set_text(&text, attrs, Shaping::Advanced);
// Perform shaping as desired
buffer.shape_until_scroll();
buffer.shape_until_scroll(true);
// Default text color (0xFF, 0xFF, 0xFF is white)
const TEXT_COLOR: Color = Color::rgb(0xFF, 0xFF, 0xFF);