Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion WooCommerce/Classes/Extensions/UIApplication+Woo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ extension UIApplication {
/// Returns the keyWindow. Accessing `UIApplication.shared.keyWindow` is deprecated from iOS 13.
///
var currentKeyWindow: UIWindow? {
UIApplication.shared.windows.filter {$0.isKeyWindow}.first
// See https://stackoverflow.com/a/58031897/809944
UIApplication
.shared
// Get all of the currently connected UIScene instances
.connectedScenes
// Filter the connected UIScene instances for those that are UIWindowScene instances
// and map to their keyWindow property
.compactMap { ($0 as? UIWindowScene)?.keyWindow }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also the option to split this into one computed var for currentWindowScene and one that calls keyWindow on it.

I suggest this because I noticed one instance in which we call this currentKeyWindow only to then access the windowScene of the result:

let orientation = UIApplication.shared.currentKeyWindow?.windowScene?.interfaceOrientation

For additional reference, we also have the code below that uses windowScene, although that one does it starting bottom-up from a UIViewController, rather than top-down from UIApplication.

private extension UIViewController {
/// Attempts to return the scene in with the current controller lives
/// If its view is attached to a window, it will use the scene from that window
/// Otherwise, it looks at it's ancestors for a valid window to find the scene
var currentScene: UIWindowScene? {
if let window = view.window {
return window.windowScene
}
if let parent = parent {
return parent.currentScene
}
return nil
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should delete this re-implementation? The original keyWindow API has been deprecated for three years. Maybe it's time to stop relying on the no longer true concept that there is one "key window" across the whole application?

Copy link
Contributor Author

@mokagio mokagio Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 🤔 I think the solution might vary on a case by case basis across the various usages of this currentKeyWindow computed property.

I left a comment in a pre-existing issue on the topic that I didn't know about till your comment prompted me to do more research.

@koke, as the author of that issue, have you got any additional context or ideas how to go about this?

I value the removal of the warning and I'm tempted to argue for merging this to reduce the warnings noise. At the same time, I'm well to aware that if we silence it, we might not address the lack of multi window support for a while.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a lot of experience dealing with windows and scenes, but I agree with @crazytonyli that the best approach is probably to get rid of this method. Looking at usage, I see two distinct use cases:

  • To speed up animations when ProcessConfiguration.shouldDisableAnimations is true. For this case, we could as well iterate through all UIApplication.shared.connectedScenes and set the layer speed for all their key windows.
  • Every other instance happens on a UIViewController, which could use view.window instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a draft to remove currentKeyWindow: #8440 as discussed above. It's mostly done, but I'll do additional testing next week on specific views functionality.

.first
}
}

Expand Down