Skip to content

Commit fb6a9dd

Browse files
committed
HexEditor: Clarify code for selecting byte colors
Instead of assigning, then sometimes reassigning these colors, set them once. This makes it easier to see how we prioritize the different factors that affect the styling. `highlight_flag` is now `selected` since it represents if the byte is within the current selection.
1 parent 8ae4464 commit fb6a9dd

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

Userland/Applications/HexEditor/HexEditor.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,6 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
634634

635635
auto const cell = m_document->get(byte_position);
636636

637-
bool const selection_inbetween_start_end = byte_position >= m_selection_start && byte_position < m_selection_end;
638-
bool const selection_inbetween_end_start = byte_position >= m_selection_end && byte_position < m_selection_start;
639-
bool const highlight_flag = selection_inbetween_start_end || selection_inbetween_end_start;
640-
641637
Gfx::IntRect hex_display_rect_high_nibble {
642638
frame_thickness() + offset_margin_width() + j * cell_width() + 2 * m_padding,
643639
frame_thickness() + m_padding + i * line_height(),
@@ -663,27 +659,39 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
663659
auto high_nibble = line.substring_from_byte_offset(0, 1).release_value_but_fixme_should_propagate_errors();
664660
auto low_nibble = line.substring_from_byte_offset(1).release_value_but_fixme_should_propagate_errors();
665661

666-
Gfx::Color background_color_hex = palette().color(background_role());
667-
Gfx::Color background_color_text = background_color_hex;
668-
Gfx::Color text_color_hex = [&]() -> Gfx::Color {
662+
bool const selected = min(m_selection_start, m_selection_end) <= byte_position
663+
&& byte_position < max(m_selection_start, m_selection_end);
664+
665+
// Styling priorities are as follows, with smaller numbers beating larger ones:
666+
// 1. Modified bytes
667+
// 2. The cursor position
668+
// 3. The selection
669+
// 4. Null bytes
670+
// 5. Regular formatting
671+
auto determine_background_color = [&](EditMode edit_mode) -> Gfx::Color {
672+
if (selected)
673+
return cell.modified ? palette().selection().inverted() : palette().selection();
674+
if (byte_position == m_position && m_edit_mode != edit_mode)
675+
return palette().inactive_selection();
676+
return palette().color(background_role());
677+
};
678+
auto determine_text_color = [&](EditMode edit_mode) -> Gfx::Color {
669679
if (cell.modified)
670680
return Color::Red;
681+
if (selected)
682+
return palette().selection_text();
683+
if (byte_position == m_position)
684+
return (m_edit_mode == edit_mode) ? palette().color(foreground_role()) : palette().inactive_selection_text();
671685
if (cell.value == 0x00)
672686
return palette().color(ColorRole::PlaceholderText);
673687
return palette().color(foreground_role());
674-
}();
675-
Gfx::Color text_color_text = text_color_hex;
688+
};
689+
Gfx::Color background_color_hex = determine_background_color(EditMode::Hex);
690+
Gfx::Color background_color_text = determine_background_color(EditMode::Text);
691+
Gfx::Color text_color_hex = determine_text_color(EditMode::Hex);
692+
Gfx::Color text_color_text = determine_text_color(EditMode::Text);
676693
auto& font = cell.modified ? this->font().bold_variant() : this->font();
677694

678-
if (highlight_flag) {
679-
background_color_hex = background_color_text = cell.modified ? palette().selection().inverted() : palette().selection();
680-
text_color_hex = text_color_text = cell.modified ? text_color_hex : palette().selection_text();
681-
} else if (byte_position == m_position) {
682-
background_color_hex = (m_edit_mode == EditMode::Hex) ? background_color_hex : palette().inactive_selection();
683-
text_color_hex = (m_edit_mode == EditMode::Hex) ? text_color_text : palette().inactive_selection_text();
684-
background_color_text = (m_edit_mode == EditMode::Text) ? background_color_text : palette().inactive_selection();
685-
text_color_text = (m_edit_mode == EditMode::Text) ? text_color_text : palette().inactive_selection_text();
686-
}
687695
painter.fill_rect(background_rect, background_color_hex);
688696

689697
Gfx::IntRect text_display_rect {

0 commit comments

Comments
 (0)