JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
16.26k stars 1.18k forks source link

[iOS] Using Compose inside a ShareExtensionViewController renders it black #4610

Closed alexstyl closed 2 months ago

alexstyl commented 7 months ago

Describe the bug I am trying to render Compose on a share extension. No matter what I tried, the view renders black.

Affected platforms

Versions

To Reproduce Create an iOS Share Extension, replace the ShareViewController with:

import UIKit
import SwiftUI
import ComposeApp

@objc(ShareExtensionViewController)
class ShareViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let rootView = ComposeView()
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .background(.blue)
        let controller = UIHostingController(rootView: rootView)
       // also tried let controller = MainViewControllerKt.ShareViewController()

        controller.view.frame = self.view.bounds
        controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        controller.view.backgroundColor = .clear

        addChild(controller)
        view.addSubview(controller.view)
        controller.didMove(toParent: self)
    }
}

struct ComposeView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UIViewController {
        MainViewControllerKt.ShareViewController()
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

Expected behavior The contents of the MainViewControllerKt.ShareViewController() to be rendered

Actual behavior The Compose View is rendered as a black view

Screenshots Screenshot 2024-04-13 at 15 08 36

elijah-semyonov commented 7 months ago

Can you try to rewrite this code using the anchor constraints instead of autoresize mask? It seems to me that the size could be calculated incorrectly in this code.

alexstyl commented 7 months ago

I tried this code and the result is the same:

let rootView = ComposeView()
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Color.white)
        let controller = UIHostingController(rootView: rootView)

        self.addChild(controller)

        controller.view.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(controller.view)

        controller.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        controller.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        controller.view.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        controller.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

        controller.didMove(toParent: self)
alexstyl commented 7 months ago

Keep in mind that I tried the same code to render a Swift View and the contents of the Swift View were displayed correctly.

elijah-semyonov commented 7 months ago

Thanks, I'll have a look

alexstyl commented 6 months ago

@elijah-semyonov did u have a look? could you kindly confirm that is indeed a bug in compose?

elijah-semyonov commented 6 months ago

@alexstyl Yes, I reproduced it. Investigating.

elijah-semyonov commented 6 months ago

UIApplication.shared is illegal to access in App Extensions and we still wrongfully do it in order to ensure, that no drawing happens when app is in background (which it says it is, it's UB most likely anyway). Working around it will also require us to update Skia. The version we use still performs MtlIsAppInBackground check (which does the same thing) and wrongfully exits not even trying to perform the draw logic.

This is possible to fix, but a bit later. We didn't plan supporting such a feature and didn't do any sort of investigations, so there are some implications in existing code (regarding lifecycle, etc).

okushnikov commented 2 months ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.