Skip to content

Commit 2a5fb27

Browse files
feat(markdown): add live markdown preview overlay
Add a full-screen markdown preview overlay that renders the active editor's content using md4c and Lexilla-based syntax highlighting. - Rendering is synchronous for files under 100KB and async via QtConcurrent for larger files, using a generation counter to discard stale results and avoid races. - MarkdownRenderer drives md4c's HTML renderer, post-processes code fences with Lexilla lexers for syntax highlighting, and injects proportional line anchors for approximate scroll sync. - PreviewBrowser extends QTextBrowser with async image loading, an LRU-style image cache, and keyboard passthrough so typing dismisses the overlay and returns focus to the editor. - md4c is vendored as a static library under thirdparty/md4c to keep the submodule untouched and upstream merges clean. - GoalAgent::stop now cancels the in-flight prompt before marking the agent terminal, preventing a use-after-free on the connection. - goalDebugLogAppended signal added to AiAgentDock so the debug dialog can stream goal events live without polling. - gitChangesContextMenuRequested signature widened to pass the full GitStatusEntry; GitTabWidget injects a Revert action for tracked unstaged files with a confirmation dialog before calling GitController::revertPaths (chunked at 50 paths per git invocation). - QTabBar styling added to both light and dark stylesheets. - Window now opens maximized by default (respects saved setting).
1 parent f4b35aa commit 2a5fb27

39 files changed

Lines changed: 12119 additions & 37 deletions

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ find_package(Qt6 6.5
3737
Network
3838
PrintSupport
3939
Core5Compat
40+
Concurrent
4041
)
4142

4243
option(NOTEPADAI_BUILD_TESTS "Build unit tests" OFF)

‎src/CMakeLists.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ qt_add_executable(NotepadAI
3535
FocusWatcher.h
3636
HtmlConverter.h
3737
IFaceTable.h
38+
MarkdownPreviewOverlay.h
39+
MarkdownRenderer.h
40+
PreviewBrowser.h
3841
IFaceTableMixer.h
3942
ISearchResultsHandler.h
4043
LanguageStylesModel.h
@@ -102,6 +105,9 @@ qt_add_executable(NotepadAI
102105
Finder.cpp
103106
HtmlConverter.cpp
104107
IFaceTable.cpp
108+
MarkdownPreviewOverlay.cpp
109+
MarkdownRenderer.cpp
110+
PreviewBrowser.cpp
105111
IFaceTableMixer.cpp
106112
LanguageStylesModel.cpp
107113
LuaExtension.cpp
@@ -448,6 +454,7 @@ target_link_libraries(NotepadAI
448454
Qt6::Widgets
449455
Qt6::Network
450456
Qt6::PrintSupport
457+
Qt6::Concurrent
451458
qtadvanceddocking-qt6
452459
QSimpleUpdater
453460
SingleApplication::SingleApplication
@@ -460,6 +467,7 @@ target_link_libraries(NotepadAI
460467
libptyqt
461468
xxhash
462469
xdiff
470+
md4c
463471
)
464472

465473
target_compile_definitions(NotepadAI

‎src/GoalAgent.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ void GoalAgent::stop()
119119
if (m_targetConnection) {
120120
disconnect(m_targetConnection, &AcpConnection::promptEnded,
121121
this, &GoalAgent::onTargetPromptEnded);
122+
m_targetConnection->cancelPrompt();
122123
}
123124
markTerminal(Cancelled, QStringLiteral("user_stop"));
124125
}

‎src/MarkdownPreviewOverlay.cpp‎

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#include "MarkdownPreviewOverlay.h"
2+
#include "PreviewBrowser.h"
3+
#include "ScintillaNext.h"
4+
#include "NotepadNextApplication.h"
5+
6+
#include <QEvent>
7+
#include <QKeyEvent>
8+
#include <QScrollBar>
9+
#include <QtConcurrent>
10+
11+
static constexpr int kSyncRenderThreshold = 100 * 1024; // 100KB
12+
13+
MarkdownPreviewOverlay::MarkdownPreviewOverlay(ScintillaNext *editor, NotepadNextApplication *app)
14+
: QObject(editor)
15+
, m_editor(editor)
16+
, m_app(app)
17+
{
18+
m_editor->installEventFilter(this);
19+
}
20+
21+
void MarkdownPreviewOverlay::ensureBrowser()
22+
{
23+
if (m_browser)
24+
return;
25+
26+
m_browser = new PreviewBrowser(m_editor);
27+
m_browser->hide();
28+
29+
if (m_editor->isFile()) {
30+
QString dir = m_editor->getFileInfo().absolutePath();
31+
m_browser->setSearchPaths({dir});
32+
}
33+
34+
connect(m_browser, &PreviewBrowser::dismissRequested, this, [this]() {
35+
deactivate();
36+
});
37+
38+
connect(m_app, &NotepadNextApplication::effectiveThemeChanged, this, [this]() {
39+
if (m_active) {
40+
m_cachedHtml.clear();
41+
renderAsync();
42+
}
43+
});
44+
}
45+
46+
void MarkdownPreviewOverlay::activate()
47+
{
48+
if (m_active)
49+
return;
50+
51+
ensureBrowser();
52+
m_active = true;
53+
m_browser->setGeometry(m_editor->rect());
54+
m_browser->show();
55+
m_browser->raise();
56+
m_browser->setFocus();
57+
58+
if (!m_cachedHtml.isEmpty()) {
59+
m_browser->setHtml(m_cachedHtml);
60+
scrollToEditorPosition();
61+
} else if (m_editor->length() <= kSyncRenderThreshold) {
62+
renderSync();
63+
} else {
64+
m_browser->setHtml(QStringLiteral("<p><em>Rendering...</em></p>"));
65+
renderAsync();
66+
}
67+
68+
emit activeChanged(true);
69+
}
70+
71+
void MarkdownPreviewOverlay::deactivate()
72+
{
73+
if (!m_active)
74+
return;
75+
76+
m_active = false;
77+
if (m_browser)
78+
m_browser->hide();
79+
80+
m_editor->grabFocus();
81+
emit activeChanged(false);
82+
}
83+
84+
void MarkdownPreviewOverlay::toggle()
85+
{
86+
if (m_active)
87+
deactivate();
88+
else
89+
activate();
90+
}
91+
92+
void MarkdownPreviewOverlay::clearCachedContent()
93+
{
94+
m_cachedHtml.clear();
95+
if (m_browser && !m_active)
96+
m_browser->clear();
97+
}
98+
99+
MarkdownRenderRequest MarkdownPreviewOverlay::buildRequest()
100+
{
101+
MarkdownRenderRequest req;
102+
req.sourceText = QString::fromUtf8(m_editor->getText(m_editor->length() + 1));
103+
req.palette = m_browser->palette();
104+
req.isDark = m_app->isEffectiveThemeDark();
105+
if (m_editor->isFile())
106+
req.basePath = m_editor->getFileInfo().absolutePath();
107+
108+
QSet<QString> labels = MarkdownRenderer::scanFenceLabels(req.sourceText);
109+
for (const QString &label : labels) {
110+
QString normalized = MarkdownRenderer::normalizeFenceLabel(label);
111+
QString lexerName = m_app->resolveLexerName(normalized);
112+
if (lexerName.isEmpty()) {
113+
static const QHash<QString, QString> fallback = {
114+
{"cpp", "cpp"}, {"c", "cpp"}, {"python", "python"},
115+
{"javascript", "cpp"}, {"rust", "rust"}, {"java", "cpp"},
116+
{"go", "cpp"}, {"bash", "bash"}, {"html", "hypertext"},
117+
{"css", "css"}, {"json", "json"}, {"lua", "lua"},
118+
};
119+
lexerName = fallback.value(normalized);
120+
}
121+
req.resolvedLexers.insert(normalized, lexerName);
122+
}
123+
return req;
124+
}
125+
126+
void MarkdownPreviewOverlay::renderSync()
127+
{
128+
MarkdownRenderRequest req = buildRequest();
129+
MarkdownRenderResult result = MarkdownRenderer::render(req);
130+
applyHtml(result.html);
131+
}
132+
133+
void MarkdownPreviewOverlay::renderAsync()
134+
{
135+
int gen = m_renderGeneration.fetch_add(1, std::memory_order_relaxed) + 1;
136+
MarkdownRenderRequest req = buildRequest();
137+
138+
if (m_watcher) {
139+
m_watcher->cancel();
140+
delete m_watcher;
141+
}
142+
143+
m_watcher = new QFutureWatcher<MarkdownRenderResult>(this);
144+
connect(m_watcher, &QFutureWatcher<MarkdownRenderResult>::finished, this, [this, gen]() {
145+
if (m_renderGeneration.load(std::memory_order_relaxed) != gen) return;
146+
if (!m_active) return;
147+
MarkdownRenderResult result = m_watcher->result();
148+
applyHtml(result.html);
149+
});
150+
151+
m_watcher->setFuture(QtConcurrent::run([req]() {
152+
return MarkdownRenderer::render(req);
153+
}));
154+
}
155+
156+
void MarkdownPreviewOverlay::applyHtml(const QString &html)
157+
{
158+
m_cachedHtml = html;
159+
if (m_browser && m_active) {
160+
m_browser->setHtml(html);
161+
scrollToEditorPosition();
162+
}
163+
}
164+
165+
void MarkdownPreviewOverlay::scrollToEditorPosition()
166+
{
167+
if (!m_browser) return;
168+
int firstLine = int(m_editor->firstVisibleLine()) + 1;
169+
int anchorLine = (firstLine / 10) * 10;
170+
if (anchorLine < 1) anchorLine = 1;
171+
m_browser->scrollToAnchor(QStringLiteral("L%1").arg(anchorLine));
172+
}
173+
174+
bool MarkdownPreviewOverlay::eventFilter(QObject *watched, QEvent *event)
175+
{
176+
if (watched == m_editor && event->type() == QEvent::Resize) {
177+
if (m_browser && m_active)
178+
m_browser->setGeometry(m_editor->rect());
179+
}
180+
return QObject::eventFilter(watched, event);
181+
}

‎src/MarkdownPreviewOverlay.h‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef MARKDOWNPREVIEWOVERLAY_H
2+
#define MARKDOWNPREVIEWOVERLAY_H
3+
4+
#include <QObject>
5+
#include <QFutureWatcher>
6+
#include <atomic>
7+
8+
#include "MarkdownRenderer.h"
9+
10+
class PreviewBrowser;
11+
class ScintillaNext;
12+
class NotepadNextApplication;
13+
14+
class MarkdownPreviewOverlay : public QObject
15+
{
16+
Q_OBJECT
17+
18+
public:
19+
explicit MarkdownPreviewOverlay(ScintillaNext *editor, NotepadNextApplication *app);
20+
21+
bool isActive() const { return m_active; }
22+
void activate();
23+
void deactivate();
24+
void toggle();
25+
void clearCachedContent();
26+
27+
signals:
28+
void activeChanged(bool active);
29+
30+
protected:
31+
bool eventFilter(QObject *watched, QEvent *event) override;
32+
33+
private:
34+
void renderAsync();
35+
void renderSync();
36+
void applyHtml(const QString &html);
37+
void scrollToEditorPosition();
38+
MarkdownRenderRequest buildRequest();
39+
void ensureBrowser();
40+
41+
ScintillaNext *m_editor;
42+
NotepadNextApplication *m_app;
43+
PreviewBrowser *m_browser = nullptr;
44+
bool m_active = false;
45+
QString m_cachedHtml;
46+
std::atomic<int> m_renderGeneration{0};
47+
QFutureWatcher<MarkdownRenderResult> *m_watcher = nullptr;
48+
};
49+
50+
#endif // MARKDOWNPREVIEWOVERLAY_H

0 commit comments

Comments
 (0)