How to know the active & foreground scene in multi-windows supported iPadOS app?

I'm considering my app to support multi windows on iPadOS. It has a chat feature, so it will have multiple chatrooms on the screen. It also has an internal webview feature to open tapped web links, so the links will be opened inside my app.

Let's say there are two windows of my app on left & right side. A web link on the left window is tapped. In order to open it in the internal webview, we need to know which window is used. In UISceneDelegate, how can we know which scene is active & foreground to open the link in our internal webview in this case?

Traverse the scene hierarchy for an active or main window something like below. Might need tweaking.

extension UIWindow {
   public static var keyWindow: UIWindow ? {
        if #available(iOS 13.0, *) {
          return UIApplication.shared.connectedScenes
                                     .compactMap { $0 as? UIWindowScene }
                                     .first?.windows.filter { $0.isKeyWindow }.first
        } else {
           return UIApplication.shared.keyWindow
        }
   }
}
How to know the active & foreground scene in multi-windows supported iPadOS app?
 
 
Q