Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ so even multi-megabyte docs stay smooth). Pick **Language → Markdown** on any
### <img src="docs/assets/icons/book.svg" width="26" height="26" alt=""> 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.

<table>
Expand Down Expand Up @@ -263,6 +265,8 @@ Windows 11; on Windows 10 it comes with Edge).
## <img src="docs/assets/icons/keyboard.svg" width="30" height="30" alt=""> 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 |
|---|---|
Expand Down Expand Up @@ -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!

Expand Down
7 changes: 7 additions & 0 deletions src/MarkdownReader.rc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/NppApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<WPARAM>(line));
const auto position = Sci(sci, SCI_POSITIONFROMLINE, static_cast<WPARAM>(line));
if (position >= 0) Sci(sci, SCI_SETEMPTYSELECTION, static_cast<WPARAM>(position)); // does not scroll
}

} // namespace mdr::npp
1 change: 1 addition & 0 deletions src/NppApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 34 additions & 6 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LPARAM>(&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:
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
});
Expand Down
Binary file added src/preview-panel-dark.ico
Binary file not shown.
Binary file added src/preview-panel.bmp
Binary file not shown.
Binary file added src/preview-panel.ico
Binary file not shown.
Binary file added src/reading-mode-dark.ico
Binary file not shown.
Binary file added src/reading-mode.bmp
Binary file not shown.
Binary file added src/reading-mode.ico
Binary file not shown.
6 changes: 6 additions & 0 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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