Automatically adjust the ElementCall PiP when changing orientations

This commit is contained in:
Stefan Ceriu
2026-06-03 14:56:50 +03:00
parent 92086d66c3
commit ee82ed976e
2 changed files with 34 additions and 1 deletions
@@ -57,6 +57,9 @@ enum CallScreenJavaScriptMessageName: String, CaseIterable {
case onOutputDeviceSelect
/// Used to handle the webview back button
case onBackButtonPressed
/// Used to handle PiP orientation changes
case onPipMediaOrientationUpdate
/// Forward logs to the native side for debugging purposes.
case forwardLogs
@@ -96,6 +99,12 @@ enum CallScreenJavaScriptMessageName: String, CaseIterable {
window.webkit.messageHandlers.\(rawValue).postMessage("");
}
"""
case .onPipMediaOrientationUpdate:
"""
window.controls.\(rawValue) = (orientation) => {
window.webkit.messageHandlers.\(rawValue).postMessage(orientation);
}
"""
case .forwardLogs:
"""
(function() {
@@ -88,7 +88,7 @@ private struct CallView: UIViewRepresentable {
init(viewModelContext: CallScreenViewModel.Context) {
self.viewModelContext = viewModelContext
pictureInPictureViewController = AVPictureInPictureVideoCallViewController()
pictureInPictureViewController.preferredContentSize = CGSize(width: 1920, height: 1080)
pictureInPictureViewController.preferredContentSize = PiPSize.portrait.size
super.init()
@@ -192,6 +192,16 @@ private struct CallView: UIViewRepresentable {
viewModelContext?.send(viewAction: .outputDeviceSelected(deviceID: deviceID))
case .onBackButtonPressed:
viewModelContext?.send(viewAction: .navigateBack)
case .onPipMediaOrientationUpdate:
guard let orientation = message.body as? String else { return }
switch orientation {
case "portrait":
pictureInPictureViewController.preferredContentSize = PiPSize.portrait.size
case "landscape":
pictureInPictureViewController.preferredContentSize = PiPSize.landscape.size
default:
break
}
case .forwardLogs:
guard let body = message.body as? [String: String],
let level = body["level"],
@@ -340,6 +350,20 @@ private struct CallView: UIViewRepresentable {
coordinator?.userContentController(userContentController, didReceive: message)
}
}
private enum PiPSize {
case portrait
case landscape
var size: CGSize {
switch self {
case .portrait:
.init(width: 1080, height: 1920)
case .landscape:
.init(width: 1920, height: 1080)
}
}
}
}
// MARK: - Previews