Do not use a hash to determine if a window must be redrawn

Collision happens
Fixes #569
This commit is contained in:
Maxime Coste 2016-02-03 09:51:56 +00:00
parent 3e0e32cfbb
commit dc3c7d593c
2 changed files with 38 additions and 16 deletions

View File

@ -72,22 +72,38 @@ void Window::center_column(CharCount buffer_column)
display_column_at(buffer_column, m_dimensions.column/2_char);
}
size_t Window::compute_hash(const Context& context) const
Window::Setup Window::build_setup(const Context& context) const
{
size_t res = hash_values(m_position, context.ui().dimensions(), context.buffer().timestamp());
Vector<BufferRange> selections;
for (auto& sel : context.selections())
selections.push_back({sel.cursor(), sel.anchor()});
auto& selections = context.selections();
res = combine_hash(res, hash_value(selections.main_index()));
for (auto& sel : selections)
res = combine_hash(res, hash_values((const ByteCoord&)sel.cursor(), sel.anchor()));
return res;
return { m_position,
context.ui().dimensions(),
context.buffer().timestamp(),
context.selections().main_index(),
std::move(selections) };
}
bool Window::needs_redraw(const Context& context) const
{
size_t hash = compute_hash(context);
return hash != m_hash;
auto& selections = context.selections();
if (m_position != m_last_setup.position or
context.ui().dimensions() != m_last_setup.dimensions or
context.buffer().timestamp() != m_last_setup.timestamp or
selections.main_index() != m_last_setup.main_selection or
selections.size() != m_last_setup.selections.size())
return true;
for (int i = 0; i < selections.size(); ++i)
{
if (selections[i].cursor() != m_last_setup.selections[i].begin or
selections[i].anchor() != m_last_setup.selections[i].end)
return true;
}
return false;
}
const DisplayBuffer& Window::update_display_buffer(const Context& context)
@ -120,7 +136,7 @@ const DisplayBuffer& Window::update_display_buffer(const Context& context)
line.trim(m_position.column, m_dimensions.column, true);
m_display_buffer.optimize();
m_hash = compute_hash(context);
m_last_setup = build_setup(context);
return m_display_buffer;
}

View File

@ -41,7 +41,7 @@ public:
Buffer& buffer() const { return *m_buffer; }
bool needs_redraw(const Context& context) const;
void force_redraw() { m_hash = -1; }
void force_redraw() { m_last_setup = Setup{}; }
ByteCoord offset_coord(ByteCoord coord, CharCount offset);
ByteCoordAndTarget offset_coord(ByteCoordAndTarget coord, LineCount offset);
@ -55,8 +55,6 @@ private:
void run_hook_in_own_context(StringView hook_name, StringView param);
size_t compute_hash(const Context& context) const;
SafePtr<Buffer> m_buffer;
CharCoord m_position;
@ -66,8 +64,16 @@ private:
HighlighterGroup m_highlighters;
HighlighterGroup m_builtin_highlighters;
// hash used to determine if a redraw is necessary
size_t m_hash = -1;
struct Setup
{
CharCoord position;
CharCoord dimensions;
size_t timestamp;
size_t main_selection;
Vector<BufferRange> selections;
};
Setup build_setup(const Context& context) const;
Setup m_last_setup;
};
}