7.6 KiB
Kryon App Integration
This document is for developers using Kryon inside applications. It describes the behavior Kryon owns, what application code should keep, and how to update downstream projects without editing vendored copies by hand.
Role
Kryon is a small native UI runtime with a Kryon-owned clean app API. The current default backend is raylib, but applications should not depend on raylib directly. Applications should use Kryon for shared UI behavior instead of duplicating pointer capture, modal backdrop, button, tab, scroll, text input, theme, and DPI logic in each project.
See docs/BOUNDARIES.md for the architectural line between reusable Kryon
runtime behavior and downstream application behavior. See docs/ARCHITECTURE.md
for the current subsystem map.
Keep application code focused on product state and domain behavior. Move repeated UI interaction rules into Kryon when more than one screen or project needs them.
Frame Lifecycle
Call BeginInterfaceFrame(width, height, dpi) once at the start of a normal
screen-space UI frame. If the application uses a transformed UI camera, call
InitInterface(width, height, dpi) and then SetFrameCamera(camera) instead. Kryon sanitizes
invalid cameras before using them; a zero-initialized Camera2D is treated as
GetDefaultCamera() so pointer input does not silently break.
After drawing all widgets, call EndInterfaceFrame() before the backend's
EndFrame(). The closing call draws deferred overlays such as dropdowns and
text-field context menus, then finalizes focus and inspection state. Applications
must not reproduce those overlay or finalization steps themselves.
Beginning a frame resets per-frame interaction state:
- input blocking
- modal capture state from the previous frame
- input clip stack
- focus registrations
- pointer gesture state
- clip state
Do not persist ui_set_input_blocked or input clips across frames. Register them again
while rendering the UI that needs them. Modal capture is registered while drawing the
modal and Kryon carries it into the next frame. Until the current frame registers the
modal bounds again, Kryon captures all pointer input so controls underneath the modal
cannot receive clicks before the modal is drawn.
Input Capture
Every clickable Kryon control should gate pointer interaction through
InputCapturesClick(point) or the lower-level helpers already used inside Kryon.
That keeps scroll drags, dropdowns, modal backdrops, and explicit input blockers from
leaking clicks to the screen underneath.
Use these APIs by responsibility:
InputCapturesClick(point): normal public check for app or component code.ui_base_input_captures_click(point, include_pointer_drag): internal/specialized components that need to decide whether pointer dragging should count as capture.ui_set_input_blocked(blocked): full-frame blocking for overlays that should stop all controls for the rest of the frame.SetModalCapture(bounds): modal-specific capture that blocks background clicks, then allows controls inside the active modal rectangle after the modal registers its bounds. It applies immediately and to the next frame.
Modal Rules
Kryon modal helpers own backdrop capture. When an application uses Modal,
it should not add separate outside-click
guards or duplicate backdrop blocking. The helper registers the modal bounds for the
current frame and the next frame.
Prefer Modal for title, message, and action-button dialogs. It measures button
labels, fits text inside buttons, and wraps actions into multiple rows when localized
labels do not fit. Use manual SetModalCapture only for specialized overlays that
cannot use the shared modal frame.
When an application draws a custom modal manually, it must call
SetModalCapture((Rectangle){x, y, w, h}) immediately after calculating the modal
rectangle and before drawing modal controls. This prevents clicks outside the modal from
activating the underlying screen, prevents controls underneath the modal from receiving
clicks before the modal is drawn, and keeps buttons, sliders, dropdowns, and text fields
inside the modal usable.
Do not manage modal click blocking in application-level route code. The application should decide whether a modal is open and what state changes happen after a modal button is clicked; Kryon should decide whether pointer input is captured.
Custom Modal Pattern
int modal_w = Scale(320);
int modal_h = Scale(240);
int modal_x = (view_width - modal_w) / 2;
int modal_y = (view_height - modal_h) / 2;
SetModalCapture((Rectangle){
(float)modal_x, (float)modal_y, (float)modal_w, (float)modal_h
});
DrawRectangle(0, 0, view_width, view_height, (Color){0, 0, 0, 180});
DrawRectangle(modal_x, modal_y, modal_w, modal_h, GetThemeSurface());
Before this call on a frame following an active modal, Kryon captures all pointer input. After this call, normal Kryon controls inside the rectangle remain clickable because their hit tests are inside the active modal bounds. Controls outside the rectangle see the click as captured in the current frame and the next frame.
What To Move Into Kryon
Move behavior into Kryon when it is general UI behavior rather than product behavior:
- repeated modal shell drawing or backdrop capture
- repeated button hit testing, hover, disabled, and focus handling
- repeated scroll clipping or scrollbar handling
- repeated dropdown open/capture behavior
- repeated text input editing and platform text input activation
- repeated theme color derivation or DPI scaling helpers
Keep behavior in the app when it is domain-specific:
- navigation route selection
- practice/session state machines
- settings persistence
- app-specific copy and locale keys
- app-specific icons and assets
- business rules for confirmation or cancellation
File Dialog Backends
Kryon owns desktop file dialog backend selection. Applications should call
LoadFileDialog, SaveFileDialog, or SelectFileDialogFolder instead of invoking
desktop-specific picker commands directly.
The default backend order is XDG Desktop Portal, GTK, zenity, then kdialog,
then yad. For local diagnostics and packaging checks,
KRYON_FILE_DIALOG_BACKEND can be set to auto, portal, gtk, zenity,
kdialog, yad, or none.
Do not add permanent app dependencies on an XFCE-specific picker. If a downstream app needs different desktop behavior, add the backend support in Kryon first, then update the downstream submodule pointer.
Linux Desktop Metadata
Kryon owns the common Linux desktop package layout. Applications should expose
portable metadata in project.kryon or the matching Make variables (APP_ID,
APP_TITLE, APP_SUMMARY, APP_CATEGORIES, APP_MIME_TYPES,
APP_URL_SCHEMES, APP_ICON, APP_DESKTOP_ID, APP_ICON_NAME,
APP_WM_CLASS) and build it with kryon package linux-desktop. The target
stages the binary, .desktop entry, AppStream metainfo, hicolor icon, and
optional MIME package under one AppDir-style tree.
Application code should call InitDesktopApp once at startup when it needs
desktop identity, XDG config/data/cache directories, notification icon identity,
single-instance locking, or open-file/open-URL dispatch.
Downstream Submodule Workflow
Applications should vendor Kryon as a git submodule. Do not edit
vendor/kryon directly for permanent changes.
Use this flow:
- Edit the real Kryon repository.
- Commit and push Kryon.
- In the downstream app, update the
vendor/kryonsubmodule to the pushed commit. - Commit the downstream app's submodule pointer and any app code needed to use the new Kryon API.
This keeps Kryon reusable and prevents project-local vendor edits from diverging.