{"record":{"id":"9c96ce11e439b687","repo":"tauri-apps/tauri","slug":"window-set-enabled-can-only-be-called-on-the-ma","errorCode":null,"errorMessage":"`Window::set_enabled` can only be called on the main thread","messagePattern":"`Window::set_enabled` can only be called on the main thread","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tauri-runtime-wry/src/window/macos.rs","lineNumber":17,"sourceCode":"// Copyright 2019-2024 Tauri Programme within The Commons Conservancy\n// SPDX-License-Identifier: Apache-2.0\n// SPDX-License-Identifier: MIT\n\nuse objc2::MainThreadMarker;\nuse objc2_app_kit::{NSBackingStoreType, NSWindow, NSWindowStyleMask};\nuse tao::platform::macos::WindowExtMacOS;\n\nimpl super::WindowExt for tao::window::Window {\n  // based on electron implementation\n  // https://github.com/electron/electron/blob/15db63e26df3e3d59ce6281f030624f746518511/shell/browser/native_window_mac.mm#L474\n  fn set_enabled(&self, enabled: bool) {\n    let ns_window: &NSWindow = unsafe { &*self.ns_window().cast() };\n    if !enabled {\n      let frame = ns_window.frame();\n      let mtm = MainThreadMarker::new()\n        .expect(\"`Window::set_enabled` can only be called on the main thread\");\n      let sheet = unsafe {\n        NSWindow::initWithContentRect_styleMask_backing_defer(\n          mtm.alloc(),\n          frame,\n          NSWindowStyleMask::Titled,\n          NSBackingStoreType::Buffered,\n          false,\n        )\n      };\n      sheet.setAlphaValue(0.5);\n      ns_window.beginSheet_completionHandler(&sheet, None);\n    } else if let Some(attached) = ns_window.attachedSheet() {\n      ns_window.endSheet(&attached);\n    }\n  }\n\n  fn is_enabled(&self) -> bool {\n    let ns_window: &NSWindow = unsafe { &*self.ns_window().cast() };","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/tauri-apps/tauri/blob/460ec35447493200d64290dd7f015d5a91d0fd58/crates/tauri-runtime-wry/src/window/macos.rs#L1-L35","documentation":"On macOS, `Window::set_enabled` manipulates the NSWindow (attach/detach a sheet to disable interaction) and requires a `MainThreadMarker`. When called from any thread other than the main thread, `MainThreadMarker::new()` returns `None` and the code panics.","triggerScenarios":"Calling `window.set_enabled(false)` (or the higher-level modal/parent APIs that use it) from a spawned thread, async task, or non-main event-loop callback on macOS.","commonSituations":"Toggling window interactivity inside `tokio::spawn`, background workers, or IPC command handlers that run off the main thread; code that works on Windows/Linux but panics on macOS.","solutions":["Dispatch the call to the main thread with `app.run_on_main_thread(|| window.set_enabled(...))`.","Gate the call with a `MainThreadMarker` you already hold in the caller.","Use a channel/event to ask the main-thread event loop to perform the toggle."],"exampleFix":"// before\nwindow.set_enabled(false);\n\n// after\napp.run_on_main_thread(move || {\n  window.set_enabled(false);\n});","handlingStrategy":"type-guard","validationCode":"// check thread before calling macOS-only APIs\nif !main_thread_marker_available() { route_to_main_thread(); }","typeGuard":"fn on_main_thread() -> bool {\n  objc2::MainThreadMarker::new().is_some()\n}","tryCatchPattern":"// wrap platform-specific toggles\nlet r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| window.set_enabled(false)));\nif r.is_err() { eprintln!(\"set_enabled called off main thread\"); }","preventionTips":["Always dispatch window mutations via app.run_on_main_thread.","Never call window APIs directly from async tasks or spawned threads on macOS.","Guard platform-specific code with #[cfg(target_os = \"macos\")] plus a main-thread assertion."],"tags":["macos","main-thread","nswindow","threading"],"backgroundTag":"unsupported-operation","analyzedSha":"460ec35447493200d64290dd7f015d5a91d0fd58","analyzedAt":"2026-09-18T23:55:51.277Z","contentChangedAt":"2026-09-18T23:55:51.277Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}