Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Open captured window/bring to front + usage button change

Richie McIlroy6adf017c1e54
diff --git a/apps/desktop/src-tauri/src/display.rs b/apps/desktop/src-tauri/src/display.rs
index 78425a652..cd25ec8e7 100644
--- a/apps/desktop/src-tauri/src/display.rs
+++ b/apps/desktop/src-tauri/src/display.rs
@@ -94,6 +94,11 @@ pub async fn start_capturing(
) -> CaptureController {
dbg!(capture_target);
+ // Bring the window into focus if we're capturing a window
+ if let CaptureTarget::Window { id: window_number } = capture_target {
+ bring_window_to_focus(*window_number);
+ }
+
let targets = scap::get_all_targets();
let mut capturer = {
@@ -270,3 +275,109 @@ fn scale_dimensions(width: u32, height: u32, max_width: u32) -> (u32, u32) {
// Ensure dimensions are divisible by 2
(new_width & !1, new_height & !1)
}
+
+use crate::macos::get_on_screen_windows;
+use std::io::Write;
+use std::process::Command;
+use tempfile::NamedTempFile;
+
+fn bring_window_to_focus(window_id: u32) {
+ println!("Attempting to bring window {} to focus", window_id);
+
+ // Get the window information associated with the window id
+ let windows = get_on_screen_windows();
+ if let Some(window) = windows.into_iter().find(|w| w.window_number == window_id) {
+ let process_id = window.process_id;
+ let window_title = window.name.clone();
+ let bounds_x = window.bounds.x;
+ let bounds_y = window.bounds.y;
+ let bounds_width = window.bounds.width;
+ let bounds_height = window.bounds.height;
+ let should_focus = true;
+
+ // Prepare the AppleScript
+ let apple_script = r#"
+ on run argv
+ set processId to item 1 of argv as number
+ set windowTitle to item 2 of argv
+ set boundsX to item 3 of argv as number
+ set boundsY to item 4 of argv as number
+ set boundsWidth to item 5 of argv as number
+ set boundsHeight to item 6 of argv as number
+ set shouldFocus to item 7 of argv as boolean or true
+
+ log "processId: " & processId
+ log "windowTitle: " & windowTitle
+ log "boundsX: " & boundsX
+ log "boundsY: " & boundsY
+ log "boundsWidth: " & boundsWidth
+ log "boundsHeight: " & boundsHeight
+
+ tell application "System Events"
+ set appProcess to first process whose unix id is processId
+ set frontmost of appProcess to true
+
+ tell appProcess
+ set appWindowsCount to count of windows
+ log "appWindowsCount: " & appWindowsCount
+
+ if appWindowsCount is equal to 1 then
+ perform action "AXRaise" of first window
+ log "--found window--"
+ return
+ end if
+
+ repeat with checkedWindow in windows
+ tell checkedWindow
+ if title contains windowTitle and position is equal to {boundsX, boundsY} and size is equal to {boundsWidth, boundsHeight} then
+ perform action "AXRaise" of checkedWindow
+ log "--found window--"
+ exit repeat
+ end if
+ end tell
+ end repeat
+ end tell
+ end tell
+ end run
+ "#;
+
+ // Prepare arguments
+ let args = vec![
+ process_id.to_string(),
+ window_title.clone(),
+ bounds_x.to_string(),
+ bounds_y.to_string(),
+ bounds_width.to_string(),
+ bounds_height.to_string(),
+ should_focus.to_string(),
+ ];
+
+ // Write the AppleScript to a temporary file
+ let mut script_file = NamedTempFile::new().expect("Failed to create temp file");
+ script_file
+ .write_all(apple_script.as_bytes())
+ .expect("Failed to write to temp file");
+
+ // Execute the AppleScript with arguments
+ let output = Command::new("osascript")
+ .arg(script_file.path())
+ .args(&args)
+ .output();
+
+ match output {
+ Ok(output) => {
+ if output.status.success() {
+ println!("Successfully executed AppleScript");
+ } else {
+ let error_message = String::from_utf8_lossy(&output.stderr);
+ eprintln!("AppleScript execution failed: {}", error_message);
+ }
+ }
+ Err(e) => eprintln!("Failed to execute AppleScript: {}", e),
+ }
+
+ println!("Finished attempt to bring window {} to focus", window_id);
+ } else {
+ eprintln!("Window with id {} not found", window_id);
+ }
+}
diff --git a/apps/desktop/src-tauri/src/macos.rs b/apps/desktop/src-tauri/src/macos.rs
index 95277a4b8..117a6d42c 100644
--- a/apps/desktop/src-tauri/src/macos.rs
+++ b/apps/desktop/src-tauri/src/macos.rs
@@ -3,7 +3,7 @@ use std::ffi::c_void;
use core_graphics::{
base::boolean_t,
display::{CFDictionaryRef, CGRect},
- window::kCGWindowBounds,
+ window::{kCGWindowBounds, kCGWindowOwnerPID},
};
#[derive(Debug)]
@@ -11,7 +11,7 @@ pub struct Window {
pub window_number: u32,
pub name: String,
pub owner_name: String,
- pub level: u32,
+ pub process_id: u32,
pub bounds: Bounds,
}
@@ -24,8 +24,6 @@ pub struct Bounds {
}
pub fn get_on_screen_windows() -> Vec<Window> {
- use std::ffi::c_void;
-
use core_foundation::{
array::CFArrayGetCount,
base::FromVoid,
@@ -34,7 +32,7 @@ pub fn get_on_screen_windows() -> Vec<Window> {
string::CFString,
};
use core_graphics::{
- display::{CFArrayGetValueAtIndex, CFDictionaryRef},
+ display::CFArrayGetValueAtIndex,
window::{
kCGNullWindowID, kCGWindowLayer, kCGWindowListExcludeDesktopElements,
kCGWindowListOptionOnScreenOnly, kCGWindowName, kCGWindowNumber, kCGWindowOwnerName,
@@ -131,14 +129,37 @@ pub fn get_on_screen_windows() -> Vec<Window> {
value
};
+ let process_id = {
+ let value_ref = CFDictionaryGetValue(
+ window_cf_dictionary_ref,
+ kCGWindowOwnerPID as *const c_void,
+ );
+ if value_ref.is_null() {
+ continue;
+ }
+
+ let mut value: u32 = 0;
+ let is_success = CFNumberGetValue(
+ value_ref as CFNumberRef,
+ kCFNumberIntType,
+ &mut value as *mut _ as *mut c_void,
+ );
+
+ if !is_success {
+ continue;
+ }
+
+ value
+ };
+
let name = {
let value_ref =
CFDictionaryGetValue(window_cf_dictionary_ref, kCGWindowName as *const c_void);
if value_ref.is_null() {
- continue;
+ String::new()
+ } else {
+ CFString::from_void(value_ref).to_string()
}
-
- CFString::from_void(value_ref).to_string()
};
let owner_name = {
@@ -147,10 +168,10 @@ pub fn get_on_screen_windows() -> Vec<Window> {
kCGWindowOwnerName as *const c_void,
);
if value_ref.is_null() {
- continue;
+ String::new()
+ } else {
+ CFString::from_void(value_ref).to_string()
}
-
- CFString::from_void(value_ref).to_string()
};
if owner_name == "Window Server" {
@@ -161,8 +182,8 @@ pub fn get_on_screen_windows() -> Vec<Window> {
array.push(Window {
name,
owner_name,
+ process_id,
window_number,
- level,
bounds,
});
}
diff --git a/apps/web/components/UsageButton.tsx b/apps/web/components/UsageButton.tsx
index 717ebc885..0cc0e9684 100644
--- a/apps/web/components/UsageButton.tsx
+++ b/apps/web/components/UsageButton.tsx
@@ -1,70 +1,40 @@
import { useEffect, useState } from "react";
import Link from "next/link";
-import { sub } from "date-fns";
export const UsageButton = () => {
- const [usage, setUsage] = useState({
- videoCount: 0,
- videoLimit: 25,
- loading: true,
- subscription: false,
- });
+ const [isSubscribed, setIsSubscribed] = useState(false);
+ const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
- fetch("/api/settings/billing/usage")
+ fetch("/api/settings/billing/subscription")
.then((response) => response.json())
.then((data) => {
- setUsage({
- videoCount: data.videoCount,
- videoLimit: data.videoLimit,
- loading: false,
- subscription: data.subscription,
- });
+ setIsSubscribed(data.subscription);
+ setIsLoading(false);
});
}, []);
- const progress = (usage.videoCount / usage.videoLimit) * 100;
-
- if (usage.loading === true) {
+ if (isLoading) {
return (
<div className="w-full h-12 bg-gray-200 rounded-xl py-2 px-4 animate-pulse"></div>
);
}
return (
- <Link
- href={
- usage.subscription === false
- ? "/pricing"
- : "/dashboard/settings/billing"
- }
- >
- <div className="w-full flex flex-col items-start justify-center bg-white border border-gray-200 rounded-xl py-2 px-4 hover:border-blue-500 transition-all">
- {usage.subscription === false ? (
- <>
- <div className="mb-1 font-medium text-gray-500 text-[0.875rem]">
- {usage.videoCount}/{usage.videoLimit} Caps
- </div>
- <div className="h-[10px] w-full bg-gray-200 rounded-xl overflow-hidden">
- <div
- className="h-[10px] bg-blue-500"
- style={{ width: `${progress}%` }}
- ></div>
- </div>
- <div className="w-full mt-4 pt-2 border-t border-gray-200">
- <span className="text-sm">
- Upgrade to{" "}
- <span className="bg-blue-500 text-sm text-white py-1 px-1.5 rounded-[8px]">
- Pro
- </span>{" "}
- plan
- </span>
- </div>
- </>
- ) : (
+ <Link href={isSubscribed ? "/dashboard/settings/billing" : "/pricing"}>
+ <div className="w-full flex items-center justify-center bg-white border border-gray-200 rounded-xl py-2 px-4 hover:border-blue-500 transition-all">
+ {isSubscribed ? (
<div className="text-primary font-medium tracking-tighter">
Cap Pro
</div>
+ ) : (
+ <span className="text-sm">
+ Upgrade to{" "}
+ <span className="bg-blue-500 text-sm text-white py-1 px-1.5 rounded-[8px]">
+ Pro
+ </span>{" "}
+ plan
+ </span>
)}
</div>
</Link>