Move Client responsibilities to InputHandler

InputHandler owns it's UserInterface, and is directly stored by the ClientManager.
This commit is contained in:
Maxime Coste 2013-09-12 23:39:34 +02:00
parent 916a0cb52e
commit ac7e437730
8 changed files with 60 additions and 63 deletions

View File

@ -11,16 +11,6 @@
namespace Kakoune namespace Kakoune
{ {
Client::Client(std::unique_ptr<UserInterface>&& ui,
Window& window, String name)
: m_user_interface(std::move(ui)),
m_input_handler(*m_user_interface),
m_name(std::move(name))
{
kak_assert(not m_name.empty());
context().change_editor(window);
}
ClientManager::ClientManager() = default; ClientManager::ClientManager() = default;
ClientManager::~ClientManager() = default; ClientManager::~ClientManager() = default;
@ -43,12 +33,12 @@ String ClientManager::generate_name() const
} }
} }
Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& ui, InputHandler* ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
const String& init_commands) const String& init_commands)
{ {
Buffer& buffer = **BufferManager::instance().begin(); Buffer& buffer = **BufferManager::instance().begin();
Client* client = new Client{std::move(ui), get_unused_window_for_buffer(buffer), InputHandler* client = new InputHandler{std::move(ui), get_unused_window_for_buffer(buffer),
generate_name()}; generate_name()};
m_clients.emplace_back(client); m_clients.emplace_back(client);
try try
{ {
@ -66,11 +56,11 @@ Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
return nullptr; return nullptr;
} }
client->m_user_interface->set_input_callback([client, this]() { client->ui().set_input_callback([client, this]() {
try try
{ {
while (client->m_user_interface->is_key_available()) while (client->ui().is_key_available())
client->m_input_handler.handle_key(client->m_user_interface->get_key()); client->handle_key(client->ui().get_key());
client->context().window().forget_timestamp(); client->context().window().forget_timestamp();
} }
catch (Kakoune::runtime_error& error) catch (Kakoune::runtime_error& error)
@ -90,7 +80,7 @@ Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
return client; return client;
} }
void ClientManager::remove_client(Client& client) void ClientManager::remove_client(InputHandler& client)
{ {
for (auto it = m_clients.begin(); it != m_clients.end(); ++it) for (auto it = m_clients.begin(); it != m_clients.end(); ++it)
{ {
@ -111,7 +101,7 @@ Window& ClientManager::get_unused_window_for_buffer(Buffer& buffer)
continue; continue;
auto it = std::find_if(m_clients.begin(), m_clients.end(), auto it = std::find_if(m_clients.begin(), m_clients.end(),
[&](const std::unique_ptr<Client>& client) [&](const std::unique_ptr<InputHandler>& client)
{ return &client->context().window() == w.get(); }); { return &client->context().window() == w.get(); });
if (it == m_clients.end()) if (it == m_clients.end())
{ {
@ -155,16 +145,16 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer)
m_windows.erase(end, m_windows.end()); m_windows.erase(end, m_windows.end());
} }
void ClientManager::set_client_name(Client& client, String name) void ClientManager::set_client_name(InputHandler& client, String name)
{ {
auto it = find_if(m_clients, [&name](std::unique_ptr<Client>& client) auto it = find_if(m_clients, [&name](std::unique_ptr<InputHandler>& client)
{ return client->m_name == name; }); { return client->m_name == name; });
if (it != m_clients.end() and it->get() != &client) if (it != m_clients.end() and it->get() != &client)
throw runtime_error("name not unique: " + name); throw runtime_error("name not unique: " + name);
client.m_name = std::move(name); client.m_name = std::move(name);
} }
Client& ClientManager::get_client(const Context& context) InputHandler& ClientManager::get_client(const Context& context)
{ {
for (auto& client : m_clients) for (auto& client : m_clients)
{ {
@ -174,7 +164,7 @@ Client& ClientManager::get_client(const Context& context)
throw runtime_error("no client for current context"); throw runtime_error("no client for current context");
} }
Client& ClientManager::get_client(const String& name) InputHandler& ClientManager::get_client(const String& name)
{ {
for (auto& client : m_clients) for (auto& client : m_clients)
{ {
@ -184,7 +174,7 @@ Client& ClientManager::get_client(const String& name)
throw runtime_error("no client named: " + name); throw runtime_error("no client named: " + name);
} }
static DisplayLine generate_status_line(Client& client) static DisplayLine generate_status_line(InputHandler& client)
{ {
auto& context = client.context(); auto& context = client.context();
auto pos = context.editor().main_selection().last(); auto pos = context.editor().main_selection().last();

View File

@ -9,34 +9,14 @@ namespace Kakoune
struct client_removed{}; struct client_removed{};
class Client
{
public:
Context& context() { return m_input_handler.context(); }
const String& name() const { return m_name; }
private:
friend class ClientManager;
Client(std::unique_ptr<UserInterface>&& ui,
Window& window, String name);
Client(Client&&) = delete;
Client& operator=(Client&& other) = delete;
const std::unique_ptr<UserInterface> m_user_interface;
InputHandler m_input_handler;
String m_name;
};
class ClientManager : public Singleton<ClientManager> class ClientManager : public Singleton<ClientManager>
{ {
public: public:
ClientManager(); ClientManager();
~ClientManager(); ~ClientManager();
Client* create_client(std::unique_ptr<UserInterface>&& ui, InputHandler* create_client(std::unique_ptr<UserInterface>&& ui,
const String& init_cmd); const String& init_cmd);
bool empty() const { return m_clients.empty(); } bool empty() const { return m_clients.empty(); }
size_t count() const { return m_clients.size(); } size_t count() const { return m_clients.size(); }
@ -46,15 +26,15 @@ public:
void redraw_clients() const; void redraw_clients() const;
Client& get_client(const Context& context); InputHandler& get_client(const Context& context);
Client& get_client(const String& name); InputHandler& get_client(const String& name);
void set_client_name(Client& client, String name); void set_client_name(InputHandler& client, String name);
void remove_client(Client& client); void remove_client(InputHandler& client);
private: private:
String generate_name() const; String generate_name() const;
std::vector<std::unique_ptr<Client>> m_clients; std::vector<std::unique_ptr<InputHandler>> m_clients;
std::vector<std::unique_ptr<Window>> m_windows; std::vector<std::unique_ptr<Window>> m_windows;
}; };

View File

@ -537,6 +537,26 @@ void declare_option(CommandParameters params, Context& context)
opt->set_from_string(params[2]); opt->set_from_string(params[2]);
} }
class DraftUI : public UserInterface
{
public:
void print_status(const DisplayLine&) override {}
void menu_show(memoryview<String>, DisplayCoord, ColorPair, ColorPair, MenuStyle) override {}
void menu_select(int) override {}
void menu_hide() override {}
void info_show(const String&, DisplayCoord, ColorPair, MenuStyle) override {}
void info_hide() override {}
void draw(const DisplayBuffer&, const DisplayLine&) override {}
DisplayCoord dimensions() override { return {0,0}; }
bool is_key_available() override { return false; }
Key get_key() override { return 'a'; }
void set_input_callback(InputCallback) override {}
};
template<typename Func> template<typename Func>
void context_wrap(CommandParameters params, Context& context, Func func) void context_wrap(CommandParameters params, Context& context, Func func)
{ {
@ -549,9 +569,10 @@ void context_wrap(CommandParameters params, Context& context, Func func)
if (parser.has_option("draft")) if (parser.has_option("draft"))
{ {
InputHandler input_handler(real_context.ui());
Editor& editor = real_context.editor(); Editor& editor = real_context.editor();
input_handler.context().change_editor(editor); InputHandler input_handler(std::unique_ptr<UserInterface>(new DraftUI()), editor,
real_context.has_input_handler() ?
real_context.input_handler().name() : "");
DynamicSelectionList sels{editor.buffer(), editor.selections()}; DynamicSelectionList sels{editor.buffer(), editor.selections()};
auto restore_sels = on_scope_end([&]{ editor.select(sels); }); auto restore_sels = on_scope_end([&]{ editor.select(sels); });
func(parser, input_handler.context()); func(parser, input_handler.context());

View File

@ -12,8 +12,8 @@ Context::Context() = default;
Context::Context(Editor& editor) Context::Context(Editor& editor)
: m_editor(&editor) {} : m_editor(&editor) {}
Context::Context(InputHandler& input_handler, UserInterface& ui) Context::Context(InputHandler& input_handler, Editor& editor)
: m_input_handler(&input_handler), m_ui(&ui) {} : m_input_handler(&input_handler), m_editor(&editor) {}
Context::~Context() = default; Context::~Context() = default;
@ -54,7 +54,7 @@ UserInterface& Context::ui() const
{ {
if (not has_ui()) if (not has_ui())
throw runtime_error("no user interface in context"); throw runtime_error("no user interface in context");
return *m_ui; return m_input_handler->ui();
} }
OptionManager& Context::options() const OptionManager& Context::options() const

View File

@ -23,7 +23,7 @@ struct Context
{ {
Context(); Context();
explicit Context(Editor& editor); explicit Context(Editor& editor);
Context(InputHandler& input_handler, UserInterface& ui); Context(InputHandler& input_handler, Editor& editor);
~Context(); ~Context();
Context(const Context&) = delete; Context(const Context&) = delete;
@ -42,7 +42,7 @@ struct Context
bool has_input_handler() const { return (bool)m_input_handler; } bool has_input_handler() const { return (bool)m_input_handler; }
UserInterface& ui() const; UserInterface& ui() const;
bool has_ui() const { return (bool)m_ui; } bool has_ui() const { return (bool)m_input_handler; }
void change_editor(Editor& editor); void change_editor(Editor& editor);
@ -60,7 +60,6 @@ struct Context
private: private:
safe_ptr<Editor> m_editor; safe_ptr<Editor> m_editor;
safe_ptr<InputHandler> m_input_handler; safe_ptr<InputHandler> m_input_handler;
safe_ptr<UserInterface> m_ui;
int m_numeric_param = 0; int m_numeric_param = 0;

View File

@ -914,8 +914,8 @@ InputMode& InputMode::reset_normal_mode()
} }
InputHandler::InputHandler(UserInterface& ui) InputHandler::InputHandler(std::unique_ptr<UserInterface>&& ui, Editor& editor, String name)
: m_context(*this, ui), m_mode(new InputModes::Normal(*this)) : m_ui(std::move(ui)), m_context(*this, editor), m_mode(new InputModes::Normal(*this)), m_name(name)
{ {
} }

View File

@ -37,7 +37,7 @@ enum class InsertMode : unsigned;
class InputHandler : public SafeCountable class InputHandler : public SafeCountable
{ {
public: public:
InputHandler(UserInterface& ui); InputHandler(std::unique_ptr<UserInterface>&& ui, Editor& editor, String name);
~InputHandler(); ~InputHandler();
// switch to insert mode // switch to insert mode
@ -72,12 +72,19 @@ public:
void stop_recording(); void stop_recording();
Context& context() { return m_context; } Context& context() { return m_context; }
const String& name() const { return m_name; }
UserInterface& ui() const { return *m_ui; }
private: private:
Context m_context; Context m_context;
friend class InputMode; friend class InputMode;
friend class ClientManager;
std::unique_ptr<UserInterface> m_ui;
std::unique_ptr<InputMode> m_mode; std::unique_ptr<InputMode> m_mode;
std::vector<std::unique_ptr<InputMode>> m_mode_trash; std::vector<std::unique_ptr<InputMode>> m_mode_trash;
String m_name;
using Insertion = std::pair<InsertMode, std::vector<Key>>; using Insertion = std::pair<InsertMode, std::vector<Key>>;
Insertion m_last_insert = {InsertMode::Insert, {}}; Insertion m_last_insert = {InsertMode::Insert, {}};

View File

@ -150,7 +150,7 @@ void create_local_client(const String& init_command)
}; };
UserInterface* ui = new LocalNCursesUI{}; UserInterface* ui = new LocalNCursesUI{};
static Client* client = ClientManager::instance().create_client( static InputHandler* client = ClientManager::instance().create_client(
std::unique_ptr<UserInterface>{ui}, init_command); std::unique_ptr<UserInterface>{ui}, init_command);
signal(SIGHUP, [](int) { signal(SIGHUP, [](int) {
if (client) if (client)