Forbid Duplicate Key Bindings

This commit forbids setting duplicate key bindings in
`Preferences > Keybindings` tab.

If a duplicate key binding is detected upon editing, such a key binding
is not assigned and instead a message dialog is displayed saying that the
key binding is a duplicate.
This commit is contained in:
dkmvs 2020-09-02 15:25:20 +03:00
parent 1ad579ce03
commit 46e78520c4
1 changed files with 36 additions and 0 deletions

View File

@ -1687,6 +1687,42 @@ class PrefsEditor:
if key_with_shift.level != 0 and keyval_lower == keyval_upper: if key_with_shift.level != 0 and keyval_lower == keyval_upper:
mods = Gdk.ModifierType(mods & ~Gdk.ModifierType.SHIFT_MASK) mods = Gdk.ModifierType(mods & ~Gdk.ModifierType.SHIFT_MASK)
accel = Gtk.accelerator_name(key, mods)
current_binding = liststore.get_value(liststore.get_iter(path), 0)
duplicate_bindings = []
for conf_binding, conf_accel in self.config["keybindings"].items():
parsed_accel = Gtk.accelerator_parse(accel)
parsed_conf_accel = Gtk.accelerator_parse(conf_accel)
if (
parsed_accel == parsed_conf_accel
and current_binding != conf_binding
):
duplicate_bindings.append((conf_binding, conf_accel))
if duplicate_bindings:
dialog = Gtk.MessageDialog(
transient_for=self.window,
flags=Gtk.DialogFlags.MODAL,
message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.CLOSE,
text="Duplicate Key Bindings Are Not Allowed",
)
accel_label = Gtk.accelerator_get_label(key, mods)
# get the first found duplicate
duplicate_keybinding_name = duplicate_bindings[0][0]
message = (
"Key binding `{0}` is already in use to trigger the `{1}` action."
).format(accel_label, self.keybindingnames[duplicate_keybinding_name])
dialog.format_secondary_text(message)
dialog.run()
dialog.destroy()
return
celliter = liststore.get_iter_from_string(path) celliter = liststore.get_iter_from_string(path)
liststore.set(celliter, 2, key, 3, mods) liststore.set(celliter, 2, key, 3, mods)