validate key in InputHandler before sending to mode, and minor refactor

This commit is contained in:
Maxime Coste 2012-10-29 18:59:41 +01:00
parent 6b664052b8
commit cc876f7107

View File

@ -62,6 +62,14 @@ private:
int m_count = 0; int m_count = 0;
}; };
String codepoint_to_str(Codepoint cp)
{
std::string str;
auto it = back_inserter(str);
utf8::dump(it, cp);
return String(str);
}
class LineEditor class LineEditor
{ {
public: public:
@ -91,10 +99,7 @@ public:
} }
else else
{ {
std::string keystr; m_line = m_line.substr(0, m_cursor_pos) + codepoint_to_str(key.key)
auto inserter = back_inserter(keystr);
utf8::dump(inserter, key.key);
m_line = m_line.substr(0, m_cursor_pos) + keystr
+ m_line.substr(m_cursor_pos); + m_line.substr(m_cursor_pos);
++m_cursor_pos; ++m_cursor_pos;
} }
@ -467,14 +472,6 @@ private:
int m_current_completion = -1; int m_current_completion = -1;
}; };
String codepoint_to_str(Codepoint cp)
{
std::string str;
auto it = back_inserter(str);
utf8::dump(it, cp);
return String(str);
}
class Insert : public InputMode class Insert : public InputMode
{ {
public: public:
@ -625,12 +622,17 @@ void InputHandler::on_next_key(KeyCallback callback)
m_mode.reset(new InputModes::NextKey(*this, callback)); m_mode.reset(new InputModes::NextKey(*this, callback));
} }
bool is_valid(const Key& key)
{
return key != Key::Invalid and key.key <= 0x10FFFF;
}
void InputHandler::handle_available_inputs(Context& context) void InputHandler::handle_available_inputs(Context& context)
{ {
while (context.ui().is_key_available()) while (context.ui().is_key_available())
{ {
Key key = context.ui().get_key(); Key key = context.ui().get_key();
if (key != Key::Invalid) if (is_valid(key))
m_mode->on_key(key, context); m_mode->on_key(key, context);
} }
context.draw_ifn(); context.draw_ifn();