CUI

Components / Data display

File browser and picker

A folder view with sorting and navigation, and the open and save dialogs built from it.

Functions: cui_file_browser, cui_file_picker

File browser and picker

Usage

static char folder[1024] = ".";
cui_file_browser_result r = cui_file_browser(ui, "files", folder, sizeof folder, .filter = "*.c;*.h");
if (r.picked) open_document(r.picked);           /* double-click or Enter on a file */

static bool saving;
static char path[1024];
if (cui_button(ui, "Save as...")) saving = true;
if (cui_file_picker(ui, "save", &saving, path, sizeof path, .save = true, .name = "report.pdf")) write_report(path);

A folder's contents with name, date modified and size, folders first and names in natural order (file2 before file10); click a column title to sort by it, again to reverse. Above the list are back, forward and up, a path bar (click a segment to go there, or click the bar and type a path), a filter field and reload; beside it a sidebar of places (home, desktop, documents, downloads and the root, or your own places). Double-click or Enter opens a folder or picks a file (r.picked, a full path), and r.selected tells you what is selected. With the list focused the arrows, Page Up, Page Down, Home and End move the selection, typing letters jumps to the next name that starts with them, Backspace goes up, Alt+Left and Alt+Right go back and forward and F5 reloads. path is yours and the browser writes it as you move around. filter takes patterns like *.png;*.jpg (folders always show), folders_only and hidden change what is listed. Narrow, the date and then the size columns step aside and the sidebar is left out. The local lister shows everything a folder holds, including devices and pipes in places like /dev: check that a picked path is a regular file before reading it, since reading a terminal or a pipe can block.

cui_file_picker puts a browser in a dialog with a footer: Open, or with save a name field and Save (selecting a file fills in its name), or with folders Choose. It returns true once with the chosen path; *open turns false then or on Cancel and Esc. It is drawn by CUI like the rest of the window; cui_app_file_dialog in cui_app.h opens the system's own dialog instead.

A folder is read when you enter it or reload, not every frame, and only the rows in view are built, so a folder of 20,000 files scrolls and filters without slowing down. Reading goes through list, which defaults to cui_fs_list (POSIX or Windows; nothing in a browser build), so the same browser can show a zip file, a remote bucket or an asset database.

Keyboard

  • Double-click or Enter opens a folder or picks a file (r.picked, a full path), and r.selected tells you what is selected.
  • With the list focused the arrows, Page Up, Page Down, Home and End move the selection, typing letters jumps to the next name that starts with them, Backspace goes up, Alt+Left and Alt+Right go back and forward and F5 reloads.
  • It returns true once with the chosen path; *open turns false then or on Cancel and Esc.

Options of cui_file_browser

OptionTypeDefaultDescription
.widthcui_sizefill and 380default fill and 380
.heightcui_sizefill and 380default fill and 380
.filterconst char *patterns that files must match, e.g. "*.png;*.jpg"; folders always show
.folders_onlyboollists folders and leaves the files out
.hiddenboolshow hidden files
.no_placesboolthe sidebar of places
.placesconst char *const *full paths for the sidebar, each shown by its last part; at most 6
.place_countintentries in places; with places NULL the sidebar shows home, desktop, documents, downloads and the root
.listcui_file_list_fnfunction that lists a folder; set it to browse something other than the local disk
.list_uservoid *cui_fs_list, the local diskpassed to list as its first argument; list defaults to cui_fs_list, the local disk
.max_entriesint20,000default 20,000

Options of cui_file_picker

OptionTypeDefaultDescription
.titleconst char *"Open", "Save" or "Choose a folder"default "Open", "Save" or "Choose a folder"
.saveboola name field and Save
.foldersboolchoose a folder
.startconst char *the current directorythe folder to start in, default the current directory
.nameconst char *save: the name to start with
.filterconst char *e.g. "*.png;*.jpg"
.filter_labelconst char *e.g. "Images"
.listcui_file_list_fn`cui_fs_list`function that lists a folder (the local disk unless set); default cui_fs_list
.list_uservoid *passed to list as its first argument

cui_file_browser_result

FieldTypeDescription
pickedconst char *a file double-clicked or Entered, as a full path; valid this frame
selectedconst char *the selected entry as a full path, or NULL; valid this frame
navigatedboolthe folder in *path changed
errorboolthe folder could not be read

Related

  • Dropzone: An area that takes files dropped from the desktop, and can open the system file dialog on a click.
  • Button: Starts an action when clicked. It comes in six variants and three sizes, with an optional icon and a loading state.
  • Code block: Read-only source code with syntax highlighting, optional line numbers and a copy button.
  • Tree table and path bar: A tree with columns, as in a file explorer or a debugger's variables, and a path bar for moving up through folders.