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 '<unnamed>' 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}'
```
This commit is contained in:
pengx17
2025-04-14 07:21:49 +00:00
parent 2db7dea46f
commit ee15b364d1

View File

@@ -42,36 +42,33 @@ use crate::{
}; };
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] struct CGSize {
struct NSSize {
width: f64, width: f64,
height: f64, height: f64,
} }
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] struct CGPoint {
struct NSPoint {
x: f64, x: f64,
y: f64, y: f64,
} }
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] struct CGRect {
struct NSRect { origin: CGPoint,
origin: NSPoint, size: CGSize,
size: NSSize,
} }
unsafe impl Encode for NSSize { unsafe impl Encode for CGSize {
const ENCODING: Encoding = Encoding::Struct("NSSize", &[f64::ENCODING, f64::ENCODING]); const ENCODING: Encoding = Encoding::Struct("CGSize", &[f64::ENCODING, f64::ENCODING]);
} }
unsafe impl Encode for NSPoint { unsafe impl Encode for CGPoint {
const ENCODING: Encoding = Encoding::Struct("NSPoint", &[f64::ENCODING, f64::ENCODING]); const ENCODING: Encoding = Encoding::Struct("CGPoint", &[f64::ENCODING, f64::ENCODING]);
} }
unsafe impl Encode for NSRect { unsafe impl Encode for CGRect {
const ENCODING: Encoding = Encoding::Struct("NSRect", &[<NSPoint>::ENCODING, <NSSize>::ENCODING]); const ENCODING: Encoding = Encoding::Struct("CGRect", &[<CGPoint>::ENCODING, <CGSize>::ENCODING]);
} }
static RUNNING_APPLICATIONS: LazyLock< static RUNNING_APPLICATIONS: LazyLock<
@@ -250,7 +247,7 @@ impl Application {
} }
let resized_image: *mut AnyObject = 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() { if resized_image.is_null() {
return Ok(Buffer::from(Vec::<u8>::new())); return Ok(Buffer::from(Vec::<u8>::new()));
} }
@@ -258,16 +255,24 @@ impl Application {
let _: () = msg_send![resized_image, lockFocus]; let _: () = msg_send![resized_image, lockFocus];
// Define drawing rectangle for 64x64 image // Define drawing rectangle for 64x64 image
let draw_rect = NSRect { let draw_rect = CGRect {
origin: NSPoint { x: 0.0, y: 0.0 }, origin: CGPoint { x: 0.0, y: 0.0 },
size: NSSize { size: CGSize {
width: 64.0, width: 64.0,
height: 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) // 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]; let _: () = msg_send![resized_image, unlockFocus];
// Get TIFF representation from the downsized image // Get TIFF representation from the downsized image
@@ -314,14 +319,14 @@ impl Application {
// Get PNG data with properties // Get PNG data with properties
let png_data: *mut AnyObject = 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() { if png_data.is_null() {
return Ok(Buffer::from(Vec::<u8>::new())); return Ok(Buffer::from(Vec::<u8>::new()));
} }
// Get bytes from NSData // 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]; let length: usize = msg_send![png_data, length];
if bytes.is_null() { if bytes.is_null() {
@@ -329,7 +334,7 @@ impl Application {
} }
// Copy bytes into a Vec<u8> instead of using the original memory // Copy bytes into a Vec<u8> 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)) Ok(Buffer::from(data))
} }
}); });