slint-ui/slint · error
The window shouldn't be initialized before this call
Error message
The window shouldn't be initialized before this call
What it means
In generated Rust code for shared-globals/experimental window creation, `self.window_adapter` is an `OnceCell`-like slot that must be empty when `create_window_from_context` installs a freshly created adapter. `.set(adapter).map_err(|_|()).expect(...)` panics if the slot already holds a window adapter, meaning the window was initialized twice.
Solutions
- Call `create_window_from_context` before any access to the window or showing the component
- Check `maybe_window_adapter_impl`/`window_adapter.get()` first and skip creation if an adapter already exists
- Avoid triggering adapter creation (e.g. via window handle access) before installing the custom one
Example fix
// before
self.window_adapter.set(adapter).map_err(|_|()).expect("The window shouldn't be initialized before this call");
// after
if self.window_adapter.set(adapter).is_err() {
return sp::Err("window adapter already initialized".into());
} Defensive patterns
Strategy: validation
Validate before calling
// call before any window use
assert!(component.window_handle_internal_maybe_window_adapter().is_none(),
"window already initialized; call create_window_from_context first"); Type guard
fn window_not_initialized(adapter_slot: &Option<Rc<dyn WindowAdapter>>) -> bool {
adapter_slot.is_none()
} Try / catch
match self.window_adapter.set(adapter) {
Ok(()) => sp::Ok(()),
Err(_) => sp::Err("window adapter already initialized".into()),
} Prevention
- Call create_window_from_context before touching the component's window
- Do not show the component or access window() before installing the adapter
- Check maybe_window_adapter_impl before attempting installation
- Keep custom-window-adapter setup at the very start of app initialization
When it happens
Trigger: Calling `create_window_from_context` (experimental shared-globals API) on a component instance whose window adapter was already set — e.g. the window was shown, `window()` was accessed triggering adapter creation, or the method is invoked twice.
Common situations: Applications using the experimental shared-window/context API that touch `slint::Window` before calling `create_window_from_context`; double initialization after re-instantiating or reconfiguring the component; ordering issues with backend selection creating an adapter eagerly.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Callbacks were set up earlier
- a setter belongs to a field
- an identifier
- binding was of the wrong type
- C++: NativeWindowHandle created with null hwnd
AI-assisted analysis of slint-ui/slint@3a7e700487 (2026-09-16).
Data as JSON: /api/errors/f029b01d877b667d.
Report an issue: GitHub.
Appendix: source
Thrown at internal/compiler/generator/rust.rs:673
// `create_window_from_context` is only invoked from a Window-rooted
// public component's `new_with_context`, and `maybe_window_adapter_impl`
// is only invoked from per-tree `register_item_tree` / PinnedDrop hooks
// — both gated out for tray-only units. Emit them only when something
// actually calls them; otherwise `#![deny(warnings)]` builds (e.g.
// test-driver-rust with `--features build-time`) trip on dead_code.
// `window_adapter_impl` / `window_adapter_ref` are kept unconditionally
// because expression codegen (layout-info, font metrics) still
// references them on every tree.
let optional_window_adapter_helpers = needs_window_adapter.then(|| {
quote!(
#[cfg(#experimental)]
fn create_window_from_context(&self, ctx: sp::SlintContext) -> sp::Result<(), slint::PlatformError> {
let adapter = ctx.platform().create_window_adapter()?;
sp::WindowInner::from_pub(adapter.window()).set_context(ctx);
let root_rc = self.root_item_tree_weak.upgrade().unwrap();
sp::WindowInner::from_pub(adapter.window()).set_component(&root_rc);
#apply_constant_scale_factor
self.window_adapter.set(adapter).map_err(|_|()).expect("The window shouldn't be initialized before this call");
sp::Ok(())
}
#[cfg(#experimental)]
fn create_window_from_existing(&self, window: &slint::Window) -> sp::Result<(), slint::PlatformError> {
let adapter = sp::WindowInner::from_pub(window).window_adapter();
let root_rc = self.root_item_tree_weak.upgrade().unwrap();
sp::WindowInner::from_pub(adapter.window()).set_component(&root_rc);
#apply_constant_scale_factor
self.window_adapter.set(adapter).map_err(|_|()).expect("The window shouldn't be initialized before this call");
sp::Ok(())
}
fn maybe_window_adapter_impl(&self) -> sp::Option<sp::Rc<dyn sp::WindowAdapter>> {
self.window_adapter.get().cloned()
}
)
});View on GitHub (pinned to 3a7e700487)