Debian Codegraph @

pynicotine/gtkgui/widgets/accelerator.py

raw
1 # COPYRIGHT (C) 2021-2024 Nicotine+ Contributors
2 #
3 # GNU GENERAL PUBLIC LICENSE
4 # Version 3, 29 June 2007
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import sys
20
21 from gi.repository import Gdk
22 from gi.repository import Gtk
23
24 from pynicotine.gtkgui.application import GTK_API_VERSION
25
26
27 class Accelerator:
28
29 if GTK_API_VERSION >= 4:
30 shortcut_triggers = {}
31 else:
32 KEYMAP = Gdk.Keymap.get_for_display(Gdk.Display.get_default())
33 keycodes_mods = {}
34
35 def __init__(self, accelerator, widget, callback, user_data=None):
36
37 if GTK_API_VERSION >= 4:
38 if sys.platform == "darwin":
39 # Use Command key instead of Ctrl in accelerators on macOS
40 accelerator = accelerator.replace("<Primary>", "<Meta>")
41
42 shortcut_trigger = self.shortcut_triggers.get(accelerator)
43
44 if not hasattr(widget, "shortcut_controller"):
45 widget.shortcut_controller = Gtk.ShortcutController(
46 propagation_phase=Gtk.PropagationPhase.CAPTURE
47 )
48 widget.add_controller(widget.shortcut_controller)
49
50 if not shortcut_trigger:
51 self.shortcut_triggers[accelerator] = shortcut_trigger = Gtk.ShortcutTrigger.parse_string(accelerator)
52
53 widget.shortcut_controller.add_shortcut(
54 Gtk.Shortcut(
55 trigger=shortcut_trigger,
56 action=Gtk.CallbackAction.new(callback, user_data)
57 )
58 )
59 return
60
61 # GTK 3 replacement for Gtk.ShortcutController
62 self.keycodes, self.required_mods = self.parse_accelerator(accelerator)
63 self.callback = callback
64 self.user_data = user_data
65
66 widget.connect("key-press-event", self._activate_accelerator)
67
68 @classmethod
69 def parse_accelerator(cls, accelerator):
70
71 keycodes_mods_accel = cls.keycodes_mods.get(accelerator)
72
73 if not keycodes_mods_accel:
74 *_args, key, mods = Gtk.accelerator_parse(accelerator)
75
76 if key:
77 _valid, keys = cls.KEYMAP.get_entries_for_keyval(key)
78 keycodes = {key.keycode for key in keys}
79 else:
80 keycodes = []
81
82 cls.keycodes_mods[accelerator] = keycodes_mods_accel = (keycodes, mods)
83
84 return keycodes_mods_accel
85
86 def _activate_accelerator(self, widget, event):
87
88 activated_mods = event.state
89 required_mods = self.required_mods
90 excluded_mods = ALL_MODIFIERS & ~required_mods
91
92 if required_mods & ~activated_mods:
93 # Missing required modifiers
94 return False
95
96 if activated_mods & excluded_mods:
97 # Too many/irrelevant modifiers
98 return False
99
100 if event.hardware_keycode not in self.keycodes:
101 # Invalid key
102 return False
103
104 return self.callback(widget, None, self.user_data)
105
106
107 if GTK_API_VERSION == 3:
108 ALL_MODIFIERS = (Accelerator.parse_accelerator("<Primary>")[1]
109 | Accelerator.parse_accelerator("<Shift>")[1]
110 | Accelerator.parse_accelerator("<Alt>")[1])
111 else:
112 ALL_MODIFIERS = []