Skip to content

Instantly share code, notes, and snippets.

@itzexor
Created March 31, 2018 04:17
Show Gist options
  • Save itzexor/edeb5126290fa2462220010ebb5e2a28 to your computer and use it in GitHub Desktop.
Save itzexor/edeb5126290fa2462220010ebb5e2a28 to your computer and use it in GitHub Desktop.
GtkShortcutsLabel alternative for use outside of a GtkShortcutsWindow
class ShortcutDisplay(Gtk.Box):
def __init__(self, accel_string):
Gtk.Box.__init__(self, Gtk.Orientation.HORIZONTAL, 2)
def get_label(text, style_class=None):
label = Gtk.Label(text)
if style_class is not None:
label.get_style_context().add_class(style_class)
return label
[accel_key, mask] = Gtk.accelerator_parse(accel_string)
labels = []
if accel_key == 0 and mask == 0:
# failed to parse
labels.append(get_label(accel_string))
else:
text = Gtk.accelerator_get_label(accel_key, mask)
if "+" not in text:
# shouldn't be reachable?
labels.append(get_label(text), "keycap")
else:
# we make a pointless label and discard it here, but it's the easiest way *shrug*
for key_name in text.split("+"):
labels.append(get_label(key_name, "keycap"))
labels.append(get_label("+", "dim-label"))
labels.pop()
for label in labels:
self.pack_start(label, False, False, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment