diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2012-09-05 00:21:19 +0200 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2012-09-05 00:21:19 +0200 |
| commit | b23425764edc155e16023b58afa3c42efda1d5cb (patch) | |
| tree | e1059d44340f9ae6969e9421ac677e6c01828c65 /src | |
| parent | aac30a27e70054799beb0c137e28c9dc9656efec (diff) | |
Add client::on_next_key method to run some code when the next key arrives
Diffstat (limited to 'src')
| -rw-r--r-- | src/client.cc | 23 | ||||
| -rw-r--r-- | src/client.hh | 8 |
2 files changed, 30 insertions, 1 deletions
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<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() : m_mode(new NormalMode(*this)) { @@ -308,6 +326,11 @@ void Client::menu(const memoryview<String>& 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<void (int, Context&)>; using PromptCallback = std::function<void (const String&, Context&)>; +using KeyCallback = std::function<void (const Key&, Context&)>; class Client : public SafeCountable { @@ -41,11 +42,12 @@ public: void menu(const memoryview<String>& 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<String>& 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 {}; |
