Skip to content

Application API ​

Create an application with app = CryKit::Application.new. Use its managers for native APIs, and the methods below for setup and lifecycle.

Lifecycle ​

Method or propertyBehavior
app.run(log_level: :info)Run startup hooks, open initial windows and start the UI loop
app.quitRequest application shutdown; window close listeners can cancel it
app.log_levelRead the selected Log::Severity
app.quit_on_last_window_closed?Read the application's last-window lifetime policy
app.macosRead immutable macOS activation options

Call run on the OS main thread. Application instances are single-use. Live native operations run on the UI context, such as a mounted view callback. See startup, shutdown, and logging for lifecycle details.

Native managers ​

PropertyMethods and controls
app.windowscreate, [], []?, each; window geometry, visibility, focus, events and file interaction
app.dialogsopen_file, open_files, open_directory, save_file, info, warning, error, confirm, question
app.menusregister, create, [], []?, find_by; menu items and action events
app.traysregister, create, [], []?, find_by, remove; status items and click events
app.systemos, screen_info, accent_color, open_in_file_manager, reveal_in_file_manager

Constructor options ​

KeywordDefaultPurpose
quit_on_last_window_closed:trueSet false for an application that runs with zero windows
macos:MacOSOptions.newSet activation_policy: :regular or :accessory
worker_parallelism:max(1, System.cpu_count - 1)Number of worker threads
worker_capacity:Execution::WorkerExecutor::DEFAULT_CAPACITYMaximum outstanding jobs
executor:Worker executorInject a JobExecutor
window_backend:Native backendInject a window backend
dialog_backend:Native backendInject a dialog backend
menu_backend:Native backendInject a menu backend
tray_backend:Native backendInject a tray backend
system_backend:Native backendInject a system backend

An optional positional scheduler defaults to PeriodicScheduler.new. Backend and executor injection support windowless tests.

crystal
app = CryKit::Application.new(
  macos: CryKit::Application::MacOSOptions.new(activation_policy: :accessory),
  quit_on_last_window_closed: false)

See activation and windowless lifetime for Dock visibility and resident applications.

Startup hooks ​

Override these protected methods in an application subclass. run calls them once, in this order, before native startup.

OrderHookTypical setup
1configure : NilAssets and application configuration
2register_handlers : Nilhandlers.register AppHandler
3register_menus : Nilmenus.register AppMenu
4register_trays : Niltrays.register TrayMenu
5register_windows : Nilwindows.create(id: :main, path: "/")

Setup directly on app before run is also supported. See the startup hook example.

Routes and handlers ​

APIBehavior
app.routes.draw { ... }Declare routes with get "/path", as: :name do ... end
app.routes[:name]Find a named Route; raises KeyError if absent
app.routes[:name]?Find a named Route, or return nil
app.handlers.register AppHandler, OtherHandlerRegister handler classes with access to the application
app.content_url("/path")Resolve a local path to its app://crykit URL

Routes match exact GET paths. Route exposes path : String and name : Symbol?. /assets is reserved; duplicate paths and names are rejected. See routes and handlers and shared state.

Assets and content ​

APIBehavior
app.assets = CryKit::Assets.directory("assets")Declare assets relative to the declaring Crystal source file
app.asset_path("main.css")Build the /assets/ URL for an asset
app.assets.path = directorySelect a disk asset directory before startup
app.assets.hot_reload?, app.assets.hot_reload = enabledRead or set development asset reloading before startup
app.contentAccess the application's local content dispatcher
app.viewsAccess the application's view registry

crykit dev serves declared assets from disk; crykit build embeds them. See assets for paths, caching and range requests.

Background work and notifications ​

APIBehavior
app.async { |job| ... }Start application-owned work; returns AppJob, cancelled on shutdown
app.broadcast_to(:channel, value)Publish a typed value; returns whether delivery was accepted
app.notificationsAccess the application's event bus
app.executorAccess the application's job executor

Application jobs publish through app.broadcast_to; views declare notification handlers with subscribe_to. View-owned jobs use job.notify for UI updates. See application jobs and channels.

Built with Crystal. Documented with VitePress.