Skip to content

feat: allow to drag tabs into new windows and move tabs between windows - #6147

Open
Gentle-Code0 wants to merge 12 commits into
marktext:developfrom
Gentle-Code0:feat/6112-tab-window-transfer
Open

Gentle-Code0 wants to merge 12 commits into
marktext:developfrom
Gentle-Code0:feat/6112-tab-window-transfer

Conversation

@Gentle-Code0

@Gentle-Code0 Gentle-Code0 commented Nov 26, 2026 •

Copy link
Copy Markdown

Closes #6112

Summary

  • Show the tab bar by default, even when a window contains only one document, while preserving explicit visibility settings and saved layouts.
  • Support dragging tabs between windows with an insertion indicator. Dropping outside any tab strip creates a new window, with a window outline preview shown during dragging. Press Escape to cancel a tab drag.
  • Transfer documents safely through sequential stages on the source and destination sides: preparation, preparation acknowledgement, receipt, receipt acknowledgement, and history restoration. Preserve unsaved contents, file metadata, selection, scroll position, and undo/redo history. If the transfer is interrupted or fails for any reason before receipt acknowledgement, restore the tab to its original position and state.
  • Incoming tabs follow the destination window’s editing mode. Newly detached windows inherit the source window’s layout and modes.
  • Reject transfers when the destination window already contains a tab for the same file path, and display a notification.
  • Preserve the original resource base path when moving untitled document tabs.
  • Preserve existing tab drag sorting within a window.
  • Add “Move Tab to New Window” and “Move Tab to Window” context-menu actions. These are available in all environments but were primarily developed for Wayland.
  • Fix an issue where the selection toolbar occasionally appeared in the upper-left corner after the editor DOM was rebuilt. Previously, a stale selection could return an empty rectangle at (0, 0) with zero width and height. The positioning code still used this result, placing the toolbar at (0, 0) and preventing the fallback position from taking effect. The fix ignores invalid rectangles and retains the last valid position.
  • Update the history cache structure to store the corresponding block structure and Markdown alongside undo records, rather than storing undo records alone.

Basic tab splitting and merging demo
https://github.com/user-attachments/assets/56d8f657-c5ae-46e8-804f-e6831d032e73

Prevent the merging of same files (the same name and the same path)
https://github.com/user-attachments/assets/a5e09852-a59e-4637-8024-004eb6311441

Moving unsaved tab
https://github.com/user-attachments/assets/2be61c82-ed56-4fd5-b28a-89d0856af661

Moving tabs using right-click menu
https://github.com/user-attachments/assets/bbe4be96-ba51-43dd-a697-80179f46fe6d

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added (or explain why not needed)
  • pnpm run lint — no errors; 135 existing warnings remain.
  • pnpm run typecheck — passed.
  • pnpm run test:unit — 1,786 tests passed; 2 tests skipped because no executable Pandoc is available in my environment.
  • pnpm run build:unpack
  • Reviewed 39 failing Muya tests: these are existing failures unrelated to this change.
  • Manually tested on: Windows 11

Added unit tests:

  • tab-drag-drop.spec.ts: detachment when dropping over editor content, unrelated windows, the desktop, and tab-strip boundaries; local sorting; insertion into another window’s tab strip; and cancellation.
  • tab-drag-platform.spec.ts: local sorting and menu actions on Wayland.
  • tab-transfer-checkpoint.spec.ts: dirty-draft transfer and insertion order, durable recovery ownership, metadata replacement failure, missing renderers, distinct untitled documents, canonical-path conflicts, stale staged snapshots, and renderer failure during ownership commit.
  • tab-window-preview.spec.ts: multi-monitor layouts with negative coordinates and different scale factors, work-area fitting, cursor offsets, window previews, delayed loading, cleanup, and preview load failures.
  • Muya selectionPosition.spec.ts: retaining valid toolbar coordinates after DOM replacement, following valid selection coordinates during scrolling, and keeping the toolbar closed when no rendered selection bounds exist.

Added Electron E2E tests:

  • tab-window-transfer.spec.ts: unsaved-document transfers with a blank replacement source tab; source-mode detachment; drops over source and destination editor bodies; preview and actual window bounds and focus; same-path conflict protection; cancellation; cross-mode undo/redo; destination closure during preparation; untitled document origin and Save As paths; native pointer capture across windows; Escape cancellation; Wayland menu actions and local sorting; scroll restoration; preservation of trailing empty paragraphs after detachment; undo/redo history validation; failure notifications shown only in the source window; special characters in failure messages rendered as plain text; and transfers of inactive tabs.
  • selection-toolbar-position.spec.ts: keeping the toolbar near the selection after its previous DOM Range is replaced.

Updated existing tests:

  • issue-5581-tabbar-visibility.spec.ts: verify default single-tab visibility without forcing the tab bar visible through the test fixture.
  • session-recovery-multi.spec.ts: expect the tab bar to be visible after window restoration, instead of hidden.

Manual testing:

  • Tested on Windows 11. All tested scenarios passed: dragging tabs into new windows, transferring tabs between windows, moving and detaching tabs through context menus, undoing text changes after a transfer, transferring tabs between windows using different editing modes, blocking transfers when the destination already contains a tab for the same file path, and allowing transfers of files with identical names at different paths.
  • No manual testing was performed on macOS or Linux.

Notes for reviewers [optional]

The breaking changes are:

  • Default-visible tab bar: explicit visibility toggles and persisted layout choices remain respected; no state migration is required.
  • Updated history cache structure: the cache now stores the corresponding block structure and Markdown alongside undo records. This captures history more accurately and resolves a history mismatch encountered when detaching tabs into new windows. The new structure introduces some overhead. Performance was tested with a single 2 MiB document and with eight 512 KiB documents. The single large document showed a small increase in input latency, with the slowest case approximately 10% slower, or about 10 ms longer. No noticeable input slowdown was observed with multiple smaller documents. Cache memory usage increased slightly in both scenarios, by approximately 2–4 MiB.

Additional considerations:

  • Because Wayland does not provide global pointer coordinates, transferring tabs between windows and detaching tabs into new windows use the tab context-menu actions. Drag sorting within a window remains unchanged.
  • Dragging tabs to transfer or detach them is supported under X11, including when MarkText runs through X11 in a Wayland session.
  • To keep document ownership records consistent with the UI during a transfer, closeWindow requests are temporarily ignored while the transfer is in progress. Ignored requests are not replayed after the transfer completes.
  • The selection toolbar positioning defect predates this feature. However, capturing edit history during a tab transfer also rebuilds the editor DOM and can trigger the issue, so the fix is included here.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Implement the tab-bar visibility prerequisite from marktext#6112 so a fresh
window exposes its single document as a tab without requiring the user
to enable View > Tab Bar first.

- Enable the shared default window layout's tab bar.
- Match the View menu's initial checkbox to that default.
- Exercise the default single-tab visibility in the existing marktext#5581 E2E
  suite instead of forcing visibility through the test fixture.

Keep explicit visibility toggles and persisted layout choices intact.
Tab detachment and cross-window transfers remain follow-up work for marktext#6112.
No new settings, state migrations, or tab-count conditions are needed.

Validation:
- git diff --check passed.
- Reviewed the default layout, renderer visibility, and menu sync paths.
- Lint, typecheck, build and E2E execution were not completed: local tool
  dependencies were absent, and pnpm's automatic dependency provisioning
  encountered slow registry requests and was stopped before checks ran.

Refs: marktext#6112
Complete the remaining tab-window interactions from marktext#6112 on top of the
existing default-visible tab bar change.

- Drag a tab outside its window to create a window near the drop position,
  or onto another window's tab bar to insert it at the highlighted position.
  Keep local sorting, tolerate pointer drift and cancel on Escape.
- Prepare and checkpoint both renderers, acknowledge the destination's
  staged editor state, then durably migrate document and recovery ownership.
  Remove the source tab only after destination confirmation, and create a
  blank tab when the source becomes empty.
- Preserve unsaved drafts, disk baselines, save formats, read-only state,
  selection, scroll and native undo/redo history. Rebuild undo boundaries
  from portable text snapshots when crossing editor engines.
- Reject every canonical same-path conflict without overwriting either
  document. Keep separate untitled documents and same-name files at different
  locations independent.
- Retain window-scoped editing modes: incoming tabs follow their destination,
  while detached windows inherit their source layout and modes. Persist an
  untitled document's resource base so links and its initial save directory
  continue to resolve against its origin after a workspace transfer.
- Add Move Tab to New Window and Move Tab to Window to every tab context
  menu, including Shift+F10 access. Use these actions on Wayland, where
  global coordinates are unavailable, while retaining local drag sorting.
- Keep source recovery on failed preparation or metadata replacement, reject
  stale received state, and restore committed ownership after a renderer
  failure. Move file watchers with durable ownership.

Validation on Linux/X11:
- pnpm run lint: passed; 135 existing warnings, unchanged from baseline.
- pnpm run typecheck and pnpm run build:unpack: passed.
- pnpm run test: 1,764 passed, 2 skipped; includes 15 new unit tests.
- Transfer and tab-click E2E: 17 passed, including real X11 pointer input,
  Escape cancellation, cross-mode undo/redo, scroll restoration, duplicate
  conflicts, destination closure, origin paths and simulated Wayland menus.
- Expanded related E2E: 54/57 passed initially. The startup-cycle timeout
  passed in isolation (16 seconds). The two remaining failures also occurred
  before this change: session-recovery-multi.spec.ts:398 and
  startup-review-regressions.spec.ts:172. Native Windows/macOS and Wayland
  compositor sessions were not available for validation.

Refs: marktext#6112
Remove the redundant document-retention sentence from all 13 locales while keeping conflict and failure notifications.

Validation: 70 locale tests, lint (0 errors; 135 existing warnings), and desktop typecheck passed.
Create a new window when releasing over editor content, another window body or the desktop. Preserve local sorting, tab-strip insertion and cancellation.

Validation: 23 related unit tests and 19 Electron E2E tests passed; build, lint (135 existing warnings), and typecheck passed. Refs marktext#6112.
Show a non-focusing, mouse-transparent outline outside tab strips. Share fitted screen geometry with the actual detached window and remove the preview on completion or cancellation.

Validation: 31 related unit tests and 20 Electron E2E tests passed, including preview/drop bounds, focus, native pointer capture and Escape. Build, lint (135 existing warnings), and typecheck passed. Refs marktext#6112.
…cross renders

A cloned DOM Range remains live and can collapse to an empty origin rectangle when editor content is rebuilt. Preserve the last valid viewport coordinates and avoid opening the toolbar without rendered selection bounds. This positioning defect predates marktext#6112; tab-transfer history capture also rebuilds editor DOM.

Three regression tests failed before the fix. Validation: 153 Muya UI unit tests, 7 Chromium toolbar tests and 24 Electron selection/transfer E2E tests passed. Desktop and Muya typechecks, build, Muya CI lint and root lint (135 existing warnings) passed.
…ansfers

Cache the exact Muya block tree with its native history so trailing empty
paragraphs survive history export, tab switches and destination mounting.
Restore folds after history capture and validate cached Markdown against
the document newline policy before restoring history.

Propagate transfer failure reasons to the source window, including after
renderer recovery. Show error reasons as plain text without appended
advice, and suppress duplicate checkpoint notifications during transfers.

Cover saved drafts, newline policies, inactive tabs, structural undo/redo
across editor engines, notification routing and committed recovery.

Validation:
- Desktop unit tests: 1,780 passed, 2 skipped.
- Tab transfer E2E: 26 passed; session error E2E: 2 passed.
- Lint: 0 errors, 135 existing warnings; typecheck and build:unpack passed.
- Four performance comparison rounds per scenario: median input processing
  increased 2.4% for a 2 MiB document and 0.6% for eight 512 KiB documents;
  retained JS heap increased about 2.1 MiB and 4.2 MiB respectively.
- Windows x64 test ZIP packaged and integrity verified.

Refs: marktext#6112
…ew bounds

Pass the fitted position and size to the detached window constructor so
Windows can select the destination monitor DPI during native creation.
Keep automatic positioning for menu-driven transfers without a drop point.

Check initial and final window bounds against the drag preview in the
Electron regression test. Fill the four missing Polish tab-transfer locale
keys with the original English text.

Validation: 24 related unit tests and 4 Electron E2E tests passed on Linux.
The initial-bounds regression fails with the previous constructor options.
Typecheck, build:unpack and lint (0 errors, 135 existing warnings) passed.
Locale validation passed for all 14 locales. Windows 11 visual behavior
still requires verification on Windows.

Refs: marktext#6112
@Azusa-299

Copy link
Copy Markdown

The test case "uses local sorting and menus on Wayland without querying global coordinates" doesn't stub the platform — canBegin() reads process.platform directly, so it always fails on Windows and macOS. It should pin process.platform to linux like the fix below:

const originalPlatform = process.platform
Object.defineProperty(process, 'platform', { value: 'linux' })
try {
  // ... assertions
} finally {
  Object.defineProperty(process, 'platform', { value: originalPlatform })
}

@Azusa-299

Copy link
Copy Markdown

Hi! I've been testing this PR and really like the tab transfer feature. One small UX thing I noticed: when I drag a window's last tab into another window, the source window stays open with a blank tab. Would it be possible to close it automatically instead, like browsers do? Or maybe make it an option in the settings? Either way, thanks for the great work!

Azusa-299 added a commit to Azusa-299/marktext that referenced this pull request Nov 29, 2026
@Gentle-Code0

Copy link
Copy Markdown
Author

@Azusa-299 Thank you for your reply.

Yes, the source window is intentionally left open. However, I will consider making it optional.

Regarding the testing issue, I will amend it so that Wayland tests are specifically set to process.platform = linux, whilst ensuring that normal testing on other system environments remains unaffected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Drag tabs into new windows and move tabs between windows

2 participants