Decouple editing from buffer

This commit is contained in:
Jeremy Soller 2022-10-31 11:24:36 -06:00
parent 26c83be35a
commit 92cad6fe13
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
11 changed files with 901 additions and 858 deletions

View file

@ -26,9 +26,10 @@ use cosmic::{
use cosmic_text::{
Attrs,
AttrsList,
Buffer,
Editor,
FontSystem,
TextBuffer,
TextMetrics,
Metrics,
};
use std::{
env,
@ -47,13 +48,13 @@ lazy_static::lazy_static! {
static ref FONT_SYSTEM: FontSystem<'static> = FontSystem::new();
}
static FONT_SIZES: &'static [TextMetrics] = &[
TextMetrics::new(10, 14), // Caption
TextMetrics::new(14, 20), // Body
TextMetrics::new(20, 28), // Title 4
TextMetrics::new(24, 32), // Title 3
TextMetrics::new(28, 36), // Title 2
TextMetrics::new(32, 44), // Title 1
static FONT_SIZES: &'static [Metrics] = &[
Metrics::new(10, 14), // Caption
Metrics::new(14, 20), // Body
Metrics::new(20, 28), // Title 4
Metrics::new(24, 32), // Title 3
Metrics::new(28, 36), // Title 2
Metrics::new(32, 44), // Title 1
];
fn main() -> cosmic::iced::Result {
@ -68,7 +69,7 @@ pub struct Window {
theme: Theme,
path_opt: Option<PathBuf>,
attrs: Attrs<'static>,
buffer: Mutex<TextBuffer<'static>>,
editor: Mutex<Editor<'static>>,
}
#[allow(dead_code)]
@ -79,22 +80,22 @@ pub enum Message {
Bold(bool),
Italic(bool),
Monospaced(bool),
MetricsChanged(TextMetrics),
MetricsChanged(Metrics),
ThemeChanged(&'static str),
}
impl Window {
pub fn open(&mut self, path: PathBuf) {
let mut buffer = self.buffer.lock().unwrap();
let mut editor = self.editor.lock().unwrap();
match fs::read_to_string(&path) {
Ok(text) => {
log::info!("opened '{}'", path.display());
buffer.set_text(&text, self.attrs);
editor.buffer.set_text(&text, self.attrs);
self.path_opt = Some(path);
},
Err(err) => {
log::error!("failed to open '{}': {}", path.display(), err);
buffer.set_text("", self.attrs);
editor.buffer.set_text("", self.attrs);
self.path_opt = None;
}
}
@ -112,17 +113,17 @@ impl Application for Window {
.monospaced(true)
.family(cosmic_text::Family::Monospace);
let mut buffer = TextBuffer::new(
let mut editor = Editor::new(Buffer::new(
&FONT_SYSTEM,
FONT_SIZES[1 /* Body */],
);
update_attrs(&mut buffer, attrs);
));
update_attrs(&mut editor, attrs);
let mut window = Window {
theme: Theme::Dark,
path_opt: None,
attrs,
buffer: Mutex::new(buffer),
editor: Mutex::new(editor),
};
if let Some(arg) = env::args().nth(1) {
window.open(PathBuf::from(arg));
@ -151,9 +152,9 @@ impl Application for Window {
},
Message::Save => {
if let Some(path) = &self.path_opt {
let buffer = self.buffer.lock().unwrap();
let editor = self.editor.lock().unwrap();
let mut text = String::new();
for line in buffer.lines.iter() {
for line in editor.buffer.lines.iter() {
text.push_str(line.text());
text.push('\n');
}
@ -174,8 +175,8 @@ impl Application for Window {
cosmic_text::Weight::NORMAL
});
let mut buffer = self.buffer.lock().unwrap();
update_attrs(&mut buffer, self.attrs);
let mut editor = self.editor.lock().unwrap();
update_attrs(&mut editor, self.attrs);
},
Message::Italic(italic) => {
self.attrs = self.attrs.style(if italic {
@ -184,8 +185,8 @@ impl Application for Window {
cosmic_text::Style::Normal
});
let mut buffer = self.buffer.lock().unwrap();
update_attrs(&mut buffer, self.attrs);
let mut editor = self.editor.lock().unwrap();
update_attrs(&mut editor, self.attrs);
},
Message::Monospaced(monospaced) => {
self.attrs = self.attrs
@ -196,12 +197,12 @@ impl Application for Window {
})
.monospaced(monospaced);
let mut buffer = self.buffer.lock().unwrap();
update_attrs(&mut buffer, self.attrs);
let mut editor = self.editor.lock().unwrap();
update_attrs(&mut editor, self.attrs);
},
Message::MetricsChanged(metrics) => {
let mut buffer = self.buffer.lock().unwrap();
buffer.set_metrics(metrics);
let mut editor = self.editor.lock().unwrap();
editor.buffer.set_metrics(metrics);
},
Message::ThemeChanged(theme) => {
self.theme = match theme {
@ -214,8 +215,8 @@ impl Application for Window {
let as_u8 = |component: f32| (component * 255.0) as u8;
self.attrs = self.attrs.color(cosmic_text::Color::rgba(as_u8(r), as_u8(g), as_u8(b), as_u8(a)));
let mut buffer = self.buffer.lock().unwrap();
update_attrs(&mut buffer, self.attrs);
let mut editor = self.editor.lock().unwrap();
update_attrs(&mut editor, self.attrs);
},
}
@ -234,10 +235,10 @@ impl Application for Window {
);
let font_size_picker = {
let buffer = self.buffer.lock().unwrap();
let editor = self.editor.lock().unwrap();
pick_list(
FONT_SIZES,
Some(buffer.metrics()),
Some(editor.buffer.metrics()),
Message::MetricsChanged
)
};
@ -261,7 +262,7 @@ impl Application for Window {
.align_items(Alignment::Center)
.spacing(8)
,
text_box(&self.buffer)
text_box(&self.editor)
]
.spacing(8)
.padding(16)
@ -272,8 +273,8 @@ impl Application for Window {
}
}
fn update_attrs<'a>(buffer: &mut TextBuffer<'a>, attrs: Attrs<'a>) {
buffer.lines.iter_mut().for_each(|line| {
fn update_attrs<'a>(editor: &mut Editor<'a>, attrs: Attrs<'a>) {
editor.buffer.lines.iter_mut().for_each(|line| {
line.set_attrs_list(AttrsList::new(attrs));
});
}

View file

@ -12,9 +12,10 @@ use cosmic::iced_native::{
widget::{self, tree, Widget},
};
use cosmic_text::{
Action,
Buffer,
Editor,
SwashCache,
TextAction,
TextBuffer,
};
use std::{
cmp,
@ -47,19 +48,19 @@ impl StyleSheet for Theme {
}
pub struct TextBox<'a> {
buffer: &'a Mutex<TextBuffer<'static>>,
editor: &'a Mutex<Editor<'static>>,
}
impl<'a> TextBox<'a> {
pub fn new(buffer: &'a Mutex<TextBuffer<'static>>) -> Self {
pub fn new(editor: &'a Mutex<Editor<'static>>) -> Self {
Self {
buffer,
editor,
}
}
}
pub fn text_box<'a>(buffer: &'a Mutex<TextBuffer<'static>>) -> TextBox<'a> {
TextBox::new(buffer)
pub fn text_box<'a>(editor: &'a Mutex<Editor<'static>>) -> TextBox<'a> {
TextBox::new(editor)
}
impl<'a, Message, Renderer> Widget<Message, Renderer> for TextBox<'a>
@ -141,28 +142,22 @@ where
let mut pixels_opt = state.pixels_opt.lock().unwrap();
let mut buffer = self.buffer.lock().unwrap();
let mut editor = self.editor.lock().unwrap();
let layout_w = layout.bounds().width as i32;
let layout_h = layout.bounds().height as i32;
buffer.set_size(layout_w, layout_h);
if buffer.cursor_moved {
buffer.shape_until_cursor();
buffer.cursor_moved = false;
} else {
buffer.shape_until_scroll();
}
editor.buffer.set_size(layout_w, layout_h);
editor.shape_as_needed();
//TODO: redraw on color change
if buffer.redraw || pixels_opt.is_none() {
if editor.buffer.redraw || pixels_opt.is_none() {
// Redraw buffer to image
let instant = Instant::now();
let mut pixels = vec![0; layout_w as usize * layout_h as usize * 4];
buffer.draw(&mut state.cache.lock().unwrap(), text_color, |start_x, start_y, w, h, color| {
editor.draw(&mut state.cache.lock().unwrap(), text_color, |start_x, start_y, w, h, color| {
let alpha = (color.0 >> 24) & 0xFF;
if alpha == 0 {
// Do not draw if alpha is zero
@ -212,7 +207,7 @@ where
*pixels_opt = Some((layout_w as u32, layout_h as u32, pixels));
buffer.redraw = false;
editor.buffer.redraw = false;
let duration = instant.elapsed();
log::debug!("redraw: {:?}", duration);
@ -235,64 +230,64 @@ where
_shell: &mut Shell<'_, Message>,
) -> Status {
let state = tree.state.downcast_mut::<State>();
let mut buffer = self.buffer.lock().unwrap();
let mut editor = self.editor.lock().unwrap();
let mut status = Status::Ignored;
match event {
Event::Keyboard(KeyEvent::KeyPressed { key_code, modifiers }) => match key_code {
KeyCode::Left => {
buffer.action(TextAction::Left);
editor.action(Action::Left);
status = Status::Captured;
},
KeyCode::Right => {
buffer.action(TextAction::Right);
editor.action(Action::Right);
status = Status::Captured;
},
KeyCode::Up => {
buffer.action(TextAction::Up);
editor.action(Action::Up);
status = Status::Captured;
},
KeyCode::Down => {
buffer.action(TextAction::Down);
editor.action(Action::Down);
status = Status::Captured;
},
KeyCode::Home => {
buffer.action(TextAction::Home);
editor.action(Action::Home);
status = Status::Captured;
},
KeyCode::End => {
buffer.action(TextAction::End);
editor.action(Action::End);
status = Status::Captured;
},
KeyCode::PageUp => {
buffer.action(TextAction::PageUp);
editor.action(Action::PageUp);
status = Status::Captured;
},
KeyCode::PageDown => {
buffer.action(TextAction::PageDown);
editor.action(Action::PageDown);
status = Status::Captured;
},
KeyCode::Enter => {
buffer.action(TextAction::Enter);
editor.action(Action::Enter);
status = Status::Captured;
},
KeyCode::Backspace => {
buffer.action(TextAction::Backspace);
editor.action(Action::Backspace);
status = Status::Captured;
},
KeyCode::Delete => {
buffer.action(TextAction::Delete);
editor.action(Action::Delete);
status = Status::Captured;
},
_ => ()
},
Event::Keyboard(KeyEvent::CharacterReceived(character)) => {
buffer.action(TextAction::Insert(character));
editor.action(Action::Insert(character));
status = Status::Captured;
},
Event::Mouse(MouseEvent::ButtonPressed(Button::Left)) => {
if layout.bounds().contains(cursor_position) {
buffer.action(TextAction::Click {
editor.action(Action::Click {
x: (cursor_position.x - layout.bounds().x) as i32,
y: (cursor_position.y - layout.bounds().y) as i32,
});
@ -306,7 +301,7 @@ where
},
Event::Mouse(MouseEvent::CursorMoved { .. }) => {
if state.is_dragging {
buffer.action(TextAction::Drag {
editor.action(Action::Drag {
x: (cursor_position.x - layout.bounds().x) as i32,
y: (cursor_position.y - layout.bounds().y) as i32,
});
@ -315,7 +310,7 @@ where
},
Event::Mouse(MouseEvent::WheelScrolled { delta }) => match delta {
ScrollDelta::Lines { x, y } => {
buffer.action(TextAction::Scroll {
editor.action(Action::Scroll {
lines: (-y * 6.0) as i32,
});
status = Status::Captured;

View file

@ -10,8 +10,8 @@ use cosmic_text::{
Attrs,
AttrsList,
SwashCache,
TextBufferLine,
TextMetrics,
BufferLine,
Metrics,
};
use std::{
cmp,
@ -44,8 +44,8 @@ impl StyleSheet for Theme {
}
pub struct Text {
line: TextBufferLine<'static>,
metrics: TextMetrics,
line: BufferLine<'static>,
metrics: Metrics,
}
impl Text {
@ -53,7 +53,7 @@ impl Text {
let instant = Instant::now();
//TODO: make it possible to set attrs
let mut line = TextBufferLine::new(
let mut line = BufferLine::new(
string,
AttrsList::new(Attrs::new())
);
@ -63,7 +63,7 @@ impl Text {
let text = Self {
line,
metrics: TextMetrics::new(14, 20),
metrics: Metrics::new(14, 20),
};
log::debug!("Text::new in {:?}", instant.elapsed());

View file

@ -3,14 +3,15 @@
use cosmic_text::{
Attrs,
AttrsList,
Buffer,
Color,
Editor,
Family,
FontSystem,
Metrics,
Style,
SwashCache,
TextAction,
TextBuffer,
TextMetrics,
Action,
Weight
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
@ -67,23 +68,23 @@ fn main() {
let font_system = FontSystem::new();
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 font_size_i = font_size_default;
let line_x = 8 * display_scale;
let mut buffer = TextBuffer::new(
let mut editor = Editor::new(Buffer::new(
&font_system,
font_sizes[font_size_i]
);
));
buffer.set_size(
editor.buffer.set_size(
window.width() as i32 - line_x * 2,
window.height() as i32
);
@ -91,7 +92,7 @@ fn main() {
let attrs = Attrs::new()
.monospaced(true)
.family(Family::Monospace);
buffer.set_text(&text, attrs);
editor.buffer.set_text(&text, attrs);
let mut bg_color = orbclient::Color::rgb(0x00, 0x00, 0x00);
let mut font_color = Color::rgb(0xFF, 0xFF, 0xFF);
@ -149,8 +150,8 @@ fn main() {
if rehighlight {
let now = Instant::now();
for line_i in 0..buffer.lines.len() {
let line = &mut buffer.lines[line_i];
for line_i in 0..editor.buffer.lines.len() {
let line = &mut editor.buffer.lines[line_i];
if ! line.is_reset() && line_i < syntax_cache.len() {
continue;
}
@ -209,8 +210,8 @@ fn main() {
if line_i < syntax_cache.len() {
if syntax_cache[line_i] != cache_item {
syntax_cache[line_i] = cache_item;
if line_i + 1 < buffer.lines.len() {
buffer.lines[line_i + 1].reset();
if line_i + 1 < editor.buffer.lines.len() {
editor.buffer.lines[line_i + 1].reset();
}
}
} else {
@ -218,25 +219,19 @@ fn main() {
}
}
buffer.redraw = true;
editor.buffer.redraw = true;
rehighlight = false;
log::info!("Syntax highlighted in {:?}", now.elapsed());
}
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(&mut swash_cache, font_color, |x, y, w, h, color| {
editor.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
window.rect(line_x + x, y, w, h, orbclient::Color { data: color.0 })
});
@ -244,7 +239,7 @@ fn main() {
{
let mut start_line_opt = None;
let mut end_line = 0;
for run in buffer.layout_runs() {
for run in editor.buffer.layout_runs() {
end_line = run.line_i;
if start_line_opt == None {
start_line_opt = Some(end_line);
@ -252,7 +247,7 @@ fn main() {
}
let start_line = start_line_opt.unwrap_or(end_line);
let lines = buffer.lines.len();
let lines = editor.buffer.lines.len();
let start_y = (start_line * window.height() as usize) / lines;
let end_y = (end_line * window.height() as usize) / lines;
if end_y > start_y {
@ -268,7 +263,7 @@ fn main() {
window.sync();
buffer.redraw = false;
editor.buffer.redraw = false;
let duration = instant.elapsed();
log::debug!("redraw: {:?}", duration);
@ -282,62 +277,62 @@ fn main() {
match event.to_option() {
EventOption::Key(event) => match event.scancode {
orbclient::K_CTRL => ctrl_pressed = event.pressed,
orbclient::K_LEFT if event.pressed => buffer.action(TextAction::Left),
orbclient::K_RIGHT if event.pressed => buffer.action(TextAction::Right),
orbclient::K_UP if event.pressed => buffer.action(TextAction::Up),
orbclient::K_DOWN if event.pressed => buffer.action(TextAction::Down),
orbclient::K_HOME if event.pressed => buffer.action(TextAction::Home),
orbclient::K_END if event.pressed => buffer.action(TextAction::End),
orbclient::K_PGUP if event.pressed => buffer.action(TextAction::PageUp),
orbclient::K_PGDN if event.pressed => buffer.action(TextAction::PageDown),
orbclient::K_LEFT if event.pressed => editor.action(Action::Left),
orbclient::K_RIGHT if event.pressed => editor.action(Action::Right),
orbclient::K_UP if event.pressed => editor.action(Action::Up),
orbclient::K_DOWN if event.pressed => editor.action(Action::Down),
orbclient::K_HOME if event.pressed => editor.action(Action::Home),
orbclient::K_END if event.pressed => editor.action(Action::End),
orbclient::K_PGUP if event.pressed => editor.action(Action::PageUp),
orbclient::K_PGDN if event.pressed => editor.action(Action::PageDown),
orbclient::K_ENTER if event.pressed => {
buffer.action(TextAction::Enter);
editor.action(Action::Enter);
rehighlight = true;
},
orbclient::K_BKSP if event.pressed => {
buffer.action(TextAction::Backspace);
editor.action(Action::Backspace);
rehighlight = true;
},
orbclient::K_DEL if event.pressed => {
buffer.action(TextAction::Delete);
editor.action(Action::Delete);
rehighlight = true;
},
orbclient::K_0 if event.pressed && ctrl_pressed => {
font_size_i = font_size_default;
buffer.set_metrics(font_sizes[font_size_i]);
editor.buffer.set_metrics(font_sizes[font_size_i]);
}
orbclient::K_MINUS if event.pressed && ctrl_pressed => {
if font_size_i > 0 {
font_size_i -= 1;
buffer.set_metrics(font_sizes[font_size_i]);
editor.buffer.set_metrics(font_sizes[font_size_i]);
}
}
orbclient::K_EQUALS if event.pressed && ctrl_pressed => {
if font_size_i + 1 < font_sizes.len() {
font_size_i += 1;
buffer.set_metrics(font_sizes[font_size_i]);
editor.buffer.set_metrics(font_sizes[font_size_i]);
}
}
_ => (),
},
EventOption::TextInput(event) if !ctrl_pressed => {
buffer.action(TextAction::Insert(event.character));
editor.action(Action::Insert(event.character));
rehighlight = true;
}
EventOption::Mouse(event) => {
mouse_x = event.x;
mouse_y = event.y;
if mouse_left {
buffer.action(TextAction::Drag {
editor.action(Action::Drag {
x: mouse_x - line_x,
y: mouse_y,
});
if mouse_y <= 5 {
buffer.action(TextAction::Scroll { lines: -3 });
editor.action(Action::Scroll { lines: -3 });
window_async = true;
} else if mouse_y + 5 >= window.height() as i32 {
buffer.action(TextAction::Scroll { lines: 3 });
editor.action(Action::Scroll { lines: 3 });
window_async = true;
}
@ -348,7 +343,7 @@ fn main() {
if event.left != mouse_left {
mouse_left = event.left;
if mouse_left {
buffer.action(TextAction::Click {
editor.action(Action::Click {
x: mouse_x - line_x,
y: mouse_y,
});
@ -357,11 +352,10 @@ fn main() {
}
}
EventOption::Resize(event) => {
buffer.set_size(event.width as i32 - line_x * 2, event.height as i32);
buffer.redraw = true;
editor.buffer.set_size(event.width as i32 - line_x * 2, event.height as i32);
}
EventOption::Scroll(event) => {
buffer.action(TextAction::Scroll {
editor.action(Action::Scroll {
lines: -event.y * 3,
});
}
@ -371,16 +365,16 @@ fn main() {
}
if mouse_left && force_drag {
buffer.action(TextAction::Drag {
editor.action(Action::Drag {
x: mouse_x - line_x,
y: mouse_y,
});
if mouse_y <= 5 {
buffer.action(TextAction::Scroll { lines: -3 });
editor.action(Action::Scroll { lines: -3 });
window_async = true;
} else if mouse_y + 5 >= window.height() as i32 {
buffer.action(TextAction::Scroll { lines: 3 });
editor.action(Action::Scroll { lines: 3 });
window_async = true;
}
}

View file

@ -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;

View file

@ -1,7 +1,20 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic_text::{Attrs, AttrsList, Color, Family, FontSystem, Style, SwashCache,
TextAction, TextBuffer, TextBufferLine, TextMetrics, Weight};
use cosmic_text::{
Action,
Attrs,
AttrsList,
Buffer,
BufferLine,
Color,
Editor,
Family,
FontSystem,
Metrics,
Style,
SwashCache,
Weight,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{process, thread, time::{Duration, Instant}};
@ -31,12 +44,12 @@ fn main() {
)
.unwrap();
let mut buffer = TextBuffer::new(
let mut editor = Editor::new(Buffer::new(
&font_system,
TextMetrics::new(32, 44).scale(display_scale)
);
Metrics::new(32, 44).scale(display_scale)
));
buffer.set_size(
editor.buffer.set_size(
window.width() as i32,
window.height() as i32
);
@ -46,7 +59,7 @@ fn main() {
let mono_attrs = attrs.monospaced(true).family(Family::Monospace);
let comic_attrs = attrs.family(Family::Name("Comic Neue"));
buffer.lines.clear();
editor.buffer.lines.clear();
let lines: &[&[(&str, Attrs)]] = &[
&[
@ -118,7 +131,7 @@ fn main() {
let end = line_text.len();
attrs_list.add_span(start..end, attrs);
}
buffer.lines.push(TextBufferLine::new(line_text, attrs_list));
editor.buffer.lines.push(BufferLine::new(line_text, attrs_list));
}
let mut swash_cache = SwashCache::new(&font_system);
@ -131,25 +144,19 @@ fn main() {
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(&mut swash_cache, font_color, |x, y, w, h, color| {
editor.draw(&mut 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);
@ -158,36 +165,35 @@ fn main() {
for event in window.events() {
match event.to_option() {
EventOption::Key(event) => match event.scancode {
orbclient::K_LEFT if event.pressed => buffer.action(TextAction::Left),
orbclient::K_RIGHT if event.pressed => buffer.action(TextAction::Right),
orbclient::K_UP if event.pressed => buffer.action(TextAction::Up),
orbclient::K_DOWN if event.pressed => buffer.action(TextAction::Down),
orbclient::K_HOME if event.pressed => buffer.action(TextAction::Home),
orbclient::K_END if event.pressed => buffer.action(TextAction::End),
orbclient::K_PGUP if event.pressed => buffer.action(TextAction::PageUp),
orbclient::K_PGDN if event.pressed => buffer.action(TextAction::PageDown),
orbclient::K_ENTER if event.pressed => buffer.action(TextAction::Enter),
orbclient::K_BKSP if event.pressed => buffer.action(TextAction::Backspace),
orbclient::K_DEL if event.pressed => buffer.action(TextAction::Delete),
orbclient::K_LEFT if event.pressed => editor.action(Action::Left),
orbclient::K_RIGHT if event.pressed => editor.action(Action::Right),
orbclient::K_UP if event.pressed => editor.action(Action::Up),
orbclient::K_DOWN if event.pressed => editor.action(Action::Down),
orbclient::K_HOME if event.pressed => editor.action(Action::Home),
orbclient::K_END if event.pressed => editor.action(Action::End),
orbclient::K_PGUP if event.pressed => editor.action(Action::PageUp),
orbclient::K_PGDN if event.pressed => editor.action(Action::PageDown),
orbclient::K_ENTER if event.pressed => editor.action(Action::Enter),
orbclient::K_BKSP if event.pressed => editor.action(Action::Backspace),
orbclient::K_DEL if event.pressed => editor.action(Action::Delete),
_ => (),
},
EventOption::TextInput(event) => buffer.action(TextAction::Insert(event.character)),
EventOption::TextInput(event) => editor.action(Action::Insert(event.character)),
EventOption::Mouse(mouse) => {
mouse_x = mouse.x;
mouse_y = mouse.y;
if mouse_left {
buffer.action(TextAction::Drag { x: mouse_x, y: mouse_y });
editor.action(Action::Drag { x: mouse_x, y: mouse_y });
}
},
EventOption::Button(button) => {
mouse_left = button.left;
if mouse_left {
buffer.action(TextAction::Click { x: mouse_x, y: mouse_y });
editor.action(Action::Click { x: mouse_x, y: mouse_y });
}
},
EventOption::Resize(resize) => {
buffer.set_size(resize.width as i32, resize.height as i32);
buffer.redraw = true;
editor.buffer.set_size(resize.width as i32, resize.height as i32);
},
EventOption::Quit(_) => process::exit(0),
_ => (),

View file

@ -1,4 +1,4 @@
use cosmic_text::{Attrs, Color, FontSystem, SwashCache, TextBuffer, TextMetrics};
use cosmic_text::{Attrs, Color, FontSystem, SwashCache, Buffer, Metrics};
use std::cmp;
use termion::{
color,
@ -13,24 +13,24 @@ fn main() {
let mut swash_cache = SwashCache::new(&font_system);
// Text metrics indicate the font size and line height of a buffer
let metrics = TextMetrics::new(14, 20);
let metrics = Metrics::new(14, 20);
// A TextBuffer provides shaping and layout for a UTF-8 string, create one per text widget
let mut text_buffer = TextBuffer::new(&font_system, metrics);
// A Buffer provides shaping and layout for a UTF-8 string, create one per text widget
let mut buffer = Buffer::new(&font_system, metrics);
// Set a size for the text buffer, in pixels
let width = 80u16;
let height = 25u16;
text_buffer.set_size(width as i32, height as i32);
buffer.set_size(width as i32, height as i32);
// Attributes indicate what font to choose
let attrs = Attrs::new();
// Add some text!
text_buffer.set_text(" Hi, Rust! 🦀", attrs);
buffer.set_text(" Hi, Rust! 🦀", attrs);
// Perform shaping as desired
text_buffer.shape_until_cursor();
buffer.shape_until_cursor();
// Default text color (0xFF, 0xFF, 0xFF is white)
let text_color = Color::rgb(0xFF, 0xFF, 0xFF);
@ -40,7 +40,7 @@ fn main() {
// Clear buffer with black background
for _y in 0..height {
for _x in 0..text_buffer.size().0 {
for _x in 0..buffer.size().0 {
print!(
"{} {}",
color::Bg(color::Rgb(0, 0, 0)),
@ -56,7 +56,7 @@ fn main() {
// Print the buffer
let mut last_x = 0;
let mut last_y = 0;
text_buffer.draw(&mut swash_cache, text_color, |x, y, w, h, color| {
buffer.draw(&mut swash_cache, text_color, |x, y, w, h, color| {
let a = color.a();
if a == 0 || x < 0 || y < 0 || w != 1 || h != 1 {
// Ignore alphas of 0, or invalid x, y coordinates, or unimplemented sizes