Automatically add .ans file extension when bare filename is entered

This applies to Save As and Copy To dialogs, but not the CLI.
This commit is contained in:
Isaiah Odhner 2023-05-16 14:26:18 -04:00
parent cdf30c49f9
commit 400c6b4251
2 changed files with 24 additions and 2 deletions

View File

@ -19,6 +19,7 @@ class FileDialogWindow(DialogWindow):
file_name: str = "", file_name: str = "",
selected_file_path: str | None, selected_file_path: str | None,
handle_selected_file_path: Callable[[str], None], handle_selected_file_path: Callable[[str], None],
auto_add_default_extension: str = "",
submit_label: str, submit_label: str,
**kwargs: Any, **kwargs: Any,
) -> None: ) -> None:
@ -33,6 +34,7 @@ class FileDialogWindow(DialogWindow):
"""Last highlighted item in the directory tree""" """Last highlighted item in the directory tree"""
self._expanding_directory_tree: bool = False self._expanding_directory_tree: bool = False
"""Flag to prevent setting the filename input when initially expanding the directory tree""" """Flag to prevent setting the filename input when initially expanding the directory tree"""
self._auto_add_default_extension: str = auto_add_default_extension
def handle_button(self, button: Button) -> None: def handle_button(self, button: Button) -> None:
"""Called when a button is clicked or activated with the keyboard.""" """Called when a button is clicked or activated with the keyboard."""
@ -47,6 +49,8 @@ class FileDialogWindow(DialogWindow):
file_path = os.path.join(self._directory_tree_selected_path, filename) file_path = os.path.join(self._directory_tree_selected_path, filename)
else: else:
file_path = filename file_path = filename
if os.path.splitext(file_path)[1] == "":
file_path += self._auto_add_default_extension
self.handle_selected_file_path(file_path) self.handle_selected_file_path(file_path)
def on_mount(self) -> None: def on_mount(self) -> None:
@ -120,7 +124,14 @@ class OpenDialogWindow(FileDialogWindow):
**kwargs: Any, **kwargs: Any,
) -> None: ) -> None:
"""Initialize the dialog window.""" """Initialize the dialog window."""
super().__init__(*children, submit_label=_("Open"), file_name="", selected_file_path=selected_file_path, handle_selected_file_path=handle_selected_file_path, **kwargs) super().__init__(
*children,
submit_label=_("Open"),
file_name="",
selected_file_path=selected_file_path,
handle_selected_file_path=handle_selected_file_path,
**kwargs
)
class SaveAsDialogWindow(FileDialogWindow): class SaveAsDialogWindow(FileDialogWindow):
"""A dialog window that lets the user select a file to save to. """A dialog window that lets the user select a file to save to.
@ -135,7 +146,16 @@ class SaveAsDialogWindow(FileDialogWindow):
file_name: str = "", file_name: str = "",
selected_file_path: str | None, selected_file_path: str | None,
handle_selected_file_path: Callable[[str], None], handle_selected_file_path: Callable[[str], None],
auto_add_default_extension: str = "",
**kwargs: Any, **kwargs: Any,
) -> None: ) -> None:
"""Initialize the dialog window.""" """Initialize the dialog window."""
super().__init__(*children, submit_label=_("Save"), file_name=file_name, selected_file_path=selected_file_path, handle_selected_file_path=handle_selected_file_path, **kwargs) super().__init__(
*children,
submit_label=_("Save"),
file_name=file_name,
selected_file_path=selected_file_path,
handle_selected_file_path=handle_selected_file_path,
auto_add_default_extension=auto_add_default_extension,
**kwargs
)

View File

@ -2342,6 +2342,7 @@ class PaintApp(App[None]):
handle_selected_file_path=handle_selected_file_path, handle_selected_file_path=handle_selected_file_path,
selected_file_path=self.file_path, selected_file_path=self.file_path,
file_name=os.path.basename(self.file_path or _("Untitled")), file_name=os.path.basename(self.file_path or _("Untitled")),
auto_add_default_extension=".ans",
) )
await self.mount(window) await self.mount(window)
await saved_future await saved_future
@ -2384,6 +2385,7 @@ class PaintApp(App[None]):
title=_("Copy To"), title=_("Copy To"),
handle_selected_file_path=handle_selected_file_path, handle_selected_file_path=handle_selected_file_path,
selected_file_path=self.file_path, # TODO: only the directory selected_file_path=self.file_path, # TODO: only the directory
auto_add_default_extension=".ans",
) )
self.mount(window) self.mount(window)