Skip to content

Commit

Permalink
uilist now draws selected row without color
Browse files Browse the repository at this point in the history
This eliminates problems with contrast between the foreground colors
in the text and the background color used to indicate which row is
selected.

Fixes #75974
  • Loading branch information
Daniel Brooks committed Sep 11, 2024
1 parent dd272dd commit 7a0ba64
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ void uilist_impl::draw_controls()
parent.need_to_scroll = true;
is_selected = parent.clicked = true;
}
bool is_hovered = ImGui::IsItemHovered( ImGuiHoveredFlags_NoNavOverride );
bool mouse_moved = ImGui::GetCurrentContext()->HoveredId !=
ImGui::GetCurrentContext()->HoveredIdPreviousFrame;
if( ImGui::IsItemHovered( ImGuiHoveredFlags_NoNavOverride ) && mouse_moved ) {
if( is_hovered && mouse_moved ) {
// this row is hovered and the hover state just changed, show context for it
parent.hovered = parent.fentries[ i ];
}
Expand All @@ -137,16 +138,20 @@ void uilist_impl::draw_controls()
is_selected ? parent.hilight_color : parent.hotkey_color );
}

std::string &str1 = entry.txt;
std::string &str2 = entry.ctxt;
nc_color color = entry.enabled || entry.force_color ? entry.text_color : parent.disabled_color;
if( is_selected || is_hovered ) {
str1 = entry._txt_nocolor;
str2 = entry._ctxt_nocolor;
color = parent.hilight_color;
}

ImGui::TableSetColumnIndex( 1 );
nc_color color = ( is_selected ?
parent.hilight_color :
( entry.enabled || entry.force_color ?
entry.text_color :
parent.disabled_color ) );
cataimgui::draw_colored_text( entry.txt, color );
cataimgui::draw_colored_text( str1, color );

ImGui::TableSetColumnIndex( 2 );
cataimgui::draw_colored_text( entry.ctxt, color );
cataimgui::draw_colored_text( str2, color );

ImGui::PopID();
}
Expand Down Expand Up @@ -212,31 +217,41 @@ uilist_entry::uilist_entry( const std::string &txt )
: retval( -1 ), enabled( true ), hotkey( std::nullopt ), txt( txt ),
text_color( c_red_red )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist_entry::uilist_entry( const std::string &txt, const std::string &desc )
: retval( -1 ), enabled( true ), hotkey( std::nullopt ), txt( txt ),
desc( desc ), text_color( c_red_red )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist_entry::uilist_entry( const std::string &txt, const int key )
: retval( -1 ), enabled( true ), hotkey( hotkey_from_char( key ) ), txt( txt ),
text_color( c_red_red )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist_entry::uilist_entry( const std::string &txt, const std::optional<input_event> &key )
: retval( -1 ), enabled( true ), hotkey( key ), txt( txt ),
text_color( c_red_red )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist_entry::uilist_entry( const int retval, const bool enabled, const int key,
const std::string &txt )
: retval( retval ), enabled( enabled ), hotkey( hotkey_from_char( key ) ), txt( txt ),
text_color( c_red_red )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist_entry::uilist_entry( const int retval, const bool enabled,
Expand All @@ -245,20 +260,26 @@ uilist_entry::uilist_entry( const int retval, const bool enabled,
: retval( retval ), enabled( enabled ), hotkey( key ), txt( txt ),
text_color( c_red_red )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist_entry::uilist_entry( const int retval, const bool enabled, const int key,
const std::string &txt, const std::string &desc )
: retval( retval ), enabled( enabled ), hotkey( hotkey_from_char( key ) ), txt( txt ),
desc( desc ), text_color( c_red_red )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist_entry::uilist_entry( const int retval, const bool enabled,
const std::optional<input_event> &key, const std::string &txt, const std::string &desc )
: retval( retval ), enabled( enabled ), hotkey( key ), txt( txt ),
desc( desc ), text_color( c_red_red )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist_entry::uilist_entry( const int retval, const bool enabled, const int key,
Expand All @@ -267,6 +288,8 @@ uilist_entry::uilist_entry( const int retval, const bool enabled, const int key,
: retval( retval ), enabled( enabled ), hotkey( hotkey_from_char( key ) ), txt( txt ),
desc( desc ), ctxt( column ), text_color( c_red_red )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist_entry::uilist_entry( const int retval, const bool enabled,
Expand All @@ -276,6 +299,8 @@ uilist_entry::uilist_entry( const int retval, const bool enabled,
: retval( retval ), enabled( enabled ), hotkey( key ), txt( txt ),
desc( desc ), ctxt( column ), text_color( c_red_red )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist_entry::uilist_entry( const int retval, const bool enabled, const int key,
Expand All @@ -284,6 +309,8 @@ uilist_entry::uilist_entry( const int retval, const bool enabled, const int key,
: retval( retval ), enabled( enabled ), hotkey( hotkey_from_char( key ) ), txt( txt ),
hotkey_color( keycolor ), text_color( txtcolor )
{
_txt_nocolor = remove_color_tags( txt );
_ctxt_nocolor = remove_color_tags( ctxt );
}

uilist::size_scalar &uilist::size_scalar::operator=( auto_assign )
Expand Down
3 changes: 3 additions & 0 deletions src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ struct uilist_entry {
explicit uilist_entry( Enum e, Args && ... args ) :
uilist_entry( static_cast<int>( e ), std::forward<Args>( args )... )
{}

std::string _txt_nocolor; // what it says on the tin
std::string _ctxt_nocolor; // second column text
};

/**
Expand Down

0 comments on commit 7a0ba64

Please sign in to comment.