cosmic-text/examples/editor-test/src/main.rs

181 lines
5.4 KiB
Rust
Raw Normal View History

2022-10-24 08:56:48 -06:00
// SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic_text::{
Action, BidiParagraphs, BorrowedWithFontSystem, Buffer, Color, Edit, Editor, FontSystem,
Metrics, Motion, SwashCache,
};
2022-10-27 09:07:47 -06:00
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{env, fs, process, time::Instant};
2022-10-27 08:37:07 -06:00
use unicode_segmentation::UnicodeSegmentation;
fn redraw(
window: &mut Window,
editor: &mut BorrowedWithFontSystem<Editor>,
swash_cache: &mut SwashCache,
) {
2022-10-27 09:07:47 -06:00
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
2023-12-15 15:03:29 -07:00
let cursor_color = Color::rgb(0xFF, 0xFF, 0xFF);
let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33);
let selected_text_color = Color::rgb(0xF0, 0xF0, 0xFF);
editor.shape_as_needed(true);
if editor.redraw() {
let instant = Instant::now();
window.set(bg_color);
2023-12-15 15:03:29 -07:00
editor.draw(
swash_cache,
font_color,
cursor_color,
selection_color,
selected_text_color,
2023-12-15 15:03:29 -07:00
|x, y, w, h, color| {
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
},
);
window.sync();
editor.set_redraw(false);
let duration = instant.elapsed();
log::debug!("redraw: {:?}", duration);
}
}
fn main() {
env_logger::init();
let display_scale = 1.0;
let mut font_system = FontSystem::new();
let mut window = Window::new_flags(
-1,
-1,
2022-10-25 22:10:05 -06:00
1024,
768,
2022-11-04 10:38:47 -06:00
&format!("COSMIC TEXT - {}", font_system.locale()),
&[WindowFlag::Async],
)
.unwrap();
let font_sizes = [
Metrics::new(10.0, 14.0).scale(display_scale), // Caption
Metrics::new(14.0, 20.0).scale(display_scale), // Body
Metrics::new(20.0, 28.0).scale(display_scale), // Title 4
Metrics::new(24.0, 32.0).scale(display_scale), // Title 3
Metrics::new(28.0, 36.0).scale(display_scale), // Title 2
Metrics::new(32.0, 44.0).scale(display_scale), // Title 1
];
let font_size_default = 1; // Body
2023-03-12 10:30:20 +01:00
let mut buffer = Buffer::new(&mut font_system, font_sizes[font_size_default]);
buffer
.borrow_with(&mut font_system)
.set_size(Some(window.width() as f32), Some(window.height() as f32));
2022-10-31 11:24:36 -06:00
let mut editor = Editor::new(buffer);
let mut editor = editor.borrow_with(&mut font_system);
2023-03-01 22:41:59 +01:00
let mut swash_cache = SwashCache::new();
2022-10-25 11:40:10 -06:00
let text = if let Some(arg) = env::args().nth(1) {
fs::read_to_string(&arg).expect("failed to open file")
} else {
#[cfg(feature = "mono")]
let default_text = include_str!("../../../sample/mono.txt");
#[cfg(not(feature = "mono"))]
let default_text = include_str!("../../../sample/proportional.txt");
default_text.to_string()
};
2022-10-20 09:16:39 -06:00
let test_start = Instant::now();
for line in BidiParagraphs::new(&text) {
2022-10-19 16:09:22 -06:00
log::debug!("Line {:?}", line);
2022-10-25 10:17:09 -06:00
for grapheme in line.graphemes(true) {
for c in grapheme.chars() {
log::trace!("Insert {:?}", c);
2022-10-19 15:31:09 -06:00
2022-10-25 10:17:09 -06:00
// Test backspace of character
{
2022-10-31 11:24:36 -06:00
let cursor = editor.cursor();
editor.action(Action::Insert(c));
editor.action(Action::Backspace);
assert_eq!(cursor, editor.cursor());
2022-10-25 10:17:09 -06:00
}
// Finally, normal insert of character
2022-10-31 11:24:36 -06:00
editor.action(Action::Insert(c));
2022-10-19 15:31:09 -06:00
}
2022-10-25 10:17:09 -06:00
// Test delete of EGC
2022-10-19 15:31:09 -06:00
{
2022-10-31 11:24:36 -06:00
let cursor = editor.cursor();
editor.action(Action::Motion(Motion::Previous));
2022-10-31 11:24:36 -06:00
editor.action(Action::Delete);
2022-10-25 10:17:09 -06:00
for c in grapheme.chars() {
2022-10-31 11:24:36 -06:00
editor.action(Action::Insert(c));
2022-10-25 10:17:09 -06:00
}
2023-03-01 11:46:41 -07:00
assert_eq!(
(cursor.line, cursor.index),
(editor.cursor().line, editor.cursor().index)
);
}
2022-10-19 15:31:09 -06:00
}
// Test backspace of newline
{
2022-10-31 11:24:36 -06:00
let cursor = editor.cursor();
editor.action(Action::Enter);
editor.action(Action::Backspace);
assert_eq!(cursor, editor.cursor());
}
2022-10-19 15:31:09 -06:00
// Test delete of newline
{
2022-10-31 11:24:36 -06:00
let cursor = editor.cursor();
editor.action(Action::Enter);
editor.action(Action::Motion(Motion::Previous));
2022-10-31 11:24:36 -06:00
editor.action(Action::Delete);
assert_eq!(cursor, editor.cursor());
2022-10-19 15:31:09 -06:00
}
// Finally, normal enter
2022-10-31 11:24:36 -06:00
editor.action(Action::Enter);
2022-10-19 15:31:09 -06:00
redraw(&mut window, &mut editor, &mut swash_cache);
for event in window.events() {
2023-02-28 19:42:53 +01:00
if let EventOption::Quit(_) = event.to_option() {
process::exit(1)
}
}
}
2022-10-20 09:16:39 -06:00
let test_elapsed = test_start.elapsed();
log::info!("Test completed in {:?}", test_elapsed);
let mut wrong = 0;
editor.with_buffer(|buffer| {
for (line_i, line) in text.lines().enumerate() {
let buffer_line = &buffer.lines[line_i];
if buffer_line.text() != line {
log::error!("line {}: {:?} != {:?}", line_i, buffer_line.text(), line);
wrong += 1;
}
}
});
if wrong == 0 {
log::info!("All lines matched!");
process::exit(0);
} else {
log::error!("{} lines did not match!", wrong);
process::exit(1);
}
}