Extract borrow of FontSystem from Buffer
This commit is contained in:
parent
bff5aaaea3
commit
057b5b6fa9
12 changed files with 381 additions and 216 deletions
|
|
@ -10,20 +10,22 @@ use unicode_segmentation::UnicodeSegmentation;
|
|||
|
||||
#[cfg(feature = "swash")]
|
||||
use crate::Color;
|
||||
use crate::{Action, Affinity, AttrsList, Buffer, BufferLine, Cursor, Edit, LayoutCursor};
|
||||
use crate::{
|
||||
Action, Affinity, AttrsList, Buffer, BufferLine, Cursor, Edit, FontSystem, LayoutCursor,
|
||||
};
|
||||
|
||||
/// A wrapper of [`Buffer`] for easy editing
|
||||
pub struct Editor<'a> {
|
||||
buffer: Buffer<'a>,
|
||||
pub struct Editor {
|
||||
buffer: Buffer,
|
||||
cursor: Cursor,
|
||||
cursor_x_opt: Option<i32>,
|
||||
select_opt: Option<Cursor>,
|
||||
cursor_moved: bool,
|
||||
}
|
||||
|
||||
impl<'a> Editor<'a> {
|
||||
impl Editor {
|
||||
/// Create a new [`Editor`] with the provided [`Buffer`]
|
||||
pub fn new(buffer: Buffer<'a>) -> Self {
|
||||
pub fn new(buffer: Buffer) -> Self {
|
||||
Self {
|
||||
buffer,
|
||||
cursor: Cursor::default(),
|
||||
|
|
@ -33,10 +35,10 @@ impl<'a> Editor<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn set_layout_cursor(&mut self, cursor: LayoutCursor) {
|
||||
fn set_layout_cursor(&mut self, font_system: &FontSystem, cursor: LayoutCursor) {
|
||||
let layout = self
|
||||
.buffer
|
||||
.line_layout(cursor.line)
|
||||
.line_layout(font_system, cursor.line)
|
||||
.expect("layout not found");
|
||||
|
||||
let layout_line = match layout.get(cursor.layout) {
|
||||
|
|
@ -68,12 +70,12 @@ impl<'a> Editor<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Edit<'a> for Editor<'a> {
|
||||
fn buffer(&self) -> &Buffer<'a> {
|
||||
impl Edit for Editor {
|
||||
fn buffer(&self) -> &Buffer {
|
||||
&self.buffer
|
||||
}
|
||||
|
||||
fn buffer_mut(&mut self) -> &mut Buffer<'a> {
|
||||
fn buffer_mut(&mut self) -> &mut Buffer {
|
||||
&mut self.buffer
|
||||
}
|
||||
|
||||
|
|
@ -92,12 +94,12 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn shape_as_needed(&mut self) {
|
||||
fn shape_as_needed(&mut self, font_system: &FontSystem) {
|
||||
if self.cursor_moved {
|
||||
self.buffer.shape_until_cursor(self.cursor);
|
||||
self.buffer.shape_until_cursor(font_system, self.cursor);
|
||||
self.cursor_moved = false;
|
||||
} else {
|
||||
self.buffer.shape_until_scroll();
|
||||
self.buffer.shape_until_scroll(font_system);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -279,7 +281,7 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
self.cursor.index = self.buffer.lines[self.cursor.line].text().len() - after_len;
|
||||
}
|
||||
|
||||
fn action(&mut self, action: Action) {
|
||||
fn action(&mut self, font_system: &FontSystem, action: Action) {
|
||||
let old_cursor = self.cursor;
|
||||
|
||||
match action {
|
||||
|
|
@ -333,9 +335,9 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
.map(|shape| shape.rtl);
|
||||
if let Some(rtl) = rtl_opt {
|
||||
if rtl {
|
||||
self.action(Action::Next);
|
||||
self.action(font_system, Action::Next);
|
||||
} else {
|
||||
self.action(Action::Previous);
|
||||
self.action(font_system, Action::Previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -346,9 +348,9 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
.map(|shape| shape.rtl);
|
||||
if let Some(rtl) = rtl_opt {
|
||||
if rtl {
|
||||
self.action(Action::Previous);
|
||||
self.action(font_system, Action::Previous);
|
||||
} else {
|
||||
self.action(Action::Next);
|
||||
self.action(font_system, Action::Next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -373,7 +375,7 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
cursor.glyph = cursor_x as usize; //TODO: glyph x position
|
||||
}
|
||||
|
||||
self.set_layout_cursor(cursor);
|
||||
self.set_layout_cursor(font_system, cursor);
|
||||
}
|
||||
Action::Down => {
|
||||
//TODO: make this preserve X as best as possible!
|
||||
|
|
@ -381,7 +383,7 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
|
||||
let layout_len = self
|
||||
.buffer
|
||||
.line_layout(cursor.line)
|
||||
.line_layout(font_system, cursor.line)
|
||||
.expect("layout not found")
|
||||
.len();
|
||||
|
||||
|
|
@ -402,18 +404,18 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
cursor.glyph = cursor_x as usize; //TODO: glyph x position
|
||||
}
|
||||
|
||||
self.set_layout_cursor(cursor);
|
||||
self.set_layout_cursor(font_system, cursor);
|
||||
}
|
||||
Action::Home => {
|
||||
let mut cursor = self.buffer.layout_cursor(&self.cursor);
|
||||
cursor.glyph = 0;
|
||||
self.set_layout_cursor(cursor);
|
||||
self.set_layout_cursor(font_system, cursor);
|
||||
self.cursor_x_opt = None;
|
||||
}
|
||||
Action::End => {
|
||||
let mut cursor = self.buffer.layout_cursor(&self.cursor);
|
||||
cursor.glyph = usize::max_value();
|
||||
self.set_layout_cursor(cursor);
|
||||
self.set_layout_cursor(font_system, cursor);
|
||||
self.cursor_x_opt = None;
|
||||
}
|
||||
Action::ParagraphStart => {
|
||||
|
|
@ -427,10 +429,10 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
self.buffer.set_redraw(true);
|
||||
}
|
||||
Action::PageUp => {
|
||||
self.action(Action::Vertical(-self.buffer.size().1 as i32));
|
||||
self.action(font_system, Action::Vertical(-self.buffer.size().1 as i32));
|
||||
}
|
||||
Action::PageDown => {
|
||||
self.action(Action::Vertical(self.buffer.size().1 as i32));
|
||||
self.action(font_system, Action::Vertical(self.buffer.size().1 as i32));
|
||||
}
|
||||
Action::Vertical(px) => {
|
||||
// TODO more efficient
|
||||
|
|
@ -438,12 +440,12 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
match lines.cmp(&0) {
|
||||
Ordering::Less => {
|
||||
for _ in 0..-lines {
|
||||
self.action(Action::Up);
|
||||
self.action(font_system, Action::Up);
|
||||
}
|
||||
}
|
||||
Ordering::Greater => {
|
||||
for _ in 0..lines {
|
||||
self.action(Action::Down);
|
||||
self.action(font_system, Action::Down);
|
||||
}
|
||||
}
|
||||
Ordering::Equal => {}
|
||||
|
|
@ -459,7 +461,7 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
// Filter out special chars (except for tab), use Action instead
|
||||
log::debug!("Refusing to insert control character {:?}", character);
|
||||
} else if character == '\n' {
|
||||
self.action(Action::Enter);
|
||||
self.action(font_system, Action::Enter);
|
||||
} else {
|
||||
let mut str_buf = [0u8; 8];
|
||||
let str_ref = character.encode_utf8(&mut str_buf);
|
||||
|
|
@ -619,9 +621,9 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
.map(|shape| shape.rtl);
|
||||
if let Some(rtl) = rtl_opt {
|
||||
if rtl {
|
||||
self.action(Action::NextWord);
|
||||
self.action(font_system, Action::NextWord);
|
||||
} else {
|
||||
self.action(Action::PreviousWord);
|
||||
self.action(font_system, Action::PreviousWord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -632,9 +634,9 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
.map(|shape| shape.rtl);
|
||||
if let Some(rtl) = rtl_opt {
|
||||
if rtl {
|
||||
self.action(Action::PreviousWord);
|
||||
self.action(font_system, Action::PreviousWord);
|
||||
} else {
|
||||
self.action(Action::NextWord);
|
||||
self.action(font_system, Action::NextWord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -673,8 +675,13 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
|
||||
/// Draw the editor
|
||||
#[cfg(feature = "swash")]
|
||||
fn draw<F>(&self, cache: &mut crate::SwashCache, color: Color, mut f: F)
|
||||
where
|
||||
fn draw<F>(
|
||||
&self,
|
||||
font_system: &FontSystem,
|
||||
cache: &mut crate::SwashCache,
|
||||
color: Color,
|
||||
mut f: F,
|
||||
) where
|
||||
F: FnMut(i32, i32, u32, u32, Color),
|
||||
{
|
||||
let font_size = self.buffer.metrics().font_size;
|
||||
|
|
@ -833,14 +840,9 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
None => color,
|
||||
};
|
||||
|
||||
cache.with_pixels(
|
||||
self.buffer.font_system(),
|
||||
cache_key,
|
||||
glyph_color,
|
||||
|x, y, color| {
|
||||
f(x_int + x, line_y as i32 + y_int + y, 1, 1, color);
|
||||
},
|
||||
);
|
||||
cache.with_pixels(font_system, cache_key, glyph_color, |x, y, color| {
|
||||
f(x_int + x, line_y as i32 + y_int + y, 1, 1, color);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue