Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Better permissions flow

Richie McIlroy3e196e499c16
diff --git a/apps/desktop/src-tauri/Cargo.lock b/apps/desktop/src-tauri/Cargo.lock
index 76fe87bac..ebcc30d26 100644
--- a/apps/desktop/src-tauri/Cargo.lock
+++ b/apps/desktop/src-tauri/Cargo.lock
@@ -496,6 +496,8 @@ dependencies = [
"byteorder",
"capture",
"chrono",
+ "core-foundation",
+ "core-graphics 0.23.1",
"cpal",
"dotenv_codegen",
"ffmpeg-sidecar",
diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml
index 2b3dc542d..2f7e676c1 100644
--- a/apps/desktop/src-tauri/Cargo.toml
+++ b/apps/desktop/src-tauri/Cargo.toml
@@ -38,7 +38,6 @@ capture = { path = "./src/capture" }
image = "0.24.9"
sentry = "0.32.2"
-
[features]
# this feature is used for production builds or when `devPath` points to the filesystem
# DO NOT REMOVE!!
diff --git a/apps/desktop/src-tauri/src/main.rs b/apps/desktop/src-tauri/src/main.rs
index 15690c770..296155ff4 100644
--- a/apps/desktop/src-tauri/src/main.rs
+++ b/apps/desktop/src-tauri/src/main.rs
@@ -20,6 +20,7 @@ mod media;
use recording::{RecordingState, start_dual_recording, stop_all_recordings};
use upload::upload_file;
use media::{enumerate_audio_devices};
+use utils::{has_screen_capture_access};
use ffmpeg_sidecar::{
command::ffmpeg_is_installed,
@@ -148,7 +149,8 @@ fn main() {
start_server,
open_screen_capture_preferences,
open_mic_preferences,
- open_camera_preferences
+ open_camera_preferences,
+ has_screen_capture_access
])
.plugin(tauri_plugin_context_menu::init())
.run(tauri::generate_context!())
diff --git a/apps/desktop/src-tauri/src/utils.rs b/apps/desktop/src-tauri/src/utils.rs
index 3f3d7d35a..d604e7729 100644
--- a/apps/desktop/src-tauri/src/utils.rs
+++ b/apps/desktop/src-tauri/src/utils.rs
@@ -3,6 +3,39 @@ use std::process::{Command};
use ffmpeg_sidecar::{
paths::sidecar_dir,
};
+use capture::{Capturer, Display};
+use std::time::{Duration, Instant};
+
+#[tauri::command]
+pub fn has_screen_capture_access() -> bool {
+ let display = match Display::primary() {
+ Ok(display) => display,
+ Err(_) => return false,
+ };
+
+ let mut capturer = match Capturer::new(display) {
+ Ok(capturer) => capturer,
+ Err(_) => return false,
+ };
+
+ let start = Instant::now();
+
+ loop {
+ if start.elapsed() > Duration::from_secs(2) {
+ return false;
+ }
+
+ match capturer.frame() {
+ Ok(_frame) => {
+ return true;
+ },
+ Err(_) => {
+ continue;
+ }
+ };
+ }
+}
+
pub fn run_command(command: &str, args: Vec<&str>) -> Result<(String, String), String> {
let output = Command::new(command)
diff --git a/apps/desktop/src/components/windows/Camera.tsx b/apps/desktop/src/components/windows/Camera.tsx
index 8dc850401..115b394c4 100644
--- a/apps/desktop/src/components/windows/Camera.tsx
+++ b/apps/desktop/src/components/windows/Camera.tsx
@@ -2,13 +2,10 @@ import React, { useEffect, useRef, useState } from "react";
import { useMediaDevices } from "@/utils/recording/MediaDeviceContext";
import { CloseX } from "@/components/icons/CloseX";
import { Focus } from "@/components/icons/Focus";
-import * as bodyPix from "@tensorflow-models/body-pix";
-import "@tensorflow/tfjs-backend-webgl";
export const Camera = () => {
const videoRef = useRef<HTMLVideoElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
- const [isBackgroundBlur, setIsBackgroundBlur] = useState(false);
const { selectedVideoDevice } = useMediaDevices();
const [isLoading, setIsLoading] = useState(true);
const tauriWindowImport = import("@tauri-apps/api/window");
@@ -43,72 +40,6 @@ export const Camera = () => {
};
}, [selectedVideoDevice]);
- useEffect(() => {
- let animationId;
-
- const performSegmentationAndBlur = async () => {
- if (!isBackgroundBlur || !videoRef.current || !canvasRef.current) {
- return;
- }
-
- const net = await bodyPix.load({
- architecture: "MobileNetV1",
- outputStride: 16,
- multiplier: 0.75,
- quantBytes: 2,
- });
-
- const draw = async () => {
- if (
- !isBackgroundBlur ||
- !videoRef.current ||
- videoRef.current.paused ||
- videoRef.current.ended
- )
- return;
-
- const segmentation = await net.segmentPerson(videoRef.current, {
- internalResolution: "medium",
- segmentationThreshold: 0.7,
- });
-
- bodyPix.drawBokehEffect(
- canvasRef.current,
- videoRef.current,
- segmentation,
- 6,
- 3
- );
-
- animationId = requestAnimationFrame(draw);
- };
-
- videoRef.current.onloadedmetadata = () => {
- canvasRef.current.width = videoRef.current.videoWidth;
- canvasRef.current.height = videoRef.current.videoHeight;
- draw();
- };
-
- if (videoRef.current.readyState >= 4) {
- canvasRef.current.width = videoRef.current.videoWidth;
- canvasRef.current.height = videoRef.current.videoHeight;
- draw();
- }
- };
-
- performSegmentationAndBlur();
-
- return () => {
- if (animationId) {
- cancelAnimationFrame(animationId);
- }
- };
- }, [isBackgroundBlur]);
-
- const toggleBackgroundBlur = () => {
- setIsBackgroundBlur(!isBackgroundBlur);
- };
-
const setWindowSize = async (type: "sm" | "lg") => {
tauriWindowImport.then(
({ currentMonitor, appWindow, LogicalSize, LogicalPosition }) => {
@@ -205,21 +136,10 @@ export const Camera = () => {
>
<span className="w-3 h-3 bg-gray-200 rounded-full"></span>
</div>
- <div
- onClick={() => {
- toggleBackgroundBlur();
- }}
- className="h-full flex items-center justify-center p-2 hover:bg-gray-900"
- >
- <div>
- <Focus className="w-5 h-5 stroke-gray-200" />
- </div>
- </div>
</div>
<canvas
ref={canvasRef}
className="absolute top-0 left-0 w-full h-full object-cover pointer-events-none"
- style={{ display: isBackgroundBlur ? "block" : "none" }}
></canvas>
<video
ref={videoRef}
@@ -227,7 +147,6 @@ export const Camera = () => {
playsInline
muted
className="absolute top-0 left-0 w-full h-full object-cover pointer-events-none"
- style={{ visibility: isBackgroundBlur ? "hidden" : "visible" }}
/>
</div>
);
diff --git a/apps/desktop/src/components/windows/Permissions.tsx b/apps/desktop/src/components/windows/Permissions.tsx
index 103d893a1..089b14065 100644
--- a/apps/desktop/src/components/windows/Permissions.tsx
+++ b/apps/desktop/src/components/windows/Permissions.tsx
@@ -16,6 +16,68 @@ export const Permissions = () => {
microphone: false,
});
+ const checkScreenCapture = async () => {
+ const hasAccess = await invoke("has_screen_capture_access");
+ console.log("hasAccess", hasAccess);
+ if (hasAccess) {
+ await savePermissions("screen", true);
+ setPermissions((prev) => ({
+ ...prev,
+ screen: true,
+ }));
+ }
+ };
+
+ const checkCameraAccess = async () => {
+ try {
+ const stream = await navigator.mediaDevices.getUserMedia({ video: true });
+ // Assuming access is granted if the above line doesn't throw an error
+ await savePermissions("camera", true);
+ setPermissions((prev) => ({
+ ...prev,
+ camera: true,
+ }));
+ // Stop using the camera after checking access
+ stream.getTracks().forEach((track) => track.stop());
+ } catch (error) {
+ console.log("Camera access denied");
+ }
+ };
+
+ const checkMicrophoneAccess = async () => {
+ try {
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
+ // Assuming access is granted if the above line doesn't throw an error
+ await savePermissions("microphone", true);
+ setPermissions((prev) => ({
+ ...prev,
+ microphone: true,
+ }));
+ // Stop using the microphone after checking access
+ stream.getTracks().forEach((track) => track.stop());
+ } catch (error) {
+ console.log("Microphone access denied");
+ }
+ };
+
+ useEffect(() => {
+ const checkPermissions = async () => {
+ if (!permissions.screen) {
+ checkScreenCapture();
+ }
+ if (!permissions.camera) {
+ await checkCameraAccess();
+ }
+ if (!permissions.microphone) {
+ await checkMicrophoneAccess();
+ }
+ };
+
+ const checkPermissionInterval = setInterval(checkPermissions, 1000);
+
+ return () => clearInterval(checkPermissionInterval);
+ }, [permissions]);
+
const handlePermissionOpened = (permission: string) => {
if (permission === "screen") {
invoke("open_screen_capture_preferences");
@@ -31,14 +93,6 @@ export const Permissions = () => {
}));
};
- const handlePermissionConfirm = async (permission: string) => {
- await savePermissions(permission, true);
- setPermissions((prev) => ({
- ...prev,
- [permission]: true,
- }));
- };
-
const handleAllPermissionsEnabled = async () => {
await savePermissions("confirmed", true);
if (window.fathom !== undefined) {
@@ -99,9 +153,7 @@ export const Permissions = () => {
: "outline"
}
onClick={() => {
- permissionsOpened[permission] === true
- ? handlePermissionConfirm(permission)
- : handlePermissionOpened(permission);
+ handlePermissionOpened(permission);
}}
>
{permissions && permissions[permission] === true