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.
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()
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)
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.
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)
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)
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.
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 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
Continue with Fonts and FontDialog to select and apply operating-system fonts through a typed API.