Repository navigation
Command Palette
turboframe.ui.palette is one dialog with two modes, as VS Code's Ctrl+P and Ctrl+Shift+P:
- Go to file: the folder's files. Type a few letters of a name, Enter opens it.
-
Commands: every entry of your menu bar (
File > Open...), with its key. Type>at the start of the line to switch to it; delete the>to go back to the files.
| Use it for | reaching any file of a project, or any menu command, from the keyboard |
| Import | from turboframe.ui.palette import CM_COMMANDS, CM_GOTO_FILE, PaletteMixin |
| Needs | nothing beyond TurboFrame |
| Extend | the mixin's hooks (palette_root, palette_items, palette_recent_files, palette_open, palette_run) |
| Key | |
|---|---|
| typing | narrows the list, best match first, the matched letters lit |
> first |
the commands; deleting it, the files |
| Up, Down, PgUp, PgDn | move the bar; the keys stay in the input line |
| Enter, double click | open the file, or run the command |
| Esc | close |
With nothing typed, the files open in windows come first (the one on top first), and the commands run last from the palette come first. Those are kept in the "commands" history (RECENT_COMMANDS; see Histories), so they survive a restart when the histories have a store.
The list shows at most MAX_SHOWN (500) rows. Disabled menu entries and separators are left out.
Mix PaletteMixin into your DesktopApp, bind two keys to its commands, and send them to palette_command from your command worker. Ctrl+P and Alt+P work well: Windows Terminal keeps Ctrl+Shift+P for itself.
from pathlib import Path
from textual import work
from textual.binding import Binding
from textual_vision.constants import Command
from textual_vision.events import CommandMessage
from textual_vision.menus import Menu, MenuItem, SubMenu
from turboframe.ui.app import DesktopApp
from turboframe.ui.palette import CM_COMMANDS, CM_GOTO_FILE, PaletteMixin
class MyApp(PaletteMixin, DesktopApp):
BINDINGS = [Binding("ctrl+p", f"cmd({CM_GOTO_FILE})", show=False, priority=True),
Binding("alt+p", f"cmd({CM_COMMANDS})", show=False, priority=True)]
def __init__(self, folder: Path) -> None:
super().__init__()
self.folder = folder # palette_root() reads it
def init_menu_bar(self) -> Menu:
return Menu([SubMenu("~F~ile", MenuItem("~G~o to file...", CM_GOTO_FILE, "Ctrl+P"),
MenuItem("~C~ommands...", CM_COMMANDS, "Alt+P"),
MenuItem("E~x~it", Command.QUIT, "Alt+X"))])
def action_cmd(self, command: int) -> None:
if not self.dialog_open():
self.post_message(CommandMessage(command))
def on_command_message(self, message: CommandMessage) -> None:
if message.command in (CM_GOTO_FILE, CM_COMMANDS):
message.stop()
message.prevent_default()
self.run_command(message.command)
@work(group="command")
async def run_command(self, command: int) -> None:
await self.palette_command(command) # False for a command that is not the palette's
async def open_file(self, path: Path) -> None:
... # Go to file calls it (EditorMixin gives one)palette_command(command) returns False for any other command, so it chains with the other dispatchers (see Application Mixins). From a worker you can also call await self.palette() or await self.palette(commands=True) directly.
CM_GOTO_FILE and CM_COMMANDS are FRAMEWORK_COMMANDS[48] and [49] (PALETTE_COMMANDS holds both): they never clash with your commands. DesktopApp turns Textual's own command palette off (ENABLE_COMMAND_PALETTE = False), so Ctrl+P is yours.
Each hook has a default; override those your application does differently.
| Hook | Default |
|---|---|
palette_root() |
the application's folder (None: Go to file lists nothing) |
palette_items(*, commands) |
commands: menu_items() of the menu bar; files: file_item() of each of project_files(root) (see Files) |
palette_recent_files() |
the files open in windows with a path inside the folder, relative to it, the one on top first |
palette_open(path) (async) |
the application's open_file(path), awaited when it is a coroutine |
palette_run(item) (async) |
a command: posts CommandMessage(command, info=param), as if chosen in the menu; a file: palette_open(root / item.value)
|
project_files is git's list in a repository (so what git ignores stays out), else a walk that skips caches and virtual environments; at most files.MAX_FILES (20000).
Add entries of your own, for commands that have no menu item:
from turboframe.ui.palette import Item
CM_TILE = Command.USER + 7
class MyApp(PaletteMixin, DesktopApp):
def palette_items(self, *, commands: bool) -> list[Item]:
items = super().palette_items(commands=commands)
if commands:
items.append(Item("Window > Tile", (CM_TILE, None), key="Alt+T")) # value: (command, param)
return itemsA command item's value is (command, param); a file item's is its path relative to the root, with /. Put the recent files of earlier sessions first (see Settings and Recent Files):
def palette_recent_files(self) -> list[str]:
root = Path(self.folder).resolve()
kept = [p.relative_to(root).as_posix() for p in self.recent.entries("files") if p.is_relative_to(root)]
return [*super().palette_recent_files(), *kept]-
match(query, text, name_from=0): the fuzzy match, as VS Code's. The query's letters, in order, anywhere in the text, any case. It scores a letter at the start of a word (after/ _ - . : >or a capital after a small letter) and a letter right after the previous one, and, fromname_fromon (a file's name), more. Returns(score, offsets), or None. -
rank(query, items, limit=MAX_SHOWN): the items that match, best first, each with theoffsetsto light. -
Item(text, value, key="", name_from=0, offsets=()): a row. -
menu_items(menu): every enabled entry of aMenu, asItem("File > Open...", (command, param), key="F3"). -
file_item(rel): a file's row, its name lit over its folders. -
PaletteDialog(files, commands, *, recent_files=(), text=""): the dialog;filesNone means they come later (set_files). Run it withawait app.run_dialog(dialog); once it ends withCommand.OK,dialog.chosenis theItem.text=">"opens it in the commands.
from turboframe.ui.palette import file_item, match, rank
match("fb", "src/foo/bar.py", name_from=8) # (6.986, [4, 8]): f of foo, b of bar
rank("app", [file_item(p) for p in ("src/myapp/app.py", "docs/apple.md")])The dialog opens at once. The folder's files are read in a thread while it shows (palette_items(commands=False)): the commands work meanwhile, and Go to file says "Reading the folder's files..." until its list is there. Keys typed before are not lost: the letters go to the line, and an Enter acts on the list as soon as it is filled, so Ctrl+P, a name and Enter typed at once open the file.
Because of that, an override of palette_items must not touch widgets when commands is False: read what you need (a folder, a list) and return items. PaletteDialog(None, commands) opens a dialog whose files come later through dialog.set_files(files); dialog.list_ready says when the list holds what the line asks for.
In v0.2.0 the files were listed on the event loop each time the dialog opened (#54), and an Enter typed before the list was filled was dropped (#58); both are fixed in v0.2.1.
API: turboframe.ui.palette.
TurboFrame · part of Divengine · GPL-3.0-or-later · report an issue
Tutorial
Examples
The desktop
- The Desktop
- Application Mixins
- Windows and Lists
- Dialogs
- Histories
- Progress
- Themes and Colors
- Glyphs
- Settings and Recent Files
- Command Palette
Editing
Tasks
Languages
Running
Reference
Links