Pass a fg and bg color when displaying a menu

This commit is contained in:
Maxime Coste 2013-04-04 13:53:47 +02:00
parent 1fd99e7e88
commit b58f614f40
8 changed files with 70 additions and 27 deletions

View File

@ -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
---------------

View File

@ -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 } },
}
{}

View File

@ -806,7 +806,7 @@ public:
void print_status(const String& , CharCount) override {}
void draw(const DisplayBuffer&, const String&) override {}
void menu_show(const memoryview<String>&,
const DisplayCoord&, MenuStyle) override {}
DisplayCoord, ColorPair, ColorPair, MenuStyle) override {}
void menu_select(int) override {}
void menu_hide() override {}

View File

@ -5,6 +5,7 @@
#include "register_manager.hh"
#include "event_manager.hh"
#include "utf8.hh"
#include "color_registry.hh"
#include <unordered_map>
@ -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<bool>();
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;
}

View File

@ -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<String>& 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;

View File

@ -27,7 +27,8 @@ public:
Key get_key() override;
void menu_show(const memoryview<String>& 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<String> 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;

View File

@ -75,12 +75,17 @@ public:
write(memoryview<T>(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<T> read_vector(int socket)
return res;
}
template<>
ColorPair read<ColorPair>(int socket)
{
ColorPair res;
res.first = read<Color>(socket);
res.second = read<Color>(socket);
return res;
}
template<>
DisplayAtom read<DisplayAtom>(int socket)
{
Color fg_color = read<Color>(socket);
Color bg_color = read<Color>(socket);
Attribute attribute = read<Attribute>(socket);
DisplayAtom atom(AtomContent(read<String>(socket)));
atom.colors = { fg_color, bg_color };
atom.attribute = attribute;
atom.colors = read<ColorPair>(socket);
atom.attribute = read<Attribute>(socket);
return atom;
}
template<>
@ -178,7 +189,8 @@ public:
void print_status(const String& status, CharCount cursor_pos) override;
void menu_show(const memoryview<String>& 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<String>& 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<String>(socket);
auto anchor = read<DisplayCoord>(socket);
auto fg = read<ColorPair>(socket);
auto bg = read<ColorPair>(socket);
auto style = read<MenuStyle>(socket);
m_ui->menu_show(choices, anchor, style);
m_ui->menu_show(choices, anchor, fg, bg, style);
break;
}
case RemoteUIMsg::MenuSelect:

View File

@ -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<String>& 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;