From 40b7ad3520bbaf8bae775ba374f9d5f1e880218c Mon Sep 17 00:00:00 2001 From: moxian Date: Fri, 6 Dec 2024 21:24:44 -0800 Subject: [PATCH 1/2] Show uilist item under cursor --- src/ui.cpp | 78 ++++++++++-------- src/ui.h | 229 +++++++++++++++++++++++++++-------------------------- 2 files changed, 160 insertions(+), 147 deletions(-) diff --git a/src/ui.cpp b/src/ui.cpp index da4a237de2eb0..98a5efc389507 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -171,8 +171,8 @@ void uilist_impl::draw_controls() 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; + str1 = entry._txt_nocolor(); + str2 = entry._ctxt_nocolor(); color = parent.hilight_color; } @@ -248,81 +248,73 @@ static std::optional hotkey_from_char( const int ch ) uilist_entry::uilist_entry( const std::string &txt ) : retval( -1 ), enabled( true ), hotkey( std::nullopt ), txt( txt ), - text_color( c_red_red ) + text_color( c_red_red ), _txt_nocolor_cached( "" ), _ctxt_nocolor_cached( "" ) { - _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 ) + desc( desc ), text_color( c_red_red ), _txt_nocolor_cached( "" ), _ctxt_nocolor_cached( "" ) { - _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 ) + text_color( c_red_red ), _txt_nocolor_cached( "" ), _ctxt_nocolor_cached( "" ) { - _txt_nocolor = remove_color_tags( txt ); - _ctxt_nocolor = remove_color_tags( ctxt ); + } uilist_entry::uilist_entry( const std::string &txt, const std::optional &key ) : retval( -1 ), enabled( true ), hotkey( key ), txt( txt ), - text_color( c_red_red ) + text_color( c_red_red ), _txt_nocolor_cached( "" ), _ctxt_nocolor_cached( "" ) { - _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 ) + text_color( c_red_red ), _txt_nocolor_cached( "" ), _ctxt_nocolor_cached( "" ) { - _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 &key, const std::string &txt ) : retval( retval ), enabled( enabled ), hotkey( key ), txt( txt ), - text_color( c_red_red ) + text_color( c_red_red ), _txt_nocolor_cached( "" ), _ctxt_nocolor_cached( "" ) { - _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 ) + desc( desc ), text_color( c_red_red ), _txt_nocolor_cached( "" ), _ctxt_nocolor_cached( "" ) { - _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 &key, const std::string &txt, const std::string &desc ) : retval( retval ), enabled( enabled ), hotkey( key ), txt( txt ), - desc( desc ), text_color( c_red_red ) + desc( desc ), text_color( c_red_red ), _txt_nocolor_cached( "" ), _ctxt_nocolor_cached( "" ) { - _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, const std::string &column ) : retval( retval ), enabled( enabled ), hotkey( hotkey_from_char( key ) ), txt( txt ), - desc( desc ), ctxt( column ), text_color( c_red_red ) + desc( desc ), ctxt( column ), text_color( c_red_red ), _txt_nocolor_cached( "" ), + _ctxt_nocolor_cached( "" ) { - _txt_nocolor = remove_color_tags( txt ); - _ctxt_nocolor = remove_color_tags( ctxt ); + } uilist_entry::uilist_entry( const int retval, const bool enabled, @@ -330,20 +322,36 @@ uilist_entry::uilist_entry( const int retval, const bool enabled, const std::string &txt, const std::string &desc, const std::string &column ) : retval( retval ), enabled( enabled ), hotkey( key ), txt( txt ), - desc( desc ), ctxt( column ), text_color( c_red_red ) + desc( desc ), ctxt( column ), text_color( c_red_red ), _txt_nocolor_cached( "" ), + _ctxt_nocolor_cached( "" ) { - _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 nc_color &keycolor, const nc_color &txtcolor ) : retval( retval ), enabled( enabled ), hotkey( hotkey_from_char( key ) ), txt( txt ), - hotkey_color( keycolor ), text_color( txtcolor ) + hotkey_color( keycolor ), text_color( txtcolor ), _txt_nocolor_cached( "" ), + _ctxt_nocolor_cached( "" ) { - _txt_nocolor = remove_color_tags( txt ); - _ctxt_nocolor = remove_color_tags( ctxt ); + +} + +const std::string uilist_entry::_txt_nocolor() +{ + if( _txt_nocolor_cached.empty() && !txt.empty() ) { + _txt_nocolor_cached = remove_color_tags( txt ); + } + return _txt_nocolor_cached; +} + +const std::string uilist_entry::_ctxt_nocolor() +{ + if( _ctxt_nocolor_cached.empty() && !ctxt.empty() ) { + _ctxt_nocolor_cached = remove_color_tags( ctxt ); + } + return _ctxt_nocolor_cached; } uilist::size_scalar &uilist::size_scalar::operator=( auto_assign ) diff --git a/src/ui.h b/src/ui.h index f2343543d254e..c712f8faf5c02 100644 --- a/src/ui.h +++ b/src/ui.h @@ -61,118 +61,123 @@ struct mvwzstr { * uilist_entry: entry line for uilist */ struct uilist_entry { - int retval; // return this int - bool enabled; // darken, and forbid scrolling if hilight_disabled is false - bool force_color = false; // Never darken this option - // std::nullopt: automatically assign an unassigned hotkey - // input_event(): disable hotkey - std::optional hotkey; - std::string txt; // what it says on the tin - std::string desc; // optional, possibly longer, description - std::string ctxt; // second column text - nc_color hotkey_color; - nc_color text_color; - mvwzstr extratxt; - - // In the following constructors, int key only support letters (a-z, A-Z) and - // digits (0-9), MENU_AUTOASSIGN, and 0 or ' ' (disable hotkey). Other - // values may not work under keycode mode. - - /** - * @param txt string that will be displayed on the entry first column - */ - explicit uilist_entry( const std::string &txt ); - /** - * @param txt string that will be displayed on the entry first column - * @param desc entry description if menu desc_enabled is true - * @see uilist::desc_enabled - */ - uilist_entry( const std::string &txt, const std::string &desc ); - /** - * @param txt string that will be displayed on the entry first column - * @param key hotkey character that when pressed will return this entry return value - */ - uilist_entry( const std::string &txt, int key ); - /** - * @param txt string that will be displayed on the entry first column - * @param key hotkey character that when pressed will return this entry return value - */ - uilist_entry( const std::string &txt, const std::optional &key ); - /** - * @param retval return value of this option when selected during menu query - * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable - * @param key hotkey character that when pressed will return this entry return value - * @param txt string that will be displayed on the entry first column - */ - uilist_entry( int retval, bool enabled, int key, const std::string &txt ); - /** - * @param retval return value of this option when selected during menu query - * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable - * @param key hotkey character that when pressed will return this entry return value - * @param txt string that will be displayed on the entry first column - */ - uilist_entry( int retval, bool enabled, const std::optional &key, - const std::string &txt ); - /** - * @param retval return value of this option when selected during menu query - * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable - * @param key hotkey character that when pressed will return this entry return value - * @param txt string that will be displayed on the entry first column - * @param desc entry description if menu desc_enabled is true - * @see uilist::desc_enabled - */ - uilist_entry( int retval, bool enabled, int key, const std::string &txt, const std::string &desc ); - /** - * @param retval return value of this option when selected during menu query - * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable - * @param key hotkey character that when pressed will return this entry return value - * @param txt string that will be displayed on the entry first column - * @param desc entry description if menu desc_enabled is true - * @see uilist::desc_enabled - */ - uilist_entry( int retval, bool enabled, const std::optional &key, - const std::string &txt, const std::string &desc ); - /** - * @param retval return value of this option when selected during menu query - * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable - * @param key hotkey character that when pressed will return this entry return value - * @param txt string that will be displayed on the entry first column - * @param desc entry description if menu desc_enabled is true - * @param column string that will be displayed on the entry second column - * @see uilist::desc_enabled - */ - uilist_entry( int retval, bool enabled, int key, const std::string &txt, const std::string &desc, - const std::string &column ); - /** - * @param retval return value of this option when selected during menu query - * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable - * @param key hotkey character that when pressed will return this entry return value - * @param txt string that will be displayed on the entry first column - * @param desc entry description if menu desc_enabled is true - * @param column string that will be displayed on the entry second column - * @see uilist::desc_enabled - */ - uilist_entry( int retval, bool enabled, const std::optional &key, - const std::string &txt, const std::string &desc, - const std::string &column ); - /** - * @param retval return value of this option when selected during menu query - * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable - * @param key hotkey character that when pressed will return this entry return value - * @param txt string that will be displayed on the entry first column - * @param keycolor color of the hotkey character - * @param txtcolor entry text string color - */ - uilist_entry( int retval, bool enabled, int key, const std::string &txt, - const nc_color &keycolor, const nc_color &txtcolor ); - template>> - explicit uilist_entry( Enum e, Args && ... args ) : - uilist_entry( static_cast( e ), std::forward( args )... ) - {} - - std::string _txt_nocolor; // what it says on the tin - std::string _ctxt_nocolor; // second column text + public: + int retval; // return this int + bool enabled; // darken, and forbid scrolling if hilight_disabled is false + bool force_color = false; // Never darken this option + // std::nullopt: automatically assign an unassigned hotkey + // input_event(): disable hotkey + std::optional hotkey; + std::string txt; // what it says on the tin + std::string desc; // optional, possibly longer, description + std::string ctxt; // second column text + nc_color hotkey_color; + nc_color text_color; + mvwzstr extratxt; + + // In the following constructors, int key only support letters (a-z, A-Z) and + // digits (0-9), MENU_AUTOASSIGN, and 0 or ' ' (disable hotkey). Other + // values may not work under keycode mode. + + /** + * @param txt string that will be displayed on the entry first column + */ + explicit uilist_entry( const std::string &txt ); + /** + * @param txt string that will be displayed on the entry first column + * @param desc entry description if menu desc_enabled is true + * @see uilist::desc_enabled + */ + uilist_entry( const std::string &txt, const std::string &desc ); + /** + * @param txt string that will be displayed on the entry first column + * @param key hotkey character that when pressed will return this entry return value + */ + uilist_entry( const std::string &txt, int key ); + /** + * @param txt string that will be displayed on the entry first column + * @param key hotkey character that when pressed will return this entry return value + */ + uilist_entry( const std::string &txt, const std::optional &key ); + /** + * @param retval return value of this option when selected during menu query + * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable + * @param key hotkey character that when pressed will return this entry return value + * @param txt string that will be displayed on the entry first column + */ + uilist_entry( int retval, bool enabled, int key, const std::string &txt ); + /** + * @param retval return value of this option when selected during menu query + * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable + * @param key hotkey character that when pressed will return this entry return value + * @param txt string that will be displayed on the entry first column + */ + uilist_entry( int retval, bool enabled, const std::optional &key, + const std::string &txt ); + /** + * @param retval return value of this option when selected during menu query + * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable + * @param key hotkey character that when pressed will return this entry return value + * @param txt string that will be displayed on the entry first column + * @param desc entry description if menu desc_enabled is true + * @see uilist::desc_enabled + */ + uilist_entry( int retval, bool enabled, int key, const std::string &txt, const std::string &desc ); + /** + * @param retval return value of this option when selected during menu query + * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable + * @param key hotkey character that when pressed will return this entry return value + * @param txt string that will be displayed on the entry first column + * @param desc entry description if menu desc_enabled is true + * @see uilist::desc_enabled + */ + uilist_entry( int retval, bool enabled, const std::optional &key, + const std::string &txt, const std::string &desc ); + /** + * @param retval return value of this option when selected during menu query + * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable + * @param key hotkey character that when pressed will return this entry return value + * @param txt string that will be displayed on the entry first column + * @param desc entry description if menu desc_enabled is true + * @param column string that will be displayed on the entry second column + * @see uilist::desc_enabled + */ + uilist_entry( int retval, bool enabled, int key, const std::string &txt, const std::string &desc, + const std::string &column ); + /** + * @param retval return value of this option when selected during menu query + * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable + * @param key hotkey character that when pressed will return this entry return value + * @param txt string that will be displayed on the entry first column + * @param desc entry description if menu desc_enabled is true + * @param column string that will be displayed on the entry second column + * @see uilist::desc_enabled + */ + uilist_entry( int retval, bool enabled, const std::optional &key, + const std::string &txt, const std::string &desc, + const std::string &column ); + /** + * @param retval return value of this option when selected during menu query + * @param enable is entry enabled. disabled entries will be grayed out and won't be selectable + * @param key hotkey character that when pressed will return this entry return value + * @param txt string that will be displayed on the entry first column + * @param keycolor color of the hotkey character + * @param txtcolor entry text string color + */ + uilist_entry( int retval, bool enabled, int key, const std::string &txt, + const nc_color &keycolor, const nc_color &txtcolor ); + template>> + explicit uilist_entry( Enum e, Args && ... args ) : + uilist_entry( static_cast( e ), std::forward( args )... ) + {} + + public: + const std::string _txt_nocolor(); // what it says on the tin + const std::string _ctxt_nocolor(); // second column text + private: + std::string _txt_nocolor_cached; // cached return values of the above + std::string _ctxt_nocolor_cached; }; /** From a2f76a04d44e832fb8bd4bcececb715f2e06f39e Mon Sep 17 00:00:00 2001 From: moxian Date: Sat, 7 Dec 2024 16:29:48 -0800 Subject: [PATCH 2/2] remove redundant public --- src/ui.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ui.h b/src/ui.h index c712f8faf5c02..03d4ce6736744 100644 --- a/src/ui.h +++ b/src/ui.h @@ -61,7 +61,6 @@ struct mvwzstr { * uilist_entry: entry line for uilist */ struct uilist_entry { - public: int retval; // return this int bool enabled; // darken, and forbid scrolling if hilight_disabled is false bool force_color = false; // Never darken this option