Rework caret display code

The new code is much simpler: what it does is, after redrawing has
been done, to mark the cursor row as changed, so that it will be
repainted on next paint event.

This avoids some crashes at the price of possibly repainting the row
when it was not necessary.

(cherry picked from commit 2f1eb35b86)
This commit is contained in:
Jean-Marc Lasgouttes 2018-04-18 10:10:08 +02:00
parent 5c6bc52b1b
commit be62d98dcf
3 changed files with 11 additions and 54 deletions

View File

@ -237,7 +237,7 @@ struct BufferView::Private
last_inset_(0), clickable_inset_(false),
mouse_position_cache_(),
bookmark_edit_position_(-1), gui_(0),
horiz_scroll_offset_(0), repaint_caret_row_(false)
horiz_scroll_offset_(0)
{
xsel_cache_.set = false;
}
@ -316,12 +316,6 @@ struct BufferView::Private
/// a slice pointing to the start of the row where cursor was
/// at previous draw event
CursorSlice last_row_slice_;
/// a slice pointing to where the cursor has been drawn after the current
/// draw() call.
CursorSlice caret_slice_;
/// indicates whether the caret slice needs to be repainted in this draw() run.
bool repaint_caret_row_;
};
@ -3063,29 +3057,6 @@ void BufferView::setCurrentRowSlice(CursorSlice const & rowSlice)
}
namespace {
bool sliceInRow(CursorSlice const & cs, Text const * text, Row const & row)
{
/* The normal case is the last line. The previous line takes care
* of empty rows (e.g. empty paragraphs). Cursor boundary issues
* are taken care of when setting caret_slice_ in
* BufferView::draw.
*/
return !cs.empty() && cs.text() == text && cs.pit() == row.pit()
&& ((row.pos() == row.endpos() && row.pos() == cs.pos())
|| (row.pos() <= cs.pos() && cs.pos() < row.endpos()));
}
}
bool BufferView::needRepaint(Text const * text, Row const & row) const
{
return d->repaint_caret_row_ && sliceInRow(d->caret_slice_, text, row);
}
void BufferView::checkCursorScrollOffset()
{
CursorSlice rowSlice = d->cursor_.bottom();
@ -3157,16 +3128,6 @@ void BufferView::draw(frontend::Painter & pain, bool paint_caret)
int const y = tm.first().second->position();
PainterInfo pi(this, pain);
/** A repaint of the previous caret row is needed if there is
* caret painted on screen and either
* 1/ a new caret has to be painted at a place different from
* the existing one;
* 2/ there is no need for a caret anymore.
*/
d->repaint_caret_row_ = !d->caret_slice_.empty() &&
((paint_caret && d->cursor_.top() != d->caret_slice_)
|| ! paint_caret);
// Check whether the row where the cursor lives needs to be scrolled.
// Update the drawing strategy if needed.
checkCursorScrollOffset();
@ -3182,7 +3143,7 @@ void BufferView::draw(frontend::Painter & pain, bool paint_caret)
if (pain.isNull()) {
pi.full_repaint = true;
tm.draw(pi, 0, y);
} else if (d->repaint_caret_row_) {
} else {
pi.full_repaint = false;
tm.draw(pi, 0, y);
}
@ -3256,15 +3217,15 @@ void BufferView::draw(frontend::Painter & pain, bool paint_caret)
d->update_flags_ = Update::None;
}
// Remember what has just been done for the next draw() step
// If a caret has to be painted, mark its text row as dirty to
//make sure that it will be repainted on next redraw.
/* FIXME: investigate whether this can be avoided when the cursor did not
* move at all
*/
if (paint_caret) {
d->caret_slice_ = d->cursor_.top();
if (d->caret_slice_.pos() > 0
&& (d->cursor_.boundary()
|| d->caret_slice_.pos() == d->caret_slice_.lastpos()))
--d->caret_slice_.pos();
} else
d->caret_slice_ = CursorSlice();
Row const & caret_row = d->cursor_.textRow();
caret_row.changed(true);
}
}

View File

@ -133,9 +133,6 @@ public:
/// Only to be called with good y coordinates (after a bv::metrics)
bool needsFitCursor() const;
/// returns true if this row needs to be repainted (to erase caret)
bool needRepaint(Text const * text, Row const & row) const;
// Returns the amount of horizontal scrolling applied to the
// top-level row where the cursor lies
int horizScrollOffset() const;

View File

@ -1891,8 +1891,7 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
// has row changed since last paint?
bool row_has_changed = row.changed()
|| bv_->hadHorizScrollOffset(text_, pit, row.pos())
|| bv_->needRepaint(text_, row);
|| bv_->hadHorizScrollOffset(text_, pit, row.pos());
// Take this opportunity to spellcheck the row contents.
if (row_has_changed && pi.do_spellcheck && lyxrc.spellcheck_continuously) {