Skip to content

fix: never focus the editor while it is hidden - #10

Open
bero wants to merge 1 commit into
GDKsoftware:mainfrom
bero:fix/editor-focus-in-preview-mode
Open

bero wants to merge 1 commit into
GDKsoftware:mainfrom
bero:fix/editor-focus-in-preview-mode

Conversation

@bero

@bero bero commented Sep 16, 2026

Copy link
Copy Markdown

Independent of #7, #8 and #9. Cut from main and touching different lines, so it can merge in any order.

The bug

In preview only mode the studio raises:

Project Markdown4DStudioVCL.exe raised exception class EInvalidOperation
with message 'Cannot focus a disabled or invisible window'.
Vcl.Forms.TCustomForm.SetActiveControl
Vcl.Forms.TCustomForm.FocusControl
Vcl.Controls.TWinControl.SetFocus
Markdown4DStudioVCL.Main.TMarkdown4DStudioVCLForm.HandleBoldClick

ApplyViewMode hides the editor in preview only mode, and the VCL refuses to focus a control that is not visible.

Reachable entry points, all in preview only mode:

  • The bold, italic, link and code block buttons.
  • ExecuteFormatCommand, so every palette formatting entry and keyboard shortcut, including headings, lists, quote, strikethrough and insert table.
  • Clicking an entry in the contents list.
  • Six palette commands: undo, redo, select all, indent, outdent and delete word left.

The fix

The codebase already had the right idiom. Zen mode, closing the find bar and closing the palette guarded with if mdEditor.CanFocus then. Those guards were simply missing everywhere else.

Every path that returns focus to the editor now goes through one FocusEditor helper carrying that guard, so no call site can reintroduce the fault.

The VCL had the guard in 4 places out of 16. The FMX studio had it in none of its 17, so it could also raise when leaving zen mode and when closing the command palette.

Testing

This is form and focus behaviour with no pure logic to unit test, so it is verified by driving the app rather than by adding to the DUnitX suite.

  • Both studios build clean on Win32 Debug with Delphi 12 Athens.
  • Ran the VCL studio, switched to preview only and clicked Bold. No exception, no error dialog, the process stays responsive, and the title bar shows the document as modified, so the command still executed.

Related

While in preview only mode a formatting command still applies at the editor's last caret position, which you cannot see. Mapping a selection made in the preview back to a source range is the proper answer to that, and is being built separately.

Preview only mode hides the editor, and the VCL refuses to focus a control
that is not visible. Clicking Bold, Italic, Link or Code then raised
EInvalidOperation with 'Cannot focus a disabled or invisible window', and so
did every command on the shared format path, clicking an entry in the
contents list, and the palette's undo, redo, select all, indent, outdent and
delete word.

Route every path that returns focus to the editor through a FocusEditor
helper carrying the CanFocus guard that the zen mode code already used. The
VCL applied that guard in four places out of sixteen. The FMX studio applied
it nowhere, so it could raise from leaving zen mode and from closing the
command palette as well.
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.

1 participant