From b58f614f40eefc737e2419cba3d9b887fa9c88eb Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 4 Apr 2013 13:53:47 +0200 Subject: [PATCH] Pass a fg and bg color when displaying a menu --- README.asciidoc | 3 ++- src/color_registry.cc | 2 ++ src/commands.cc | 2 +- src/input_handler.cc | 30 ++++++++++++++++++++++-------- src/ncurses.cc | 12 ++++++++---- src/ncurses.hh | 5 ++++- src/remote.cc | 39 ++++++++++++++++++++++++++++----------- src/user_interface.hh | 4 +++- 8 files changed, 70 insertions(+), 27 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index b4df725c..b7adcea3 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -405,7 +405,8 @@ there are some builtins color aliases: * +PrimaryCursor+: last character of the primary selection * +SecondaryCursor+: last character of the secondary selection * +LineNumbers+: colors used by the number_lines highlighter - + * +MenuForeground+: colors for the selected element in menus + * +MenuBackground+: colors for the not selected elements in menus Shell expansion --------------- diff --git a/src/color_registry.cc b/src/color_registry.cc index bee5f9eb..7d532b18 100644 --- a/src/color_registry.cc +++ b/src/color_registry.cc @@ -46,6 +46,8 @@ ColorRegistry::ColorRegistry() { "PrimaryCursor", { Color::Black, Color::White } }, { "SecondaryCursor", { Color::Black, Color::White } }, { "LineNumbers", { Color::Black, Color::White } }, + { "MenuForeground", { Color::Blue, Color::Cyan } }, + { "MenuBackground", { Color::Cyan, Color::Blue } }, } {} diff --git a/src/commands.cc b/src/commands.cc index b4e4f46c..d7133baf 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -806,7 +806,7 @@ public: void print_status(const String& , CharCount) override {} void draw(const DisplayBuffer&, const String&) override {} void menu_show(const memoryview&, - const DisplayCoord&, MenuStyle) override {} + DisplayCoord, ColorPair, ColorPair, MenuStyle) override {} void menu_select(int) override {} void menu_hide() override {} diff --git a/src/input_handler.cc b/src/input_handler.cc index a6b77fdf..ef2afd2a 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -5,6 +5,7 @@ #include "register_manager.hh" #include "event_manager.hh" #include "utf8.hh" +#include "color_registry.hh" #include @@ -153,7 +154,9 @@ public: m_selected(m_choices.begin()) { DisplayCoord menu_pos{ context().ui().dimensions().line, 0_char }; - context().ui().menu_show(choices, menu_pos, MenuStyle::Prompt); + ColorRegistry& colreg = ColorRegistry::instance(); + context().ui().menu_show(choices, menu_pos, colreg["MenuForeground"], + colreg["MenuBackground"], MenuStyle::Prompt); } void on_key(const Key& key) override @@ -375,7 +378,9 @@ public: context().ui().menu_hide(); DisplayCoord menu_pos{ context().ui().dimensions().line, 0_char }; - context().ui().menu_show(candidates, menu_pos, MenuStyle::Prompt); + ColorRegistry& colreg = ColorRegistry::instance(); + context().ui().menu_show(candidates, menu_pos, colreg["MenuForeground"], + colreg["MenuBackground"], MenuStyle::Prompt); bool use_common_prefix = context().options()["complete_prefix"].get(); String prefix = use_common_prefix ? common_prefix(candidates) : String(); @@ -573,10 +578,8 @@ public: { m_context.ui().menu_hide(); m_current_candidate = m_matching_candidates.size(); - DisplayCoord menu_pos = m_context.window().display_position(m_completions.begin); - m_context.ui().menu_show(m_matching_candidates, menu_pos, MenuStyle::Inline); - m_context.ui().menu_select(m_current_candidate); m_completions.end = cursor; + menu_show(); m_matching_candidates.push_back(prefix); return; } @@ -601,6 +604,18 @@ private: } } + void menu_show() + { + DisplayCoord menu_pos = m_context.window().display_position(m_completions.begin); + + ColorRegistry& colreg = ColorRegistry::instance(); + m_context.ui().menu_show(m_matching_candidates, menu_pos, + colreg["MenuForeground"], + colreg["MenuBackground"], + MenuStyle::Inline); + m_context.ui().menu_select(m_current_candidate); + } + bool setup_ifn() { if (not m_completions.is_valid()) @@ -615,10 +630,9 @@ private: assert(cursor >= m_completions.begin); m_matching_candidates = m_completions.candidates; - DisplayCoord menu_pos = m_context.window().display_position(m_completions.begin); - m_context.ui().menu_show(m_matching_candidates, menu_pos, MenuStyle::Inline); + m_current_candidate = m_matching_candidates.size(); + menu_show(); m_matching_candidates.push_back(m_context.buffer().string(m_completions.begin, m_completions.end)); - m_current_candidate = m_matching_candidates.size() - 1; } return true; } diff --git a/src/ncurses.cc b/src/ncurses.cc index 330d9508..d14fcccb 100644 --- a/src/ncurses.cc +++ b/src/ncurses.cc @@ -322,11 +322,11 @@ void NCursesUI::draw_menu() { assert(m_menu_win); - auto menu_fg = get_color_pair({ Color::Blue, Color::Cyan }); - auto menu_bg = get_color_pair({ Color::Cyan, Color::Blue }); + auto menu_fg = get_color_pair(m_menu_fg); + auto menu_bg = get_color_pair(m_menu_bg); auto scroll_fg = get_color_pair({ Color::White, Color::White }); - auto scroll_bg = get_color_pair({ Color::White, Color::Blue }); + auto scroll_bg = get_color_pair(m_menu_bg); wattron(m_menu_win, COLOR_PAIR(menu_bg)); wbkgdset(m_menu_win, COLOR_PAIR(menu_bg)); @@ -362,11 +362,15 @@ void NCursesUI::draw_menu() } void NCursesUI::menu_show(const memoryview& choices, - const DisplayCoord& anchor, MenuStyle style) + DisplayCoord anchor, ColorPair fg, ColorPair bg, + MenuStyle style) { assert(m_menu_win == nullptr); assert(m_choices.empty()); + m_menu_fg = fg; + m_menu_bg = bg; + DisplayCoord maxsize = window_size(stdscr); maxsize.column -= anchor.column; diff --git a/src/ncurses.hh b/src/ncurses.hh index d07f095d..6c37ad93 100644 --- a/src/ncurses.hh +++ b/src/ncurses.hh @@ -27,7 +27,8 @@ public: Key get_key() override; void menu_show(const memoryview& choices, - const DisplayCoord& anchor, MenuStyle style) override; + DisplayCoord anchor, ColorPair fg, ColorPair bg, + MenuStyle style) override; void menu_select(int selected) override; void menu_hide() override; @@ -51,6 +52,8 @@ private: WINDOW* m_menu_win = nullptr; std::vector m_choices; + ColorPair m_menu_fg; + ColorPair m_menu_bg; int m_selected_choice = 0; int m_menu_columns = 1; LineCount m_menu_top_line = 0; diff --git a/src/remote.cc b/src/remote.cc index 2e19cc45..18cb48e3 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -75,12 +75,17 @@ public: write(memoryview(vec)); } + void write(const ColorPair& colors) + { + write(colors.first); + write(colors.second); + } + void write(const DisplayAtom& atom) { - write(atom.colors.first); - write(atom.colors.second); - write(atom.attribute); write(atom.content.content()); + write(atom.colors); + write(atom.attribute); } void write(const DisplayLine& line) @@ -144,15 +149,21 @@ std::vector read_vector(int socket) return res; } +template<> +ColorPair read(int socket) +{ + ColorPair res; + res.first = read(socket); + res.second = read(socket); + return res; +} + template<> DisplayAtom read(int socket) { - Color fg_color = read(socket); - Color bg_color = read(socket); - Attribute attribute = read(socket); DisplayAtom atom(AtomContent(read(socket))); - atom.colors = { fg_color, bg_color }; - atom.attribute = attribute; + atom.colors = read(socket); + atom.attribute = read(socket); return atom; } template<> @@ -178,7 +189,8 @@ public: void print_status(const String& status, CharCount cursor_pos) override; void menu_show(const memoryview& choices, - const DisplayCoord& anchor, MenuStyle style) override; + DisplayCoord anchor, ColorPair fg, ColorPair bg, + MenuStyle style) override; void menu_select(int selected) override; void menu_hide() override; @@ -223,12 +235,15 @@ void RemoteUI::print_status(const String& status, CharCount cursor_pos) } void RemoteUI::menu_show(const memoryview& choices, - const DisplayCoord& anchor, MenuStyle style) + DisplayCoord anchor, ColorPair fg, ColorPair bg, + MenuStyle style) { Message msg(m_socket_watcher.fd()); msg.write(RemoteUIMsg::MenuShow); msg.write(choices); msg.write(anchor); + msg.write(fg); + msg.write(bg); msg.write(style); } @@ -338,8 +353,10 @@ void RemoteClient::process_next_message() { auto choices = read_vector(socket); auto anchor = read(socket); + auto fg = read(socket); + auto bg = read(socket); auto style = read(socket); - m_ui->menu_show(choices, anchor, style); + m_ui->menu_show(choices, anchor, fg, bg, style); break; } case RemoteUIMsg::MenuSelect: diff --git a/src/user_interface.hh b/src/user_interface.hh index 9353fe7e..dd6b0448 100644 --- a/src/user_interface.hh +++ b/src/user_interface.hh @@ -5,6 +5,7 @@ #include "keys.hh" #include "units.hh" #include "utils.hh" +#include "color.hh" namespace Kakoune { @@ -28,7 +29,8 @@ public: virtual void print_status(const String& status, CharCount cursor_pos = -1) = 0; virtual void menu_show(const memoryview& choices, - const DisplayCoord& anchor, MenuStyle style) = 0; + DisplayCoord anchor, ColorPair fg, ColorPair bg, + MenuStyle style) = 0; virtual void menu_select(int selected) = 0; virtual void menu_hide() = 0;