Add func TreeViewHelper.get_popup_rectangle

This commit is contained in:
Teemu Ikonen 2022-07-09 20:09:22 +03:00
parent 27aed1fbea
commit 06cb0aa451
1 changed files with 30 additions and 0 deletions

View File

@ -382,6 +382,36 @@ class TreeViewHelper(object):
treeview.set_cursor(path)
return True
@staticmethod
def get_popup_rectangle(treeview, event, column=0):
"""
:return: Gdk.Rectangle to pass to Gtk.Popover.set_pointing_to()
If event is given, return a zero-width and height rectangle with the
event coordinates. If event is None, get the area of the column in the
first selected treeview row.
Used for instance when the popup trigger is the Menu key: It will
position the popover on top of the column on the selected row, even if
the mouse is elsewhere
"""
if event is not None:
area = Gdk.Rectangle()
area.x, area.y = treeview.convert_bin_window_to_widget_coords(event.x, event.y)
return area
# If there's a selection, place the popup menu on top of
# the first-selected row and given column (otherwise in the top left corner)
selection = treeview.get_selection()
model, paths = selection.get_selected_rows()
if paths:
path = paths[0]
area = treeview.get_cell_area(path, treeview.get_column(column))
else:
area = Gdk.Rectangle() # x, y, width, height are all 0
area.x, area.y = treeview.convert_bin_window_to_widget_coords(area.x, area.y)
class ExtensionMenuHelper(object):
"""A helper class to handle extension submenus"""