Allow Editor to use reference or Arc of Buffer
This commit is contained in:
parent
ae030e9885
commit
cbbf6f0d8f
9 changed files with 1160 additions and 1021 deletions
|
|
@ -86,7 +86,7 @@ pub struct Window {
|
|||
path_opt: Option<PathBuf>,
|
||||
attrs: Attrs<'static>,
|
||||
font_size: FontSize,
|
||||
editor: Mutex<cosmic_text::ViEditor<'static>>,
|
||||
editor: Mutex<cosmic_text::ViEditor<'static, 'static>>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
|
@ -185,10 +185,12 @@ impl Application for Window {
|
|||
if let Some(path) = &self.path_opt {
|
||||
let editor = self.editor.lock().unwrap();
|
||||
let mut text = String::new();
|
||||
for line in editor.buffer().lines.iter() {
|
||||
text.push_str(line.text());
|
||||
text.push('\n');
|
||||
}
|
||||
editor.with_buffer(|buffer| {
|
||||
for line in buffer.lines.iter() {
|
||||
text.push_str(line.text());
|
||||
text.push('\n');
|
||||
}
|
||||
});
|
||||
match fs::write(path, text) {
|
||||
Ok(()) => {
|
||||
log::info!("saved '{}'", path.display());
|
||||
|
|
@ -234,15 +236,13 @@ impl Application for Window {
|
|||
let mut editor = self.editor.lock().unwrap();
|
||||
editor
|
||||
.borrow_with(&mut FONT_SYSTEM.lock().unwrap())
|
||||
.buffer_mut()
|
||||
.set_metrics(font_size.to_metrics());
|
||||
.with_buffer_mut(|buffer| buffer.set_metrics(font_size.to_metrics()));
|
||||
}
|
||||
Message::WrapChanged(wrap) => {
|
||||
let mut editor = self.editor.lock().unwrap();
|
||||
editor
|
||||
.borrow_with(&mut FONT_SYSTEM.lock().unwrap())
|
||||
.buffer_mut()
|
||||
.set_wrap(wrap);
|
||||
.with_buffer_mut(|buffer| buffer.set_wrap(wrap));
|
||||
}
|
||||
Message::AlignmentChanged(align) => {
|
||||
let mut editor = self.editor.lock().unwrap();
|
||||
|
|
@ -304,7 +304,7 @@ impl Application for Window {
|
|||
let editor = self.editor.lock().unwrap();
|
||||
pick_list(
|
||||
WRAP_MODE,
|
||||
Some(editor.buffer().wrap()),
|
||||
Some(editor.with_buffer(|buffer| buffer.wrap())),
|
||||
Message::WrapChanged,
|
||||
)
|
||||
};
|
||||
|
|
@ -373,20 +373,25 @@ impl Application for Window {
|
|||
}
|
||||
|
||||
fn update_attrs<T: Edit>(editor: &mut T, attrs: Attrs) {
|
||||
editor.buffer_mut().lines.iter_mut().for_each(|line| {
|
||||
line.set_attrs_list(AttrsList::new(attrs));
|
||||
editor.with_buffer_mut(|buffer| {
|
||||
buffer.lines.iter_mut().for_each(|line| {
|
||||
line.set_attrs_list(AttrsList::new(attrs));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn update_alignment<T: Edit>(editor: &mut T, align: Align) {
|
||||
let current_line = editor.cursor().line;
|
||||
if let Some((start, end)) = editor.selection_bounds() {
|
||||
if let Some(lines) = editor.buffer_mut().lines.get_mut(start.line..=end.line) {
|
||||
for line in lines.iter_mut() {
|
||||
line.set_align(Some(align));
|
||||
let selection_bounds_opt = editor.selection_bounds();
|
||||
editor.with_buffer_mut(|buffer| {
|
||||
if let Some((start, end)) = selection_bounds_opt {
|
||||
if let Some(lines) = buffer.lines.get_mut(start.line..=end.line) {
|
||||
for line in lines.iter_mut() {
|
||||
line.set_align(Some(align));
|
||||
}
|
||||
}
|
||||
} else if let Some(line) = buffer.lines.get_mut(current_line) {
|
||||
line.set_align(Some(align));
|
||||
}
|
||||
} else if let Some(line) = editor.buffer_mut().lines.get_mut(current_line) {
|
||||
line.set_align(Some(align));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,13 +31,13 @@ impl StyleSheet for Theme {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct TextBox<'a, 'editor> {
|
||||
editor: &'a Mutex<ViEditor<'editor>>,
|
||||
pub struct TextBox<'a, 'editor, 'buffer> {
|
||||
editor: &'a Mutex<ViEditor<'editor, 'buffer>>,
|
||||
padding: Padding,
|
||||
}
|
||||
|
||||
impl<'a, 'editor> TextBox<'a, 'editor> {
|
||||
pub fn new(editor: &'a Mutex<ViEditor<'editor>>) -> Self {
|
||||
impl<'a, 'editor, 'buffer> TextBox<'a, 'editor, 'buffer> {
|
||||
pub fn new(editor: &'a Mutex<ViEditor<'editor, 'buffer>>) -> Self {
|
||||
Self {
|
||||
editor,
|
||||
padding: Padding::new(0.),
|
||||
|
|
@ -50,7 +50,9 @@ impl<'a, 'editor> TextBox<'a, 'editor> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn text_box<'a, 'editor>(editor: &'a Mutex<ViEditor<'editor>>) -> TextBox<'a, 'editor> {
|
||||
pub fn text_box<'a, 'editor, 'buffer>(
|
||||
editor: &'a Mutex<ViEditor<'editor, 'buffer>>,
|
||||
) -> TextBox<'a, 'editor, 'buffer> {
|
||||
TextBox::new(editor)
|
||||
}
|
||||
|
||||
|
|
@ -103,7 +105,8 @@ fn draw_pixel(
|
|||
buffer[offset + 3] = (current >> 24) as u8;
|
||||
}
|
||||
|
||||
impl<'a, 'editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, 'editor>
|
||||
impl<'a, 'editor, 'buffer, Message, Renderer> Widget<Message, Renderer>
|
||||
for TextBox<'a, 'editor, 'buffer>
|
||||
where
|
||||
Renderer: cosmic::iced_core::Renderer + image::Renderer<Handle = image::Handle>,
|
||||
Renderer::Theme: StyleSheet,
|
||||
|
|
@ -133,14 +136,17 @@ where
|
|||
.shape_as_needed(true);
|
||||
|
||||
let mut layout_lines = 0;
|
||||
for line in editor.buffer().lines.iter() {
|
||||
match line.layout_opt() {
|
||||
Some(layout) => layout_lines += layout.len(),
|
||||
None => (),
|
||||
editor.with_buffer(|buffer| {
|
||||
for line in buffer.lines.iter() {
|
||||
match line.layout_opt() {
|
||||
Some(layout) => layout_lines += layout.len(),
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let height = layout_lines as f32 * editor.buffer().metrics().line_height;
|
||||
let height =
|
||||
layout_lines as f32 * editor.with_buffer(|buffer| buffer.metrics().line_height);
|
||||
let size = Size::new(limits.max().width, height);
|
||||
|
||||
layout::Node::new(limits.resolve(size))
|
||||
|
|
@ -205,13 +211,11 @@ where
|
|||
let mut editor = editor.borrow_with(&mut font_system);
|
||||
|
||||
// Scale metrics
|
||||
let metrics = editor.buffer().metrics();
|
||||
editor
|
||||
.buffer_mut()
|
||||
.set_metrics(metrics.scale(SCALE_FACTOR as f32));
|
||||
let metrics = editor.with_buffer(|buffer| buffer.metrics());
|
||||
editor.with_buffer_mut(|buffer| buffer.set_metrics(metrics.scale(SCALE_FACTOR as f32)));
|
||||
|
||||
// Set size
|
||||
editor.buffer_mut().set_size(image_w as f32, image_h as f32);
|
||||
editor.with_buffer_mut(|buffer| buffer.set_size(image_w as f32, image_h as f32));
|
||||
|
||||
// Shape and layout
|
||||
editor.shape_as_needed(true);
|
||||
|
|
@ -228,7 +232,7 @@ where
|
|||
});
|
||||
|
||||
// Restore original metrics
|
||||
editor.buffer_mut().set_metrics(metrics);
|
||||
editor.with_buffer_mut(|buffer| buffer.set_metrics(metrics));
|
||||
|
||||
let handle = image::Handle::from_pixels(image_w as u32, image_h as u32, pixels);
|
||||
image::Renderer::draw(
|
||||
|
|
@ -355,12 +359,13 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'editor, Message, Renderer> From<TextBox<'a, 'editor>> for Element<'a, Message, Renderer>
|
||||
impl<'a, 'editor, 'buffer, Message, Renderer> From<TextBox<'a, 'editor, 'buffer>>
|
||||
for Element<'a, Message, Renderer>
|
||||
where
|
||||
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
|
||||
Renderer::Theme: StyleSheet,
|
||||
{
|
||||
fn from(text_box: TextBox<'a, 'editor>) -> Self {
|
||||
fn from(text_box: TextBox<'a, 'editor, 'buffer>) -> Self {
|
||||
Self::new(text_box)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,9 +69,9 @@ fn main() {
|
|||
|
||||
let mut editor = editor.borrow_with(&mut font_system);
|
||||
|
||||
editor
|
||||
.buffer_mut()
|
||||
.set_size(window.width() as f32 - line_x * 2.0, window.height() as f32);
|
||||
editor.with_buffer_mut(|buffer| {
|
||||
buffer.set_size(window.width() as f32 - line_x * 2.0, window.height() as f32)
|
||||
});
|
||||
|
||||
let attrs = Attrs::new().family(Family::Monospace);
|
||||
match editor.load_text(&path, attrs) {
|
||||
|
|
@ -89,7 +89,7 @@ fn main() {
|
|||
let mut mouse_left = false;
|
||||
loop {
|
||||
editor.shape_as_needed(true);
|
||||
if editor.buffer().redraw() {
|
||||
if editor.redraw() {
|
||||
let instant = Instant::now();
|
||||
|
||||
let bg = editor.background_color();
|
||||
|
|
@ -109,15 +109,17 @@ fn main() {
|
|||
{
|
||||
let mut start_line_opt = None;
|
||||
let mut end_line = 0;
|
||||
for run in editor.buffer().layout_runs() {
|
||||
end_line = run.line_i;
|
||||
if start_line_opt.is_none() {
|
||||
start_line_opt = Some(end_line);
|
||||
editor.with_buffer(|buffer| {
|
||||
for run in buffer.layout_runs() {
|
||||
end_line = run.line_i;
|
||||
if start_line_opt.is_none() {
|
||||
start_line_opt = Some(end_line);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let start_line = start_line_opt.unwrap_or(end_line);
|
||||
let lines = editor.buffer().lines.len();
|
||||
let lines = editor.with_buffer(|buffer| 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 {
|
||||
|
|
@ -133,7 +135,7 @@ fn main() {
|
|||
|
||||
window.sync();
|
||||
|
||||
editor.buffer_mut().set_redraw(false);
|
||||
editor.set_redraw(false);
|
||||
|
||||
log::debug!("redraw: {:?}", instant.elapsed());
|
||||
}
|
||||
|
|
@ -172,18 +174,23 @@ fn main() {
|
|||
orbclient::K_DEL if event.pressed => editor.action(Action::Delete),
|
||||
orbclient::K_0 if event.pressed && ctrl_pressed => {
|
||||
font_size_i = font_size_default;
|
||||
editor.buffer_mut().set_metrics(font_sizes[font_size_i]);
|
||||
editor
|
||||
.with_buffer_mut(|buffer| 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;
|
||||
editor.buffer_mut().set_metrics(font_sizes[font_size_i]);
|
||||
editor.with_buffer_mut(|buffer| {
|
||||
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;
|
||||
editor.buffer_mut().set_metrics(font_sizes[font_size_i]);
|
||||
editor.with_buffer_mut(|buffer| {
|
||||
buffer.set_metrics(font_sizes[font_size_i])
|
||||
});
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
|
|
@ -224,9 +231,9 @@ fn main() {
|
|||
}
|
||||
}
|
||||
EventOption::Resize(event) => {
|
||||
editor
|
||||
.buffer_mut()
|
||||
.set_size(event.width as f32 - line_x * 2.0, event.height as f32);
|
||||
editor.with_buffer_mut(|buffer| {
|
||||
buffer.set_size(event.width as f32 - line_x * 2.0, event.height as f32);
|
||||
});
|
||||
}
|
||||
EventOption::Scroll(event) => {
|
||||
editor.action(Action::Scroll {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ fn redraw(
|
|||
let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33);
|
||||
|
||||
editor.shape_as_needed(true);
|
||||
if editor.buffer().redraw() {
|
||||
if editor.redraw() {
|
||||
let instant = Instant::now();
|
||||
|
||||
window.set(bg_color);
|
||||
|
|
@ -36,7 +36,7 @@ fn redraw(
|
|||
|
||||
window.sync();
|
||||
|
||||
editor.buffer_mut().set_redraw(false);
|
||||
editor.set_redraw(false);
|
||||
|
||||
let duration = instant.elapsed();
|
||||
log::debug!("redraw: {:?}", duration);
|
||||
|
|
@ -159,13 +159,15 @@ fn main() {
|
|||
log::info!("Test completed in {:?}", test_elapsed);
|
||||
|
||||
let mut wrong = 0;
|
||||
for (line_i, line) in text.lines().enumerate() {
|
||||
let buffer_line = &editor.buffer().lines[line_i];
|
||||
if buffer_line.text() != line {
|
||||
log::error!("line {}: {:?} != {:?}", line_i, buffer_line.text(), line);
|
||||
wrong += 1;
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -42,9 +42,7 @@ fn main() {
|
|||
|
||||
let mut editor = editor.borrow_with(&mut font_system);
|
||||
|
||||
editor
|
||||
.buffer_mut()
|
||||
.set_size(window.width() as f32, window.height() as f32);
|
||||
editor.with_buffer_mut(|buffer| buffer.set_size(window.width() as f32, window.height() as f32));
|
||||
|
||||
let attrs = Attrs::new();
|
||||
let serif_attrs = attrs.family(Family::Serif);
|
||||
|
|
@ -117,9 +115,9 @@ fn main() {
|
|||
),
|
||||
];
|
||||
|
||||
editor
|
||||
.buffer_mut()
|
||||
.set_rich_text(spans.iter().copied(), attrs, Shaping::Advanced);
|
||||
editor.with_buffer_mut(|buffer| {
|
||||
buffer.set_rich_text(spans.iter().copied(), attrs, Shaping::Advanced)
|
||||
});
|
||||
|
||||
let mut swash_cache = SwashCache::new();
|
||||
|
||||
|
|
@ -134,7 +132,7 @@ fn main() {
|
|||
let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33);
|
||||
|
||||
editor.shape_as_needed(true);
|
||||
if editor.buffer().redraw() {
|
||||
if editor.redraw() {
|
||||
let instant = Instant::now();
|
||||
|
||||
window.set(bg_color);
|
||||
|
|
@ -151,7 +149,7 @@ fn main() {
|
|||
|
||||
window.sync();
|
||||
|
||||
editor.buffer_mut().set_redraw(false);
|
||||
editor.set_redraw(false);
|
||||
|
||||
let duration = instant.elapsed();
|
||||
log::debug!("redraw: {:?}", duration);
|
||||
|
|
@ -206,9 +204,9 @@ fn main() {
|
|||
}
|
||||
}
|
||||
EventOption::Resize(resize) => {
|
||||
editor
|
||||
.buffer_mut()
|
||||
.set_size(resize.width as f32, resize.height as f32);
|
||||
editor.with_buffer_mut(|buffer| {
|
||||
buffer.set_size(resize.width as f32, resize.height as f32)
|
||||
});
|
||||
}
|
||||
EventOption::Quit(_) => process::exit(0),
|
||||
_ => (),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue