From 81b51f489b3a69a3e823ccb1c4c4974053ee9581 Mon Sep 17 00:00:00 2001
From: Arthur Porto <227830219+Art-Porto@users.noreply.github.com>
Date: Sat, 14 Nov 2026 12:31:52 -0300
Subject: [PATCH] Add toolbar buttons for reading mode and live preview
Reading mode now only turns off from its toggle (menu, toolbar button or
shortcut). Double-clicking a paragraph in reading mode moves the hidden
editor's caret to that line without scrolling, so toggling off lands on
the paragraph. The preview panel keeps jumping to the source.
Reading mode also refreshes after a file is reloaded from disk. Reloading
detaches the editor from its document, so no SCN_MODIFIED reaches the
plugin, and Notepad++ only re-activates the buffer afterwards. A reading
view that kept the same document now renders again on that activation
instead of showing the old text until another tab is selected.
---
README.md | 8 ++++++--
src/MarkdownReader.rc | 7 +++++++
src/NppApi.cpp | 6 ++++++
src/NppApi.h | 1 +
src/Plugin.cpp | 40 +++++++++++++++++++++++++++++++------
src/preview-panel-dark.ico | Bin 0 -> 5430 bytes
src/preview-panel.bmp | Bin 0 -> 206 bytes
src/preview-panel.ico | Bin 0 -> 5430 bytes
src/reading-mode-dark.ico | Bin 0 -> 5430 bytes
src/reading-mode.bmp | Bin 0 -> 198 bytes
src/reading-mode.ico | Bin 0 -> 5430 bytes
src/resource.h | 6 ++++++
12 files changed, 60 insertions(+), 8 deletions(-)
create mode 100644 src/preview-panel-dark.ico
create mode 100644 src/preview-panel.bmp
create mode 100644 src/preview-panel.ico
create mode 100644 src/reading-mode-dark.ico
create mode 100644 src/reading-mode.bmp
create mode 100644 src/reading-mode.ico
diff --git a/README.md b/README.md
index c588354..e2494d7 100644
--- a/README.md
+++ b/README.md
@@ -89,7 +89,9 @@ so even multi-megabyte docs stay smooth). Pick **Language → Markdown** on any
###
Reading mode
**Toggle Reading Mode** and the editor area itself becomes the rendered document, at the exact spot you were
-reading. Toggle again, or **double-click any paragraph**, to jump back into the source at that line. Turn on
+reading. Toggle again to jump back into the source. Double-clicking a paragraph keeps you in reading mode and puts the
+source cursor on that line, so toggling off lands there. In the preview panel, double-click jumps straight to the
+source line. Turn on
*Open Markdown in Reading Mode* and `.md` files open as documents, not code.
@@ -263,6 +265,8 @@ Windows 11; on Windows 10 it comes with Edge).
##
Using it
Everything lives under **Plugins → MarkdownReader**. Bind any of these to keys in *Settings → Shortcut Mapper*.
+The toolbar also has separate buttons for **Toggle Reading Mode** and **Toggle Preview Panel**.
+Both modes can be active at the same time.
| Command | What it does |
|---|---|
@@ -375,7 +379,7 @@ every push and pull request.
- [x] CI on GitHub Actions: ARM64, x64 and x86 builds plus the web and native tests (thanks to
[chcg](https://github.com/chcg))
- [ ] Hands-on verification of export, print, links and shortcuts → **v0.2**
-- [ ] Toolbar buttons, a settings dialog and an x86 release
+- [ ] A settings dialog and an x86 release
- [ ] Native Markdown syntax highlighting with its own *Language* menu entry
- [ ] Ideas welcome: open an issue!
diff --git a/src/MarkdownReader.rc b/src/MarkdownReader.rc
index cecab92..e3cb1dc 100644
--- a/src/MarkdownReader.rc
+++ b/src/MarkdownReader.rc
@@ -3,6 +3,13 @@
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
+IDB_READING_MODE BITMAP "reading-mode.bmp"
+IDI_READING_MODE ICON "reading-mode.ico"
+IDI_READING_MODE_DARK ICON "reading-mode-dark.ico"
+IDB_PREVIEW_PANEL BITMAP "preview-panel.bmp"
+IDI_PREVIEW_PANEL ICON "preview-panel.ico"
+IDI_PREVIEW_PANEL_DARK ICON "preview-panel-dark.ico"
+
VS_VERSION_INFO VERSIONINFO
FILEVERSION MDR_VERSION_MAJOR, MDR_VERSION_MINOR, MDR_VERSION_PATCH, MDR_VERSION_BUILD
PRODUCTVERSION MDR_VERSION_MAJOR, MDR_VERSION_MINOR, MDR_VERSION_PATCH, MDR_VERSION_BUILD
diff --git a/src/NppApi.cpp b/src/NppApi.cpp
index d4aa6be..b1e1988 100644
--- a/src/NppApi.cpp
+++ b/src/NppApi.cpp
@@ -133,4 +133,10 @@ void GotoDocLine(HWND sci, int line) {
ScrollToDocLine(sci, line > 3 ? line - 3 : 0);
}
+void PlaceCaretAtDocLine(HWND sci, int line) {
+ Sci(sci, SCI_ENSUREVISIBLE, static_cast(line));
+ const auto position = Sci(sci, SCI_POSITIONFROMLINE, static_cast(line));
+ if (position >= 0) Sci(sci, SCI_SETEMPTYSELECTION, static_cast(position)); // does not scroll
+}
+
} // namespace mdr::npp
diff --git a/src/NppApi.h b/src/NppApi.h
index 0a295d5..7694e99 100644
--- a/src/NppApi.h
+++ b/src/NppApi.h
@@ -37,5 +37,6 @@ size_t DocumentLength(HWND sci);
int FirstVisibleDocLine(HWND sci);
void ScrollToDocLine(HWND sci, int line);
void GotoDocLine(HWND sci, int line);
+void PlaceCaretAtDocLine(HWND sci, int line); // moves the caret without scrolling
} // namespace mdr::npp
diff --git a/src/Plugin.cpp b/src/Plugin.cpp
index 0309054..fb9a962 100644
--- a/src/Plugin.cpp
+++ b/src/Plugin.cpp
@@ -216,6 +216,23 @@ void Plugin::OnNotification(SCNotification* notification) {
const auto& header = notification->nmhdr;
if (header.hwndFrom == npp::Window()) {
switch (header.code) {
+ case NPPN_TBMODIFICATION: {
+ // Notepad++ retains the image handles for the lifetime of the toolbar.
+ static const wil::unique_hbitmap readingBitmap(::LoadBitmapW(module_, MAKEINTRESOURCEW(IDB_READING_MODE)));
+ static const wil::unique_hbitmap panelBitmap(::LoadBitmapW(module_, MAKEINTRESOURCEW(IDB_PREVIEW_PANEL)));
+ const auto addButton = [this](Command command, HBITMAP bitmap, int icon, int darkIcon) {
+ toolbarIconsWithDarkMode icons{
+ bitmap,
+ ::LoadIconW(module_, MAKEINTRESOURCEW(icon)),
+ ::LoadIconW(module_, MAKEINTRESOURCEW(darkIcon)),
+ };
+ ::SendMessageW(npp::Window(), NPPM_ADDTOOLBARICON_FORDARKMODE, commands_[command]._cmdID,
+ reinterpret_cast(&icons));
+ };
+ addButton(kCmdReading, readingBitmap.get(), IDI_READING_MODE, IDI_READING_MODE_DARK);
+ addButton(kCmdPanel, panelBitmap.get(), IDI_PREVIEW_PANEL, IDI_PREVIEW_PANEL_DARK);
+ break;
+ }
case NPPN_READY: OnReady(); break;
case NPPN_SHUTDOWN: OnShutdown(); break;
case NPPN_BUFFERACTIVATED:
@@ -289,6 +306,9 @@ void Plugin::OnActiveBufferChanged() {
}
if (!currentIsMarkdown) state.reading = false;
+ auto& currentReading = reading_[currentView];
+ const bool wasReadingCurrent = currentReading.overlay.IsActive() && currentReading.bufferId == current;
+
// Both views: moving a document between views changes the other view too.
for (int view = 0; view < 2; ++view) {
const UINT_PTR id = view == currentView ? current : npp::BufferInView(view);
@@ -297,6 +317,14 @@ void Plugin::OnActiveBufferChanged() {
UpdateReadingView(view, id, active);
}
+ // Reloading a file from disk detaches the editor from the document, so no
+ // SCN_MODIFIED arrives; Notepad++ only re-activates the buffer afterwards.
+ // A reading view that kept its document renders it again to catch that.
+ if (wasReadingCurrent && currentReading.overlay.IsActive() && currentReading.host &&
+ currentReading.host->IsPageReady()) {
+ RenderTo(*currentReading.host, current, npp::ScintillaForView(currentView), false);
+ }
+
if (currentIsMarkdown && !state.reading && settings_.autoOpenPanel && !panelDismissed_ && !PanelOpen()) {
ShowPanel(true);
}
@@ -703,14 +731,14 @@ void Plugin::OnPageMessage(WebViewHost& host, PageMessage message) {
break;
case PageMessage::Type::GotoLine:
- Defer([this, view, line = message.line] {
- const int targetView = view < 0 ? npp::CurrentView() : view;
+ Defer([view, line = message.line] {
if (view >= 0) {
- buffers_[reading_[view].bufferId].reading = false;
- UpdateReadingView(view, reading_[view].bufferId, false);
- UpdateMenuChecks();
+ // Reading mode stays on until it is toggled off. The hidden
+ // editor's caret moves to the line, so toggling off lands there.
+ npp::PlaceCaretAtDocLine(npp::ScintillaForView(view), line);
+ return;
}
- const HWND scintilla = npp::ScintillaForView(targetView);
+ const HWND scintilla = npp::CurrentScintilla();
npp::GotoDocLine(scintilla, line);
::SetFocus(scintilla);
});
diff --git a/src/preview-panel-dark.ico b/src/preview-panel-dark.ico
new file mode 100644
index 0000000000000000000000000000000000000000..c994b11267b1c461efb8a8b1573979abd3109084
GIT binary patch
literal 5430
zcmeH~!A(Ok42E6G03CqCJ5>z90Wl3ih#8oGIT(Wri~uumMB>0L#CrN9$4Z(PN<@J?
z$@*i*fBx8cO;ZqNKKayw-^zyv!hd~#3ToPmT>Y94$ByCK9+vp*U6Hkx
z@-5en;n((WId%-cwtvg9<0X8}X*J%zpTrP91!}jeHiM`4%oOcBn%+m_86DWwf!5E+
zzWIJH`>xl@FX`L+rDcr1y~}cA@`ire0s7L?bDIl4*K->qFI5*I&YJdnmY79958rC#
z#^eqCv;*{o9{c_h2mL(!T+eNcyi~n@{$9`ftcw=Ei9q7y{T6*+CqBq|P#1NUki4b!
zdmVhRw?6B8{OkI@-kQ(3a_+qMAaU|)=U>zF^Q?;wx*&1#dh4U-ap=$g#}2jsoKJ5o
Y)!$o}^}YV^|7Qn+dmcpAJlsQa6_Hdz
vn2ds8Ai$`kq@?KLf}oX^l)-#Bt*or*8X~}`Y-Efg4$=n_N7oNE7u`YtX9yV6
literal 0
HcmV?d00001
diff --git a/src/preview-panel.ico b/src/preview-panel.ico
new file mode 100644
index 0000000000000000000000000000000000000000..5e45550817a90b822f87228e9ce244222996b103
GIT binary patch
literal 5430
zcmeH~F-}7<42E6G0V*cO$AUNpH-QDI2Vi021e}A7D{v5Ic1Bo2tfx>9`KjylTR)9t$cVO{M);;pr*aZ)vx(*>=?f7VTsS)6Owlf*>3uYw(ScnZX#I@r
zoA39s?|QBLlD@rPTE^(xyDT>*Z|J8Tpf4>wx4H0hJ-0FPQgspHtZBbziCOgX@U2#E
zOy1B>J3wFPvF|T&(9grq_1wnDOV#V=@AbUTx@hs62qaG4Z_)R4;)9$Abx~&t$y-{#
z*TDyS>$ASczpn4=t@)fQ=gxZ%5+|>A{xv;6&${TK3lb-`?p9`Siw8
Y{k?Tr-|G+me|8|a=UHUk)0|xR1Yj4yqyPW_
literal 0
HcmV?d00001
diff --git a/src/reading-mode-dark.ico b/src/reading-mode-dark.ico
new file mode 100644
index 0000000000000000000000000000000000000000..5e630217a382176377d97176c66a969389c5be54
GIT binary patch
literal 5430
zcmeH|QBDFu5JVgCUyiU}#9PR5^c2FkiRbVZl5hs^;HUo@$8I+-8QawEf=EDj$)xD&
z>Z+NEE@l%uv)RlX8@oO?o10nFwDp7Y_s(59=U-jX@(w5WyguHQSbZBuw=Loub2p3m
zw}qeT)$`@~>C1f7dA`;}Z=3m;WyOBfJQ;!H*Idlf%lWkqO?YsA`d#_8mi(IId)_~M
zjd?!z>iyTf_}=GV_rr%Q5N{A3gg0Yaq*=Ez~nTz*K<7?q7-bdpz
z-oQR@fd4mepWZ{f$OVVYbZPK86z07gDwiCUpWoN2#s^fo%%eZn{P-=ce^2xN>2nTH
z=`xRAoge=^M@=6ekUofx8btqF`Q&Ci)qKvu{;9b~5I*a2jeh9;D_?v-rQ0=M`N5M5
zk^`cn2GRGOKhINx_<;C>=%_*Tefj7!;)@TcbeTtgtoeDrs^%P^(q$gKI)B!y8Xr*U
zGLL@O{J%QSeQ-X7=oG3scr^!)P9ZvlY7Sn_jr@26|M3P&e%`K1@wGOa{%B3tpP!eD
J_LF0%KLBA~eVhOQ
literal 0
HcmV?d00001
diff --git a/src/reading-mode.bmp b/src/reading-mode.bmp
new file mode 100644
index 0000000000000000000000000000000000000000..3490efd1596f4676673a4708e0f110b35b2c2ea9
GIT binary patch
literal 198
zcmZ?rJ;ne5Za_){hy|dSk%0vwfkeVY4jeeZFg-+;;s5*l47DzjNGjk=Mvys-f?y!P
zsHCJQq@*OkXryE)qzt8vjevY3Lt$lQ0Y+nGgg8hZrXHjRh#3S08Ne8BJSqbKJmL_)
literal 0
HcmV?d00001
diff --git a/src/reading-mode.ico b/src/reading-mode.ico
new file mode 100644
index 0000000000000000000000000000000000000000..23630db3735b80c94e2d35fa93f432bb00fc0bcf
GIT binary patch
literal 5430
zcmeH|!A-+J5Je}1JGpW|@(Ixe94LVzXaYeR;7SXW5h;aG0u+GQ>|i}<81!u?geUxaaBStoZ8NIJ$Ka
zU!Nqn7;0
z@jdS!K4PBFy}19_^Y4BBvF|=)fp~-PApD_YsegsOKJLvPCf#d3H(M@SJ#+q^Y3jPF
z%il-iGv2^9Z-D?)TWm7m|&s>TOYy3C{B*L?pijlZXP
z|MWQrsC1b}ug;Huo};FZ4@e(GM-8I?t$cDb9%?@4VE@$IBM6^$xkf+q{*^C2pwexc
zul(T21<3)?QG@9F&Y$P0L3}{`L3Gq0`o4Vh8S%vjRJzQg-`D)SUsZDsQ0X#{UY$Sd
zRgDj*beTuLY5rfG=RP=}LUan%9K4!?N2d^-LNy1k=0<+Jf&X{|B|q<%rPQ@BoBn7`
N7oVTEllGHis8{;#ftUaQ
literal 0
HcmV?d00001
diff --git a/src/resource.h b/src/resource.h
index 267327a..38105c9 100644
--- a/src/resource.h
+++ b/src/resource.h
@@ -9,3 +9,9 @@
#define MDR_VERSION_WSTRING L"0.1.0"
#define IDD_PREVIEW_PANEL 100
+#define IDB_READING_MODE 101
+#define IDI_READING_MODE 102
+#define IDI_READING_MODE_DARK 103
+#define IDB_PREVIEW_PANEL 104
+#define IDI_PREVIEW_PANEL 105
+#define IDI_PREVIEW_PANEL_DARK 106