Skip to content

Commit

Permalink
Work around broken GDK DnD API
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
kra-mo committed Feb 15, 2024
1 parent 093e709 commit fc9dd5f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
9 changes: 4 additions & 5 deletions hyperplane/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later

"""An item representing a file to be set up through a `GtkSignalListItemFactory`."""
import logging
from pathlib import Path
from typing import Any, Optional

Expand Down Expand Up @@ -353,17 +354,15 @@ def __drag_end(
self, _src: Gtk.DragSource, _drag: Gdk.Drag, delete_data: bool
) -> None:
self.page.view.set_enable_rubberband(True)
if delete_data and shared.last_drop_internal:
for gfile in self.dragged_gfiles:
rm(gfile)

shared.last_drop_internal = False
if delete_data:
# This is to prevent data loss
logging.debug("Dropped data should be deleted but won't be.")

def __drag_cancel(
self, _src: Gtk.DragSource, _drag: Gdk.Drag, _reason: Gdk.DragCancelReason
) -> None:
self.page.view.set_enable_rubberband(True)
shared.last_drop_internal = False

def __dir_children_cb(self, gfile: Gio.File, result: Gio.AsyncResult) -> None:
try:
Expand Down
4 changes: 0 additions & 4 deletions hyperplane/shared.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ search = "" # pylint: disable=invalid-name
right_clicked_file = None # pylint: disable=invalid-name
undo_queue = {}

# This is so Hyperplane only deletes files moved during DnD if they were moved whithin the app.
# See https://bugzilla.mozilla.org/show_bug.cgi?id=1877555
last_drop_internal = False # pylint: disable=invalid-name

grid_view = state_schema.get_boolean("grid-view")
show_hidden = state_schema.get_boolean("show-hidden")
sort_by = state_schema.get_string("sort-by")
Expand Down
37 changes: 23 additions & 14 deletions hyperplane/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
get_gfile_display_name,
get_gfile_path,
get_paste_gfile,
move,
trash,
validate_name,
)
Expand Down Expand Up @@ -1053,7 +1054,7 @@ def __drop(

def __drop_file_list(self, file_list: Gdk.FileList, action: Gdk.DragAction) -> bool:
dst = self.get_visible_page().get_dst()
move = action == Gdk.DragAction.MOVE
should_move = action == Gdk.DragAction.MOVE

files = []

Expand All @@ -1070,22 +1071,30 @@ def __drop_file_list(self, file_list: Gdk.FileList, action: Gdk.DragAction) -> b
logging.warning("Cannot drop files: Can't get child for display name.")
return False

try:
copy(src, child)
except FileExistsError:
if should_move:
try:
copy(src, get_paste_gfile(child))
except (FileExistsError, FileNotFoundError) as error:
logging.warning("Cannot drop files: %s", error)
return False

files.append((src, child) if move else child)

shared.undo_queue[time()] = ("move" if move else "copy", files)
move(src, child)
except FileExistsError:
# TODO: Export this toast to a public method
self.send_toast(
_("A folder with that name already exists")
if src.query_file_type(Gio.FileQueryInfoFlags.NONE)
== Gio.FileType.DIRECTORY
else _("A file with that name already exists")
)
else:
try:
copy(src, child)
except FileExistsError:
try:
copy(src, get_paste_gfile(child))
except (FileExistsError, FileNotFoundError) as error:
logging.warning("Cannot drop files: %s", error)
return False

if move:
shared.last_drop_internal = True
files.append((src, child) if should_move else child)

shared.undo_queue[time()] = ("move" if should_move else "copy", files)
return True

def __drop_texture(self, texture: Gdk.Texture) -> None:
Expand Down

0 comments on commit fc9dd5f

Please sign in to comment.