Fix applying safe area insets on iOS

This commit is contained in:
Arkadiusz Fal 2022-08-07 14:11:57 +02:00
parent bc4b5fefe8
commit 50424c8dbf
4 changed files with 25 additions and 14 deletions

View File

@ -68,9 +68,9 @@ struct PlayerBackendView: View {
guard fullScreenLayout else { return 0 } guard fullScreenLayout else { return 0 }
if UIDevice.current.userInterfaceIdiom != .pad { if UIDevice.current.userInterfaceIdiom != .pad {
return verticalSizeClass == .compact ? safeAreaInsets.top : 0 return verticalSizeClass == .compact ? SafeArea.insets.top : 0
} else { } else {
return safeAreaInsets.top.isZero ? safeAreaInsets.bottom : safeAreaInsets.top return SafeArea.insets.top.isZero ? SafeArea.insets.bottom : SafeArea.insets.top
} }
} }
@ -78,15 +78,11 @@ struct PlayerBackendView: View {
guard fullScreenLayout else { return 0 } guard fullScreenLayout else { return 0 }
if UIDevice.current.userInterfaceIdiom != .pad { if UIDevice.current.userInterfaceIdiom != .pad {
return fullScreenLayout && verticalSizeClass == .compact ? safeAreaInsets.bottom : 0 return fullScreenLayout && verticalSizeClass == .compact ? SafeArea.insets.bottom : 0
} else { } else {
return fullScreenLayout ? safeAreaInsets.bottom : 0 return fullScreenLayout ? SafeArea.insets.bottom : 0
} }
} }
var safeAreaInsets: UIEdgeInsets {
UIApplication.shared.windows.first?.safeAreaInsets ?? .init()
}
#endif #endif
} }

View File

@ -146,11 +146,11 @@ struct VideoPlayerView: View {
#if os(iOS) #if os(iOS)
var playerWidth: Double? { var playerWidth: Double? {
fullScreenLayout ? (UIScreen.main.bounds.size.width - safeAreaInsets.left - safeAreaInsets.right) : nil fullScreenLayout ? (UIScreen.main.bounds.size.width - SafeArea.insets.left - SafeArea.insets.right) : nil
} }
var playerHeight: Double? { var playerHeight: Double? {
fullScreenLayout ? UIScreen.main.bounds.size.height - (OrientationTracker.shared.currentInterfaceOrientation.isPortrait ? (safeAreaInsets.top + safeAreaInsets.bottom) : 0) : nil fullScreenLayout ? UIScreen.main.bounds.size.height - (OrientationTracker.shared.currentInterfaceOrientation.isPortrait ? (SafeArea.insets.top + SafeArea.insets.bottom) : 0) : nil
} }
var playerEdgesIgnoringSafeArea: Edge.Set { var playerEdgesIgnoringSafeArea: Edge.Set {
@ -403,10 +403,6 @@ struct VideoPlayerView: View {
} }
} }
var safeAreaInsets: UIEdgeInsets {
UIApplication.shared.windows.first?.safeAreaInsets ?? .init()
}
private func configureOrientationUpdatesBasedOnAccelerometer() { private func configureOrientationUpdatesBasedOnAccelerometer() {
if OrientationTracker.shared.currentInterfaceOrientation.isLandscape, if OrientationTracker.shared.currentInterfaceOrientation.isLandscape,
Defaults[.enterFullscreenInLandscape], Defaults[.enterFullscreenInLandscape],

View File

@ -744,6 +744,7 @@
37EBD8CA27AF26C200F1C24B /* MPVBackend.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EBD8C927AF26C200F1C24B /* MPVBackend.swift */; }; 37EBD8CA27AF26C200F1C24B /* MPVBackend.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EBD8C927AF26C200F1C24B /* MPVBackend.swift */; };
37EBD8CB27AF26C200F1C24B /* MPVBackend.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EBD8C927AF26C200F1C24B /* MPVBackend.swift */; }; 37EBD8CB27AF26C200F1C24B /* MPVBackend.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EBD8C927AF26C200F1C24B /* MPVBackend.swift */; };
37EBD8CC27AF26C200F1C24B /* MPVBackend.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EBD8C927AF26C200F1C24B /* MPVBackend.swift */; }; 37EBD8CC27AF26C200F1C24B /* MPVBackend.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EBD8C927AF26C200F1C24B /* MPVBackend.swift */; };
37ECED56289FE166002BC2C9 /* SafeArea.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37ECED55289FE166002BC2C9 /* SafeArea.swift */; };
37EF5C222739D37B00B03725 /* MenuModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EF5C212739D37B00B03725 /* MenuModel.swift */; }; 37EF5C222739D37B00B03725 /* MenuModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EF5C212739D37B00B03725 /* MenuModel.swift */; };
37EF5C232739D37B00B03725 /* MenuModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EF5C212739D37B00B03725 /* MenuModel.swift */; }; 37EF5C232739D37B00B03725 /* MenuModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EF5C212739D37B00B03725 /* MenuModel.swift */; };
37EF5C242739D37B00B03725 /* MenuModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EF5C212739D37B00B03725 /* MenuModel.swift */; }; 37EF5C242739D37B00B03725 /* MenuModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37EF5C212739D37B00B03725 /* MenuModel.swift */; };
@ -1203,6 +1204,7 @@
37EBD8C327AF0DA800F1C24B /* PlayerBackend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerBackend.swift; sourceTree = "<group>"; }; 37EBD8C327AF0DA800F1C24B /* PlayerBackend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerBackend.swift; sourceTree = "<group>"; };
37EBD8C527AF26B300F1C24B /* AVPlayerBackend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AVPlayerBackend.swift; sourceTree = "<group>"; }; 37EBD8C527AF26B300F1C24B /* AVPlayerBackend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AVPlayerBackend.swift; sourceTree = "<group>"; };
37EBD8C927AF26C200F1C24B /* MPVBackend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPVBackend.swift; sourceTree = "<group>"; }; 37EBD8C927AF26C200F1C24B /* MPVBackend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPVBackend.swift; sourceTree = "<group>"; };
37ECED55289FE166002BC2C9 /* SafeArea.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafeArea.swift; sourceTree = "<group>"; };
37EF5C212739D37B00B03725 /* MenuModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuModel.swift; sourceTree = "<group>"; }; 37EF5C212739D37B00B03725 /* MenuModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuModel.swift; sourceTree = "<group>"; };
37EF9A75275BEB8E0043B585 /* CommentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentView.swift; sourceTree = "<group>"; }; 37EF9A75275BEB8E0043B585 /* CommentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentView.swift; sourceTree = "<group>"; };
37F0F4E9286F397E00C06C2E /* SettingsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsModel.swift; sourceTree = "<group>"; }; 37F0F4E9286F397E00C06C2E /* SettingsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsModel.swift; sourceTree = "<group>"; };
@ -1804,6 +1806,7 @@
37B4E802277D0A72004BF56A /* AppDelegate.swift */, 37B4E802277D0A72004BF56A /* AppDelegate.swift */,
37B4E804277D0AB4004BF56A /* Orientation.swift */, 37B4E804277D0AB4004BF56A /* Orientation.swift */,
379B0252287A1CDF001015B5 /* OrientationTracker.swift */, 379B0252287A1CDF001015B5 /* OrientationTracker.swift */,
37ECED55289FE166002BC2C9 /* SafeArea.swift */,
3784B23A272894DA00B09468 /* ShareSheet.swift */, 3784B23A272894DA00B09468 /* ShareSheet.swift */,
3749BF9227ADA142000480FF /* BridgingHeader.h */, 3749BF9227ADA142000480FF /* BridgingHeader.h */,
37992DC726CC50BC003D4C27 /* Info.plist */, 37992DC726CC50BC003D4C27 /* Info.plist */,
@ -2685,6 +2688,7 @@
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
374710052755291C00CE0F87 /* SearchField.swift in Sources */, 374710052755291C00CE0F87 /* SearchField.swift in Sources */,
37ECED56289FE166002BC2C9 /* SafeArea.swift in Sources */,
37CEE4BD2677B670005A1EFE /* SingleAssetStream.swift in Sources */, 37CEE4BD2677B670005A1EFE /* SingleAssetStream.swift in Sources */,
37C2211D27ADA33300305B41 /* MPVViewController.swift in Sources */, 37C2211D27ADA33300305B41 /* MPVViewController.swift in Sources */,
371B7E612759706A00D21217 /* CommentsView.swift in Sources */, 371B7E612759706A00D21217 /* CommentsView.swift in Sources */,

15
iOS/SafeArea.swift Normal file
View File

@ -0,0 +1,15 @@
import Foundation
import UIKit
struct SafeArea {
static var insets: UIEdgeInsets {
let keyWindow = UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.compactMap { $0 as? UIWindowScene }
.first?
.windows
.first { $0.isKeyWindow }
return keyWindow?.safeAreaInsets ?? .init()
}
}