InputHandler: call callback on abort and on selected element change as well

This commit is contained in:
Maxime Coste 2012-12-14 19:38:11 +01:00
parent 3184159572
commit d208067589
3 changed files with 23 additions and 10 deletions

View File

@ -596,8 +596,8 @@ void menu(const CommandParameters& params, Context& context)
} }
context.input_handler().menu(choices, context.input_handler().menu(choices,
[=](int choice, Context& context) { [=](int choice, MenuEvent event, Context& context) {
if (choice >= 0 and choice < commands.size()) if (event == MenuEvent::Validate and choice >= 0 and choice < commands.size())
CommandManager::instance().execute(commands[choice], context); CommandManager::instance().execute(commands[choice], context);
}, context); }, context);
} }

View File

@ -152,7 +152,7 @@ public:
context.ui().print_status(""); context.ui().print_status("");
reset_normal_mode(); reset_normal_mode();
int selected = m_selected - m_choices.begin(); int selected = m_selected - m_choices.begin();
m_callback(selected, context); m_callback(selected, MenuEvent::Validate, context);
return; return;
} }
else if (key == Key::Escape or key == Key{ Key::Modifiers::Control, 'c' }) else if (key == Key::Escape or key == Key{ Key::Modifiers::Control, 'c' })
@ -168,6 +168,8 @@ public:
{ {
context.ui().menu_hide(); context.ui().menu_hide();
reset_normal_mode(); reset_normal_mode();
int selected = m_selected - m_choices.begin();
m_callback(selected, MenuEvent::Abort, context);
} }
} }
else if (key == Key::Down or else if (key == Key::Down or
@ -178,8 +180,7 @@ public:
auto it = std::find_if(m_selected+1, m_choices.end(), match_filter); auto it = std::find_if(m_selected+1, m_choices.end(), match_filter);
if (it == m_choices.end()) if (it == m_choices.end())
it = std::find_if(m_choices.begin(), m_selected, match_filter); it = std::find_if(m_choices.begin(), m_selected, match_filter);
m_selected = it; select(it, context);
context.ui().menu_select(m_selected - m_choices.begin());
} }
else if (key == Key::Up or else if (key == Key::Up or
key == Key::BackTab or key == Key::BackTab or
@ -190,8 +191,7 @@ public:
auto it = std::find_if(selected+1, m_choices.rend(), match_filter); auto it = std::find_if(selected+1, m_choices.rend(), match_filter);
if (it == m_choices.rend()) if (it == m_choices.rend())
it = std::find_if(m_choices.rbegin(), selected, match_filter); it = std::find_if(m_choices.rbegin(), selected, match_filter);
m_selected = it.base()-1; select(it.base()-1, context);
context.ui().menu_select(m_selected - m_choices.begin());
} }
else if (key == '/' and not m_edit_filter) else if (key == '/' and not m_edit_filter)
{ {
@ -206,8 +206,7 @@ public:
auto it = std::find_if(m_selected, m_choices.end(), match_filter); auto it = std::find_if(m_selected, m_choices.end(), match_filter);
if (it == m_choices.end()) if (it == m_choices.end())
it = std::find_if(m_choices.begin(), m_selected, match_filter); it = std::find_if(m_choices.begin(), m_selected, match_filter);
m_selected = it; select(it, context);
context.ui().menu_select(m_selected - m_choices.begin());
} }
if (m_edit_filter) if (m_edit_filter)
@ -222,6 +221,14 @@ private:
const ChoiceList m_choices; const ChoiceList m_choices;
ChoiceList::const_iterator m_selected; ChoiceList::const_iterator m_selected;
void select(ChoiceList::const_iterator it, Context& context)
{
m_selected = it;
int selected = m_selected - m_choices.begin();
context.ui().menu_select(selected);
m_callback(selected, MenuEvent::Select, context);
}
boost::regex m_filter = boost::regex(".*"); boost::regex m_filter = boost::regex(".*");
bool m_edit_filter = false; bool m_edit_filter = false;
LineEditor m_filter_editor; LineEditor m_filter_editor;

View File

@ -12,7 +12,13 @@ namespace Kakoune
class Editor; class Editor;
class Context; class Context;
using MenuCallback = std::function<void (int, Context&)>; enum class MenuEvent
{
Select,
Abort,
Validate
};
using MenuCallback = std::function<void (int, MenuEvent, Context&)>;
enum class PromptEvent enum class PromptEvent
{ {