GUI Files Folders

File and Folder Dialogs

The klyn.gui.windows.dialogs package provides typed dialogs for opening files, choosing save destinations, and selecting folders. Application code uses one stable API while Klyn selects a native operating-system dialog or its themed portable implementation. Each operation is also available as an embedded modal LightBox, letting an application choose between a blocking standalone window and an asynchronous in-window workflow.

Dialog or LightBox

OpenFileDialog, SaveFileDialog, and OpenFolderDialog derive from Dialog. Their showDialog() method blocks until the operation finishes. The corresponding OpenFileLightBox, SaveFileLightBox, and OpenFolderLightBox classes are added once to a window root and report completion through accepted and cancelled events. A lightbox is always modal and deliberately exposes no modal property.

chooser = OpenFileLightBox("Open Klyn sources")
chooser.filters = [FileDialogFilter("Klyn sources", "*.kn")]
chooser.accepted += lambda(event: ActionEvent):
    for file in chooser.selectedFiles:
        print(file)

chooser.layoutParams = DockLayoutParams("fill")
window.centralWidget.add(chooser)
chooser.open()
Open Files

OpenFileDialog accepts filename filters and can select either one file or several files. A successful result always contains normalized absolute paths to existing regular files. Filter labels include their wildcard extensions, for example Klyn sources (*.kn; *.kss).

import klyn.gui.windows.dialogs
import klyn.io

dialog = OpenFileDialog("Open Klyn sources")
dialog.initialFolder = Path.cwd
dialog.allowMultiple = true
dialog.filters = [
    FileDialogFilter("Klyn sources", "*.kn", "*.kss"),
    FileDialogFilter("All files", "*")
]

if dialog.showDialog(window):
    for file in dialog.selectedFiles:
        print(file)
Choose a Save Destination

SaveFileDialog chooses a destination but never creates or writes the file. This separation keeps persistence explicit and lets the application control encoding, transactions, and error handling. If the suggested name has no extension, the filename field immediately displays the explicit defaultExtension. When that property is empty, the extension is inferred from the selected filter and follows filter changes.

dialog = SaveFileDialog("Export report")
dialog.initialFolder = Path.cwd
dialog.suggestedName = "report"
dialog.defaultExtension = "html"
dialog.filters = [FileDialogFilter("HTML document", "*.html")]

if dialog.showDialog(window):
    dialog.selectedFile.write("<h1>Klyn report</h1>")

Existing files require confirmation by default. Set confirmOverwrite to false only when replacement is already managed by the surrounding workflow.

Select a Folder

OpenFolderDialog returns one existing folder as a normalized absolute FolderPath.

dialog = OpenFolderDialog("Select workspace")
dialog.initialFolder = Path.userHome

if dialog.showDialog(window):
    print("Workspace: " + dialog.selectedFolder)
Navigate Locations

The portable chooser and every file-selection lightbox display the current location as an Explorer-style breadcrumb. Click a folder name to return directly to that location. Click a > separator to list the folders available at that level; long menus remain accessible with the mouse wheel. Clicking the empty area to the right switches the breadcrumb to a selectable text field. Press Enter to open the entered path or Escape to restore the breadcrumb. Ctrl+L and Alt+D activate the same text-entry mode while the file list has focus.

On Windows, the first > opens the logical-drive selector. The same roots are available to application code through Path.roots(); Unix-like systems expose /.

import klyn.io

for root in Path.roots():
    print(root)
Result and Error Contract

showDialog() returns false only when the user cancels. It resets the previous selection before every invocation. Filesystem validation or backend failures throw DialogException, so cancellation never gets confused with an operational error.

try:
    if dialog.showDialog(window):
        print(dialog.selectedFiles)
    else:
        print("Selection cancelled")
catch error as DialogException:
    print("Dialog failed: " + error.message)
Workbench Integration

KlynEditor uses the embedded lightboxes for Open File, Open Workspace, and Save As. Its file filters are assembled from the language plugins loaded by the Workbench rather than from a fixed extension list. The graphical test runner uses the same lightboxes to load and save JSON reports without starting a nested window event loop.

Native and Portable Rendering

Windows uses the native Unicode file chooser. Other supported GUI backends use the Klyn chooser shown below. The portable implementation supports keyboard navigation, multiple selection, mouse-wheel scrolling, draggable scrollbars, filters, overwrite confirmation, breadcrumb navigation, light and dark KSS themes, and runtime content zoom. Double-click a folder to open it. The up-arrow button, Backspace, or Alt+Up returns to the containing folder; the house button opens the user home folder.

The portable chooser follows the same result contract as the native backend. Click the image to inspect it at full size.
Run the Sample

The distribution sample demonstrates all three embedded lightboxes and displays their typed result in the application window. The blocking dialog snippets above remain valid when a standalone chooser is preferable.

klyn samples/gui/FileDialogsSample.kn
Next Step

Continue with Fonts and FontDialog to select and apply operating-system fonts through a typed API.