Skip to content

Instantly share code, notes, and snippets.

@gferreira
Last active May 27, 2020 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gferreira/8bccbb070b55118b464e0d476cfdf85a to your computer and use it in GitHub Desktop.
Save gferreira/8bccbb070b55118b464e0d476cfdf85a to your computer and use it in GitHub Desktop.
from AppKit import NSApp
from vanilla import FloatingWindow, EditText, List, Button
from lib.tools.shortCutTools import getShortCuts
from lib.UI.fileBrowser import shortKeyToString
class CommandPalette:
def __init__(self):
shortcuts = getShortCuts()
self.shortcuts = {}
self.menuItems = {}
for key, item in shortcuts.items():
if item.keyEquivalent():
shortkey = item.keyEquivalentModifierMask(), item.keyEquivalent()
self.shortcuts[key] = shortkey
self.menuItems[key] = item
self.w = FloatingWindow((400, 280), 'CommandPalette',
minSize=(400, 200), maxSize=(400, 480))
x = y = p = 10
textHeight = 20
self.w.text = EditText((x, y, -p, textHeight), '',
placeholder='type to search...',
continuous=True, callback=self.updateListCallback)
y += textHeight + p
self.w.list = List(
(x, y, -p, -p),
[{"command": key, "keys": shortKeyToString(shortkey)} for key, shortkey in self.shortcuts.items()],
columnDescriptions=[{"title": "command"}, {"title": "keys", "width" : 100}],
allowsMultipleSelection=False, allowsEmptySelection=False,
doubleClickCallback=self.doubleClickCallback)
self.w.open()
def updateListCallback(self, sender):
search = sender.get()
foundItems = [{"command": key, "keys": shortKeyToString(shortkey)} for key, shortkey in self.shortcuts.items() if search in key]
self.w.list.set(foundItems)
def doubleClickCallback(self, sender):
items = self.w.list.get()
i = self.w.list.getSelection()[0]
command = items[i]['command']
menuItem = self.menuItems[command]
NSApp().sendAction_to_from_(menuItem.action(), menuItem.target(), menuItem)
CommandPalette()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment