Markpad icon indicating copy to clipboard operation
Markpad copied to clipboard

Option to choose which window a file opens in when several windows are open

Open HellfireXD opened this issue 1 month ago • 13 comments

What problem would this solve?

I use window tags to keep separate groups of documents in separate windows, for example one window for a project's docs and another for personal notes. That works well until I open a .md file from Explorer or Finder (by double-clicking it or using "Open with"). The file always lands in whichever Markpad window was focused last, which is often the wrong group. I then have to use "Move to window" on the tab, or close it and reopen it from the right window.

The workaround is to remember to click the right window before opening a file from outside the app. That's easy to forget, especially when the file comes from another program.

What would you like?

An opt-in setting under Settings → Files, next to "Open files in":

When opening a file with several windows open: Open in last window (default) / Always ask me

The default keeps today's behavior exactly. With Always ask me set, opening a file from outside the app while more than one window is open shows a standard Markpad dialog in the window that would have received it:

  • It lists the open windows the same way the "Move to window" menu does: tag color and name (or "Window 2"), plus the active tab's title. The current window is marked.
  • The selection starts on the window Markpad would pick today. So Enter, or y, does exactly what happens now, and the cost is one keystroke if I don't care where it goes.
  • Use the ↑ or ↓ arrow change the selection, and Open opens the file in that window and brings it to the front.
  • Cancel, Escape, n or clicking the background cancels, and the file isn't opened. That's the same as the app's other dialogs.
  • Selecting several files at once in Explorer gives one dialog ("Open 3 files in…"), not one per file.

The dialog doesn't appear when only one window is open, when the receiving window already has the file open (it switches to that tab as it does now), for files passed at launch, or for files opened from inside Markpad (Ctrl+O, the folder tree, recent files, links, drag and drop).

A "New window" choice in the dialog would be a natural addition later, but I left it out to keep this small.

Has another app got this right?

VS Code lets you choose where files opened from outside the app go, with its window.openFilesInNewWindow setting (on / off / default). This request is the same idea with an "ask me" option, which suits Markpad's tagged windows, because the right window depends on the file.

Which platform is this for?

All platforms

Your Markpad version and how you installed it

2.8.4 x64 windows installer / 2.8.4 Linux AppImage (Arch)

HellfireXD avatar Oct 26 '26 01:10 HellfireXD

I built a working prototype of this in my fork so you can try it before deciding anything. I haven't opened a PR. I'd rather hear whether you like the idea and the design first, and I'm happy to change any of it.

Code: feature/ask-which-window compared with master

Builds: latest fork CI runs for the branch. "Test Build (No Release)" has Windows, AppImage and macOS artifacts.

How it works

Settings → Files → Open external files in: Last window (default, which is today's behaviour) or Ask me. Hovering the label explains that it only asks when more than one window is open.

Image

With Ask me, opening a file from Explorer or Finder while several windows are open shows a standard Markpad dialog in the window the file would have gone to. It lists the open windows the same way "Move to window" names them.

Image
  • The current window starts selected, so Enter or y keeps today's behaviour.
  • ↑/↓ (and Home/End) change the selection, and Open opens the file there and brings that window forward.
  • Cancel, Escape, n or clicking outside cancels, and the file isn't opened. That's the same as the other dialogs.
  • Selecting several files at once gives one dialog ("Open 3 files in…").
  • It never asks for launch files, for a file the window already has open, with only one window open, or for files opened from inside Markpad.

Implementation

  • Frontend only. Rust still picks and focuses the receiving window as it does now, and that window decides whether to ask.
  • Modal.svelte gets three optional props (confirmLabel, children, onkeydown). Existing dialogs render and behave exactly as before.
  • English strings only. Other languages fall back to English, and i18nCoverage passes.

Testing

  • npm run check, npm test and npm run test:vitest pass, as do cargo test and the build on all three platforms in my fork's CI.
  • 11 new vitest cases cover the decision logic, plus a settings round-trip test. I broke the logic seven different ways and each break made a test fail.
  • I tested the Windows build by hand and it works as described. I haven't tried macOS by hand.

Left out for now

  • A "New window" choice. A new window has no way to receive a file yet, so it would need a small Rust addition.
  • Spotting that another window already has the file open. The window list doesn't track which files each window holds.

Both could be follow-ups if you want them.

If you like the direction, I'll open a PR against master.

HellfireXD avatar Oct 26 '26 06:10 HellfireXD

Thanks for putting together a working prototype. It makes the idea much easier to judge. I haven't decided whether this belongs in Markpad yet, so a few notes first.

How other apps handle this. VS Code doesn't ask. Its window.openFilesInNewWindow setting's off value is described as "Files will open in the window with the files' folder open or the last active window" (source). So it guesses the right window from where the file lives, and only falls back to the last active one. Your request is closer to "pick it yourself every time", which I haven't seen in a comparable app. Markpad windows don't have a folder they belong to, so we can't copy VS Code directly. Still, I'd rather look at a way to guess the window before adding a dialog that can appear on every open.

Two questions about the prototype.

  1. If a different window already has the file open, the dialog still appears instead of going straight to that window. To me that's the one case where the answer is obvious. Is there a reason it asks there too, or is it just because the window list doesn't know about open files yet?
  2. Multi-select relies on a 250 ms window to collect files into one dialog. What happens with a lot of files on a slow machine, where the deliveries are spread out more? Do some of them end up in a second dialog, or opened without asking? Did you test that case?

PathGao avatar Oct 26 '26 06:10 PathGao

A few more apps, for comparison:

  • Obsidian added opening files from outside a vault in 1.14, and its changelog says "The file opens in your current window" (changelog). No prompt.
  • Chrome opens a link or file handed over by another app as a new tab in the last active window. No prompt. That's what Markpad does today.
  • Typora has no tabs on Windows and Linux (shortcut list, "New Tab: Not Supported"), so as far as I can tell each file gets its own window and the question never comes up.

So none of these asks. They either guess (VS Code by folder) or use the current or last active window.

PathGao avatar Oct 26 '26 06:10 PathGao

Thanks for looking at it so quickly.

To answer the questions you brought up,


1. A file already open in another window. This is an existing bug, and a gap the prototype needs to close.

The existing bug: in the current release (2.8.4), with a file open in one window, focus another window and open the same file from Explorer. It opens a second time, in the window you just focused. That gives two independent buffers on one file, each with its own unsaved edits and auto-save, which is what the one-tab-per-file rule prevents inside a single window.

The gap: the prototype has the same blind spot, which is why the dialog appears in that case. The receiving window can't see other windows' tabs, because the window list only knows each window's tag, active tab title and tab count.

The fix is the same for both: each window also reports its open files, so a file that's already open somewhere goes straight to that window, in either mode and without a dialog.


2. Multi-select timing. The batching is for one action: selecting several files in Explorer and choosing Open. The "%1" open command makes Explorer start one Markpad process per file. Each one hands its path to the running instance and exits, so the paths arrive spread out by however long those processes take to start. No file contents are read until you've chosen a window. That spread only affects when the dialog appears: a path that arrives after the 250 ms still joins the dialog while it's open (the "Open N files in…" count updates). A path that arrives after you've already answered starts a second dialog rather than opening anywhere unasked.

I tested it by launching the prototype once per file, the way Explorer's "%1" command does:

  • 30 files at once: the paths arrived over about 360 ms, so some came after the 250 ms. All 30 landed in one dialog.
  • 30 more, launched 100 ms apart to imitate a slow machine: the paths arrived over about 3.2 seconds. Each one joined the dialog that was already open, and it ended at "Open 60 files in…" because I hadn't answered the first batch yet.

On how other apps handle it. The ones that don't ask also don't have groups that aren't tied to a place:

  • VS Code can guess because a window is a folder or workspace, so the file's location says where it belongs. For a file opened from outside, findWindowOnFile in windowsFinder.ts tries, in order:

    1. A multi-root workspace window whose folders contain the file. The first one found wins.
    2. A single-folder window whose folder contains the file. If several do, the deepest folder wins.
    3. Otherwise, the last active window.

    It never asks, and it doesn't check which files a window already has open. It also makes the choice once per batch: it looks at the first file and sends all of them to the same window, the same way the dialog asks once for a multi-select.

  • Browsers can group tabs inside one window in multiple groups so there is no need for multiple windows.

Markpad's window tags group documents by what they're for, not where they live. A "Work" window can hold files from three projects and Downloads. There's nothing in a file's path to guess from, so I don't think a VS Code-style guess would place them reliably without tying tags to folders, and that's a much bigger change. That's why I went for an opt-in that's off by default: people who don't use tags never see it.

That said, the "already open in another window" rule from point 1 is a guess everyone benefits from, and it can apply in both modes.

HellfireXD avatar Oct 26 '26 07:10 HellfireXD

Thanks, that's a thorough answer, and you're right about point 1. I checked: duplicate detection only looks at the receiving window's own tabs, so the same file can end up open in two windows with separate unsaved edits. That's a bug on its own, separate from this feature.

Could you split this into two PRs?

  1. The bug fix first: if a file opened from outside is already open in another window, switch to that window and tab instead of opening it again. No setting, same behaviour for everyone.
  2. The "Ask me" option on top of it, as a separate PR.

That way the fix can land on its own, and the dialog only has to handle files that aren't open anywhere yet. I'll review both.

The multi-select testing answers my question, thanks.

PathGao avatar Oct 26 '26 11:10 PathGao

One more thing from current master that affects both PRs. Since #990 a launch reopens every window from the last session, and since #1001 files passed at launch always go to the main window. So double-clicking a file while Markpad is closed brings back all the tagged windows, but the file always lands in the main one.

  • For the bug-fix PR, please cover that path too. If a restored window already has the file, launch should switch to it instead of opening a second copy in the main window.
  • For the "Ask me" PR, launch files are now the case where there is a real choice, so it's worth deciding whether they should be asked about as well.

Several test reorganisations are landing on master today (#1030 already split up scripts/settingsPersistence.spec.ts), so rebasing the current branch would mean fighting conflicts. Could you wait until tomorrow, start from a fresh master, and open both PRs from there? If you'd rather not redo it, that's fine too, just say so and I'll take it over.

PathGao avatar Oct 26 '26 11:10 PathGao

I can certainly wait until tomorrow to proceed. I will make sure to rebase and replay my edits then. Additionally, I would be happy to look into the bug and address it in a separate pull request as you suggested.

HellfireXD avatar Oct 26 '26 13:10 HellfireXD

Thank you very much. You can start now

PathGao avatar Oct 27 '26 01:10 PathGao

@PathGao I’ve opened sftwrdotdev/Markpad#1038 for the fix: it covers a file opened while Markpad is running and launch files that a reopened window is restoring. Next I’ll rebuild the “Ask me” option on top of that branch and open it as a separate PR once sftwrdotdev/Markpad#1038 is merged.

The workflows are waiting for approval to run, since this is my first contribution. My fork’s runs on the same commit passed on all three platforms.

HellfireXD avatar Oct 27 '26 20:10 HellfireXD

@PathGao I’ve opened https://github.com/sftwrdotdev/Markpad/pull/1040 for the “Ask me” option. While testing it I found a separate bug at launch: a file opened when Markpad starts could end up hidden behind a reopened window. It happens with either setting, so I fixed it in the same PR and described it under “Also fixed”.

HellfireXD avatar Oct 28 '26 06:10 HellfireXD

at me at one place is good enough,thank you。pr will be merged today and ship in few days

PathGao avatar Oct 28 '26 06:10 PathGao

20. at me at one place is good enough,thank you。pr will be merged today and ship in few days

Apologies, its late for me an i was adding the comment on the wrong threads.

HellfireXD avatar Oct 28 '26 06:10 HellfireXD

This is in 2.9.0: https://github.com/sftwrdotdev/Markpad/releases/tag/v2.9.0. Settings → Files → "Open external files in" can be "Last window" or "Ask me". A file that is already open in another window now switches to that window instead of opening twice.

PathGao avatar Oct 29 '26 03:10 PathGao