Fix in-app editor: typing was triggering Browser's global keybinds (Backspace navigated up, Enter reopened the file

This commit is contained in:
sarah committed 2026-09-29 06:50:40 +02:00
1 parent 117532e017
commit 65f5b89908
2 files changed
+125 -4

No files matched your search

+23
View File
@@ -48,10 +48,18 @@ class EditorView(ft.Container):
FontFamily: str,
OnClose: Callable[[], None],
OnNotify: Callable[[str, str], None],
OnFocusChange: Optional[Callable[[bool], None]] = None,
):
self.EntryPath = EntryPath
self.OnClose = OnClose
self.OnNotify = OnNotify
# Browser's Enter/Backspace/Delete/etc shortcuts are a page-level
# keydown listener (see Browser._OnKeyboardEvent), not scoped to
# whatever has focus — without this, typing in the TextField below
# was also navigating directories (Backspace) or re-opening the file
# (Enter) out from under you. Same ShortcutsPaused mechanism Picker.py
# already uses for its own inline (non-dialog) text field.
self.OnFocusChange = OnFocusChange
self.Language = LanguageFor(EntryPath)
try:
@@ -73,6 +81,8 @@ class EditorView(ft.Container):
border=ft.InputBorder.NONE,
text_style=CodeFont,
on_change=self._OnTextChanged,
on_focus=lambda e: self._SetFocused(True),
on_blur=lambda e: self._SetFocused(False),
)
self.MarkdownView = ft.Markdown(
value=FencedBlock(self.OriginalContent, self.Language),
@@ -118,6 +128,10 @@ class EditorView(ft.Container):
padding=12,
)
def _SetFocused(self, Focused: bool) -> None:
if self.OnFocusChange is not None:
self.OnFocusChange(Focused)
def _OnTextChanged(self, Event: ft.ControlEvent) -> None:
if not self.IsDirty:
self.IsDirty = True
@@ -133,6 +147,11 @@ class EditorView(ft.Container):
if self.IsEditing:
self.Body.content = self.TextField
self.ToggleButton.content = "View"
# Don't wait on Flet's own focus event for this — swapping the
# TextField into Body.content isn't guaranteed to fire on_focus
# (or fire it before the next keypress), so shortcuts would stay
# live for a keystroke or two right after clicking Edit.
self._SetFocused(True)
else:
# Re-render the highlighted view from whatever's currently typed
# (even if unsaved), so Edit -> View -> Edit doesn't discard it.
@@ -142,6 +161,10 @@ class EditorView(ft.Container):
self.PlainView.value = self.TextField.value
self.Body.content = self.PlainView if self.SkipHighlight else self.MarkdownView
self.ToggleButton.content = "Edit"
# Same reasoning in reverse: the TextField leaving the tree isn't
# guaranteed to fire on_blur, so explicitly un-pause here rather
# than risk shortcuts staying dead after leaving Edit mode.
self._SetFocused(False)
self.Body.update()
self.ToggleButton.update()