Commit dcb867b7 authored by Kai Willadsen's avatar Kai Willadsen

tree: Move common popup handling code to new class

parent e94759d8
...@@ -504,7 +504,7 @@ ...@@ -504,7 +504,7 @@
<object class="GtkTreeView" id="treeview"> <object class="GtkTreeView" id="treeview">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<signal name="button-press-event" handler="on_button_press_event" swapped="no"/> <signal name="button-press-event" handler="on_treeview_button_press_event" swapped="no"/>
<signal name="cursor-changed" handler="on_treeview_cursor_changed" swapped="no"/> <signal name="cursor-changed" handler="on_treeview_cursor_changed" swapped="no"/>
<signal name="row-activated" handler="on_row_activated" swapped="no"/> <signal name="row-activated" handler="on_row_activated" swapped="no"/>
<signal name="popup-menu" handler="on_treeview_popup_menu" swapped="no"/> <signal name="popup-menu" handler="on_treeview_popup_menu" swapped="no"/>
......
...@@ -285,7 +285,7 @@ class CanonicalListing: ...@@ -285,7 +285,7 @@ class CanonicalListing:
return element.lower() return element.lower()
class DirDiff(MeldDoc, Component): class DirDiff(tree.TreeviewCommon, MeldDoc, Component):
"""Two or three way folder comparison""" """Two or three way folder comparison"""
__gtype_name__ = "DirDiff" __gtype_name__ = "DirDiff"
...@@ -1465,52 +1465,6 @@ class DirDiff(MeldDoc, Component): ...@@ -1465,52 +1465,6 @@ class DirDiff(MeldDoc, Component):
}) })
return different return different
def on_treeview_popup_menu(self, treeview):
cursor_path, cursor_col = treeview.get_cursor()
if not cursor_path:
self.popup_menu.popup_at_pointer(None)
return True
# We always want to pop up to the right of the first column,
# ignoring the actual cursor column location.
rect = treeview.get_background_area(
cursor_path, treeview.get_column(0))
self.popup_menu.popup_at_rect(
treeview.get_bin_window(),
rect,
Gdk.Gravity.SOUTH_EAST,
Gdk.Gravity.NORTH_WEST,
None,
)
return True
def on_treeview_button_press_event(self, treeview, event):
# Unselect any selected files in other panes
for t in [v for v in self.treeview[:self.num_panes] if v != treeview]:
t.get_selection().unselect_all()
if (event.triggers_context_menu() and
event.type == Gdk.EventType.BUTTON_PRESS):
treeview.grab_focus()
path = treeview.get_path_at_pos(int(event.x), int(event.y))
if path is None:
return False
selection = treeview.get_selection()
model, rows = selection.get_selected_rows()
if path[0] not in rows:
selection.unselect_all()
selection.select_path(path[0])
treeview.set_cursor(path[0])
self.popup_menu.popup_at_pointer(event)
return True
return False
def get_state_traversal(self, diffmapindex): def get_state_traversal(self, diffmapindex):
def tree_state_iter(): def tree_state_iter():
treeindex = (0, self.num_panes-1)[diffmapindex] treeindex = (0, self.num_panes-1)[diffmapindex]
......
...@@ -220,6 +220,59 @@ class DiffTreeStore(SearchableTreeStore): ...@@ -220,6 +220,59 @@ class DiffTreeStore(SearchableTreeStore):
self.set(treeiter, safe_keys_values) self.set(treeiter, safe_keys_values)
class TreeviewCommon:
def on_treeview_popup_menu(self, treeview):
cursor_path, cursor_col = treeview.get_cursor()
if not cursor_path:
self.popup_menu.popup_at_pointer(None)
return True
# We always want to pop up to the right of the first column,
# ignoring the actual cursor column location.
rect = treeview.get_background_area(
cursor_path, treeview.get_column(0))
self.popup_menu.popup_at_rect(
treeview.get_bin_window(),
rect,
Gdk.Gravity.SOUTH_EAST,
Gdk.Gravity.NORTH_WEST,
None,
)
return True
def on_treeview_button_press_event(self, treeview, event):
# If we have multiple treeviews, unselect clear other tree selections
num_panes = getattr(self, 'num_panes', 1)
if num_panes > 1:
for t in self.treeview[:self.num_panes]:
if t != treeview:
t.get_selection().unselect_all()
if (event.triggers_context_menu() and
event.type == Gdk.EventType.BUTTON_PRESS):
treeview.grab_focus()
path = treeview.get_path_at_pos(int(event.x), int(event.y))
if path is None:
return False
selection = treeview.get_selection()
model, rows = selection.get_selected_rows()
if path[0] not in rows:
selection.unselect_all()
selection.select_path(path[0])
treeview.set_cursor(path[0])
self.popup_menu.popup_at_pointer(event)
return True
return False
def treeview_search_cb(model, column, key, it, data): def treeview_search_cb(model, column, key, it, data):
# If the key contains a path separator, search the whole path, # If the key contains a path separator, search the whole path,
# otherwise just use the filename. If the key is all lower-case, do a # otherwise just use the filename. If the key is all lower-case, do a
......
...@@ -119,7 +119,7 @@ class VcTreeStore(tree.DiffTreeStore): ...@@ -119,7 +119,7 @@ class VcTreeStore(tree.DiffTreeStore):
return self.get_value(it, self.column_index(tree.COL_PATH, 0)) return self.get_value(it, self.column_index(tree.COL_PATH, 0))
class VcView(MeldDoc, Component): class VcView(tree.TreeviewCommon, MeldDoc, Component):
__gtype_name__ = "VcView" __gtype_name__ = "VcView"
...@@ -493,48 +493,6 @@ class VcView(MeldDoc, Component): ...@@ -493,48 +493,6 @@ class VcView(MeldDoc, Component):
self.emit("create-diff", self.emit("create-diff",
[Gio.File.new_for_path(d) for d in diffs], kwargs) [Gio.File.new_for_path(d) for d in diffs], kwargs)
def on_treeview_popup_menu(self, treeview):
cursor_path, cursor_col = treeview.get_cursor()
if not cursor_path:
self.popup_menu.popup_at_pointer(None)
return True
# We always want to pop up to the right of the first column,
# ignoring the actual cursor column location.
rect = treeview.get_background_area(
cursor_path, treeview.get_column(0))
self.popup_menu.popup_at_rect(
treeview.get_bin_window(),
rect,
Gdk.Gravity.SOUTH_EAST,
Gdk.Gravity.NORTH_WEST,
None,
)
return True
def on_button_press_event(self, treeview, event):
if (event.triggers_context_menu() and
event.type == Gdk.EventType.BUTTON_PRESS):
treeview.grab_focus()
path = treeview.get_path_at_pos(int(event.x), int(event.y))
if path is None:
return False
selection = treeview.get_selection()
model, rows = selection.get_selected_rows()
if path[0] not in rows:
selection.unselect_all()
selection.select_path(path[0])
treeview.set_cursor(path[0])
self.popup_menu.popup_at_pointer(event)
return True
return False
def on_filter_state_toggled(self, button): def on_filter_state_toggled(self, button):
active_filters = [ active_filters = [
k for k, (action_name, fn) in self.state_actions.items() k for k, (action_name, fn) in self.state_actions.items()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment