Keep a pointer to current client in windows so that window hooks can access it

This commit is contained in:
Maxime Coste 2016-05-09 13:48:48 +01:00
parent 3338ba5c6c
commit d3aff03062
3 changed files with 12 additions and 0 deletions

View File

@ -28,6 +28,8 @@ Client::Client(std::unique_ptr<UserInterface>&& ui,
std::move(name)}, std::move(name)},
m_env_vars(env_vars) m_env_vars(env_vars)
{ {
m_window->set_client(this);
context().set_client(*this); context().set_client(*this);
context().set_window(*m_window); context().set_window(*m_window);
@ -42,6 +44,7 @@ Client::Client(std::unique_ptr<UserInterface>&& ui,
Client::~Client() Client::~Client()
{ {
m_window->options().unregister_watcher(*this); m_window->options().unregister_watcher(*this);
m_window->set_client(nullptr);
} }
Optional<Key> Client::get_next_key(EventMode mode) Optional<Key> Client::get_next_key(EventMode mode)
@ -149,11 +152,13 @@ void Client::change_buffer(Buffer& buffer)
auto& client_manager = ClientManager::instance(); auto& client_manager = ClientManager::instance();
m_window->options().unregister_watcher(*this); m_window->options().unregister_watcher(*this);
m_window->set_client(nullptr);
client_manager.add_free_window(std::move(m_window), client_manager.add_free_window(std::move(m_window),
std::move(context().selections())); std::move(context().selections()));
WindowAndSelections ws = client_manager.get_free_window(buffer); WindowAndSelections ws = client_manager.get_free_window(buffer);
m_window = std::move(ws.window); m_window = std::move(ws.window);
m_window->set_client(this);
m_window->options().register_watcher(*this); m_window->options().register_watcher(*this);
m_ui->set_ui_options(m_window->options()["ui_options"].get<UserInterface::Options>()); m_ui->set_ui_options(m_window->options()["ui_options"].get<UserInterface::Options>());

View File

@ -377,6 +377,9 @@ void Window::run_hook_in_own_context(StringView hook_name, StringView param)
{ {
InputHandler hook_handler({ *m_buffer, Selection{} }, Context::Flags::Transient); InputHandler hook_handler({ *m_buffer, Selection{} }, Context::Flags::Transient);
hook_handler.context().set_window(*this); hook_handler.context().set_window(*this);
if (m_client)
hook_handler.context().set_client(*m_client);
hooks().run_hook(hook_name, param, hook_handler.context()); hooks().run_hook(hook_name, param, hook_handler.context());
} }
} }

View File

@ -1,6 +1,7 @@
#ifndef window_hh_INCLUDED #ifndef window_hh_INCLUDED
#define window_hh_INCLUDED #define window_hh_INCLUDED
#include "client.hh"
#include "display_buffer.hh" #include "display_buffer.hh"
#include "highlighter_group.hh" #include "highlighter_group.hh"
#include "option_manager.hh" #include "option_manager.hh"
@ -46,6 +47,8 @@ public:
ByteCoord offset_coord(ByteCoord coord, CharCount offset); ByteCoord offset_coord(ByteCoord coord, CharCount offset);
ByteCoordAndTarget offset_coord(ByteCoordAndTarget coord, LineCount offset); ByteCoordAndTarget offset_coord(ByteCoordAndTarget coord, LineCount offset);
void set_client(Client* client) { m_client = client; }
void clear_display_buffer(); void clear_display_buffer();
private: private:
Window(const Window&) = delete; Window(const Window&) = delete;
@ -56,6 +59,7 @@ private:
void run_hook_in_own_context(StringView hook_name, StringView param); void run_hook_in_own_context(StringView hook_name, StringView param);
SafePtr<Buffer> m_buffer; SafePtr<Buffer> m_buffer;
SafePtr<Client> m_client;
CharCoord m_position; CharCoord m_position;
CharCoord m_dimensions; CharCoord m_dimensions;