Update cosmic-text

This commit is contained in:
Jeremy Soller 2023-12-19 12:07:45 -07:00
parent e706e44e0c
commit 83bf38f899
4 changed files with 158 additions and 139 deletions

92
Cargo.lock generated
View file

@ -246,9 +246,9 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
[[package]]
name = "as-raw-xcb-connection"
version = "1.0.0"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d5f312b0a56c5cdf967c0aeb67f6289603354951683bc97ddc595ab974ba9aa"
checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
[[package]]
name = "ash"
@ -305,7 +305,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ca33f4bc4ed1babef42cad36cc1f51fa88be00420404e5b1e80ab1b18f7678c"
dependencies = [
"concurrent-queue",
"event-listener 4.0.0",
"event-listener 4.0.1",
"event-listener-strategy",
"futures-core",
"pin-project-lite",
@ -391,7 +391,7 @@ version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7125e42787d53db9dd54261812ef17e937c95a51e4d291373b670342fa44310c"
dependencies = [
"event-listener 4.0.0",
"event-listener 4.0.1",
"event-listener-strategy",
"pin-project-lite",
]
@ -444,9 +444,9 @@ dependencies = [
[[package]]
name = "async-task"
version = "4.5.0"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4eb2cdb97421e01129ccb49169d8279ed21e829929144f4a22a6e54ac549ca1"
checksum = "e1d90cd0b264dfdd8eb5bad0a2c217c1f88fa96a8573f40e7b12de23fb468f46"
[[package]]
name = "async-trait"
@ -992,7 +992,7 @@ dependencies = [
[[package]]
name = "cosmic-config"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"atomicwrites",
"cosmic-config-derive",
@ -1006,7 +1006,7 @@ dependencies = [
[[package]]
name = "cosmic-config-derive"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"quote",
"syn 1.0.109",
@ -1061,7 +1061,7 @@ dependencies = [
[[package]]
name = "cosmic-text"
version = "0.10.0"
source = "git+https://github.com/pop-os/cosmic-text?branch=refactor#51ae8aa7aaf9d498092a7dbb6c69060b4c6c21fd"
source = "git+https://github.com/pop-os/cosmic-text?branch=refactor#af7b0325198cfde22c1a3e3eef1be898fd04a437"
dependencies = [
"cosmic_undo_2",
"fontdb 0.16.0",
@ -1084,7 +1084,7 @@ dependencies = [
[[package]]
name = "cosmic-theme"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"almost",
"cosmic-config",
@ -1656,9 +1656,9 @@ dependencies = [
[[package]]
name = "event-listener"
version = "4.0.0"
version = "4.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "770d968249b5d99410d61f5bf89057f3199a077a04d087092f58e7d10692baae"
checksum = "84f2cdcf274580f2d63697192d744727b3198894b1bf02923643bf59e2c26712"
dependencies = [
"concurrent-queue",
"parking",
@ -1671,7 +1671,7 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "958e4d70b6d5e81971bebec42271ec641e7ff4e170a6fa605f2b8a8b65cb97d3"
dependencies = [
"event-listener 4.0.0",
"event-listener 4.0.1",
"pin-project-lite",
]
@ -1870,7 +1870,7 @@ checksum = "98b88c54a38407f7352dd2c4238830115a6377741098ffd1f997c813d0e088a6"
dependencies = [
"fontconfig-parser",
"log",
"memmap2 0.9.0",
"memmap2 0.9.3",
"slotmap",
"tinyvec",
"ttf-parser 0.20.0",
@ -2416,7 +2416,7 @@ dependencies = [
"grep-matcher",
"log",
"memchr",
"memmap2 0.9.0",
"memmap2 0.9.3",
]
[[package]]
@ -2623,7 +2623,7 @@ dependencies = [
[[package]]
name = "iced"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"iced_accessibility",
"iced_core",
@ -2638,7 +2638,7 @@ dependencies = [
[[package]]
name = "iced_accessibility"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"accesskit",
"accesskit_winit",
@ -2647,7 +2647,7 @@ dependencies = [
[[package]]
name = "iced_core"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"bitflags 1.3.2",
"instant",
@ -2663,7 +2663,7 @@ dependencies = [
[[package]]
name = "iced_futures"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"futures",
"iced_core",
@ -2676,7 +2676,7 @@ dependencies = [
[[package]]
name = "iced_graphics"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"bitflags 1.3.2",
"bytemuck",
@ -2699,7 +2699,7 @@ dependencies = [
[[package]]
name = "iced_renderer"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"iced_graphics",
"iced_tiny_skia",
@ -2712,7 +2712,7 @@ dependencies = [
[[package]]
name = "iced_runtime"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"iced_core",
"iced_futures",
@ -2722,7 +2722,7 @@ dependencies = [
[[package]]
name = "iced_style"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"iced_core",
"once_cell",
@ -2732,7 +2732,7 @@ dependencies = [
[[package]]
name = "iced_tiny_skia"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"bytemuck",
"cosmic-text 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2750,7 +2750,7 @@ dependencies = [
[[package]]
name = "iced_wgpu"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"bitflags 1.3.2",
"bytemuck",
@ -2770,7 +2770,7 @@ dependencies = [
[[package]]
name = "iced_widget"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"iced_renderer",
"iced_runtime",
@ -2784,7 +2784,7 @@ dependencies = [
[[package]]
name = "iced_winit"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"iced_graphics",
"iced_runtime",
@ -3094,7 +3094,7 @@ checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
[[package]]
name = "libcosmic"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#d53f693a3715fdd79481d75652cbf74286f4f387"
source = "git+https://github.com/pop-os/libcosmic#11428723e55dfebad617cedf2636a1a8c804d42c"
dependencies = [
"apply",
"ashpd",
@ -3292,13 +3292,13 @@ dependencies = [
[[package]]
name = "lyon_tessellation"
version = "1.0.12"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f5bcf02928361d18e6edb8ad3bc5b93cba8aa57e2508deb072c2d2ade8bbd0d"
checksum = "8c7c67b5bc8123b352b2e7e742b47d1f236a13fe77619433be9568fbd888e9c0"
dependencies = [
"float_next_after",
"lyon_path",
"thiserror",
"num-traits",
]
[[package]]
@ -3345,9 +3345,9 @@ dependencies = [
[[package]]
name = "memmap2"
version = "0.9.0"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "deaba38d7abf1d4cca21cc89e932e542ba2b9258664d2a9ef0e61512039c9375"
checksum = "45fd3a57831bf88bc63f8cebc0cf956116276e97fef3966103e96416209f7c92"
dependencies = [
"libc",
]
@ -5121,7 +5121,7 @@ dependencies = [
"foreign-types 0.5.0",
"js-sys",
"log",
"memmap2 0.9.0",
"memmap2 0.9.3",
"objc",
"raw-window-handle 0.5.2",
"redox_syscall 0.4.1",
@ -5335,18 +5335,18 @@ dependencies = [
[[package]]
name = "thiserror"
version = "1.0.50"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
checksum = "f11c217e1416d6f036b870f14e0413d480dbf28edbee1f877abaf0206af43bb7"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.50"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
checksum = "01742297787513b79cf8e29d1056ede1313e2420b7b3b15d0a768b4921f549df"
dependencies = [
"proc-macro2",
"quote",
@ -5377,9 +5377,9 @@ dependencies = [
[[package]]
name = "time"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a34ab300f2dee6e562c10a046fc05e358b29f9bf92277f30c3c8d82275f6f5"
checksum = "f657ba42c3f86e7680e53c8cd3af8abbe56b5491790b46e22e19c0d57463583e"
dependencies = [
"deranged",
"itoa",
@ -5397,9 +5397,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
[[package]]
name = "time-macros"
version = "0.2.15"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ad70d68dba9e1f8aceda7aa6711965dfec1cac869f311a51bd08b3a2ccbce20"
checksum = "26197e33420244aeb70c3e8c78376ca46571bc4e701e4791c2cd9f57dcb3a43f"
dependencies = [
"time-core",
]
@ -5493,9 +5493,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.35.0"
version = "1.35.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "841d45b238a16291a4e1584e61820b8ae57d696cc5015c459c229ccc6990cc1c"
checksum = "c89b4efa943be685f629b149f53829423f8f5531ea21249408e8e2f8671ec104"
dependencies = [
"backtrace",
"bytes",
@ -6637,9 +6637,9 @@ dependencies = [
[[package]]
name = "winnow"
version = "0.5.28"
version = "0.5.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c830786f7720c2fd27a1a0e27a709dbd3c4d009b56d098fc742d4f4eab91fe2"
checksum = "9b5c3db89721d50d0e2a673f5043fc4722f76dcc352d7b1ab8b8288bed4ed2c5"
dependencies = [
"memchr",
]

View file

@ -483,7 +483,7 @@ impl App {
Some(tab) => {
if let Tab::Editor(inner) = tab {
// Force redraw on tab switches
inner.editor.lock().unwrap().buffer_mut().set_redraw(true);
inner.editor.lock().unwrap().set_redraw(true);
}
tab.title()
}
@ -503,18 +503,18 @@ impl App {
match self.active_tab() {
Some(Tab::Editor(tab)) => {
let editor = tab.editor.lock().unwrap();
let buffer = editor.buffer();
line_count = buffer.lines.len();
for line in buffer.lines.iter() {
//TODO: do graphemes?
for c in line.text().chars() {
character_count += 1;
if !c.is_whitespace() {
character_count_no_spaces += 1;
editor.with_buffer(|buffer| {
line_count = buffer.lines.len();
for line in buffer.lines.iter() {
//TODO: do graphemes?
for c in line.text().chars() {
character_count += 1;
if !c.is_whitespace() {
character_count_no_spaces += 1;
}
}
}
}
});
}
_ => {}
}
@ -1134,9 +1134,11 @@ impl Application for App {
self.tab_model.data_mut::<Tab>(entity)
{
let mut editor = tab.editor.lock().unwrap();
for line in editor.buffer_mut().lines.iter_mut() {
line.reset();
}
editor.with_buffer_mut(|buffer| {
for line in buffer.lines.iter_mut() {
line.reset();
}
});
}
}
@ -1465,10 +1467,12 @@ impl Application for App {
editor.set_cursor(Cursor::new(0, 0));
// Set selection end to highest possible value
let buffer = editor.buffer();
let last_line = buffer.lines.len().saturating_sub(1);
let last_index = buffer.lines[last_line].text().len();
editor.set_selection(Selection::Normal(Cursor::new(last_line, last_index)));
let selection = editor.with_buffer(|buffer| {
let last_line = buffer.lines.len().saturating_sub(1);
let last_index = buffer.lines[last_line].text().len();
Selection::Normal(Cursor::new(last_line, last_index))
});
editor.set_selection(selection);
}
_ => {}
}
@ -1629,7 +1633,7 @@ impl Application for App {
for entity in entities {
if let Some(Tab::Editor(tab)) = self.tab_model.data_mut::<Tab>(entity) {
let mut editor = tab.editor.lock().unwrap();
editor.buffer_mut().set_redraw(true);
editor.set_redraw(true);
}
}

View file

@ -6,7 +6,11 @@ use cosmic::{
};
use cosmic_text::{Attrs, Buffer, Edit, Shaping, SyntaxEditor, ViEditor, Wrap};
use notify::Watcher;
use std::{fs, path::PathBuf, sync::Mutex};
use std::{
fs,
path::PathBuf,
sync::{Arc, Mutex},
};
use crate::{fl, git::GitDiff, mime_icon, Config, FALLBACK_MIME_ICON, FONT_SYSTEM, SYNTAX_SYSTEM};
@ -32,7 +36,7 @@ pub struct GitDiffTab {
pub struct EditorTab {
pub path_opt: Option<PathBuf>,
attrs: Attrs<'static>,
pub editor: Mutex<ViEditor<'static>>,
pub editor: Mutex<ViEditor<'static, 'static>>,
pub context_menu: Option<Point>,
}
@ -49,7 +53,8 @@ impl EditorTab {
Shaping::Advanced,
);
let editor = SyntaxEditor::new(buffer, &SYNTAX_SYSTEM, config.syntax_theme()).unwrap();
let editor =
SyntaxEditor::new(Arc::new(buffer), &SYNTAX_SYSTEM, config.syntax_theme()).unwrap();
let mut tab = Self {
path_opt: None,
@ -71,10 +76,12 @@ impl EditorTab {
editor.set_auto_indent(config.auto_indent);
editor.set_passthrough(!config.vim_bindings);
editor.set_tab_width(config.tab_width);
editor.buffer_mut().set_wrap(if config.word_wrap {
Wrap::Word
} else {
Wrap::None
editor.with_buffer_mut(|buffer| {
buffer.set_wrap(if config.word_wrap {
Wrap::Word
} else {
Wrap::None
})
});
//TODO: dynamically discover light/dark changes
editor.update_theme(config.syntax_theme());
@ -108,7 +115,7 @@ impl EditorTab {
let mut editor = editor.borrow_with(&mut font_system);
if let Some(path) = &self.path_opt {
// Save scroll
let scroll = editor.buffer().scroll();
let scroll = editor.with_buffer(|buffer| buffer.scroll());
//TODO: save/restore more?
match editor.load_text(path, self.attrs) {
@ -121,7 +128,7 @@ impl EditorTab {
}
// Restore scroll
editor.buffer_mut().set_scroll(scroll);
editor.with_buffer_mut(|buffer| buffer.set_scroll(scroll));
} else {
log::warn!("tried to reload with no path");
}
@ -131,10 +138,12 @@ impl EditorTab {
if let Some(path) = &self.path_opt {
let mut 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(()) => {
editor.set_changed(false);

View file

@ -27,7 +27,7 @@ use std::{
use crate::{line_number::LineNumberKey, FONT_SYSTEM, LINE_NUMBER_CACHE, SWASH_CACHE};
pub struct TextBox<'a, Message> {
editor: &'a Mutex<ViEditor<'static>>,
editor: &'a Mutex<ViEditor<'static, 'static>>,
metrics: Metrics,
padding: Padding,
on_changed: Option<Message>,
@ -41,7 +41,7 @@ impl<'a, Message> TextBox<'a, Message>
where
Message: Clone,
{
pub fn new(editor: &'a Mutex<ViEditor<'static>>, metrics: Metrics) -> Self {
pub fn new(editor: &'a Mutex<ViEditor<'static, 'static>>, metrics: Metrics) -> Self {
Self {
editor,
metrics,
@ -89,7 +89,7 @@ where
}
pub fn text_box<'a, Message>(
editor: &'a Mutex<ViEditor<'static>>,
editor: &'a Mutex<ViEditor<'static, 'static>>,
metrics: Metrics,
) -> TextBox<'a, Message>
where
@ -207,18 +207,20 @@ where
.borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.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| {
let mut layout_lines = 0;
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 size = Size::new(limits.max().width, height);
let height = layout_lines as f32 * buffer.metrics().line_height;
let size = Size::new(limits.max().width, height);
layout::Node::new(limits.resolve(size))
layout::Node::new(limits.resolve(size))
})
}
fn mouse_interaction(
@ -240,7 +242,7 @@ where
let editor_offset_x = state.editor_offset_x.get();
let scale_factor = state.scale_factor.get();
let editor = self.editor.lock().unwrap();
let buffer_size = editor.buffer().size();
let buffer_size = editor.with_buffer(|buffer| buffer.size());
let x_logical = p.x - self.padding.left;
let y_logical = p.y - self.padding.top;
@ -299,7 +301,7 @@ where
let (line_number_chars, editor_offset_x) = if self.line_numbers {
// Calculate number of characters needed in line number
let mut line_number_chars = 1;
let mut line_count = editor.buffer().lines.len();
let mut line_count = editor.with_buffer(|buffer| buffer.lines.len());
while line_count >= 10 {
line_count /= 10;
line_number_chars += 1;
@ -334,31 +336,33 @@ where
// Save editor offset in state
if state.editor_offset_x.replace(editor_offset_x) != editor_offset_x {
// Mark buffer as needing redraw if editor offset has changed
editor.buffer_mut().set_redraw(true);
editor.set_redraw(true);
}
// Set metrics and size
editor.buffer_mut().set_metrics_and_size(
&mut font_system,
metrics,
(image_w - editor_offset_x) as f32,
image_h as f32,
);
editor.with_buffer_mut(|buffer| {
buffer.set_metrics_and_size(
&mut font_system,
metrics,
(image_w - editor_offset_x) as f32,
image_h as f32,
)
});
// Shape and layout as needed
editor.shape_as_needed(&mut font_system, true);
let mut handle_opt = state.handle_opt.lock().unwrap();
if editor.buffer().redraw() || handle_opt.is_none() {
if editor.redraw() || handle_opt.is_none() {
// Draw to pixel buffer
let mut pixels = vec![0; image_w as usize * image_h as usize * 4];
let mut pixels_u8 = vec![0; image_w as usize * image_h as usize * 4];
{
let mut swash_cache = SWASH_CACHE.lock().unwrap();
let buffer = unsafe {
let pixels = unsafe {
std::slice::from_raw_parts_mut(
pixels.as_mut_ptr() as *mut u32,
pixels.len() / 4,
pixels_u8.as_mut_ptr() as *mut u32,
pixels_u8.len() / 4,
)
};
@ -381,7 +385,7 @@ where
// Ensure fill with gutter color
draw_rect(
buffer,
pixels,
image_w,
image_h,
0,
@ -393,10 +397,10 @@ where
// Draw line numbers
//TODO: move to cosmic-text?
{
editor.with_buffer(|buffer| {
let mut line_number_cache = LINE_NUMBER_CACHE.lock().unwrap();
let mut last_line_number = 0;
for run in editor.buffer().layout_runs() {
for run in buffer.layout_runs() {
let line_number = run.line_i.saturating_add(1);
if line_number == last_line_number {
// Skip duplicate lines
@ -434,7 +438,7 @@ where
gutter_foreground,
|x, y, color| {
draw_rect(
buffer,
pixels,
image_w,
image_h,
physical_glyph.x + x,
@ -448,13 +452,13 @@ where
}
}
}
}
});
}
// Draw editor
editor.draw(&mut font_system, &mut swash_cache, |x, y, w, h, color| {
draw_rect(
buffer,
pixels,
image_w,
image_h,
editor_offset_x + x,
@ -466,10 +470,10 @@ where
});
// Calculate scrollbar
{
editor.with_buffer(|buffer| {
let mut start_line_opt = None;
let mut end_line = 0;
for run in editor.buffer().layout_runs() {
for run in buffer.layout_runs() {
end_line = run.line_i;
if start_line_opt.is_none() {
start_line_opt = Some(end_line);
@ -477,7 +481,7 @@ where
}
let start_line = start_line_opt.unwrap_or(end_line);
let lines = editor.buffer().lines.len();
let lines = buffer.lines.len();
let start_y = (start_line * image_h as usize) / lines;
let end_y = ((end_line + 1) * image_h as usize) / lines;
@ -489,17 +493,17 @@ where
),
);
state.scrollbar_rect.set(rect);
}
});
}
// Clear redraw flag
editor.buffer_mut().set_redraw(false);
editor.set_redraw(false);
state.scale_factor.set(scale_factor);
*handle_opt = Some(image::Handle::from_pixels(
image_w as u32,
image_h as u32,
pixels,
pixels_u8,
));
}
@ -558,7 +562,7 @@ where
let scale_factor = state.scale_factor.get();
let scrollbar_rect = state.scrollbar_rect.get();
let mut editor = self.editor.lock().unwrap();
let buffer_size = editor.buffer().size();
let buffer_size = editor.with_buffer(|buffer| buffer.size());
let last_changed = editor.changed();
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut editor = editor.borrow_with(&mut font_system);
@ -681,21 +685,22 @@ where
} else if scrollbar_rect.contains(Point::new(x_logical, y_logical)) {
state.dragging = Some(Dragging::Scrollbar {
start_y: y,
start_scroll: editor.buffer().scroll(),
start_scroll: editor.with_buffer(|buffer| buffer.scroll()),
});
} else if x_logical >= scrollbar_rect.x
&& x_logical < (scrollbar_rect.x + scrollbar_rect.width)
{
let mut buffer = editor.buffer_mut();
let scroll_line =
((y / buffer.size().1) * buffer.lines.len() as f32) as i32;
buffer.set_scroll(Scroll::new(
scroll_line.try_into().unwrap_or_default(),
0,
));
state.dragging = Some(Dragging::Scrollbar {
start_y: y,
start_scroll: editor.buffer().scroll(),
editor.with_buffer_mut(|buffer| {
let scroll_line =
((y / buffer.size().1) * buffer.lines.len() as f32) as i32;
buffer.set_scroll(Scroll::new(
scroll_line.try_into().unwrap_or_default(),
0,
));
state.dragging = Some(Dragging::Scrollbar {
start_y: y,
start_scroll: buffer.scroll(),
});
});
}
}
@ -736,16 +741,17 @@ where
start_y,
start_scroll,
} => {
let mut buffer = editor.buffer_mut();
let scroll_offset = (((y - start_y) / buffer.size().1)
* buffer.lines.len() as f32)
as i32;
buffer.set_scroll(Scroll::new(
(start_scroll.line as i32 + scroll_offset)
.try_into()
.unwrap_or_default(),
0,
));
editor.with_buffer_mut(|buffer| {
let scroll_offset = (((y - start_y) / buffer.size().1)
* buffer.lines.len() as f32)
as i32;
buffer.set_scroll(Scroll::new(
(start_scroll.line as i32 + scroll_offset)
.try_into()
.unwrap_or_default(),
0,
));
});
}
}
}
@ -768,7 +774,7 @@ where
//TODO: this adjustment is just a guess!
state.scroll_pixels -= y * 6.0;
let mut lines = 0;
let metrics = editor.buffer().metrics();
let metrics = editor.with_buffer(|buffer| buffer.metrics());
while state.scroll_pixels <= -metrics.line_height {
lines -= 1;
state.scroll_pixels += metrics.line_height;