Skip to content

Commit 673d4a1

Browse files
committed
wip: lazy tab restore for faster startup
Refactor tab recovery to only immediately load the focused tab on startup; non-focused tabs remain in a pending state and are loaded on demand when the user switches to them. This avoids blocking the UI with N file loads during session restore.
1 parent c8c7d2a commit 673d4a1

7 files changed

Lines changed: 507 additions & 204 deletions

File tree

src/editor/editwrapper.cpp

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,44 +1022,60 @@ void EditWrapper::handleFileLoadFinished(const QByteArray &encode, const QByteAr
10221022

10231023
m_pTextEdit->setTextFinished();
10241024

1025-
QStringList temFileList = Settings::instance()->settings->option("advance.editor.browsing_history_temfile")->value().toStringList();
1026-
1027-
for (int var = 0; var < temFileList.count(); ++var) {
1028-
QJsonParseError jsonError;
1029-
// 转化为 JSON 文档
1030-
QJsonDocument doucment = QJsonDocument::fromJson(temFileList.value(var).toUtf8(), &jsonError);
1031-
// 解析未发生错误
1032-
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) {
1033-
qDebug() << "EditWrapper handleFileLoadFinished, doucment is not null";
1034-
if (doucment.isObject()) {
1035-
qDebug() << "EditWrapper handleFileLoadFinished, doucment is object";
1036-
// JSON 文档为对象
1037-
QJsonObject object = doucment.object(); // 转化为对象
1038-
1039-
if (object.contains("localPath") || object.contains("temFilePath")) {
1040-
qDebug() << "EditWrapper handleFileLoadFinished, doucment contains localPath or temFilePath";
1041-
// 包含指定的 key
1042-
QJsonValue localPathValue = object.value("localPath"); // 获取指定 key 对应的 value
1043-
QJsonValue temFilePathValue = object.value("temFilePath"); // 获取指定 key 对应的 value
1044-
1045-
if (localPathValue.toString() == m_pTextEdit->getFilePath()
1046-
|| temFilePathValue.toString() == m_pTextEdit->getFilePath()) {
1047-
qDebug() << "EditWrapper handleFileLoadFinished, localPathValue or temFilePathValue is equal to m_pTextEdit->getFilePath()";
1048-
QJsonValue value = object.value("cursorPosition"); // 获取指定 key 对应的 value
1049-
1050-
if (value.isString()) {
1051-
qDebug() << "EditWrapper handleFileLoadFinished, value is string";
1052-
QTextCursor cursor = m_pTextEdit->textCursor();
1053-
cursor.setPosition(value.toString().toInt());
1054-
m_pTextEdit->setTextCursor(cursor);
1055-
OnUpdateHighlighter();
1056-
break;
1025+
// 仅在文件加载成功时恢复光标位置
1026+
if (!error) {
1027+
// 恢复光标位置:优先使用预设值(O(1)),否则遍历历史记录(O(N))
1028+
bool cursorRestored = false;
1029+
if (m_nRestoreCursorPosition >= 0) {
1030+
QTextCursor cursor = m_pTextEdit->textCursor();
1031+
cursor.setPosition(m_nRestoreCursorPosition);
1032+
m_pTextEdit->setTextCursor(cursor);
1033+
OnUpdateHighlighter();
1034+
m_nRestoreCursorPosition = -1; // 重置,仅使用一次
1035+
cursorRestored = true;
1036+
}
1037+
1038+
if (!cursorRestored) {
1039+
QStringList temFileList = Settings::instance()->settings->option("advance.editor.browsing_history_temfile")->value().toStringList();
1040+
1041+
for (int var = 0; var < temFileList.count(); ++var) {
1042+
QJsonParseError jsonError;
1043+
// 转化为 JSON 文档
1044+
QJsonDocument doucment = QJsonDocument::fromJson(temFileList.value(var).toUtf8(), &jsonError);
1045+
// 解析未发生错误
1046+
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) {
1047+
qDebug() << "EditWrapper handleFileLoadFinished, doucment is not null";
1048+
if (doucment.isObject()) {
1049+
qDebug() << "EditWrapper handleFileLoadFinished, doucment is object";
1050+
// JSON 文档为对象
1051+
QJsonObject object = doucment.object(); // 转化为对象
1052+
1053+
if (object.contains("localPath") || object.contains("temFilePath")) {
1054+
qDebug() << "EditWrapper handleFileLoadFinished, doucment contains localPath or temFilePath";
1055+
// 包含指定的 key
1056+
QJsonValue localPathValue = object.value("localPath"); // 获取指定 key 对应的 value
1057+
QJsonValue temFilePathValue = object.value("temFilePath"); // 获取指定 key 对应的 value
1058+
1059+
if (localPathValue.toString() == m_pTextEdit->getFilePath()
1060+
|| temFilePathValue.toString() == m_pTextEdit->getFilePath()) {
1061+
qDebug() << "EditWrapper handleFileLoadFinished, localPathValue or temFilePathValue is equal to m_pTextEdit->getFilePath()";
1062+
QJsonValue value = object.value("cursorPosition"); // 获取指定 key 对应的 value
1063+
1064+
if (value.isString()) {
1065+
qDebug() << "EditWrapper handleFileLoadFinished, value is string";
1066+
QTextCursor cursor = m_pTextEdit->textCursor();
1067+
cursor.setPosition(value.toString().toInt());
1068+
m_pTextEdit->setTextCursor(cursor);
1069+
OnUpdateHighlighter();
1070+
break;
1071+
}
1072+
}
10571073
}
10581074
}
10591075
}
10601076
}
1061-
}
1062-
}
1077+
} // end if (!cursorRestored)
1078+
} // end if (!error)
10631079

10641080
//备份显示修改状态
10651081
if (m_bIsTemFile) {
@@ -1242,6 +1258,11 @@ void EditWrapper::setTemFile(bool value)
12421258
qDebug() << "EditWrapper setTemFile, exit";
12431259
}
12441260

1261+
void EditWrapper::setRestoreCursorPosition(int position)
1262+
{
1263+
m_nRestoreCursorPosition = position;
1264+
}
1265+
12451266
void EditWrapper::updateHighlighterAll()
12461267
{
12471268
qDebug() << "EditWrapper updateHighlighterAll";

src/editor/editwrapper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ public slots:
145145
void OnUpdateHighlighter();
146146
//set the value of m_bIsTemFile
147147
void setTemFile(bool value);
148+
// 设置恢复光标位置(用于懒加载恢复,避免 O(N²) 扫描)
149+
void setRestoreCursorPosition(int position);
148150

149151
private:
150152
//第一次打开文件编码
@@ -180,6 +182,7 @@ public slots:
180182

181183
bool m_bAsyncReadFileFinished = false;
182184
bool m_bHasPreProcess = false; // 预处理标识
185+
int m_nRestoreCursorPosition = -1; // 恢复光标位置提示(-1 表示不指定)
183186
};
184187

185188
#endif

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ int main(int argc, char *argv[])
9999
#endif
100100

101101
StartManager *startManager = StartManager::instance();
102+
102103
//埋点记录启动数据
103104
QJsonObject objStartEvent{
104105
{"tid", Eventlogutils::StartUp},

0 commit comments

Comments
 (0)