1313# You should have received a copy of the GNU General Public License
1414# along with this program. If not, see <http://www.gnu.org/licenses/>.
1515
16- from gi .repository import Gtk
16+ from gi .repository import Gio , Gtk
1717
1818import meld .conf
1919# Import support module to get all builder-constructed widgets in the namespace
@@ -34,3 +34,38 @@ def get_builder(filename):
3434 path = meld .conf .ui_file (filename )
3535 builder .add_from_file (path )
3636 return builder
37+
38+
39+ # The functions `extract_accel_from_menu_item` and `extract_accels_from_menu`
40+ # are converted straight from GTK+'s GtkApplication handling. I don't
41+ # understand why these aren't public API, but here we are.
42+
43+
44+ def extract_accel_from_menu_item (
45+ model : Gio .MenuModel , item : int , app : Gtk .Application ):
46+
47+ accel , action , target = None , None , None
48+
49+ more , it = True , model .iterate_item_attributes (item )
50+ while more :
51+ more , key , value = it .get_next ()
52+ if key == 'action' :
53+ action = value .get_string ()
54+ elif key == 'accel' :
55+ accel = value .get_string ()
56+ # TODO: Handle targets
57+
58+ if accel and action :
59+ detailed_action_name = Gio .Action .print_detailed_name (action , target )
60+ app .set_accels_for_action (detailed_action_name , [accel ])
61+
62+
63+ def extract_accels_from_menu (model : Gio .MenuModel , app : Gtk .Application ):
64+ for i in range (model .get_n_items ()):
65+ extract_accel_from_menu_item (model , i , app )
66+
67+ more , it = True , model .iterate_item_links (i )
68+ while more :
69+ more , name , submodel = it .get_next ()
70+ if submodel :
71+ extract_accels_from_menu (submodel , app )
0 commit comments