Skip to content

Commit

Permalink
macOS: restore a few Cmd shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiascode committed May 19, 2024
1 parent b4004b6 commit c6a7996
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
14 changes: 14 additions & 0 deletions pynicotine/gtkgui/ui/dialogs/shortcuts.ui
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@
<property name="visible">True</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="action-name">accel.select-all</property>
<property name="title" translatable="yes">Select All</property>
<property name="visible">True</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="action-name">accel.find</property>
<property name="title" translatable="yes">Find</property>
<property name="visible">True</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="action-name">accel.remove</property>
Expand Down
2 changes: 1 addition & 1 deletion pynicotine/gtkgui/widgets/popupmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def _callback_click_gtk4_darwin(self, controller, _num_p, pos_x, pos_y):

event = controller.get_last_event()

if event.get_modifier_state() == Gdk.ModifierType.CONTROL_MASK:
if event.get_modifier_state() & Gdk.ModifierType.CONTROL_MASK:
return self._callback(controller, pos_x, pos_y)

return False
Expand Down
56 changes: 56 additions & 0 deletions pynicotine/gtkgui/widgets/treeview.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sys
import time

import gi.module
Expand Down Expand Up @@ -89,6 +90,9 @@ def __init__(self, window, parent, columns, has_tree=False, multi_select=False,
self._initialise_columns(columns)

Accelerator("<Primary>c", self.widget, self.on_copy_cell_data_accelerator)
Accelerator("<Primary>a", self.widget, self.on_select_all)
Accelerator("<Primary>f", self.widget, self.on_start_search)

self._column_menu = self.widget.column_menu = PopupMenu(
self.window.application, self.widget, callback=self.on_column_header_menu, connect_events=False)

Expand Down Expand Up @@ -121,6 +125,13 @@ def __init__(self, window, parent, columns, has_tree=False, multi_select=False,

add_css_class(self.widget, "treeview-spacing")

if GTK_API_VERSION == 4 and sys.platform == "darwin":
# Workaround to restore Cmd-click behavior on macOS
gesture_click = Gtk.GestureClick()
gesture_click.set_propagation_phase(Gtk.PropagationPhase.CAPTURE)
gesture_click.connect("pressed", self.on_treeview_click_gtk4_darwin)
self.widget.add_controller(gesture_click)

def destroy(self):

# Prevent updates while destroying widget
Expand Down Expand Up @@ -570,6 +581,9 @@ def select_row(self, iterator=None, expand_rows=True, should_scroll=True):

self._selection.select_iter(iterator)

def select_all_rows(self):
self._selection.select_all()

def unselect_all_rows(self):
self._selection.unselect_all()

Expand Down Expand Up @@ -834,6 +848,36 @@ def on_tooltip(self, _widget, pos_x, pos_y, _keyboard_mode, tooltip):
tooltip.set_text(value)
return True

def on_treeview_click_gtk4_darwin(self, controller, _num_p, pos_x, pos_y, *_args):

event = controller.get_last_event()
cmd_mask = 16

if not event.get_modifier_state() & cmd_mask:
return False

widget = self.widget.pick(pos_x, pos_y, Gtk.PickFlags.DEFAULT)

if not isinstance(widget, Gtk.TreeView):
return False

bin_x, bin_y = widget.convert_widget_to_bin_window_coords(pos_x, pos_y)
pathinfo = widget.get_path_at_pos(bin_x, bin_y)

if pathinfo is None:
return False

selection = widget.get_selection()
path, _column, _cell_x, _cell_y = pathinfo

if selection.path_is_selected(path):
selection.unselect_path(path)
else:
selection.select_path(path)

controller.set_state(Gtk.EventSequenceState.CLAIMED)
return True

def on_copy_cell_data_accelerator(self, *_args):
"""Ctrl+C: copy cell data."""

Expand All @@ -854,6 +898,18 @@ def on_copy_cell_data_accelerator(self, *_args):
clipboard.copy_text(value)
return True

def on_select_all(self, *_args):
"""Ctrl+A: select all rows."""

self.select_all_rows()
return True

def on_start_search(self, *_args):
"""Ctrl+F: start search."""

self.widget.emit("start-interactive-search")
return True


# Legacy Functions (to be removed) #

Expand Down
6 changes: 3 additions & 3 deletions pynicotine/gtkgui/widgets/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ def _menu_popup(self, controller, widget):
if controller.is_active():
widget.activate_action("menu.popup")

def _callback_click_gtk4_darwin(self, controller, _num_p, x, y, *_args):
def _callback_click_gtk4_darwin(self, controller, _num_p, pos_x, pos_y, *_args):

event = controller.get_last_event()

if event.get_modifier_state() != Gdk.ModifierType.CONTROL_MASK:
if not event.get_modifier_state() & Gdk.ModifierType.CONTROL_MASK:
return False

cursor_widget = self.widget.pick(x, y, Gtk.PickFlags.DEFAULT)
cursor_widget = self.widget.pick(pos_x, pos_y, Gtk.PickFlags.DEFAULT)
widget = cursor_widget.get_ancestor(Gtk.Text)

if widget is None:
Expand Down

0 comments on commit c6a7996

Please sign in to comment.