Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: More Windows implementation for functions

Richie McIlroye5dc3aba7b8d
diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml
index 8e4330fd0..432a48d16 100644
--- a/apps/desktop/src-tauri/Cargo.toml
+++ b/apps/desktop/src-tauri/Cargo.toml
@@ -25,7 +25,6 @@ tauri = { workspace = true, features = [
"image-png",
] }
tauri-plugin-shell = "2.0.0"
-tauri-nspanel = { git = "https://github.com/ahkohd/tauri-nspanel", branch = "v2" }
tauri-plugin-store = "2.0.0"
tauri-plugin-os = "2.0.0"
tauri-plugin-process = "2.0.0"
@@ -96,6 +95,7 @@ windows = { version = "0.52.0", features = [
"Win32_Foundation",
"Win32_System",
"Win32_UI_WindowsAndMessaging",
+ "Win32_Graphics_Gdi",
] }
# Lock the version of windows-capture used by scap@691bd88798d3
# TODO: Remove this once scap uses the latest version of `windows` and `windows-capture`
diff --git a/apps/desktop/src-tauri/src/cursor.rs b/apps/desktop/src-tauri/src/cursor.rs
index f5d67d695..e1d19c369 100644
--- a/apps/desktop/src-tauri/src/cursor.rs
+++ b/apps/desktop/src-tauri/src/cursor.rs
@@ -185,5 +185,133 @@ fn get_cursor_image_data() -> Option<Vec<u8>> {
#[cfg(windows)]
fn get_cursor_image_data() -> Option<Vec<u8>> {
- todo!()
+ use windows::Win32::UI::WindowsAndMessaging::{GetCursorInfo, CURSORINFO, CURSORINFO_FLAGS};
+ use windows::Win32::UI::WindowsAndMessaging::{GetIconInfo, ICONINFO};
+ use windows::Win32::Graphics::Gdi::{
+ CreateCompatibleBitmap, CreateCompatibleDC, CreateDIBSection, DeleteDC, DeleteObject,
+ GetDC, GetObjectA, ReleaseDC, SelectObject, BITMAP, BITMAPINFO, BITMAPINFOHEADER,
+ DIB_RGB_COLORS, SRCCOPY, BitBlt,
+ };
+ use windows::Win32::Foundation::{BOOL, POINT, HWND};
+
+ unsafe {
+ // Get cursor info
+ let mut cursor_info = CURSORINFO {
+ cbSize: std::mem::size_of::<CURSORINFO>() as u32,
+ flags: CURSORINFO_FLAGS(0),
+ hCursor: Default::default(),
+ ptScreenPos: POINT::default(),
+ };
+
+ // Handle Result return type
+ if GetCursorInfo(&mut cursor_info).is_err() {
+ return None;
+ }
+
+ // If no cursor, return None
+ if cursor_info.hCursor.is_invalid() {
+ return None;
+ }
+
+ // Get icon info
+ let mut icon_info = ICONINFO::default();
+ // Handle Result return type
+ if GetIconInfo(cursor_info.hCursor, &mut icon_info).is_err() {
+ return None;
+ }
+
+ // Get bitmap info
+ let mut bitmap = BITMAP::default();
+ if GetObjectA(
+ icon_info.hbmColor,
+ std::mem::size_of::<BITMAP>() as i32,
+ Some(&mut bitmap as *mut _ as *mut _),
+ ) == 0 {
+ return None;
+ }
+
+ // Create compatible DC
+ let screen_dc = GetDC(HWND(0));
+ let mem_dc = CreateCompatibleDC(screen_dc);
+
+ // Create bitmap info header
+ let bi = BITMAPINFOHEADER {
+ biSize: std::mem::size_of::<BITMAPINFOHEADER>() as u32,
+ biWidth: bitmap.bmWidth,
+ biHeight: -bitmap.bmHeight, // Negative height for top-down bitmap
+ biPlanes: 1,
+ biBitCount: 32,
+ biCompression: 0,
+ biSizeImage: 0,
+ biXPelsPerMeter: 0,
+ biYPelsPerMeter: 0,
+ biClrUsed: 0,
+ biClrImportant: 0,
+ };
+
+ let bitmap_info = BITMAPINFO {
+ bmiHeader: bi,
+ bmiColors: [Default::default()],
+ };
+
+ // Create DIB section
+ let mut bits: *mut std::ffi::c_void = std::ptr::null_mut();
+ let dib = CreateDIBSection(
+ mem_dc,
+ &bitmap_info,
+ DIB_RGB_COLORS,
+ &mut bits,
+ None,
+ 0,
+ );
+
+ if dib.is_err() {
+ return None;
+ }
+
+ let dib = dib.unwrap();
+
+ // Select DIB into DC
+ let old_bitmap = SelectObject(mem_dc, dib);
+
+ // Copy cursor image
+ if BitBlt(
+ mem_dc,
+ 0,
+ 0,
+ bitmap.bmWidth,
+ bitmap.bmHeight,
+ screen_dc,
+ cursor_info.ptScreenPos.x,
+ cursor_info.ptScreenPos.y,
+ SRCCOPY,
+ ).is_err() {
+ return None;
+ }
+
+ // Get image data
+ let size = (bitmap.bmWidth * bitmap.bmHeight * 4) as usize;
+ let mut image_data = vec![0u8; size];
+ std::ptr::copy_nonoverlapping(bits, image_data.as_mut_ptr() as *mut _, size);
+
+ // Cleanup
+ SelectObject(mem_dc, old_bitmap);
+ DeleteObject(dib);
+ DeleteDC(mem_dc);
+ ReleaseDC(HWND(0), screen_dc);
+ DeleteObject(icon_info.hbmColor);
+ DeleteObject(icon_info.hbmMask);
+
+ // Convert to PNG format
+ let image = image::RgbaImage::from_raw(
+ bitmap.bmWidth as u32,
+ bitmap.bmHeight as u32,
+ image_data,
+ )?;
+
+ let mut png_data = Vec::new();
+ image.write_to(&mut std::io::Cursor::new(&mut png_data), image::ImageFormat::Png).ok()?;
+
+ Some(png_data)
+ }
}
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs
index e8699b565..588ee17be 100644
--- a/apps/desktop/src-tauri/src/lib.rs
+++ b/apps/desktop/src-tauri/src/lib.rs
@@ -30,7 +30,6 @@ use cap_project::{
ZoomSegment,
};
use cap_rendering::{ProjectUniforms, ZOOM_DURATION};
-use cap_utils::create_named_pipe;
// use display::{list_capture_windows, Bounds, CaptureTarget, FPS};
use general_settings::GeneralSettingsStore;
use image::{ImageBuffer, Rgba};
diff --git a/apps/desktop/src-tauri/src/windows.rs b/apps/desktop/src-tauri/src/windows.rs
index bdb6efcf5..45024711d 100644
--- a/apps/desktop/src-tauri/src/windows.rs
+++ b/apps/desktop/src-tauri/src/windows.rs
@@ -181,10 +181,16 @@ impl CapWindow {
let mut window_builder = self
.window_builder(app, format!("/editor?id={project_id}"))
.inner_size(1150.0, 800.0)
- .hidden_title(true)
- .title_bar_style(tauri::TitleBarStyle::Overlay)
.theme(Some(tauri::Theme::Light));
+ #[cfg(target_os = "macos")]
+ {
+ use tauri::TitleBarStyle;
+ window_builder = window_builder
+ .hidden_title(true)
+ .title_bar_style(TitleBarStyle::Overlay);
+ }
+
window_builder.build()?
}
Self::Permissions => {
diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml
index c84b5963a..0968af3a9 100644
--- a/crates/media/Cargo.toml
+++ b/crates/media/Cargo.toml
@@ -25,6 +25,7 @@ tempfile = "3.12.0"
thiserror = "1.0"
tracing = "0.1"
cap-flags = { path = "../flags" }
+windows = { version = "0.52", features = ["Win32_Media_MediaFoundation", "Win32_System_Com"] }
[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.26.0"
diff --git a/crates/media/src/feeds/camera.rs b/crates/media/src/feeds/camera.rs
index a23692e93..c7bb8b2d4 100644
--- a/crates/media/src/feeds/camera.rs
+++ b/crates/media/src/feeds/camera.rs
@@ -78,11 +78,51 @@ impl CameraFeed {
}
pub fn list_cameras() -> Vec<String> {
- nokhwa::query(ApiBackend::Auto)
- .unwrap()
- .into_iter()
- .map(|i| i.human_name().to_string())
- .collect()
+ #[cfg(target_os = "windows")]
+ {
+ use windows::Win32::Media::MediaFoundation::{MFStartup, MFSTARTUP_FULL};
+ use windows::Win32::System::Com::{CoInitializeEx, COINIT_MULTITHREADED};
+
+ // On Windows, wrap the entire operation in a catch_unwind to prevent crashes
+ match std::panic::catch_unwind(|| {
+ // Initialize COM and Media Foundation
+ unsafe {
+ let _ = CoInitializeEx(None, COINIT_MULTITHREADED);
+ let _ = MFStartup(0x20070, MFSTARTUP_FULL);
+ }
+
+ match nokhwa::query(ApiBackend::Auto) {
+ Ok(cameras) => cameras
+ .into_iter()
+ .map(|i| i.human_name().to_string())
+ .collect::<Vec<String>>(),
+ Err(e) => {
+ error!("Failed to query cameras: {}", e);
+ Vec::new()
+ }
+ }
+ }) {
+ Ok(cameras) => cameras,
+ Err(e) => {
+ error!("Camera query panicked: {:?}", e);
+ Vec::new()
+ }
+ }
+ }
+
+ #[cfg(not(target_os = "windows"))]
+ {
+ match nokhwa::query(ApiBackend::Auto) {
+ Ok(cameras) => cameras
+ .into_iter()
+ .map(|i| i.human_name().to_string())
+ .collect(),
+ Err(e) => {
+ error!("Failed to query cameras: {}", e);
+ Vec::new()
+ }
+ }
+ }
}
pub fn video_info(&self) -> VideoInfo {