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

@ -33,6 +33,7 @@ pub struct Config {
pub auto_indent: bool,
pub font_name: String,
pub font_size: u16,
pub highlight_current_line: bool,
pub line_numbers: bool,
//TODO: move to state?
pub recent_files: VecDeque<PathBuf>,
@ -51,6 +52,7 @@ impl Default for Config {
auto_indent: true,
font_name: "Fira Mono".to_string(),
font_size: 14,
highlight_current_line: true,
line_numbers: true,
recent_files: VecDeque::new(),
recent_projects: VecDeque::new(),

View file

@ -195,6 +195,7 @@ pub enum Action {
ToggleAutoIndent,
ToggleDocumentStatistics,
ToggleGitManagement,
ToggleHighlightCurrentLine,
ToggleLineNumbers,
ToggleProjectSearch,
ToggleSettingsPage,
@ -241,6 +242,7 @@ impl Action {
Message::ToggleContextPage(ContextPage::DocumentStatistics)
}
Self::ToggleGitManagement => Message::ToggleContextPage(ContextPage::GitManagement),
Self::ToggleHighlightCurrentLine => Message::ToggleHighlightCurrentLine,
Self::ToggleLineNumbers => Message::ToggleLineNumbers,
Self::ToggleProjectSearch => Message::ToggleContextPage(ContextPage::ProjectSearch),
Self::ToggleSettingsPage => Message::ToggleContextPage(ContextPage::Settings),
@ -335,6 +337,7 @@ pub enum Message {
Todo,
ToggleAutoIndent,
ToggleContextPage(ContextPage),
ToggleHighlightCurrentLine,
ToggleLineNumbers,
ToggleWordWrap,
Undo,
@ -474,7 +477,7 @@ impl App {
*open = true;
*root = true;
for (project_name, project_path) in self.projects.iter() {
for (_project_name, project_path) in self.projects.iter() {
if project_path == path {
// Project already open
return;
@ -2014,6 +2017,20 @@ impl Application for App {
// Ensure focus of correct input
return self.update_focus();
}
Message::ToggleHighlightCurrentLine => {
self.config.highlight_current_line = !self.config.highlight_current_line;
// This forces a redraw of all buffers
let entities: Vec<_> = self.tab_model.iter().collect();
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.set_redraw(true);
}
}
return self.save_config();
}
Message::ToggleLineNumbers => {
self.config.line_numbers = !self.config.line_numbers;
@ -2134,6 +2151,9 @@ impl Application for App {
.on_context_menu(move |position_opt| {
Message::TabContextMenu(tab_id, position_opt)
});
if self.config.highlight_current_line {
text_box = text_box.highlight_current_line();
}
if self.config.line_numbers {
text_box = text_box.line_numbers();
}

View file

@ -258,7 +258,11 @@ pub fn menu_bar<'a>(
config.line_numbers,
Action::ToggleLineNumbers,
),
menu_checkbox(fl!("highlight-current-line"), false, Action::Todo),
menu_checkbox(
fl!("highlight-current-line"),
config.highlight_current_line,
Action::ToggleHighlightCurrentLine,
),
//TODO: menu_item(fl!("syntax-highlighting"), Action::Todo),
MenuTree::new(horizontal_rule(1)),
menu_item(fl!("menu-settings"), Action::ToggleSettingsPage),

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(