Reduce metrics updates from 4 to 1 when loading file

The number of metrics updates when loading file and showing it in a
new work area is unreasonable.

The first avoided call to updateMetrics() was an explicit resize in
BufferView::init(). Instead, an assertion is suppressed by exiting
early BufferView::processUpdateFlags() when BufferView::ready()
returns false. This is a new method introduced to factor in some
existing tests.

Two other metrics computations are avoided by setting the enclosing
View object busy() while creating the new tab. To make this work
properly, GuiWorkArea::scheduleRedraw has to return early in this
case.

When saving an unnamed document or invoking "Save as...", call
setBusy(false) earlier so that repainting occurs correctly.

Fixes bug #12976.
This commit is contained in:
Jean-Marc Lasgouttes 2023-11-22 12:07:51 +01:00
parent a23522073c
commit 1d1f95d2ed
4 changed files with 14 additions and 9 deletions

View File

@ -5562,6 +5562,7 @@ Buffer::ReadStatus Buffer::reload()
Buffer const * oldparent = d->parent(); Buffer const * oldparent = d->parent();
d->setParent(nullptr); d->setParent(nullptr);
ReadStatus const status = loadLyXFile(); ReadStatus const status = loadLyXFile();
setBusy(false);
if (status == ReadSuccess) { if (status == ReadSuccess) {
updateBuffer(); updateBuffer();
changed(true); changed(true);
@ -5578,7 +5579,6 @@ Buffer::ReadStatus Buffer::reload()
} else { } else {
message(bformat(_("Could not reload document %1$s."), disp_fn)); message(bformat(_("Could not reload document %1$s."), disp_fn));
} }
setBusy(false);
removePreviews(); removePreviews();
updatePreviews(); updatePreviews();
errors("Parse"); errors("Parse");

View File

@ -532,7 +532,7 @@ void BufferView::processUpdateFlags(Update::flags flags)
<< flagsAsString(flags) << ") buffer: " << &buffer_); << flagsAsString(flags) << ") buffer: " << &buffer_);
// Case when no explicit update is requested. // Case when no explicit update is requested.
if (flags == Update::None) if (flags == Update::None || !ready())
return; return;
/* FIXME We would like to avoid doing this here, since it is very /* FIXME We would like to avoid doing this here, since it is very
@ -611,7 +611,7 @@ void BufferView::processUpdateFlags(Update::flags flags)
void BufferView::updateScrollbarParameters() void BufferView::updateScrollbarParameters()
{ {
if (height_ == 0 && width_ == 0) if (!ready())
return; return;
// We prefer fixed size line scrolling. // We prefer fixed size line scrolling.
@ -2625,6 +2625,9 @@ void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
{ {
//lyxerr << "[ cmd0 " << cmd0 << "]" << endl; //lyxerr << "[ cmd0 " << cmd0 << "]" << endl;
if (!ready())
return;
// This is only called for mouse related events including // This is only called for mouse related events including
// LFUN_FILE_OPEN generated by drag-and-drop. // LFUN_FILE_OPEN generated by drag-and-drop.
FuncRequest cmd = cmd0; FuncRequest cmd = cmd0;
@ -3125,7 +3128,7 @@ void BufferView::updateMetrics()
void BufferView::updateMetrics(bool force) void BufferView::updateMetrics(bool force)
{ {
if (height_ == 0 || width_ == 0) if (!ready())
return; return;
Text & buftext = buffer_.text(); Text & buftext = buffer_.text();
@ -3586,7 +3589,7 @@ bool BufferView::busy() const
void BufferView::draw(frontend::Painter & pain, bool paint_caret) void BufferView::draw(frontend::Painter & pain, bool paint_caret)
{ {
if (height_ == 0 || width_ == 0) if (!ready())
return; return;
LYXERR(Debug::PAINTING, (pain.isNull() ? "\t\t--- START NODRAW ---" LYXERR(Debug::PAINTING, (pain.isNull() ? "\t\t--- START NODRAW ---"
: "\t\t*** START DRAWING ***")); : "\t\t*** START DRAWING ***"));

View File

@ -348,6 +348,8 @@ public:
/// the shape of the caret /// the shape of the caret
frontend::CaretGeometry const & caretGeometry() const; frontend::CaretGeometry const & caretGeometry() const;
/// Returns true when metrics have been computed at least once
bool ready() const { return width_ > 0 && height_ > 0; }
/// Returns true when the BufferView is not ready for drawing /// Returns true when the BufferView is not ready for drawing
bool busy() const; bool busy() const;
/// ///

View File

@ -199,9 +199,6 @@ void GuiWorkArea::init()
}); });
d->resetScreen(); d->resetScreen();
// A mouse event will happen before the first paint event,
// so make sure that the buffer view has an up to date metrics.
d->buffer_view_->resize(viewport()->width(), viewport()->height());
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setAcceptDrops(true); setAcceptDrops(true);
@ -345,7 +342,7 @@ void GuiWorkArea::toggleCaret()
void GuiWorkArea::scheduleRedraw(bool update_metrics) void GuiWorkArea::scheduleRedraw(bool update_metrics)
{ {
if (!isVisible()) if (!isVisible() || view().busy())
// No need to redraw in this case. // No need to redraw in this case.
return; return;
@ -1793,6 +1790,7 @@ bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view) GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
{ {
view.setBusy(true);
GuiWorkArea * wa = new GuiWorkArea(buffer, view); GuiWorkArea * wa = new GuiWorkArea(buffer, view);
GuiWorkAreaContainer * wac = new GuiWorkAreaContainer(wa); GuiWorkAreaContainer * wac = new GuiWorkAreaContainer(wa);
wa->setUpdatesEnabled(false); wa->setUpdatesEnabled(false);
@ -1811,6 +1809,8 @@ GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
updateTabTexts(); updateTabTexts();
view.setBusy(false);
return wa; return wa;
} }