GUI Fonts Dialog

Fonts and FontDialog

Font is the immutable font value used by every window widget. FontDialog lists the families installed by the operating system, renders a live sample for every visible family, and returns a typed font ready to assign to a widget. FontLightBox exposes the same selector as an embedded modal overlay.

The Font Value

A font combines a family, a logical pixel size, and optional bold and italic styles. Assigning it to Widget.font invalidates only that widget's resolved text style and requests one repaint.

import klyn.gui.windows

heading = Label("Quarterly report")
heading.font = Font(
    "Noto Sans",
    24,
    bold=true
)

code = TextBox()
code.font = Font("Cascadia Code, DejaVu Sans Mono, monospace", 15)

Font is immutable. Use withSize(), withBold(), or withItalic() when deriving another value. Font.parse() converts an existing CSS-like painter description when configuration is still stored as text.

base = Font("Noto Serif", 16)
heading = base.withSize(22).withBold()
emphasis = base.withItalic()

legacy = Font.parse("italic 18px Noto Serif")
Embedded Font Selection

Use FontLightBox in workbench-style applications that must not create a separate window or nested event loop. Add it once to the root overlay container, react to accepted, and call open() when needed. The lightbox is implicitly modal and therefore has no modal property.

chooser = FontLightBox(label.font, "Application font")
chooser.previewText = label.text
chooser.accepted += lambda(event: ActionEvent):
    label.font = chooser.selectedFont

chooser.layoutParams = DockLayoutParams("fill")
window.centralWidget.add(chooser)
chooser.open()
Installed Families

Font.installedFamilies() asks the native backend for the operating-system catalogue. The result is sorted, case-insensitively deduplicated, and returned as a fresh mutable list. The catalogue itself is discovered only once per process.

for family in Font.installedFamilies():
    print(family)

Windows uses the Unicode font enumeration API. Linux uses the system Fontconfig catalogue when available. Klyn keeps a small deterministic fallback list for minimal environments.

Select a Font

Initialize FontDialog with the current widget font. The dialog preserves the accepted selection between openings and leaves it unchanged when the user cancels.

import klyn.gui.windows.dialogs

dialog = FontDialog(label.font, "Application font")
dialog.previewText = "Sphinx of black quartz, judge my vow."

if dialog.showDialog(window):
    label.font = dialog.selectedFont

The search field filters families immediately. Arrow keys navigate the list, Enter or a double-click accepts a family, and Escape cancels. The list is virtualized: only visible rows are measured and painted, even on systems with thousands of installed fonts.

Themes and Preview

The dialog is implemented with standard Klyn widgets and follows the owner window's light or dark appearance. Runtime content zoom scales the controls, row metrics, scrollbar, and preview consistently.

Every visible family row uses its own font. Click the image to inspect it at full size.
Run the Sample

The sample opens FontLightBox and applies the accepted Font to a preview label. The preceding section retains the blocking FontDialog form for standalone dialog workflows.

klyn samples/gui/FontDialogSample.kn
Next Step

Continue with DockWorkspace when your application needs an IDE-like shell with retractable panes and central editor tabs.