Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Add camera window controls (exit, small, large)

Richie McIlroy3961835721e4
diff --git a/apps/desktop/src-tauri/src/audio.rs b/apps/desktop/src-tauri/src/audio.rs
index cba78e3cf..667327909 100644
--- a/apps/desktop/src-tauri/src/audio.rs
+++ b/apps/desktop/src-tauri/src/audio.rs
@@ -5,8 +5,8 @@ use byteorder::{ByteOrder, LittleEndian};
use std::sync::{Arc};
use tokio::sync::Mutex;
-use tokio::io::{AsyncWriteExt, BufReader};
-use tokio::process::{Command, ChildStderr, ChildStdin};
+use tokio::io::{AsyncWriteExt};
+use tokio::process::{Command, ChildStdin};
use crate::recording::RecordingOptions;
use crate::utils::{ffmpeg_path_as_str, monitor_and_log_recording_start};
@@ -119,9 +119,10 @@ impl AudioRecorder {
.await
.map_err(|e| e.to_string())?;
- let mut stdin = child.stdin.take().expect("failed to take child stdin");
- let stdin = Arc::new(Mutex::new(stdin));
- let stdin_clone = Arc::clone(&stdin);
+ let stdin = child.stdin.take().expect("failed to take child stdin");
+ let stdin_arc = Arc::new(Mutex::new(stdin));
+ let stdin_clone = Arc::clone(&stdin_arc);
+ let stdin_global = Arc::clone(&stdin_arc);
tokio::spawn(async move {
while let Some(bytes) = rx.recv().await {
@@ -191,7 +192,7 @@ impl AudioRecorder {
self.stream = Some(stream);
self.ffmpeg_process = Some(child);
- self.ffmpeg_stdin = Some(stdin);
+ self.ffmpeg_stdin = Some(stdin_global);
self.device_name = Some(device.name().expect("Failed to get device name"));
self.trigger_play()?;
@@ -219,22 +220,30 @@ impl AudioRecorder {
}
if let Some(stdin_arc) = &self.ffmpeg_stdin {
- let mut stdin = stdin_arc.lock().await;
- if stdin.write_all(b"q").await.is_err() {
- return Err("Failed to write to FFmpeg stdin".to_string());
- }
+ drop(stdin_arc); // Explicitly drop the stdin Arc<Mutex<ChildStdin>> to close stdin.
+ println!("Stdin closed for FFmpeg process.");
}
+ println!("Killing FFmpeg process...");
if let Some(ref mut child) = self.ffmpeg_process {
- if let Err(e) = child.wait().await {
- return Err(format!("Failed to wait for FFmpeg process: {}", e));
+ match child.kill().await {
+ Ok(_) => println!("FFmpeg process was killed successfully."),
+ Err(e) => return Err(format!("Failed to kill FFmpeg process: {}", e)),
+ }
+
+ // Await the process to ensure it has been terminated.
+ match child.wait().await {
+ Ok(status) => println!("FFmpeg process terminated with status: {}", status),
+ Err(e) => println!("Error waiting for FFmpeg process to terminate: {}", e),
}
} else {
- return Err("FFmpeg process was not available".to_string());
+ return Err("FFmpeg process was not started".to_string());
}
+ println!("Audio recording stopped.");
Ok(())
}
+
}
#[tauri::command]
diff --git a/apps/desktop/src-tauri/src/recording.rs b/apps/desktop/src-tauri/src/recording.rs
index 9d9ac90d8..187f75209 100644
--- a/apps/desktop/src-tauri/src/recording.rs
+++ b/apps/desktop/src-tauri/src/recording.rs
@@ -321,6 +321,8 @@ async fn construct_recording_args(
}
}
+use futures::future::join_all;
+
async fn start_upload_loop(
chunks_dir: PathBuf,
options: RecordingOptions,
@@ -329,10 +331,9 @@ async fn start_upload_loop(
uploading_finished: Arc<AtomicBool>,
) -> Result<(), String> {
let mut watched_segments: HashSet<String> = HashSet::new();
- let mut ongoing_tasks: Vec<JoinHandle<Result<(), String>>> = vec![];
let mut is_final_loop = false;
-
loop {
+ let mut upload_tasks = vec![];
if shutdown_flag.load(Ordering::SeqCst) {
if is_final_loop {
break;
@@ -351,40 +352,25 @@ async fn start_upload_loop(
if segment_path.is_file() {
let options_clone = options.clone();
let video_type_clone = video_type.clone();
- let filepath_str = segment_path.to_str().unwrap_or_default().to_owned();
-
- // Spawn an upload task for each new segment
- let upload_task = tokio::spawn(async move {
+ let segment_path_clone = segment_path.clone();
+ // Create a task for each file to be uploaded
+ upload_tasks.push(tokio::spawn(async move {
+ let filepath_str = segment_path_clone.to_str().unwrap_or_default().to_owned();
println!("Uploading video for {}: {}", video_type_clone, filepath_str);
upload_file(Some(options_clone), filepath_str, video_type_clone).await.map(|_| ())
- });
- ongoing_tasks.push(upload_task);
+ }));
}
watched_segments.insert(segment_filename.clone());
}
- let mut i = 0;
- while i != ongoing_tasks.len() {
- let task = &mut ongoing_tasks[i];
- tokio::select! {
- _ = task => {
- ongoing_tasks.remove(i);
- },
- _ = tokio::time::sleep(Duration::from_millis(100)) => {
- i += 1;
- },
- }
+ // Await all initiated upload tasks in parallel
+ if !upload_tasks.is_empty() {
+ let _ = join_all(upload_tasks).await;
}
tokio::time::sleep(Duration::from_millis(500)).await;
}
-
- for task in ongoing_tasks {
- let _ = task.await;
- }
-
uploading_finished.store(true, Ordering::SeqCst);
-
Ok(())
}
diff --git a/apps/desktop/src/app/page.tsx b/apps/desktop/src/app/page.tsx
index d80f21d89..405c073e3 100644
--- a/apps/desktop/src/app/page.tsx
+++ b/apps/desktop/src/app/page.tsx
@@ -38,22 +38,14 @@ export default function CameraPage() {
setCameraWindowOpen(true);
currentMonitor().then((monitor) => {
- const windowWidth = 250;
- const windowHeight = 250;
+ const windowWidth = 230;
+ const windowHeight = 230;
if (monitor && monitor.size) {
- const x = 100;
- const y = monitor.size.height * 0.66;
const scalingFactor = monitor.scaleFactor;
-
- console.log("x", x);
- console.log("y", y);
- console.log("scalingFactor", scalingFactor);
- console.log("monitor", monitor);
- console.log("monitor.size", monitor.size);
- console.log("monitor.size.height", monitor.size.height);
- console.log("windowHeight", windowHeight);
- console.log("windowWidth", windowWidth);
+ const x = 100;
+ const y =
+ monitor.size.height / scalingFactor - windowHeight - 100;
const existingCameraWindow = WebviewWindow.getByLabel("camera");
if (existingCameraWindow) {
@@ -66,7 +58,7 @@ export default function CameraPage() {
width: windowWidth,
height: windowHeight,
x: x / scalingFactor,
- y: y / scalingFactor,
+ y: y,
maximized: false,
resizable: false,
fullscreen: false,
diff --git a/apps/desktop/src/app/styles.css b/apps/desktop/src/app/styles.css
index bf625914e..3b5b1925e 100644
--- a/apps/desktop/src/app/styles.css
+++ b/apps/desktop/src/app/styles.css
@@ -156,6 +156,7 @@ body {
width: 100%;
height: 100%;
display: flex;
+ flex-direction: column;
align-items: center;
justify-content: center;
border-radius: 16px;
diff --git a/apps/desktop/src/components/WindowActions.tsx b/apps/desktop/src/components/WindowActions.tsx
index c00dbd3b5..7168a9c23 100644
--- a/apps/desktop/src/components/WindowActions.tsx
+++ b/apps/desktop/src/components/WindowActions.tsx
@@ -1,23 +1,45 @@
import { exit } from "@tauri-apps/api/process";
+import { Home } from "@/components/icons/Home";
+import { Settings } from "@/components/icons/Settings";
+import { openLinkInBrowser } from "@/utils/helpers";
export const WindowActions = () => {
const actionButtonBase = "w-3 h-3 bg-gray-500 rounded-full m-0 p-0 block";
return (
- <div className="absolute top-3 left-3 w-full flex items-center justify-start gap-x-2">
- <div>
- <button
- onClick={async () => {
- await exit();
- }}
- className={`bg-red-500 hover:bg-red-700 transition-all ${actionButtonBase}`}
- ></button>
- </div>
- <div>
- <span className={actionButtonBase}></span>
- </div>
- <div>
- <span className={actionButtonBase}></span>
+ <div className="w-full flex items-center -mt-3">
+ <div className="flex flex-grow items-center justify-between px-3">
+ <div className="flex space-x-2">
+ <div>
+ <button
+ onClick={async () => {
+ await exit();
+ }}
+ className={`bg-red-500 hover:bg-red-700 transition-all ${actionButtonBase}`}
+ ></button>
+ </div>
+ <div>
+ <span className={actionButtonBase}></span>
+ </div>
+ <div>
+ <span className={actionButtonBase}></span>
+ </div>
+ </div>
+ <div className="flex">
+ <button
+ onClick={async () => {
+ await openLinkInBrowser(
+ `${process.env.NEXT_PUBLIC_URL}/dashboard`
+ );
+ }}
+ className="p-1.5 bg-transparent hover:bg-gray-200 rounded-full transition-all"
+ >
+ <Home className="w-5 h-5" />
+ </button>
+ <button className="p-1.5 bg-transparent hover:bg-gray-200 rounded-full transition-all">
+ <Settings className="w-5 h-5" />
+ </button>
+ </div>
</div>
</div>
);
diff --git a/apps/desktop/src/components/icons/CloseX.tsx b/apps/desktop/src/components/icons/CloseX.tsx
new file mode 100644
index 000000000..b67aec4d1
--- /dev/null
+++ b/apps/desktop/src/components/icons/CloseX.tsx
@@ -0,0 +1,16 @@
+export const CloseX = ({ className }: { className: string }) => {
+ return (
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ className={className}
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="2"
+ viewBox="0 0 24 24"
+ >
+ <path d="M18 6L6 18M6 6l12 12"></path>
+ </svg>
+ );
+};
diff --git a/apps/desktop/src/components/icons/Home.tsx b/apps/desktop/src/components/icons/Home.tsx
new file mode 100644
index 000000000..cec793d31
--- /dev/null
+++ b/apps/desktop/src/components/icons/Home.tsx
@@ -0,0 +1,17 @@
+export const Home = ({ className }: { className: string }) => {
+ return (
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ className={className}
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="2"
+ viewBox="0 0 24 24"
+ >
+ <path d="M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2z"></path>
+ <path d="M9 22L9 12 15 12 15 22"></path>
+ </svg>
+ );
+};
diff --git a/apps/desktop/src/components/windows/Camera.tsx b/apps/desktop/src/components/windows/Camera.tsx
index 38702c147..46bbe35da 100644
--- a/apps/desktop/src/components/windows/Camera.tsx
+++ b/apps/desktop/src/components/windows/Camera.tsx
@@ -2,11 +2,13 @@
import React, { useEffect, useRef, useState } from "react";
import { useMediaDevices } from "@/utils/recording/MediaDeviceContext";
+import { CloseX } from "@/components/icons/CloseX";
export const Camera = () => {
const videoRef = useRef<HTMLVideoElement>(null);
const { selectedVideoDevice } = useMediaDevices();
const [isLoading, setIsLoading] = useState(true);
+ const tauriWindowImport = import("@tauri-apps/api/window");
useEffect(() => {
if (!videoRef.current || !selectedVideoDevice) return;
@@ -38,10 +40,45 @@ export const Camera = () => {
};
}, [selectedVideoDevice]);
+ const setWindowSize = async (type: "sm" | "lg") => {
+ tauriWindowImport.then(
+ ({ currentMonitor, appWindow, LogicalSize, LogicalPosition }) => {
+ currentMonitor().then((monitor) => {
+ const windowWidth = type === "sm" ? 230 : 400;
+ const windowHeight = type === "sm" ? 230 : 400;
+
+ if (monitor && monitor.size) {
+ const scalingFactor = monitor.scaleFactor;
+ const x = 100;
+ const y = monitor.size.height / scalingFactor - windowHeight - 100;
+
+ console.log(
+ scalingFactor,
+ x,
+ y,
+ windowWidth,
+ windowHeight,
+ monitor
+ );
+
+ appWindow.setSize(new LogicalSize(windowWidth, windowHeight));
+ appWindow.setPosition(new LogicalPosition(x / scalingFactor, y));
+ }
+ });
+ }
+ );
+ };
+
+ const closeWindow = () => {
+ import("@tauri-apps/api/window").then(({ appWindow }) => {
+ appWindow.close();
+ });
+ };
+
return (
<div
data-tauri-drag-region
- className="w-full h-full bg-gray-200 rounded-full m-0 p-0 relative overflow-hidden flex items-center justify-center border-none outline-none focus:outline-none"
+ className="group w-full h-full bg-gray-200 rounded-full m-0 p-0 relative overflow-hidden flex items-center justify-center border-none outline-none focus:outline-none"
>
{isLoading && (
<div className="w-full h-full absolute top-0 left-0 bg-gray-200 z-10 flex items-center justify-center">
@@ -72,6 +109,33 @@ export const Camera = () => {
</svg>
</div>
)}
+ <div className="opacity-0 group-hover:opacity-100 absolute bottom-3 left-1/2 transform -translate-x-1/2 bg-gray-800 rounded-xl z-20 grid grid-cols-3 overflow-hidden">
+ <div className="h-full flex items-center justify-center p-2 hover:bg-gray-900">
+ <button
+ onClick={() => {
+ closeWindow();
+ }}
+ >
+ <CloseX className="w-5 h-5 stroke-gray-200" />
+ </button>
+ </div>
+ <div className="h-full flex items-center justify-center p-2 hover:bg-gray-900">
+ <button
+ onClick={async () => {
+ await setWindowSize("sm");
+ }}
+ className="w-2 h-2 bg-gray-200 rounded-full"
+ ></button>
+ </div>
+ <div className="h-full flex items-center justify-center p-2 hover:bg-gray-900">
+ <button
+ onClick={async () => {
+ await setWindowSize("lg");
+ }}
+ className="w-4 h-4 bg-gray-200 rounded-full"
+ ></button>
+ </div>
+ </div>
<video
ref={videoRef}
autoPlay