mirror of
https://github.com/RGBCube/serenity
synced 2025-05-19 17:45:08 +00:00
LibGUI: Implement trailing whitespace visualization in TextEditor
This patch adds an optional mode where TextEditor highlights trailing whitespace characters on each line with a nice reddish dither pattern. We should probably make this themable and I'm sure it could be nicer somehow, but this is just a first cut and I do kinda like it. :^)
This commit is contained in:
parent
08f1ea3e45
commit
aa70d8c217
4 changed files with 52 additions and 0 deletions
|
@ -495,6 +495,26 @@ void TextEditor::paint_event(PaintEvent& event)
|
|||
}
|
||||
}
|
||||
|
||||
if (m_visualize_trailing_whitespace && line.ends_in_whitespace()) {
|
||||
size_t physical_column;
|
||||
auto last_non_whitespace_column = line.last_non_whitespace_column();
|
||||
if (last_non_whitespace_column.has_value())
|
||||
physical_column = last_non_whitespace_column.value() + 1;
|
||||
else
|
||||
physical_column = 0;
|
||||
size_t end_of_visual_line = (start_of_visual_line + visual_line_text.length());
|
||||
if (physical_column < end_of_visual_line) {
|
||||
size_t visual_column = physical_column > start_of_visual_line ? (physical_column - start_of_visual_line) : 0;
|
||||
Gfx::IntRect whitespace_rect {
|
||||
content_x_for_position({ line_index, visual_column }),
|
||||
visual_line_rect.y(),
|
||||
font().width(visual_line_text.substring_view(visual_column, visual_line_text.length() - visual_column)),
|
||||
visual_line_rect.height()
|
||||
};
|
||||
painter.fill_rect_with_dither_pattern(whitespace_rect, Color(), Color(255, 192, 192));
|
||||
}
|
||||
}
|
||||
|
||||
if (physical_line_has_selection) {
|
||||
size_t start_of_selection_within_visual_line = (size_t)max(0, (int)selection_start_column_within_line - (int)start_of_visual_line);
|
||||
size_t end_of_selection_within_visual_line = selection_end_column_within_line - start_of_visual_line;
|
||||
|
@ -536,6 +556,7 @@ void TextEditor::paint_event(PaintEvent& event)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
++visual_line_index;
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
@ -1718,4 +1739,12 @@ void TextEditor::set_icon(const Gfx::Bitmap* icon)
|
|||
update();
|
||||
}
|
||||
|
||||
void TextEditor::set_visualize_trailing_whitespace(bool enabled)
|
||||
{
|
||||
if (m_visualize_trailing_whitespace == enabled)
|
||||
return;
|
||||
m_visualize_trailing_whitespace = enabled;
|
||||
update();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue