From b23425764edc155e16023b58afa3c42efda1d5cb Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 5 Sep 2012 00:21:19 +0200 Subject: [PATCH] Add client::on_next_key method to run some code when the next key arrives --- src/client.cc | 23 +++++++++++++++++++++++ src/client.hh | 8 +++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/client.cc b/src/client.cc index 362e155c..0aab50fc 100644 --- a/src/client.cc +++ b/src/client.cc @@ -291,6 +291,24 @@ private: }; std::unordered_map> 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() : m_mode(new NormalMode(*this)) { @@ -308,6 +326,11 @@ void Client::menu(const memoryview& choices, 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) { m_mode->on_key(get_key(), context); diff --git a/src/client.hh b/src/client.hh index da28fc84..c31dd0a6 100644 --- a/src/client.hh +++ b/src/client.hh @@ -25,6 +25,7 @@ enum class MenuCommand using MenuCallback = std::function; using PromptCallback = std::function; +using KeyCallback = std::function; class Client : public SafeCountable { @@ -41,11 +42,12 @@ public: void menu(const memoryview& choices, MenuCallback callback); + void on_next_key(KeyCallback callback); + void handle_next_input(Context& context); virtual Key get_key() = 0; private: - virtual void show_menu(const memoryview& choices) = 0; virtual void menu_ctrl(MenuCommand command) = 0; @@ -56,6 +58,9 @@ private: public: Mode(Client& client) : m_client(client) {} virtual ~Mode() {} + Mode(const Mode&) = delete; + Mode& operator=(const Mode&) = delete; + virtual void on_key(const Key& key, Context& context) = 0; protected: Client& m_client; @@ -65,6 +70,7 @@ private: class NormalMode; class MenuMode; class PromptMode; + class NextKeyMode; }; struct prompt_aborted {};