{"record":{"id":"f029b01d877b667d","repo":"slint-ui/slint","slug":"the-window-shouldn-t-be-initialized-before-this-ca","errorCode":null,"errorMessage":"The window shouldn't be initialized before this call","messagePattern":"The window shouldn't be initialized before this call","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/compiler/generator/rust.rs","lineNumber":673,"sourceCode":"    // `create_window_from_context` is only invoked from a Window-rooted\n    // public component's `new_with_context`, and `maybe_window_adapter_impl`\n    // is only invoked from per-tree `register_item_tree` / PinnedDrop hooks\n    // — both gated out for tray-only units. Emit them only when something\n    // actually calls them; otherwise `#![deny(warnings)]` builds (e.g.\n    // test-driver-rust with `--features build-time`) trip on dead_code.\n    // `window_adapter_impl` / `window_adapter_ref` are kept unconditionally\n    // because expression codegen (layout-info, font metrics) still\n    // references them on every tree.\n    let optional_window_adapter_helpers = needs_window_adapter.then(|| {\n        quote!(\n            #[cfg(#experimental)]\n            fn create_window_from_context(&self, ctx: sp::SlintContext) -> sp::Result<(), slint::PlatformError> {\n                let adapter = ctx.platform().create_window_adapter()?;\n                sp::WindowInner::from_pub(adapter.window()).set_context(ctx);\n                let root_rc = self.root_item_tree_weak.upgrade().unwrap();\n                sp::WindowInner::from_pub(adapter.window()).set_component(&root_rc);\n                #apply_constant_scale_factor\n                self.window_adapter.set(adapter).map_err(|_|()).expect(\"The window shouldn't be initialized before this call\");\n                sp::Ok(())\n            }\n\n            #[cfg(#experimental)]\n            fn create_window_from_existing(&self, window: &slint::Window) -> sp::Result<(), slint::PlatformError> {\n                let adapter = sp::WindowInner::from_pub(window).window_adapter();\n                let root_rc = self.root_item_tree_weak.upgrade().unwrap();\n                sp::WindowInner::from_pub(adapter.window()).set_component(&root_rc);\n                #apply_constant_scale_factor\n                self.window_adapter.set(adapter).map_err(|_|()).expect(\"The window shouldn't be initialized before this call\");\n                sp::Ok(())\n            }\n\n            fn maybe_window_adapter_impl(&self) -> sp::Option<sp::Rc<dyn sp::WindowAdapter>> {\n                self.window_adapter.get().cloned()\n            }\n        )\n    });","sourceCodeStart":655,"sourceCodeEnd":691,"githubUrl":"https://github.com/slint-ui/slint/blob/3a7e700487dc74d404cad0726378053ab763d4e0/internal/compiler/generator/rust.rs#L655-L691","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nself.window_adapter.set(adapter).map_err(|_|()).expect(\"The window shouldn't be initialized before this call\");\n// after\nif self.window_adapter.set(adapter).is_err() {\n    return sp::Err(\"window adapter already initialized\".into());\n}","handlingStrategy":"validation","validationCode":"// call before any window use\nassert!(component.window_handle_internal_maybe_window_adapter().is_none(),\n    \"window already initialized; call create_window_from_context first\");","typeGuard":"fn window_not_initialized(adapter_slot: &Option<Rc<dyn WindowAdapter>>) -> bool {\n    adapter_slot.is_none()\n}","tryCatchPattern":"match self.window_adapter.set(adapter) {\n    Ok(()) => sp::Ok(()),\n    Err(_) => sp::Err(\"window adapter already initialized\".into()),\n}","preventionTips":["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"],"tags":["rust-codegen","windowing","initialization-order","panic"],"backgroundTag":"invalid-state-transition","analyzedSha":"3a7e700487dc74d404cad0726378053ab763d4e0","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}