fix(macos): support airplay and external stage display when running ios app on apple silicon

Issue

When running the iOS App Store build on an Apple Silicon Mac (“Designed for iPad” compatibility mode, ProcessInfo.processInfo.isiOSAppOnMac == true), AirPlay / External Stage View does not open a dedicated 16:9 / 4:3 stage display on the Apple TV while keeping the Presenter View & controls on the Mac. Instead, macOS only mirrors the screen/window or extends the desktop without the app detecting or presenting a separate stage window.

On iOS and iPadOS, this works automatically: connecting an Apple TV via Screen Mirroring presents HfwExternalStageView full-screen on the Apple TV while keeping HfwPresenterControlView and controls on the iPhone/iPad.


Root Cause & Findings

  1. macOS AirPlay WindowServer behavior vs. iOS UIWindowSceneSessionRoleExternalDisplayNonInteractive:
    • On iOS/iPadOS, selecting an Apple TV in Control Center → Screen Mirroring creates a secondary UIScreen and connects a UIWindowScene with role .windowExternalDisplayNonInteractive (packages/hfw_flutter/ios/Runner/AppDelegate.swift), which HfwExternalDisplayManager attaches HfwExternalStageView to.
    • On macOS, AirPlay to Apple TV operates at the macOS WindowServer level in either Mirror Display/Window mode or Use as Separate Display mode (which attaches a secondary NSScreen).
    • Apple’s “Designed for iPad” (isiOSAppOnMac) runtime on macOS does not synthesize a .windowExternalDisplayNonInteractive UIWindowScene or expose secondary NSScreen displays in UIScreen.screens.
  2. Compile-time #if os(macOS) gating in HfwExternalDisplayManager.swift:
    • Because the iOS App Store build is compiled under #if os(iOS), all of the macOS external stage support in packages/hfw_flutter/ios/Runner/HfwExternalDisplayManager.swift is compiled out when running on Apple Silicon Mac via the iOS App Store:
      • NSScreen.screens.dropFirst() detection (refreshExternalDisplays())
      • toggleMacExternalStage(store:) and external stage window management
      • The “Start External Stage on [Display]” button and macOS setup instructions in HfwStageConnectionSheet

Implementation Plan

  1. Runtime Secondary Display Detection on isiOSAppOnMac:
    • Update HfwExternalDisplayManager (packages/hfw_flutter/ios/Runner/HfwExternalDisplayManager.swift) so that when HfwRuntimePlatform.isRunningOnMac (ProcessInfo.processInfo.isiOSAppOnMac) is true, it observes NSApplicationDidChangeScreenParametersNotification and inspects NSScreen.screens via the Objective-C runtime (NSClassFromString("NSScreen"), matching the pattern in HfwStore.updateMacWindowTitle()) to set hasPhysicalExternalDisplay and externalDisplayName.
  2. Dedicated Stage Window / Scene on isiOSAppOnMac:
    • Implement stage window presentation for isiOSAppOnMac by activating a dedicated stage UIWindowScene (via UIApplication.shared.requestSceneSessionActivation with a dedicated stage activity/configuration in AppDelegate.swift) hosting HfwExternalStageView, and positioning/fullscreening its underlying NSWindow onto the secondary NSScreen when Use as Separate Display is active (or allowing the user to mirror that standalone stage window to Apple TV).
    • Ensure HfwStore.updateMacWindowTitle() titles the stage window appropriately (e.g. Hymns for Worship — Stage) and preserves borderless/fullscreen chrome on the external display.
  3. Unify Stage Connection Sheet UI & Auto-Presenter Workflow:
    • Update HfwStageConnectionSheet to use HfwRuntimePlatform.isRunningOnMac (instead of only #if os(macOS)) so both the native macOS build and the isiOSAppOnMac build display the “Start External Stage on [Display]” toggle button and macOS AirPlay instructions.
    • Automatically enable store.isPresenterControllerVisible = true on the main Mac window when starting the external stage so the worship leader immediately gets the Current/Next slide previews and transport controls on the Mac while the Apple TV displays the clean stage output.

Automated Issue Triage

Metadata Updates:

  • Assigned label: bug (55)
  • Verified title adheres to Conventional Commits: fix(macos): support airplay and external stage display when running ios app on apple silicon

Codebase Analysis & Root Cause:

  1. External Screen Detection Gap on isiOSAppOnMac (HfwExternalDisplayManager.swift):
    • packages/hfw_flutter/ios/Runner/HfwExternalDisplayManager.swift (setupNotifications, lines 87–137; refreshExternalDisplays, lines 201–261) is compiled under #if os(iOS) in the iOS build.
    • It relies on UIScene.willConnectNotification and UIScreen.screens.filter { $0 != UIScreen.main }. On Apple Silicon Mac running the iOS app (ProcessInfo.processInfo.isiOSAppOnMac == true), macOS WindowServer manages AirPlay and external monitors as NSScreen instances rather than synthesizing UIScreen connect events or .windowExternalDisplayNonInteractive scenes. Consequently, hasPhysicalExternalDisplay remains false.
  2. Compile-Time #if os(macOS) Gating (HfwExternalDisplayManager.swift):
    • The stage window lifecycle methods (toggleMacExternalStage(store:), presentMacExternalWindow, and closeMacExternalWindow, lines 351–391) are gated inside #elseif os(macOS), causing them to be excluded from the iOS binary.
    • In HfwStageConnectionSheet (lines 505–530), the "Start External Stage on [Display]" button is wrapped in #if os(macOS) and therefore hidden on isiOSAppOnMac.
    • The instructional steps in HfwStageConnectionSheet (lines 617–644) fall back to iOS Control Center swipe instructions rather than macOS AirPlay / “Use as Separate Display” guidance.
  3. AppKit Dynamic Runtime Solution (HfwPlatformCompat.swift & HfwStore.swift):
    • packages/hfw_flutter/ios/Runner/HfwPlatformCompat.swift (HfwRuntimePlatform.isRunningOnMac, lines 12–23) already detects ProcessInfo.processInfo.isiOSAppOnMac.
    • packages/hfw_flutter/ios/Runner/HfwStore.swift (updateMacWindowTitle, lines 802–816) establishes the project convention for interacting with AppKit classes via NSClassFromString without compile-time AppKit imports. A similar dynamic bridge can observe NSApplicationDidChangeScreenParametersNotification, inspect NSScreen.screens, and manage the borderless secondary stage window or scene.
  4. Scene Configuration (AppDelegate.swift & Info.plist):
    • packages/hfw_flutter/ios/Runner/AppDelegate.swift (HfwExternalDisplaySceneDelegate, lines 1027–1050) and packages/hfw_flutter/ios/Runner/Info.plist configure UIWindowSceneSessionRoleExternalDisplayNonInteractive. Supporting multi-display presentation on isiOSAppOnMac can be achieved by either hosting HfwExternalStageView inside a dynamic AppKit NSWindow or activating an auxiliary scene session.

Codebase Research & Implementation Path

Confirmed still unfixed on main and in the current working tree (packages/hfw_flutter/ios/Runner/HfwExternalDisplayManager.swift, lines 201–391). Uncommitted local edits refine the native macOS stage sheet, but NSScreen detection and toggleMacExternalStage remain gated behind #elseif os(macOS).

Strategic check first: Since the native macOS target (packages/hfw_flutter/macos/) is actively being built out, confirm whether isiOSAppOnMac App Store compatibility mode still needs dedicated stage support or will be superseded by the native macOS app.

If implementing for isiOSAppOnMac:

  1. Runtime NSScreen detection (HfwExternalDisplayManager.swift): Inside #if os(iOS), when HfwRuntimePlatform.isRunningOnMac (ProcessInfo.processInfo.isiOSAppOnMac) is true, observe Notification.Name("NSApplicationDidChangeScreenParametersNotification") and inspect NSClassFromString("NSScreen")?.value(forKey: "screens") as? [NSObject] (following the dynamic KVC pattern already established in HfwStore.updateMacWindowTitle(), lines 802–816) to populate hasPhysicalExternalDisplay and externalDisplayName (localizedName).
  2. Dedicated stage scene/window (AppDelegate.swift + HfwExternalDisplayManager.swift): Register a secondary stage UIWindowScene configuration (e.g., activity type app.hymnsforworship.stage) in AppDelegate.configurationForConnecting, activate it via UIApplication.shared.requestSceneSessionActivation(...) hosting HfwExternalStageView, position/borderless-fullscreen its backing NSWindow onto the secondary NSScreen, and title it Hymns for Worship — Stage (excluding it from HfwStore.updateMacWindowTitle() overwriting the title).
  3. UI & Presenter auto-enable (HfwExternalDisplayManager.swift): Switch HfwStageConnectionSheet controls and macOS AirPlay (“Use as Separate Display”) instructions from #if os(macOS) to HfwRuntimePlatform.isRunningOnMac, and automatically set store.isPresenterControllerVisible = true when external stage starts.