home/src/input_handler.hh

84 lines
2.1 KiB
C++
Raw Normal View History

2012-10-17 13:14:03 +02:00
#ifndef input_handler_hh_INCLUDED
#define input_handler_hh_INCLUDED
#include "keys.hh"
#include "completion.hh"
2012-08-15 22:36:45 +02:00
#include "utils.hh"
#include "string.hh"
2013-01-28 13:48:34 +01:00
#include "context.hh"
namespace Kakoune
{
class Editor;
enum class MenuEvent
{
Select,
Abort,
Validate
};
using MenuCallback = std::function<void (int, MenuEvent, Context&)>;
enum class PromptEvent
{
Change,
Abort,
Validate
};
using PromptCallback = std::function<void (const String&, PromptEvent, Context&)>;
using KeyCallback = std::function<void (const Key&, Context&)>;
2012-10-17 13:14:03 +02:00
class InputMode;
enum class InsertMode : unsigned;
2012-09-24 19:39:40 +02:00
2012-10-17 13:14:03 +02:00
class InputHandler : public SafeCountable
{
public:
2013-01-28 13:48:34 +01:00
InputHandler(UserInterface& ui);
2012-10-17 13:14:03 +02:00
~InputHandler();
2013-01-17 14:27:32 +01:00
// switch to insert mode
void insert(InsertMode mode);
2013-01-17 14:27:32 +01:00
// repeat last insert mode key sequence
void repeat_last_insert();
2012-09-05 14:27:14 +02:00
2013-01-17 14:27:32 +01:00
// enter prompt mode, callback is called on each change,
// abort or validation with corresponding PromptEvent value
// returns to normal mode after validation if callback does
// not change the mode itself
void prompt(const String& prompt, Completer completer,
PromptCallback callback);
2013-01-17 14:27:32 +01:00
// enter menu mode, callback is called on each selection change,
// abort or validation with corresponding MenuEvent value
// returns to normal mode after validation if callback does
// not change the mode itself
void menu(const memoryview<String>& choices,
MenuCallback callback);
2013-01-17 14:27:32 +01:00
// execute callback on next keypress and returns to normal mode
// if callback does not change the mode itself
void on_next_key(KeyCallback callback);
2013-01-17 14:27:32 +01:00
// read and process all inputs available in context
// user interface
void handle_available_inputs();
2013-01-28 13:48:34 +01:00
Context& context() { return m_context; }
private:
2013-01-28 13:48:34 +01:00
Context m_context;
2012-10-17 13:14:03 +02:00
friend class InputMode;
std::unique_ptr<InputMode> m_mode;
std::vector<std::unique_ptr<InputMode>> m_mode_trash;
using Insertion = std::pair<InsertMode, std::vector<Key>>;
Insertion m_last_insert = {InsertMode::Insert, {}};
};
struct prompt_aborted {};
}
2012-10-17 13:14:03 +02:00
#endif // input_handler_hh_INCLUDED