Rework Window creation, avoid using the same window in multiple clients

This commit is contained in:
Maxime Coste 2012-11-05 19:15:42 +01:00
parent 42315c4b5c
commit fedabb4176
5 changed files with 35 additions and 16 deletions

View File

@ -388,18 +388,16 @@ void Buffer::erase(BufferIterator begin, BufferIterator end)
do_erase(begin, end);
}
Window* Buffer::get_or_create_window()
Window& Buffer::new_window()
{
if (m_windows.empty())
m_windows.push_front(std::unique_ptr<Window>(new Window(*this)));
return m_windows.front().get();
m_windows.emplace_back(new Window(*this));
return *m_windows.back();
}
void Buffer::delete_window(Window* window)
void Buffer::delete_window(Window& window)
{
assert(&window->buffer() == this);
auto window_it = std::find(m_windows.begin(), m_windows.end(), window);
assert(&window.buffer() == this);
auto window_it = std::find(m_windows.begin(), m_windows.end(), &window);
assert(window_it != m_windows.end());
m_windows.erase(window_it);
}

View File

@ -143,8 +143,11 @@ public:
const String& name() const { return m_name; }
Window* get_or_create_window();
void delete_window(Window* window);
// Window handling
using WindowList = std::vector<std::unique_ptr<Window>>;
const WindowList& windows() const { return m_windows; }
Window& new_window();
void delete_window(Window& window);
// returns true if the buffer is in a different state than
// the last time it was saved
@ -216,7 +219,7 @@ private:
void apply_modification(const Modification& modification);
void revert_modification(const Modification& modification);
std::list<std::unique_ptr<Window>> m_windows;
WindowList m_windows;
size_t m_last_save_undo_index;
size_t m_timestamp;

View File

@ -8,7 +8,7 @@ namespace Kakoune
void ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
Buffer& buffer, int event_fd)
{
m_clients.emplace_back(std::move(ui), *buffer.get_or_create_window());
m_clients.emplace_back(std::move(ui), get_unused_window_for_buffer(buffer));
InputHandler* input_handler = m_clients.back().input_handler.get();
Context* context = m_clients.back().context.get();
@ -43,4 +43,18 @@ void ClientManager::remove_client_by_context(Context& context)
assert(false);
}
Window& ClientManager::get_unused_window_for_buffer(Buffer& buffer) const
{
for (auto& w : buffer.windows())
{
auto it = std::find_if(m_clients.begin(), m_clients.end(),
[&](const Client& client) {
return &client.context->window() == w.get();
});
if (it == m_clients.end())
return *w;
}
return buffer.new_window();
}
}

View File

@ -19,6 +19,8 @@ public:
bool empty() const { return m_clients.empty(); }
size_t count() const { return m_clients.size(); }
Window& get_unused_window_for_buffer(Buffer& buffer) const;
private:
struct Client
{

View File

@ -282,13 +282,13 @@ void edit(const CommandParameters& params, Context& context)
}
BufferManager::instance().set_last_used_buffer(*buffer);
Window& window = *buffer->get_or_create_window();
Window& window = ClientManager::instance().get_unused_window_for_buffer(*buffer);
if (param_count > 1)
{
int line = std::max(0, str_to_int(parser[1]) - 1);
int column = param_count > 2 ?
std::max(0, str_to_int(parser[2]) - 1) : 0;
std::max(0, str_to_int(parser[2]) - 1) : 0;
window.select(window.buffer().iterator_at({ line, column }));
window.center_selection();
@ -378,7 +378,8 @@ void show_buffer(const CommandParameters& params, Context& context)
throw runtime_error("buffer " + buffer_name + " does not exists");
BufferManager::instance().set_last_used_buffer(*buffer);
context.change_editor(*buffer->get_or_create_window());
Window& window = ClientManager::instance().get_unused_window_for_buffer(*buffer);
context.change_editor(window);
}
void delete_buffer(const CommandParameters& params, Context& context)
@ -408,7 +409,8 @@ void delete_buffer(const CommandParameters& params, Context& context)
{
if (buf != buffer)
{
context.change_editor(*buf->get_or_create_window());
Window& w = ClientManager::instance().get_unused_window_for_buffer(*buf);
context.change_editor(w);
break;
}
}