Skip to content

Commit

Permalink
fix: hide toast after refresh.
Browse files Browse the repository at this point in the history
Change-Id: I27cda4cd64f5c32eb856be3459db7b30fc4d15f3
  • Loading branch information
rekols committed Dec 14, 2018
1 parent 27c6b2e commit c2b07f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
47 changes: 16 additions & 31 deletions src/editwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ EditWrapper::EditWrapper(QWidget *parent)
m_textCodec(QTextCodec::codecForName("UTF-8")),
m_endOfLineMode(eolUnix),
m_isLoadFinished(true),
m_toast(new Toast(this))
m_toast(new Toast(this)),
m_isRefreshing(false)
{
// Init layout and widgets.
m_layout->setContentsMargins(0, 0, 0, 0);
Expand Down Expand Up @@ -174,22 +175,23 @@ void EditWrapper::updatePath(const QString &file)

void EditWrapper::refresh()
{
if (filePath().isEmpty() || Utils::isDraftFile(filePath())) {
if (filePath().isEmpty() || Utils::isDraftFile(filePath()) || m_isRefreshing) {
return;
}

QFile file(filePath());
int curPos = m_textEdit->textCursor().position();
int yoffset = m_textEdit->verticalScrollBar()->value();
int xoffset = m_textEdit->horizontalScrollBar()->value();
m_textEdit->setPlainText(QString());

if (file.open(QIODevice::ReadOnly)) {
m_isRefreshing = true;

QTextStream out(&file);
out.setCodec(m_textCodec);
QString content = out.readAll();

m_textEdit->setUpdatesEnabled(false);
m_textEdit->setPlainText(QString());
m_textEdit->setPlainText(content);
m_textEdit->setModified(false);

Expand All @@ -202,11 +204,17 @@ void EditWrapper::refresh()
QFileInfo fi(filePath());
m_modified = fi.lastModified();

QTimer::singleShot(50, this, [=] {
file.close();
m_toast->hideAnimation();

m_textEdit->setUpdatesEnabled(false);

QTimer::singleShot(10, this, [=] {
m_textEdit->setUpdatesEnabled(true);
m_isRefreshing = false;
});

file.close();
} else {
m_isRefreshing = false;
}
}

Expand All @@ -227,30 +235,7 @@ void EditWrapper::setTextCodec(QTextCodec *codec, bool reload)
if (!reload)
return;

QFile file(filePath());
int curPos = m_textEdit->textCursor().position();
int yoffset = m_textEdit->verticalScrollBar()->value();
int xoffset = m_textEdit->horizontalScrollBar()->value();
m_textEdit->setPlainText(QString());

if (file.open(QIODevice::ReadOnly)) {
QTextStream out(&file);
out.setCodec(codec);
QString content = out.readAll();

m_textEdit->setUpdatesEnabled(false);
m_textEdit->setPlainText(content);
m_textEdit->setModified(false);

QTextCursor textcur = m_textEdit->textCursor();
textcur.setPosition(curPos);
m_textEdit->setTextCursor(textcur);
m_textEdit->verticalScrollBar()->setValue(yoffset);
m_textEdit->horizontalScrollBar()->setValue(xoffset);
m_textEdit->setUpdatesEnabled(true);

file.close();
}
refresh();

// TODO: enforce bom for some encodings
}
Expand Down
2 changes: 2 additions & 0 deletions src/editwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class EditWrapper : public QWidget
bool m_isLoadFinished;
QDateTime m_modified;
Toast *m_toast;

bool m_isRefreshing;
};

#endif

0 comments on commit c2b07f4

Please sign in to comment.