Philipp Hörist pushed to branch master at gajim / gajim
Commits: 2ba73b51 by mjk at 2022-12-15T00:07:16+03:00 cfix: File choosers: Fix type mismatches in accept callback
Fixes the regression introduced in 84ff274e345da79e075589d48026ec767729eb8e
- - - - - 834d4a31 by mjk at 2022-12-15T00:07:16+03:00 refactor: File choosers: Type-related changes
* Simplify accept callback signature * Allow more type checker visibility
- - - - -
8 changed files:
- gajim/gtk/avatar_selector.py - gajim/gtk/file_transfer_selector.py - gajim/gtk/filechoosers.py - gajim/gtk/filetransfer.py - gajim/gtk/groupchat_manage.py - gajim/gtk/main.py - gajim/gtk/plugins.py - gajim/gtk/profile.py
Changes:
===================================== gajim/gtk/avatar_selector.py ===================================== @@ -128,11 +128,11 @@ def prepare_crop_area(self, path: str) -> None: self._crop_area.show()
def _on_load_clicked(self, _button: Gtk.Button) -> None: - def _on_file_selected(path: str) -> None: - self.prepare_crop_area(path) + def _on_file_selected(paths: list[str]) -> None: + self.prepare_crop_area(paths[0])
AvatarChooserDialog(_on_file_selected, - transient_for=self.get_toplevel(), + transient_for=cast(Gtk.Window, self.get_toplevel()), modal=True)
def _on_drag_data_received(self,
===================================== gajim/gtk/file_transfer_selector.py ===================================== @@ -223,7 +223,9 @@ def _on_resource_selection(self, self.emit('changed', state)
def _on_choose_files_clicked(self, _button: Gtk.Button) -> None: - FileChooserDialog(self.add_files, + def _on_chosen(paths: list[str]) -> None: + self.add_files([Path(p).as_uri() for p in paths]) + FileChooserDialog(_on_chosen, select_multiple=True, transient_for=app.window, path=app.settings.get('last_send_dir') or None)
===================================== gajim/gtk/filechoosers.py ===================================== @@ -36,6 +36,8 @@
from .const import Filter
+AcceptCallbackT = Callable[[list[str]], None] +
def _require_native() -> bool: if app.is_flatpak(): @@ -60,11 +62,11 @@ def set_filter(self, filter: Gtk.FileFilter) -> None: # noqa def _on_response(self, dialog: Union[Gtk.FileChooser, Gtk.FileChooserNative], response: Gtk.ResponseType, - accept_cb: Callable[[list[str]], None], + accept_cb: AcceptCallbackT, cancel_cb: Optional[Callable[..., Any]] ) -> None: if response == Gtk.ResponseType.ACCEPT: - accept_cb(dialog.get_uris()) + accept_cb(dialog.get_filenames())
if response in (Gtk.ResponseType.CANCEL, Gtk.ResponseType.DELETE_EVENT): @@ -120,7 +122,7 @@ class NativeFileChooserDialog(Gtk.FileChooserNative, BaseFileChooser): _action = Gtk.FileChooserAction.OPEN
def __init__(self, - accept_cb: Callable[..., Any], + accept_cb: AcceptCallbackT, cancel_cb: Optional[Callable[..., Any]] = None, transient_for: Optional[Gtk.Window] = None, path: Optional[str] = None, @@ -180,7 +182,7 @@ class GtkFileChooserDialog(Gtk.FileChooserDialog, BaseFileChooser): _preview_size = (200, 200)
def __init__(self, - accept_cb: Callable[[list[str]], None], + accept_cb: AcceptCallbackT, cancel_cb: Optional[Callable[..., Any]] = None, transient_for: Optional[Gtk.Window] = None, path: Optional[str] = None, @@ -232,18 +234,9 @@ class GtkAvatarChooserDialog(BaseAvatarChooserDialog, GtkFileChooserDialog): pass
-def FileChooserDialog(*args: Any, - **kwargs: Any - ) -> Union[NativeFileOpenDialog, GtkFileOpenDialog]: - if _require_native(): - return NativeFileOpenDialog(*args, **kwargs) - return GtkFileOpenDialog(*args, **kwargs) - - -def AvatarChooserDialog(*args: Any, - **kwargs: Any - ) -> Union[NativeAvatarChooserDialog, - GtkAvatarChooserDialog]: - if _require_native(): - return NativeAvatarChooserDialog(*args, **kwargs) - return GtkAvatarChooserDialog(*args, **kwargs) +if _require_native(): + FileChooserDialog = NativeFileOpenDialog + AvatarChooserDialog = NativeAvatarChooserDialog +else: + FileChooserDialog = GtkFileOpenDialog + AvatarChooserDialog = GtkAvatarChooserDialog
===================================== gajim/gtk/filetransfer.py ===================================== @@ -467,7 +467,8 @@ def on_file_request_accepted(self, contact: BareContact, file_props: FileProp ) -> None: - def _on_accepted(account, contact, file_props, file_path): + def _on_accepted(account, contact, file_props, file_paths): + file_path = file_paths[0] if os.path.exists(file_path): app.settings.set('last_save_dir', os.path.dirname(file_path))
===================================== gajim/gtk/groupchat_manage.py ===================================== @@ -11,6 +11,7 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
+from typing import cast from typing import Any
import logging @@ -193,25 +194,25 @@ def _on_avatar_cancel_clicked(self, _button: Gtk.Button) -> None: self._ui.stack.set_visible_child_name('manage')
def _on_change_avatar_clicked(self, _button: Gtk.Button) -> None: - def _on_accept(path: str) -> None: - self._avatar_selector.prepare_crop_area(path) + def _on_accept(paths: list[str]) -> None: + self._avatar_selector.prepare_crop_area(paths[0]) self._ui.avatar_update_button.set_sensitive( self._avatar_selector.get_prepared()) self._ui.stack.set_visible_child_name('avatar') self._ui.avatar_update_button.grab_default()
AvatarChooserDialog(_on_accept, - transient_for=self.get_toplevel(), + transient_for=cast(Gtk.Window, self.get_toplevel()), modal=True)
def _on_avatar_select_file_clicked(self, _button: Gtk.Button) -> None: - def _on_accept(path: str) -> None: - self._avatar_selector.prepare_crop_area(path) + def _on_accept(paths: list[str]) -> None: + self._avatar_selector.prepare_crop_area(paths[0]) self._ui.avatar_update_button.set_sensitive( self._avatar_selector.get_prepared())
AvatarChooserDialog(_on_accept, - transient_for=self.get_toplevel(), + transient_for=cast(Gtk.Window, self.get_toplevel()), modal=True)
def _on_upload_avatar_result(self, task: Task) -> None:
===================================== gajim/gtk/main.py ===================================== @@ -525,7 +525,8 @@ def _on_preview_action(self, open_file(preview.orig_path)
elif action_name == 'preview-save-as': - def _on_ok(target: str) -> None: + def _on_ok(paths: list[str]) -> None: + target = paths[0] assert preview is not None assert preview.orig_path is not None
===================================== gajim/gtk/plugins.py ===================================== @@ -420,7 +420,8 @@ def _on_yes(): callback=_on_yes)], transient_for=self).show()
- def _try_install(zip_filename: str) -> None: + def _try_install(paths: list[str]) -> None: + zip_filename = paths[0] try: plugin = app.plugin_manager.install_from_zip(zip_filename) except PluginsystemError as er_type:
===================================== gajim/gtk/profile.py ===================================== @@ -319,18 +319,19 @@ def _on_remove_avatar(self, _button: Gtk.Button) -> None: self._new_avatar = None
def _on_edit_avatar(self, button: Gtk.Button) -> None: - def _on_file_selected(path: str) -> None: + def _on_file_selected(paths: list[str]) -> None: if self._avatar_selector is None: self._avatar_selector = AvatarSelector() self._ui.avatar_selector_box.add(self._avatar_selector)
- self._avatar_selector.prepare_crop_area(path) + self._avatar_selector.prepare_crop_area(paths[0]) self._ui.avatar_update_button.set_sensitive( self._avatar_selector.get_prepared()) self._ui.profile_stack.set_visible_child_name('avatar_selector')
AvatarChooserDialog(_on_file_selected, - transient_for=button.get_toplevel()) + transient_for=cast(Gtk.Window, + button.get_toplevel()))
def _on_cancel_update_avatar(self, _button: Gtk.Button) -> None: self._ui.profile_stack.set_visible_child_name('profile')
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/267526313a02d7e74da8bb6065db78da...