Skip to content

API turboframe.ui.palette

rafageist edited this page Dec 4, 2026 · 4 revisions

turboframe.ui.palette

Source: turboframe/ui/palette.py. Generated from the code of TurboFrame 0.3.0 by tools/wiki_api.py.

Go to file and the command palette, as VS Code's Ctrl+P and Ctrl+Shift+P.

One dialog, two modes: a line to type in and the list of what matches, best first, the letters that matched lit. In "Go to file" the list holds the folder's files (turboframe.files.project_files: git's list when the folder is a repository, so what git ignores stays out); a > at the start of the line turns it into the command palette, which holds every entry of the menu bar (File > Open...) with its key, and deleting the > goes back to the files. Up/Down (PgUp/PgDn) move the bar while the keys stay in the line; Enter opens the file or runs the command, Esc closes. With nothing typed, the files open in windows and the commands run last come first (the "commands" history of turboframe.ui.history).

The matching is fuzzy as VS Code's: the letters typed, in order, anywhere in the text; it scores words that start with them, letters that follow one another and, for files, the file name over its folders.

An application mixes PaletteMixin into its DesktopApp, binds the keys it wants (Ctrl+P and Alt+P work well: Windows Terminal keeps Ctrl+Shift+P) to CM_GOTO_FILE and CM_COMMANDS, and runs palette_command(command) from its run_command worker. The hooks have defaults: the folder is the application's folder, a file chosen goes to its open_file, a command chosen is posted as if chosen in the menu.

Contents

CM_COMMANDS, CM_GOTO_FILE, COMMANDS_PREFIX, COMMANDS_TITLE, FILES_TITLE, MAX_SHOWN, PALETTE_COMMANDS, RECENT_COMMANDS, Item, PaletteDialog, PaletteList, PaletteMixin, file_item, match, menu_items, rank

CM_COMMANDS

10305

CM_GOTO_FILE

10304

COMMANDS_PREFIX

'>'

COMMANDS_TITLE

'Commands'

FILES_TITLE

'Go to File'

MAX_SHOWN

500

PALETTE_COMMANDS

frozenset({10304, 10305})

RECENT_COMMANDS

'commands'

class Item

class Item
Item(text: str, value: Any, key: str = '', name_from: int = 0, offsets: tuple[int, ...] = ())

A row: text (lit where it matched), key at the right, value what it opens or runs.

class PaletteDialog

class PaletteDialog(TVDialog)
PaletteDialog(files: list[str | Item] | None, commands: list[Item], *, recent_files: list[str] = (), text: str = '')

Go to file, or (>) the commands. chosen holds the item once it ends with OK: a file's value is its path relative to the folder, a command's (command, param).

files are paths relative to the folder (or items: file_item), or None while they are being read: set_files gives them later, and the commands work meanwhile. commands are items (menu_items); recent_files the files open in windows, the one on top first.

The keys typed before the list is ready (the dialog's first refresh, the files read) are not lost: the letters go to the line, and an Enter acts on the list as soon as it is filled.

  • set_files(files: list[str | Item]) -> None: The folder's files, once read (files None at the start): Go to file lists them.
  • list_ready (property): The list holds what the line asks for: an Enter acts on it.
  • candidates() -> list[Item]: What the list holds before anything is typed: the recent ones first.
  • refilter() -> None
  • accept(index: int) -> None

class PaletteList

class PaletteList(WheelList, ListBox)
PaletteList(dialog: 'PaletteDialog', **kwargs)

The matches on the dialog lists' colours: the letters typed lit, folders and keys dimmed.

  • show(rows: list[Item]) -> None
  • select_item(item: int) -> None

class PaletteMixin

class PaletteMixin

Go to file and the command palette, mixed into a DesktopApp: await self.palette() (or palette(commands=True)) from a worker, or palette_command(command) for PALETTE_COMMANDS.

The hooks below have defaults; an application overrides the ones that differ.

  • palette_root() -> Path | None: The folder whose files Go to file lists: the application's folder by default (None: no files).
  • palette_items(*, commands: bool) -> list[Item]: The items of a mode: the menu bar's entries for the commands; the folder's files (file_item) for Go to file, none without a folder. The files are read in a thread (palette): an override reads what it needs there, and touches no widget.
  • palette_recent_files() -> list[str]: The files open in windows (those with a path) inside the folder, relative to it, the one on top first: Go to file shows them first.
  • async palette_open(path: Path) -> None: Open a file chosen in Go to file: the application's open_file(path) (awaited when it is a coroutine) by default.
  • async palette_run(item: Item) -> None: What a chosen item does: a command is posted as if chosen in the menu; a file goes to palette_open with its full path.
  • async palette(*, commands: bool = False) -> None: Go to file (commands: the command palette). Call from a worker.

The dialog opens at once: the files (palette_items(commands=False), a listing of the folder) are read in a thread meanwhile, and Go to file lists them when they are there.

  • async palette_command(command: int) -> bool: Run one of PALETTE_COMMANDS (from a worker); False for another command.

file_item

def file_item(rel: str) -> Item

A file's row: its path relative to the folder, with /, its name lit over its folders.

match

def match(query: str, text: str, name_from: int = 0) -> tuple[float, list[int]] | None

Score text for query (its letters in order, any case), or None.

The best placement of the letters: each one scores, more at the start of a word (after / _ - . or a capital after a small letter) and when it follows the one before; letters from name_from on (a file's name) score more. Linear in the places each letter occurs, so long paths cost little. Returns the score and the offsets of the matched letters.

menu_items

def menu_items(menu: Menu, path: tuple[str, ...] = ()) -> list[Item]

Every enabled entry of the menu bar: File > Open..., its key, (command, param).

rank

def rank(query: str, items: list[Item], limit: int = 500) -> list[Item]

The items that match query, best first, each with the offsets to light.

Clone this wiki locally