tauri-apps/tauri · error

`Window::set_enabled` can only be called on the main thread

Error message

`Window::set_enabled` can only be called on the main thread

What it means

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.

Solutions

  1. Dispatch the call to the main thread with `app.run_on_main_thread(|| window.set_enabled(...))`.
  2. Gate the call with a `MainThreadMarker` you already hold in the caller.
  3. Use a channel/event to ask the main-thread event loop to perform the toggle.

Example fix

// before
window.set_enabled(false);

// after
app.run_on_main_thread(move || {
  window.set_enabled(false);
});
Defensive patterns

Strategy: type-guard

Validate before calling

// check thread before calling macOS-only APIs
if !main_thread_marker_available() { route_to_main_thread(); }

Type guard

fn on_main_thread() -> bool {
  objc2::MainThreadMarker::new().is_some()
}

Try / catch

// wrap platform-specific toggles
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| window.set_enabled(false)));
if r.is_err() { eprintln!("set_enabled called off main thread"); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of tauri-apps/tauri@460ec35447 (2026-09-18). Data as JSON: /api/errors/9c96ce11e439b687. Report an issue: GitHub.

Appendix: source

Thrown at crates/tauri-runtime-wry/src/window/macos.rs:17

// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use objc2::MainThreadMarker;
use objc2_app_kit::{NSBackingStoreType, NSWindow, NSWindowStyleMask};
use tao::platform::macos::WindowExtMacOS;

impl super::WindowExt for tao::window::Window {
  // based on electron implementation
  // https://github.com/electron/electron/blob/15db63e26df3e3d59ce6281f030624f746518511/shell/browser/native_window_mac.mm#L474
  fn set_enabled(&self, enabled: bool) {
    let ns_window: &NSWindow = unsafe { &*self.ns_window().cast() };
    if !enabled {
      let frame = ns_window.frame();
      let mtm = MainThreadMarker::new()
        .expect("`Window::set_enabled` can only be called on the main thread");
      let sheet = unsafe {
        NSWindow::initWithContentRect_styleMask_backing_defer(
          mtm.alloc(),
          frame,
          NSWindowStyleMask::Titled,
          NSBackingStoreType::Buffered,
          false,
        )
      };
      sheet.setAlphaValue(0.5);
      ns_window.beginSheet_completionHandler(&sheet, None);
    } else if let Some(attached) = ns_window.attachedSheet() {
      ns_window.endSheet(&attached);
    }
  }

  fn is_enabled(&self) -> bool {
    let ns_window: &NSWindow = unsafe { &*self.ns_window().cast() };

View on GitHub (pinned to 460ec35447)