Skip to content

Commit dcb867b

Browse files
committed
tree: Move common popup handling code to new class
1 parent e94759d commit dcb867b

4 files changed

Lines changed: 56 additions & 91 deletions

File tree

data/ui/vcview.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@
504504
<object class="GtkTreeView" id="treeview">
505505
<property name="visible">True</property>
506506
<property name="can_focus">True</property>
507-
<signal name="button-press-event" handler="on_button_press_event" swapped="no"/>
507+
<signal name="button-press-event" handler="on_treeview_button_press_event" swapped="no"/>
508508
<signal name="cursor-changed" handler="on_treeview_cursor_changed" swapped="no"/>
509509
<signal name="row-activated" handler="on_row_activated" swapped="no"/>
510510
<signal name="popup-menu" handler="on_treeview_popup_menu" swapped="no"/>

meld/dirdiff.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def canonicalize_lower(element):
285285
return element.lower()
286286

287287

288-
class DirDiff(MeldDoc, Component):
288+
class DirDiff(tree.TreeviewCommon, MeldDoc, Component):
289289
"""Two or three way folder comparison"""
290290

291291
__gtype_name__ = "DirDiff"
@@ -1465,52 +1465,6 @@ def format_name_override(f):
14651465
})
14661466
return different
14671467

1468-
def on_treeview_popup_menu(self, treeview):
1469-
cursor_path, cursor_col = treeview.get_cursor()
1470-
if not cursor_path:
1471-
self.popup_menu.popup_at_pointer(None)
1472-
return True
1473-
1474-
# We always want to pop up to the right of the first column,
1475-
# ignoring the actual cursor column location.
1476-
rect = treeview.get_background_area(
1477-
cursor_path, treeview.get_column(0))
1478-
1479-
self.popup_menu.popup_at_rect(
1480-
treeview.get_bin_window(),
1481-
rect,
1482-
Gdk.Gravity.SOUTH_EAST,
1483-
Gdk.Gravity.NORTH_WEST,
1484-
None,
1485-
)
1486-
return True
1487-
1488-
def on_treeview_button_press_event(self, treeview, event):
1489-
# Unselect any selected files in other panes
1490-
for t in [v for v in self.treeview[:self.num_panes] if v != treeview]:
1491-
t.get_selection().unselect_all()
1492-
1493-
if (event.triggers_context_menu() and
1494-
event.type == Gdk.EventType.BUTTON_PRESS):
1495-
1496-
treeview.grab_focus()
1497-
1498-
path = treeview.get_path_at_pos(int(event.x), int(event.y))
1499-
if path is None:
1500-
return False
1501-
1502-
selection = treeview.get_selection()
1503-
model, rows = selection.get_selected_rows()
1504-
1505-
if path[0] not in rows:
1506-
selection.unselect_all()
1507-
selection.select_path(path[0])
1508-
treeview.set_cursor(path[0])
1509-
1510-
self.popup_menu.popup_at_pointer(event)
1511-
return True
1512-
return False
1513-
15141468
def get_state_traversal(self, diffmapindex):
15151469
def tree_state_iter():
15161470
treeindex = (0, self.num_panes-1)[diffmapindex]

meld/tree.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,59 @@ def unsafe_set(self, treeiter, pane, keys_values):
220220
self.set(treeiter, safe_keys_values)
221221

222222

223+
class TreeviewCommon:
224+
225+
def on_treeview_popup_menu(self, treeview):
226+
cursor_path, cursor_col = treeview.get_cursor()
227+
if not cursor_path:
228+
self.popup_menu.popup_at_pointer(None)
229+
return True
230+
231+
# We always want to pop up to the right of the first column,
232+
# ignoring the actual cursor column location.
233+
rect = treeview.get_background_area(
234+
cursor_path, treeview.get_column(0))
235+
236+
self.popup_menu.popup_at_rect(
237+
treeview.get_bin_window(),
238+
rect,
239+
Gdk.Gravity.SOUTH_EAST,
240+
Gdk.Gravity.NORTH_WEST,
241+
None,
242+
)
243+
return True
244+
245+
def on_treeview_button_press_event(self, treeview, event):
246+
247+
# If we have multiple treeviews, unselect clear other tree selections
248+
num_panes = getattr(self, 'num_panes', 1)
249+
if num_panes > 1:
250+
for t in self.treeview[:self.num_panes]:
251+
if t != treeview:
252+
t.get_selection().unselect_all()
253+
254+
if (event.triggers_context_menu() and
255+
event.type == Gdk.EventType.BUTTON_PRESS):
256+
257+
treeview.grab_focus()
258+
259+
path = treeview.get_path_at_pos(int(event.x), int(event.y))
260+
if path is None:
261+
return False
262+
263+
selection = treeview.get_selection()
264+
model, rows = selection.get_selected_rows()
265+
266+
if path[0] not in rows:
267+
selection.unselect_all()
268+
selection.select_path(path[0])
269+
treeview.set_cursor(path[0])
270+
271+
self.popup_menu.popup_at_pointer(event)
272+
return True
273+
return False
274+
275+
223276
def treeview_search_cb(model, column, key, it, data):
224277
# If the key contains a path separator, search the whole path,
225278
# otherwise just use the filename. If the key is all lower-case, do a

meld/vcview.py

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def get_file_path(self, it):
119119
return self.get_value(it, self.column_index(tree.COL_PATH, 0))
120120

121121

122-
class VcView(MeldDoc, Component):
122+
class VcView(tree.TreeviewCommon, MeldDoc, Component):
123123

124124
__gtype_name__ = "VcView"
125125

@@ -493,48 +493,6 @@ def run_diff(self, path):
493493
self.emit("create-diff",
494494
[Gio.File.new_for_path(d) for d in diffs], kwargs)
495495

496-
def on_treeview_popup_menu(self, treeview):
497-
cursor_path, cursor_col = treeview.get_cursor()
498-
if not cursor_path:
499-
self.popup_menu.popup_at_pointer(None)
500-
return True
501-
502-
# We always want to pop up to the right of the first column,
503-
# ignoring the actual cursor column location.
504-
rect = treeview.get_background_area(
505-
cursor_path, treeview.get_column(0))
506-
507-
self.popup_menu.popup_at_rect(
508-
treeview.get_bin_window(),
509-
rect,
510-
Gdk.Gravity.SOUTH_EAST,
511-
Gdk.Gravity.NORTH_WEST,
512-
None,
513-
)
514-
return True
515-
516-
def on_button_press_event(self, treeview, event):
517-
if (event.triggers_context_menu() and
518-
event.type == Gdk.EventType.BUTTON_PRESS):
519-
520-
treeview.grab_focus()
521-
522-
path = treeview.get_path_at_pos(int(event.x), int(event.y))
523-
if path is None:
524-
return False
525-
526-
selection = treeview.get_selection()
527-
model, rows = selection.get_selected_rows()
528-
529-
if path[0] not in rows:
530-
selection.unselect_all()
531-
selection.select_path(path[0])
532-
treeview.set_cursor(path[0])
533-
534-
self.popup_menu.popup_at_pointer(event)
535-
return True
536-
return False
537-
538496
def on_filter_state_toggled(self, button):
539497
active_filters = [
540498
k for k, (action_name, fn) in self.state_actions.items()

0 commit comments

Comments
 (0)