From ee15b364d10d8f5843b354b8e2deb8d90856677e Mon Sep 17 00:00:00 2001 From: pengx17 Date: Mon, 14 Apr 2025 07:21:49 +0000 Subject: [PATCH] fix(electron): use CG* instead of NS* (#11668) I encounter the following error when developing locally. Not sure if the current PR is the correct fix. ``` thread '' panicked at packages/frontend/native/media_capture/src/macos/screen_capture_kit.rs:253:11: invalid message send to -[NSImage initWithSize:]: expected argument at index 0 to have type code '{CGSize=dd}', but found '{NSSize=dd}' ``` --- .../src/macos/screen_capture_kit.rs | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/packages/frontend/native/media_capture/src/macos/screen_capture_kit.rs b/packages/frontend/native/media_capture/src/macos/screen_capture_kit.rs index 2481d89fa2..c98de4ab9a 100644 --- a/packages/frontend/native/media_capture/src/macos/screen_capture_kit.rs +++ b/packages/frontend/native/media_capture/src/macos/screen_capture_kit.rs @@ -42,36 +42,33 @@ use crate::{ }; #[repr(C)] -#[derive(Debug, Copy, Clone)] -struct NSSize { +struct CGSize { width: f64, height: f64, } #[repr(C)] -#[derive(Debug, Copy, Clone)] -struct NSPoint { +struct CGPoint { x: f64, y: f64, } #[repr(C)] -#[derive(Debug, Copy, Clone)] -struct NSRect { - origin: NSPoint, - size: NSSize, +struct CGRect { + origin: CGPoint, + size: CGSize, } -unsafe impl Encode for NSSize { - const ENCODING: Encoding = Encoding::Struct("NSSize", &[f64::ENCODING, f64::ENCODING]); +unsafe impl Encode for CGSize { + const ENCODING: Encoding = Encoding::Struct("CGSize", &[f64::ENCODING, f64::ENCODING]); } -unsafe impl Encode for NSPoint { - const ENCODING: Encoding = Encoding::Struct("NSPoint", &[f64::ENCODING, f64::ENCODING]); +unsafe impl Encode for CGPoint { + const ENCODING: Encoding = Encoding::Struct("CGPoint", &[f64::ENCODING, f64::ENCODING]); } -unsafe impl Encode for NSRect { - const ENCODING: Encoding = Encoding::Struct("NSRect", &[::ENCODING, ::ENCODING]); +unsafe impl Encode for CGRect { + const ENCODING: Encoding = Encoding::Struct("CGRect", &[::ENCODING, ::ENCODING]); } static RUNNING_APPLICATIONS: LazyLock< @@ -250,7 +247,7 @@ impl Application { } let resized_image: *mut AnyObject = - msg_send![resized_image, initWithSize: NSSize { width: 64.0, height: 64.0 }]; + msg_send![resized_image, initWithSize: CGSize { width: 64.0, height: 64.0 }]; if resized_image.is_null() { return Ok(Buffer::from(Vec::::new())); } @@ -258,16 +255,24 @@ impl Application { let _: () = msg_send![resized_image, lockFocus]; // Define drawing rectangle for 64x64 image - let draw_rect = NSRect { - origin: NSPoint { x: 0.0, y: 0.0 }, - size: NSSize { + let draw_rect = CGRect { + origin: CGPoint { x: 0.0, y: 0.0 }, + size: CGSize { width: 64.0, height: 64.0, }, }; + let from_rect = CGRect { + origin: CGPoint { x: 0.0, y: 0.0 }, + size: CGSize { + width: 0.0, + height: 0.0, + }, + }; + // Draw the original icon into draw_rect (using NSCompositingOperationCopy = 2) - let _: () = msg_send![icon, drawInRect: draw_rect, fromRect: NSRect { origin: NSPoint { x: 0.0, y: 0.0 }, size: NSSize { width: 0.0, height: 0.0 } }, operation: 2, fraction: 1.0]; + let _: () = msg_send![icon, drawInRect: draw_rect, fromRect: from_rect, operation: 2u64, fraction: 1.0]; let _: () = msg_send![resized_image, unlockFocus]; // Get TIFF representation from the downsized image @@ -314,14 +319,14 @@ impl Application { // Get PNG data with properties let png_data: *mut AnyObject = - msg_send![bitmap, representationUsingType: 4, properties: properties]; // 4 = PNG + msg_send![bitmap, representationUsingType: 4u64, properties: properties]; // 4 = PNG if png_data.is_null() { return Ok(Buffer::from(Vec::::new())); } // Get bytes from NSData - let bytes: *const u8 = msg_send![png_data, bytes]; + let bytes: *const libc::c_void = msg_send![png_data, bytes]; let length: usize = msg_send![png_data, length]; if bytes.is_null() { @@ -329,7 +334,7 @@ impl Application { } // Copy bytes into a Vec instead of using the original memory - let data = std::slice::from_raw_parts(bytes, length).to_vec(); + let data = std::slice::from_raw_parts(bytes as *const u8, length).to_vec(); Ok(Buffer::from(data)) } });