Add client::on_next_key method to run some code when the next key arrives
This commit is contained in:
parent
aac30a27e7
commit
b23425764e
|
@ -291,6 +291,24 @@ private:
|
||||||
};
|
};
|
||||||
std::unordered_map<String, std::vector<String>> Client::PromptMode::ms_history;
|
std::unordered_map<String, std::vector<String>> Client::PromptMode::ms_history;
|
||||||
|
|
||||||
|
class Client::NextKeyMode : public Client::Mode
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
NextKeyMode(Client& client, KeyCallback callback)
|
||||||
|
: Client::Mode(client), m_callback(callback) {}
|
||||||
|
|
||||||
|
void on_key(const Key& key, Context& context) override
|
||||||
|
{
|
||||||
|
// save callback as reset_normal_mode will delete this
|
||||||
|
KeyCallback callback = std::move(m_callback);
|
||||||
|
m_client.reset_normal_mode();
|
||||||
|
callback(key, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
KeyCallback m_callback;
|
||||||
|
};
|
||||||
|
|
||||||
Client::Client()
|
Client::Client()
|
||||||
: m_mode(new NormalMode(*this))
|
: m_mode(new NormalMode(*this))
|
||||||
{
|
{
|
||||||
|
@ -308,6 +326,11 @@ void Client::menu(const memoryview<String>& choices,
|
||||||
m_mode.reset(new MenuMode(*this, choices, callback));
|
m_mode.reset(new MenuMode(*this, choices, callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Client::on_next_key(KeyCallback callback)
|
||||||
|
{
|
||||||
|
m_mode.reset(new NextKeyMode(*this, callback));
|
||||||
|
}
|
||||||
|
|
||||||
void Client::handle_next_input(Context& context)
|
void Client::handle_next_input(Context& context)
|
||||||
{
|
{
|
||||||
m_mode->on_key(get_key(), context);
|
m_mode->on_key(get_key(), context);
|
||||||
|
|
|
@ -25,6 +25,7 @@ enum class MenuCommand
|
||||||
|
|
||||||
using MenuCallback = std::function<void (int, Context&)>;
|
using MenuCallback = std::function<void (int, Context&)>;
|
||||||
using PromptCallback = std::function<void (const String&, Context&)>;
|
using PromptCallback = std::function<void (const String&, Context&)>;
|
||||||
|
using KeyCallback = std::function<void (const Key&, Context&)>;
|
||||||
|
|
||||||
class Client : public SafeCountable
|
class Client : public SafeCountable
|
||||||
{
|
{
|
||||||
|
@ -41,11 +42,12 @@ public:
|
||||||
void menu(const memoryview<String>& choices,
|
void menu(const memoryview<String>& choices,
|
||||||
MenuCallback callback);
|
MenuCallback callback);
|
||||||
|
|
||||||
|
void on_next_key(KeyCallback callback);
|
||||||
|
|
||||||
void handle_next_input(Context& context);
|
void handle_next_input(Context& context);
|
||||||
virtual Key get_key() = 0;
|
virtual Key get_key() = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
virtual void show_menu(const memoryview<String>& choices) = 0;
|
virtual void show_menu(const memoryview<String>& choices) = 0;
|
||||||
virtual void menu_ctrl(MenuCommand command) = 0;
|
virtual void menu_ctrl(MenuCommand command) = 0;
|
||||||
|
|
||||||
|
@ -56,6 +58,9 @@ private:
|
||||||
public:
|
public:
|
||||||
Mode(Client& client) : m_client(client) {}
|
Mode(Client& client) : m_client(client) {}
|
||||||
virtual ~Mode() {}
|
virtual ~Mode() {}
|
||||||
|
Mode(const Mode&) = delete;
|
||||||
|
Mode& operator=(const Mode&) = delete;
|
||||||
|
|
||||||
virtual void on_key(const Key& key, Context& context) = 0;
|
virtual void on_key(const Key& key, Context& context) = 0;
|
||||||
protected:
|
protected:
|
||||||
Client& m_client;
|
Client& m_client;
|
||||||
|
@ -65,6 +70,7 @@ private:
|
||||||
class NormalMode;
|
class NormalMode;
|
||||||
class MenuMode;
|
class MenuMode;
|
||||||
class PromptMode;
|
class PromptMode;
|
||||||
|
class NextKeyMode;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct prompt_aborted {};
|
struct prompt_aborted {};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user