Decouple editing from buffer
This commit is contained in:
parent
26c83be35a
commit
92cad6fe13
11 changed files with 901 additions and 858 deletions
|
|
@ -1,33 +1,27 @@
|
|||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||
|
||||
use cosmic_text::{Color, FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
|
||||
use cosmic_text::{Action, Buffer, Color, Editor, FontSystem, Metrics, SwashCache};
|
||||
use orbclient::{EventOption, Renderer, Window, WindowFlag};
|
||||
use std::{env, fs, process, thread, time::{Duration, Instant}};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut SwashCache) {
|
||||
fn redraw(window: &mut Window, editor: &mut Editor<'_>, swash_cache: &mut SwashCache) {
|
||||
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
|
||||
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
|
||||
|
||||
if buffer.cursor_moved {
|
||||
buffer.shape_until_cursor();
|
||||
buffer.cursor_moved = false;
|
||||
} else {
|
||||
buffer.shape_until_scroll();
|
||||
}
|
||||
|
||||
if buffer.redraw {
|
||||
editor.shape_as_needed();
|
||||
if editor.buffer.redraw {
|
||||
let instant = Instant::now();
|
||||
|
||||
window.set(bg_color);
|
||||
|
||||
buffer.draw(swash_cache, font_color, |x, y, w, h, color| {
|
||||
editor.draw(swash_cache, font_color, |x, y, w, h, color| {
|
||||
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
|
||||
});
|
||||
|
||||
window.sync();
|
||||
|
||||
buffer.redraw = false;
|
||||
editor.buffer.redraw = false;
|
||||
|
||||
let duration = instant.elapsed();
|
||||
log::debug!("redraw: {:?}", duration);
|
||||
|
|
@ -51,16 +45,16 @@ fn main() {
|
|||
.unwrap();
|
||||
|
||||
let font_sizes = [
|
||||
TextMetrics::new(10, 14).scale(display_scale), // Caption
|
||||
TextMetrics::new(14, 20).scale(display_scale), // Body
|
||||
TextMetrics::new(20, 28).scale(display_scale), // Title 4
|
||||
TextMetrics::new(24, 32).scale(display_scale), // Title 3
|
||||
TextMetrics::new(28, 36).scale(display_scale), // Title 2
|
||||
TextMetrics::new(32, 44).scale(display_scale), // Title 1
|
||||
Metrics::new(10, 14).scale(display_scale), // Caption
|
||||
Metrics::new(14, 20).scale(display_scale), // Body
|
||||
Metrics::new(20, 28).scale(display_scale), // Title 4
|
||||
Metrics::new(24, 32).scale(display_scale), // Title 3
|
||||
Metrics::new(28, 36).scale(display_scale), // Title 2
|
||||
Metrics::new(32, 44).scale(display_scale), // Title 1
|
||||
];
|
||||
let font_size_default = 1; // Body
|
||||
|
||||
let mut buffer = TextBuffer::new(
|
||||
let mut buffer = Buffer::new(
|
||||
&font_system,
|
||||
font_sizes[font_size_default]
|
||||
);
|
||||
|
|
@ -69,6 +63,8 @@ fn main() {
|
|||
window.height() as i32
|
||||
);
|
||||
|
||||
let mut editor = Editor::new(buffer);
|
||||
|
||||
let mut swash_cache = SwashCache::new(&font_system);
|
||||
|
||||
let text = if let Some(arg) = env::args().nth(1) {
|
||||
|
|
@ -93,49 +89,49 @@ fn main() {
|
|||
|
||||
// Test backspace of character
|
||||
{
|
||||
let cursor = buffer.cursor();
|
||||
buffer.action(TextAction::Insert(c));
|
||||
buffer.action(TextAction::Backspace);
|
||||
assert_eq!(cursor, buffer.cursor());
|
||||
let cursor = editor.cursor();
|
||||
editor.action(Action::Insert(c));
|
||||
editor.action(Action::Backspace);
|
||||
assert_eq!(cursor, editor.cursor());
|
||||
}
|
||||
|
||||
// Finally, normal insert of character
|
||||
buffer.action(TextAction::Insert(c));
|
||||
editor.action(Action::Insert(c));
|
||||
}
|
||||
|
||||
// Test delete of EGC
|
||||
{
|
||||
let cursor = buffer.cursor();
|
||||
buffer.action(TextAction::Previous);
|
||||
buffer.action(TextAction::Delete);
|
||||
let cursor = editor.cursor();
|
||||
editor.action(Action::Previous);
|
||||
editor.action(Action::Delete);
|
||||
for c in grapheme.chars() {
|
||||
buffer.action(TextAction::Insert(c));
|
||||
editor.action(Action::Insert(c));
|
||||
}
|
||||
assert_eq!(cursor, buffer.cursor());
|
||||
assert_eq!(cursor, editor.cursor());
|
||||
}
|
||||
}
|
||||
|
||||
// Test backspace of newline
|
||||
{
|
||||
let cursor = buffer.cursor();
|
||||
buffer.action(TextAction::Enter);
|
||||
buffer.action(TextAction::Backspace);
|
||||
assert_eq!(cursor, buffer.cursor());
|
||||
let cursor = editor.cursor();
|
||||
editor.action(Action::Enter);
|
||||
editor.action(Action::Backspace);
|
||||
assert_eq!(cursor, editor.cursor());
|
||||
}
|
||||
|
||||
// Test delete of newline
|
||||
{
|
||||
let cursor = buffer.cursor();
|
||||
buffer.action(TextAction::Enter);
|
||||
buffer.action(TextAction::Previous);
|
||||
buffer.action(TextAction::Delete);
|
||||
assert_eq!(cursor, buffer.cursor());
|
||||
let cursor = editor.cursor();
|
||||
editor.action(Action::Enter);
|
||||
editor.action(Action::Previous);
|
||||
editor.action(Action::Delete);
|
||||
assert_eq!(cursor, editor.cursor());
|
||||
}
|
||||
|
||||
// Finally, normal enter
|
||||
buffer.action(TextAction::Enter);
|
||||
editor.action(Action::Enter);
|
||||
|
||||
redraw(&mut window, &mut buffer, &mut swash_cache);
|
||||
redraw(&mut window, &mut editor, &mut swash_cache);
|
||||
|
||||
for event in window.events() {
|
||||
match event.to_option() {
|
||||
|
|
@ -150,7 +146,7 @@ fn main() {
|
|||
|
||||
let mut wrong = 0;
|
||||
for (line_i, line) in text.lines().enumerate() {
|
||||
let buffer_line = &buffer.lines[line_i];
|
||||
let buffer_line = &editor.buffer.lines[line_i];
|
||||
if buffer_line.text() != line {
|
||||
log::error!("line {}: {:?} != {:?}", line_i, buffer_line.text(), line);
|
||||
wrong += 1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue