Fix drag and drop of files with CharInput focused

This commit is contained in:
Isaiah Odhner 2023-07-11 01:57:28 -04:00
parent 85ba439cbd
commit b885436966
2 changed files with 13 additions and 1 deletions

View File

@ -201,7 +201,6 @@ To preview ANSI art files in file managers like Nautilus, Thunar, Nemo, or Caja,
- Hitting Enter in View Bitmap mode exits the mode but may also trigger a menu item. Menu items ought to be disabled when hidden, and View Bitmap should prevent the key event from taking other actions if possible.
- Airbrush is continuous in space instead of time. It should keep spraying while the mouse stays still.
- After setting a custom zoom level, the magnifier tool will not return to the custom zoom level when un-magnifying and re-magnifying.
- Dragging and dropping files may fail if the character input box is focused. Click in the area around the canvas to remove focus.
The program has only been tested on Linux. Issues on other platforms are as-yet _unknown_ :)

View File

@ -371,6 +371,16 @@ class CharInput(Input, inherit_bindings=False):
with self.prevent(Input.Changed):
self.post_message(self.CharSelected(event.value))
def on_paste(self, event: events.Paste) -> None:
"""Called when text is pasted, OR a file is dropped on the terminal."""
# _on_paste in Input stops the event from propagating,
# but this breaks file drag and drop.
# This can't be overridden since the event system calls
# methods of each class in the MRO.
# So instead, I'll call the app's on_paste method directly.
assert isinstance(self.app, PaintApp)
self.app.on_paste(event)
def validate_cursor_position(self, cursor_position: int) -> int:
"""Force the cursor position to 0 so that it's over the character."""
return 0
@ -4871,6 +4881,9 @@ Columns: {len(palette) // 2}
def on_paste(self, event: events.Paste) -> None:
"""Called when a file is dropped into the terminal, or when text is pasted with middle click."""
# Note: this method is called directly by CharInput,
# to work around Input stopping propagation of Paste events.
# Detect file drop
def _extract_filepaths(text: str) -> list[str]:
"""Extracts escaped filepaths from text.