Beispiel #1
0
 def validate_hotkey(self, value):
     if value is not None:
         if isinstance(value, str):
             value = parse_hotkey(value)
         assert len(value) == 2, "invalid hotkey tuple: %r" % (value,)
         # TODO check if a hot key is already in use; ignore if it is
         return value
     return "", 0
Beispiel #2
0
 def make_native_item(self, target):
     tag = target.add_callback(self.callback)
     key, mask = parse_hotkey(self.hotkey or "")
     item = ak.NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
         self.title,
         target.action_selector,
         key,
     )
     item.setKeyEquivalentModifierMask_(mask)
     item.setTag_(tag)
     item.setTarget_(target)
     return item
Beispiel #3
0
    def load_shortcuts(self, menu):
        shortcuts = schema_to_dict(self.app.config.schema["shortcuts"])
        shortcuts.update(self.app.config.lookup("shortcuts", as_dict=True))

        def make_command(text):
            from editxt.command.base import command

            cmd, ignore = self.find_command(text)
            if cmd is None:
                log.warn("unrecognized command: %r", text)
                return None

            @command(is_enabled=None if cmd is None else cmd.is_enabled)
            def exec(editor, args):
                editor.project.window.command.execute(text)

            exec.__name__ = text
            return exec

        def sortkey(item):
            cmd = item[1]
            if isinstance(cmd, str):
                return sys.maxsize, cmd.lstrip()
            name = cmd["name"] if "name" in cmd else cmd["command"].lstrip()
            return cmd.get("rank", sys.maxsize), name

        for hotkey, cmd in sorted(shortcuts.items(), key=sortkey):
            key, mask = parse_hotkey(hotkey)
            if isinstance(cmd, str):
                text = cmd
                name = cmd.lstrip()
            else:
                text = cmd["command"]
                name = cmd["name"] if "name" in cmd else text.lstrip()
            command = make_command(text)
            if key is not None and command is not None:
                tag = self.add_menu_item(menu, name, key, mask)
                self.commands[tag] = command
            elif key is None:
                log.warn("unrecognized hotkey: %s", hotkey)