unified logic of button and key press handling

This commit is contained in:
Martin Wagner 2020-12-12 13:04:47 +01:00
parent fd822aad94
commit 9e4068f399

View File

@ -1960,7 +1960,6 @@ class AlbumWindow(FocusFrame):
self._client=client
self._artist_window=artist_window
self._button_event=(None, None)
self._key_event=(None, None)
self.stop_flag=False
self._done=True
self._pending=[]
@ -1984,7 +1983,6 @@ class AlbumWindow(FocusFrame):
self._iconview.connect("item-activated", self._on_item_activated)
self._iconview.connect("button-press-event", self._on_button_press_event)
self._iconview.connect("button-release-event", self._on_button_release_event)
self._iconview.connect("key-press-event", self._on_key_press_event)
self._iconview.connect("key-release-event", self._on_key_release_event)
self._client.emitter.connect("update", self._clear)
self._client.emitter.connect("disconnected", self._on_disconnected)
@ -2183,31 +2181,22 @@ class AlbumWindow(FocusFrame):
pop.popup()
self._button_event=(None, None)
def _on_key_press_event(self, widget, event):
if event.keyval in (Gdk.keyval_from_name("p"),Gdk.keyval_from_name("a"),Gdk.keyval_from_name("Menu")):
paths=widget.get_selected_items()
if len(paths) != 0:
self._key_event=(event.keyval, paths[0])
def _on_key_release_event(self, widget, event):
if event.keyval in (Gdk.keyval_from_name("p"),Gdk.keyval_from_name("a"),Gdk.keyval_from_name("Menu")):
paths=widget.get_selected_items()
if len(paths) != 0:
if self._key_event == (event.keyval, paths[0]):
if event.keyval == Gdk.keyval_from_name("p"):
self._path_to_playlist(paths[0])
elif event.keyval == Gdk.keyval_from_name("a"):
self._path_to_playlist(paths[0], "append")
elif event.keyval == Gdk.keyval_from_name("Menu"):
album=self._store[paths[0]][4]
year=self._store[paths[0]][5]
artist=self._store[paths[0]][6]
rect=widget.get_cell_rect(paths[0], None)[1]
x=rect.x+rect.width//2
y=rect.y+rect.height//2
pop=AlbumPopover(self._client, self._settings, album, artist, year, widget, x, y)
pop.popup()
self._key_event=(None, None)
paths=widget.get_selected_items()
if len(paths) != 0:
if event.keyval == Gdk.keyval_from_name("p"):
self._path_to_playlist(paths[0])
elif event.keyval == Gdk.keyval_from_name("a"):
self._path_to_playlist(paths[0], "append")
elif event.keyval == Gdk.keyval_from_name("Menu"):
album=self._store[paths[0]][4]
year=self._store[paths[0]][5]
artist=self._store[paths[0]][6]
rect=widget.get_cell_rect(paths[0], None)[1]
x=rect.x+rect.width//2
y=rect.y+rect.height//2
pop=AlbumPopover(self._client, self._settings, album, artist, year, widget, x, y)
pop.popup()
def _on_item_activated(self, widget, path):
treeiter=self._store.get_iter(path)
@ -2605,6 +2594,7 @@ class PlaylistWindow(Gtk.Box):
self._playlist_version=None
self._icon_size=self._settings.get_int("icon-size-sec")
self._inserted_path=None # needed for drag and drop
self._button_event=(None, None)
# buttons
provider=Gtk.CssProvider()
@ -2685,8 +2675,9 @@ class PlaylistWindow(Gtk.Box):
# connect
self._treeview.connect("row-activated", self._on_row_activated)
self._key_press_event=self._treeview.connect("key-press-event", self._on_key_press_event)
self._treeview.connect("button-press-event", self._on_button_press_event)
self._treeview.connect("button-release-event", self._on_button_release_event)
self._treeview.connect("key-release-event", self._on_key_release_event)
self._treeview.connect("drag-begin", lambda *args: self._frame.disable())
self._treeview.connect("drag-end", lambda *args: self._frame.enable())
self._row_deleted=self._store.connect("row-deleted", self._on_row_deleted)
@ -2762,8 +2753,26 @@ class PlaylistWindow(Gtk.Box):
except:
self._selection.unselect_all()
def _on_key_press_event(self, widget, event):
self._treeview.handler_block(self._key_press_event)
def _on_button_press_event(self, widget, event):
path_re=widget.get_path_at_pos(int(event.x), int(event.y))
if path_re is not None:
if event.type == Gdk.EventType.BUTTON_PRESS:
self._button_event=(event.button, path_re[0])
def _on_button_release_event(self, widget, event):
path_re=widget.get_path_at_pos(int(event.x), int(event.y))
if path_re is not None:
path=path_re[0]
if self._button_event == (event.button, path):
if event.button == 2 and event.type == Gdk.EventType.BUTTON_RELEASE:
self._store.remove(self._store.get_iter(path))
elif event.button == 3 and event.type == Gdk.EventType.BUTTON_RELEASE:
song=self._client.wrapped_call("get_metadata", self._store[path][8])
pop=SongPopover(song, widget, int(event.x), int(event.y))
pop.popup()
self._button_event=(None, None)
def _on_key_release_event(self, widget, event):
if event.keyval == Gdk.keyval_from_name("Delete"):
treeview, treeiter=self._selection.get_selected()
if treeiter is not None:
@ -2779,22 +2788,6 @@ class PlaylistWindow(Gtk.Box):
file_name=self._store[path][8]
pop=SongPopover(self._client.wrapped_call("get_metadata", file_name), widget, int(cell.x), int(cell.y))
pop.popup()
self._treeview.handler_unblock(self._key_press_event)
def _on_button_press_event(self, widget, event):
if event.button == 2 and event.type == Gdk.EventType.BUTTON_PRESS:
try:
path=widget.get_path_at_pos(int(event.x), int(event.y))[0]
self._store.remove(self._store.get_iter(path))
except:
pass
elif event.button == 3 and event.type == Gdk.EventType.BUTTON_PRESS:
try:
path=widget.get_path_at_pos(int(event.x), int(event.y))[0]
pop=SongPopover(self._client.wrapped_call("get_metadata", self._store[path][8]), widget, int(event.x), int(event.y))
pop.popup()
except:
pass
def _on_row_deleted(self, model, path): # sync treeview to mpd
if self._inserted_path is not None: # move