Decouple editing from buffer
This commit is contained in:
parent
26c83be35a
commit
92cad6fe13
11 changed files with 901 additions and 858 deletions
|
|
@ -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));
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue