From 4dc832795338da4c059d0923eaa53e33aca4dc99 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Fri, 16 Aug 2024 09:14:15 +0200 Subject: [PATCH] Use drag&drop gestures to move multi element options in settings Mostly uses the same code as the playlist reorder code does. --- melon/widgets/preferencerow.py | 101 +++++++++++++++++++++++++-------- po/de.po | 38 ++++++------- po/fa.po | 38 ++++++------- po/melon.pot | 32 ++++------- po/nl.po | 38 ++++++------- 5 files changed, 142 insertions(+), 105 deletions(-) diff --git a/melon/widgets/preferencerow.py b/melon/widgets/preferencerow.py index 95baacf..4ccde26 100644 --- a/melon/widgets/preferencerow.py +++ b/melon/widgets/preferencerow.py @@ -1,4 +1,4 @@ -from gi.repository import Gtk, Adw +from gi.repository import Gdk, Gtk, Adw, GObject from gettext import gettext as _ from copy import deepcopy as copy @@ -129,38 +129,89 @@ class MultiRow(Adw.PreferencesRow): self.adder = IconButton(_("Add"), "list-add-symbolic") self.adder.connect("clicked", lambda x: self.open_add()) self.inner.set_header_suffix(self.adder) - counter = 0 - length = len(self.values) # create items list - for item in self.values: + for index, item in enumerate(self.values): row = Adw.ActionRow() row.set_title(item) - actions = Gtk.Box() - # move up button - # not shown for first element - if counter > 0: - move_up = IconButton("", "go-up-symbolic") - move_up.set_tooltip_text(_("Move up")) - move_up.add_css_class("circular") - move_up.connect("clicked", pass_me(self.move_up, item, counter)) - actions.append(move_up) - # move down button - # not shown for last element - if counter < length - 1: - move_down = IconButton("", "go-down-symbolic") - move_down.set_tooltip_text(_("Move down")) - move_down.add_css_class("circular") - move_down.connect("clicked", pass_me(self.move_down, item, counter)) - actions.append(move_down) + drag_source = Gtk.DragSource() + drag_source.set_content( + Gdk.ContentProvider.new_union( + [ + Gdk.ContentProvider.new_for_value(index), + Gdk.ContentProvider.new_for_value(row), + ] + ) + ) + drag_source.set_actions(Gdk.DragAction.MOVE) + drag_source.connect("drag-begin", pass_me(self.entry_drag_begin, row)) + drag_source.connect("drag-end", self.entry_drag_end) + drag_source.connect("drag-cancel", self.entry_drag_cancel) + row.add_controller(drag_source) + drop_target = Gtk.DropTarget.new(GObject.TYPE_NONE, Gdk.DragAction.MOVE) + drop_target.set_gtypes([int, Adw.ActionRow]) + drop_target.connect("drop", pass_me(self.entry_drop, row, index)) + drop_target.connect("motion", pass_me(self.entry_drop_motion, row)) + drop_target.connect("leave", pass_me(self.entry_drop_leave, row)) + row.add_controller(drop_target) + # remove button remove = IconButton("", "list-remove-symbolic") remove.set_tooltip_text(_("Remove from list")) remove.add_css_class("circular") - remove.connect("clicked", pass_me(self.confirm_delete, item, counter)) - actions.append(remove) - row.add_suffix(actions) + remove.connect("clicked", pass_me(self.confirm_delete, item, index)) + row.add_suffix(remove) + self.inner.add(row) - counter += 1 + + def entry_drag_begin(self, source, drag, widget): + preview = Gtk.WidgetPaintable.new(widget) + source.set_icon(preview, 0, 0) + + def entry_drag_end(self, source, drag, delete_data): + source.set_icon(None, 0, 0) + + def entry_drag_cancel(self, source, drag, reason): + source.set_icon(None, 0, 0) + + def entry_drop(self, target, value, x, y, widget, index): + height = widget.get_height() + if value == index: + # do nothing + # because the item does not need to be moved + self.do_update() + return + if y > height / 2: + # insert below + target_index = index * 2 + 2 + else: + # insert above + target_index = index * 2 + # create database containing all entries, but spaced two apart + # this way, we can easily fit in new entries + db = dict([[i * 2 + 1, x] for i, x in enumerate(self.values)]) + # remove dropped entry + entry = db.pop(value * 2 + 1) + # insert entry at new position + db[target_index] = entry + # build updated video list + plist = [x[1] for x in sorted(db.items(), key=lambda x: x[0])] + self.values = plist + self.notify() + + def entry_drop_motion(self, target, x, y, widget): + height = widget.get_height() + indicator_distance = 10 + if y > height / 2: + widget.set_margin_top(0) + widget.set_margin_bottom(indicator_distance) + else: + widget.set_margin_top(indicator_distance) + widget.set_margin_bottom(0) + return Gdk.DragAction.MOVE + + def entry_drop_leave(self, target, widget): + widget.set_margin_top(0) + widget.set_margin_bottom(0) def notify(self): if self.callback is not None: diff --git a/po/de.po b/po/de.po index 4e8f1f6..964dbe7 100644 --- a/po/de.po +++ b/po/de.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: Melon 0.1.2\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-15 15:36+0200\n" +"POT-Creation-Date: 2024-08-16 09:14+0200\n" "PO-Revision-Date: 2024-04-20 14:18+0000\n" "Last-Translator: hurzelchen \n" @@ -510,12 +510,12 @@ msgstr "" #: ../melon/playlist/__init__.py:282 ../melon/playlist/create.py:49 #: ../melon/playlist/create.py:104 ../melon/playlist/pick.py:82 -#: ../melon/playlist/pick.py:120 ../melon/widgets/preferencerow.py:185 -#: ../melon/widgets/preferencerow.py:224 +#: ../melon/playlist/pick.py:120 ../melon/widgets/preferencerow.py:236 +#: ../melon/widgets/preferencerow.py:275 msgid "Cancel" msgstr "Abbruch" -#: ../melon/playlist/__init__.py:284 ../melon/widgets/preferencerow.py:226 +#: ../melon/playlist/__init__.py:284 ../melon/widgets/preferencerow.py:277 msgid "Delete" msgstr "Löschen" @@ -544,7 +544,7 @@ msgstr "Unbenannte Wiedergabeliste" msgid "Do not create playlist" msgstr "Wiedergabeliste nicht erstellen" -#: ../melon/playlist/create.py:52 ../melon/widgets/preferencerow.py:188 +#: ../melon/playlist/create.py:52 ../melon/widgets/preferencerow.py:239 msgid "Create" msgstr "Erstellen" @@ -821,39 +821,31 @@ msgstr "" msgid "Add" msgstr "Hinzufügen" -#: ../melon/widgets/preferencerow.py:143 -msgid "Move up" -msgstr "Nach oben verschieben" - -#: ../melon/widgets/preferencerow.py:151 -msgid "Move down" -msgstr "Nach unten verschieben" - -#: ../melon/widgets/preferencerow.py:157 +#: ../melon/widgets/preferencerow.py:159 msgid "Remove from list" msgstr "Von der Liste entfernen" -#: ../melon/widgets/preferencerow.py:172 +#: ../melon/widgets/preferencerow.py:223 msgid "Add Item" msgstr "Eintrag hinzufügen" -#: ../melon/widgets/preferencerow.py:175 +#: ../melon/widgets/preferencerow.py:226 msgid "Create a new list entry" msgstr "Neuen Listeneintrag erstellen" -#: ../melon/widgets/preferencerow.py:176 +#: ../melon/widgets/preferencerow.py:227 msgid "Enter the new value here" msgstr "Gibt den neuen Wert hier ein" -#: ../melon/widgets/preferencerow.py:179 +#: ../melon/widgets/preferencerow.py:230 msgid "Value" msgstr "Wert" -#: ../melon/widgets/preferencerow.py:219 +#: ../melon/widgets/preferencerow.py:270 msgid "Delete Item?" msgstr "" -#: ../melon/widgets/preferencerow.py:221 +#: ../melon/widgets/preferencerow.py:272 msgid "" "Do you really want to delete this item? You won't be able to restore it " "afterwards" @@ -863,6 +855,12 @@ msgstr "" msgid "Stream videos on the go" msgstr "Videos unterwegs streamen" +#~ msgid "Move up" +#~ msgstr "Nach oben verschieben" + +#~ msgid "Move down" +#~ msgstr "Nach unten verschieben" + #~ msgid "Do you really want to delete this playlist?" #~ msgstr "Möchtest du diese Wiedergabeliste wirklich löschen?" diff --git a/po/fa.po b/po/fa.po index c2f6c22..0348faf 100644 --- a/po/fa.po +++ b/po/fa.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: Melon 0.1.2\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-15 15:36+0200\n" +"POT-Creation-Date: 2024-08-16 09:14+0200\n" "PO-Revision-Date: 2024-05-30 09:18+0000\n" "Last-Translator: sohrabbehdani \n" @@ -507,12 +507,12 @@ msgstr "" #: ../melon/playlist/__init__.py:282 ../melon/playlist/create.py:49 #: ../melon/playlist/create.py:104 ../melon/playlist/pick.py:82 -#: ../melon/playlist/pick.py:120 ../melon/widgets/preferencerow.py:185 -#: ../melon/widgets/preferencerow.py:224 +#: ../melon/playlist/pick.py:120 ../melon/widgets/preferencerow.py:236 +#: ../melon/widgets/preferencerow.py:275 msgid "Cancel" msgstr "لغو" -#: ../melon/playlist/__init__.py:284 ../melon/widgets/preferencerow.py:226 +#: ../melon/playlist/__init__.py:284 ../melon/widgets/preferencerow.py:277 msgid "Delete" msgstr "حذف" @@ -541,7 +541,7 @@ msgstr "سیاههٔ پخش بدون نام" msgid "Do not create playlist" msgstr "لیست پخش را ایجاد نکن." -#: ../melon/playlist/create.py:52 ../melon/widgets/preferencerow.py:188 +#: ../melon/playlist/create.py:52 ../melon/widgets/preferencerow.py:239 msgid "Create" msgstr "ایجاد" @@ -809,39 +809,31 @@ msgstr "" msgid "Add" msgstr "افزودن" -#: ../melon/widgets/preferencerow.py:143 -msgid "Move up" -msgstr "انتقال به بالا" - -#: ../melon/widgets/preferencerow.py:151 -msgid "Move down" -msgstr "انتقال به پایین" - -#: ../melon/widgets/preferencerow.py:157 +#: ../melon/widgets/preferencerow.py:159 msgid "Remove from list" msgstr "حذف از لیست" -#: ../melon/widgets/preferencerow.py:172 +#: ../melon/widgets/preferencerow.py:223 msgid "Add Item" msgstr "افزودن مورد" -#: ../melon/widgets/preferencerow.py:175 +#: ../melon/widgets/preferencerow.py:226 msgid "Create a new list entry" msgstr "ایجاد یک ورودی لیست جدید" -#: ../melon/widgets/preferencerow.py:176 +#: ../melon/widgets/preferencerow.py:227 msgid "Enter the new value here" msgstr "مقادیر را اینجا وارد کنید." -#: ../melon/widgets/preferencerow.py:179 +#: ../melon/widgets/preferencerow.py:230 msgid "Value" msgstr "مقدار" -#: ../melon/widgets/preferencerow.py:219 +#: ../melon/widgets/preferencerow.py:270 msgid "Delete Item?" msgstr "" -#: ../melon/widgets/preferencerow.py:221 +#: ../melon/widgets/preferencerow.py:272 msgid "" "Do you really want to delete this item? You won't be able to restore it " "afterwards" @@ -851,6 +843,12 @@ msgstr "" msgid "Stream videos on the go" msgstr "ویدیو‌هارا استریم کن" +#~ msgid "Move up" +#~ msgstr "انتقال به بالا" + +#~ msgid "Move down" +#~ msgstr "انتقال به پایین" + #~ msgid "Do you really want to delete this playlist?" #~ msgstr "آیا از حذف این لیست‌پخش اطمینان دارید؟" diff --git a/po/melon.pot b/po/melon.pot index 85dd99a..e67757a 100644 --- a/po/melon.pot +++ b/po/melon.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Melon 0.2.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-15 15:36+0200\n" +"POT-Creation-Date: 2024-08-16 09:14+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -492,12 +492,12 @@ msgstr "" #: ../melon/playlist/__init__.py:282 ../melon/playlist/create.py:49 #: ../melon/playlist/create.py:104 ../melon/playlist/pick.py:82 -#: ../melon/playlist/pick.py:120 ../melon/widgets/preferencerow.py:185 -#: ../melon/widgets/preferencerow.py:224 +#: ../melon/playlist/pick.py:120 ../melon/widgets/preferencerow.py:236 +#: ../melon/widgets/preferencerow.py:275 msgid "Cancel" msgstr "" -#: ../melon/playlist/__init__.py:284 ../melon/widgets/preferencerow.py:226 +#: ../melon/playlist/__init__.py:284 ../melon/widgets/preferencerow.py:277 msgid "Delete" msgstr "" @@ -526,7 +526,7 @@ msgstr "" msgid "Do not create playlist" msgstr "" -#: ../melon/playlist/create.py:52 ../melon/widgets/preferencerow.py:188 +#: ../melon/playlist/create.py:52 ../melon/widgets/preferencerow.py:239 msgid "Create" msgstr "" @@ -781,39 +781,31 @@ msgstr "" msgid "Add" msgstr "" -#: ../melon/widgets/preferencerow.py:143 -msgid "Move up" -msgstr "" - -#: ../melon/widgets/preferencerow.py:151 -msgid "Move down" -msgstr "" - -#: ../melon/widgets/preferencerow.py:157 +#: ../melon/widgets/preferencerow.py:159 msgid "Remove from list" msgstr "" -#: ../melon/widgets/preferencerow.py:172 +#: ../melon/widgets/preferencerow.py:223 msgid "Add Item" msgstr "" -#: ../melon/widgets/preferencerow.py:175 +#: ../melon/widgets/preferencerow.py:226 msgid "Create a new list entry" msgstr "" -#: ../melon/widgets/preferencerow.py:176 +#: ../melon/widgets/preferencerow.py:227 msgid "Enter the new value here" msgstr "" -#: ../melon/widgets/preferencerow.py:179 +#: ../melon/widgets/preferencerow.py:230 msgid "Value" msgstr "" -#: ../melon/widgets/preferencerow.py:219 +#: ../melon/widgets/preferencerow.py:270 msgid "Delete Item?" msgstr "" -#: ../melon/widgets/preferencerow.py:221 +#: ../melon/widgets/preferencerow.py:272 msgid "" "Do you really want to delete this item? You won't be able to restore it " "afterwards" diff --git a/po/nl.po b/po/nl.po index 8c1ec5a..6129294 100644 --- a/po/nl.po +++ b/po/nl.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: Melon 0.2.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-15 15:36+0200\n" +"POT-Creation-Date: 2024-08-16 09:14+0200\n" "PO-Revision-Date: 2024-07-17 09:18+0000\n" "Last-Translator: Vistaus \n" "Language-Team: Dutch