Implement highlight current line, fixes #133

This commit is contained in:
Jeremy Soller 2024-02-20 09:45:19 -07:00
parent 22ec51b7e7
commit 803ef549d0
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
5 changed files with 151 additions and 78 deletions

View file

@ -44,6 +44,7 @@ pub struct TextBox<'a, Message> {
click_timing: Duration,
has_context_menu: bool,
on_context_menu: Option<Box<dyn Fn(Option<Point>) -> Message + 'a>>,
highlight_current_line: bool,
line_numbers: bool,
}
@ -61,6 +62,7 @@ where
click_timing: Duration::from_millis(500),
has_context_menu: false,
on_context_menu: None,
highlight_current_line: false,
line_numbers: false,
}
}
@ -98,6 +100,11 @@ where
self
}
pub fn highlight_current_line(mut self) -> Self {
self.highlight_current_line = true;
self
}
pub fn line_numbers(mut self) -> Self {
self.line_numbers = true;
self
@ -501,6 +508,46 @@ where
});
}
if self.highlight_current_line {
let line_highlight = {
let convert_color = |color: syntect::highlighting::Color| {
cosmic_text::Color::rgba(color.r, color.g, color.b, color.a)
};
let syntax_theme = editor.theme();
//TODO: ideal fallback for line highlight color
syntax_theme
.settings
.line_highlight
.map_or(editor.background_color(), convert_color)
};
let cursor = editor.cursor();
editor.with_buffer(|buffer| {
for run in buffer.layout_runs() {
if run.line_i != cursor.line {
continue;
}
draw_rect(
pixels,
Canvas {
w: image_w,
h: image_h,
},
Canvas {
w: image_w - editor_offset_x,
h: metrics.line_height as i32,
},
Offset {
x: editor_offset_x,
y: run.line_top as i32,
},
line_highlight,
);
}
});
}
// Draw editor
editor.draw(font_system.raw(), &mut swash_cache, |x, y, w, h, color| {
draw_rect(