Repository: tauri-apps/wry (language: rust)

Issue:
getDisplayMedia() does not work on MacOS Sonoma 14.0+
**Describe the bug**
If you try getting the user's screen capture it fails with a `NotAllowedError`, this is also an issue reported in tauri, https://github.com/tauri-apps/tauri/issues/2600

**Steps To Reproduce**
- Change the simple.rs example to display any webpage that has a getDisplayMedia call (Gmeet, etc)
- Run `cargo run --example simple`
- Try to share screen from the video calling app
- It will fail

**Expected behavior**
Ideally, the display media should be captured and shown on the screen, just like camera and audio work out of the box

**Screenshots**
Copied the screen shot from the same issue
![image](https://github.com/tauri-apps/wry/assets/24592076/b0c4cd76-55ef-40a8-9b59-e418ae91ac86)


**Platform and Versions (please complete the following information):**
OS: macOS 14.0 (23A344)
Rustc: 1.72.1

**Would you want to assign yourself to resolve this bug?**
- [ ] Yes
- [x] No






Discussion / hints:
It looks like the fix https://github.com/tauri-apps/wry/pull/694 suppressed the permission prompt and just denied the permission request. After I commented out the patch, the prompt showed up and worked as expected.

Since I have messed up my environment, the prompt no longer shows up and always grants permission, I can't test if the original issue (https://github.com/tauri-apps/tauri/issues/4750) still persisted after macOS 14. If it doesn't, maybe we can do a version check and decide if the patch should be used.

Ps. A mini repo to test the capture function after commenting out the fix: https://github.com/pewsheen/screen-capture-test

cc @FabianLars @lucasfernog 
@pewsheen Hey, I've raised the PR, tested locally on my MacOS 14.0 and it works fine, let me know if anything else is required
@tarushnagpal Did you test whether this messes with the microphone/camera permissions on 14.0?
Yes @FabianLars I checked with "audio" only, "video" only and "audio and video", only seeing a single permission request for microphone/camera on 14.0

Fix patch:
diff --git a/.changes/fix-screenshare-media-dialog.md b/.changes/fix-screenshare-media-dialog.md
new file mode 100644
index 000000000..1979f5673
--- /dev/null
+++ b/.changes/fix-screenshare-media-dialog.md
@@ -0,0 +1,5 @@
+---
+"wry": patch
+---
+
+Fix screen share permissions dialog not showing up on macOS 14.0+
diff --git a/src/wkwebview/mod.rs b/src/wkwebview/mod.rs
index 49d8f62e4..6fbcfe18d 100644
--- a/src/wkwebview/mod.rs
+++ b/src/wkwebview/mod.rs
@@ -787,11 +787,16 @@ impl InnerWebView {
             run_file_upload_panel as extern "C" fn(&Object, Sel, id, id, id, id),
           );
 
-          // Disable media dialogs
-          ctl.add_method(
-            sel!(webView:requestMediaCapturePermissionForOrigin:initiatedByFrame:type:decisionHandler:),
-            request_media_capture_permission as extern "C" fn(&Object, Sel, id, id, id, id, id),
-          );
+          // Only disable media dialogs on macOS < 14.0
+          // https://tauri.app/v1/references/webview-versions/
+          let webview_system_version = platform_webview_system_version()?.parse::<i32>();
+          if webview_system_version.is_err() || webview_system_version.unwrap() < 19 {
+            // Disable media dialogs
+            ctl.add_method(
+              sel!(webView:requestMediaCapturePermissionForOrigin:initiatedByFrame:type:decisionHandler:),
+              request_media_capture_permission as extern "C" fn(&Object, Sel, id, id, id, id, id),
+            );
+          }
 
           ctl.register()
         }
@@ -1170,6 +1175,17 @@ pub fn platform_webview_version() -> Result<String> {
   }
 }
 
+pub fn platform_webview_system_version() -> Result<String> {
+  platform_webview_version().map(|webview_version| {
+    let webview_system_and_major_version = webview_version.split('.').next().unwrap();
+    if webview_system_and_major_version.chars().count() < 5 {
+      webview_system_and_major_version[..1].to_string()
+    } else {
+      webview_system_and_major_version[..2].to_string()
+    }
+  })
+}
+
 impl Drop for InnerWebView {
   fn drop(&mut self) {
     // We need to drop handler closures here


Test patch: