Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add tray menu

Ilya70ff7b14b7e9
diff --git a/apps/desktop/src-tauri/Cargo.lock b/apps/desktop/src-tauri/Cargo.lock
index cfef6e0f2..759d8f045 100644
--- a/apps/desktop/src-tauri/Cargo.lock
+++ b/apps/desktop/src-tauri/Cargo.lock
@@ -2320,6 +2320,15 @@ dependencies = [
"serde",
]
+[[package]]
+name = "infer"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f178e61cdbfe084aa75a2f4f7a25a5bb09701a47ae1753608f194b15783c937a"
+dependencies = [
+ "cfb",
+]
+
[[package]]
name = "infer"
version = "0.13.0"
@@ -4738,6 +4747,7 @@ dependencies = [
"http",
"ignore",
"indexmap 1.9.3",
+ "infer 0.9.0",
"minisign-verify",
"nix 0.26.4",
"notify-rust",
@@ -4747,6 +4757,7 @@ dependencies = [
"os_info",
"os_pipe",
"percent-encoding",
+ "png",
"rand 0.8.5",
"raw-window-handle 0.5.2",
"regex",
@@ -4950,7 +4961,7 @@ dependencies = [
"glob",
"heck 0.4.1",
"html5ever",
- "infer",
+ "infer 0.13.0",
"json-patch",
"kuchikiki",
"log",
diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml
index 523243ade..a495a64ab 100644
--- a/apps/desktop/src-tauri/Cargo.toml
+++ b/apps/desktop/src-tauri/Cargo.toml
@@ -14,7 +14,7 @@ tauri-build = { version = "1.5.1", features = [] }
ffmpeg-sidecar = "0.5.1"
[dependencies]
-tauri = { version = "1.6.1", features = [ "updater", "macos-private-api", "window-set-position", "fs-write-file", "fs-remove-file", "fs-read-file", "fs-rename-file", "fs-exists", "fs-remove-dir", "fs-read-dir", "fs-copy-file", "fs-create-dir", "window-set-ignore-cursor-events", "window-unminimize", "window-minimize", "window-close", "window-show", "window-start-dragging", "window-hide", "window-unmaximize", "window-maximize", "window-set-always-on-top", "shell-open", "devtools", "os-all", "http-all"] }
+tauri = { version = "1.6.1", features = [ "system-tray", "updater", "macos-private-api", "window-set-position", "fs-write-file", "fs-remove-file", "fs-read-file", "fs-rename-file", "fs-exists", "fs-remove-dir", "fs-read-dir", "fs-copy-file", "fs-create-dir", "window-set-ignore-cursor-events", "window-unminimize", "window-minimize", "window-close", "window-show", "window-start-dragging", "window-hide", "window-unmaximize", "window-maximize", "window-set-always-on-top", "shell-open", "devtools", "os-all", "http-all", "icon-png"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tauri-plugin-context-menu = "0.7.0"
diff --git a/apps/desktop/src-tauri/icons/tray-default-icon.png b/apps/desktop/src-tauri/icons/tray-default-icon.png
new file mode 100644
index 000000000..826d9ea52
Binary files /dev/null and b/apps/desktop/src-tauri/icons/tray-default-icon.png differ
diff --git a/apps/desktop/src-tauri/icons/tray-stop-icon.png b/apps/desktop/src-tauri/icons/tray-stop-icon.png
new file mode 100644
index 000000000..acc363e98
Binary files /dev/null and b/apps/desktop/src-tauri/icons/tray-stop-icon.png differ
diff --git a/apps/desktop/src-tauri/src/main.rs b/apps/desktop/src-tauri/src/main.rs
index cd4559780..9feb3a2fb 100644
--- a/apps/desktop/src-tauri/src/main.rs
+++ b/apps/desktop/src-tauri/src/main.rs
@@ -1,11 +1,17 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
+use std::any::Any;
+use std::borrow::Borrow;
+use std::os::macos::raw::stat;
use std::sync::{Arc};
use std::path::PathBuf;
+use ffmpeg_sidecar::event;
+use serde::Deserialize;
+use serde_json::Value;
use tokio::sync::Mutex;
use std::sync::atomic::{AtomicBool};
use std::env;
-use tauri::{command, Manager, Window};
+use tauri::{command, CustomMenuItem, Manager, State, SystemTray, SystemTrayEvent, SystemTrayMenu, Window};
use window_vibrancy::{apply_blur, apply_vibrancy, NSVisualEffectMaterial};
use window_shadows::set_shadow;
use tauri_plugin_positioner::{WindowExt, Position};
@@ -156,6 +162,17 @@ fn main() {
(0, 0)
};
+ let tray_menu = SystemTrayMenu::new()
+ .add_item(CustomMenuItem::new("toggle-window", "Show Cap"))
+ .add_native_item(tauri::SystemTrayMenuItem::Separator)
+ .add_item(
+ CustomMenuItem::new("quit".to_string(), "Quit")
+ .native_image(tauri::NativeImage::StopProgress)
+ );
+
+ let tray = SystemTray::new().with_menu(tray_menu).with_menu_on_left_click(false);
+
+
tauri::Builder::default()
.plugin(tauri_plugin_oauth::init())
.plugin(tauri_plugin_positioner::init())
@@ -188,6 +205,18 @@ fn main() {
app.manage(Arc::new(Mutex::new(recording_state)));
+ let handle = app.tray_handle().clone();
+ app.listen_global("toggle-recording", move|event| {
+ let payload_error_msg = format!("Error while deserializing recording state from event payload: {:?}", event.payload());
+ let recording_state: Value = serde_json::from_str(event.payload().expect(payload_error_msg.as_str())).unwrap();
+
+ if recording_state.as_bool().expect(payload_error_msg.as_str()) {
+ handle.set_icon(tauri::Icon::Raw(include_bytes!("../icons/tray-stop-icon.png").to_vec())).unwrap();
+ } else {
+ handle.set_icon(tauri::Icon::Raw(include_bytes!("../icons/tray-default-icon.png").to_vec())).unwrap();
+ }
+ });
+
Ok(())
})
.invoke_handler(tauri::generate_handler![
@@ -205,6 +234,22 @@ fn main() {
reset_camera_permissions,
])
.plugin(tauri_plugin_context_menu::init())
+ .system_tray(tray)
+ .on_system_tray_event(move |app, event| match event {
+ SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
+ "toggle-window" => {
+
+ }
+ "quit" => {
+ app.exit(0);
+ }
+ _ => {}
+ },
+ SystemTrayEvent::LeftClick { position: _, size: _, .. } => {
+ app.emit_all("tray-on-left-click", {}).unwrap();
+ },
+ _ => {}
+ })
.run(tauri::generate_context!())
.expect("Error while running tauri application");
}
\ No newline at end of file
diff --git a/apps/desktop/src-tauri/tauri.conf.json b/apps/desktop/src-tauri/tauri.conf.json
index ccf9d0023..0518fc471 100644
--- a/apps/desktop/src-tauri/tauri.conf.json
+++ b/apps/desktop/src-tauri/tauri.conf.json
@@ -99,6 +99,10 @@
"alwaysOnTop": false,
"center": true
}
- ]
+ ],
+ "systemTray": {
+ "iconPath": "icons/tray-default-icon.png",
+ "iconAsTemplate": true
+ }
}
}
diff --git a/apps/desktop/src/components/windows/inner/Recorder.tsx b/apps/desktop/src/components/windows/inner/Recorder.tsx
index bf0c494ec..c8a96871b 100644
--- a/apps/desktop/src/components/windows/inner/Recorder.tsx
+++ b/apps/desktop/src/components/windows/inner/Recorder.tsx
@@ -9,7 +9,7 @@ import { Window } from "@/components/icons/Window";
import { ActionButton } from "@/components/recording/ActionButton";
import { Button } from "@cap/ui";
import { Logo } from "@/components/icons/Logo";
-import { emit } from "@tauri-apps/api/event";
+import { emit, listen, UnlistenFn } from "@tauri-apps/api/event";
import { invoke } from "@tauri-apps/api/tauri";
import { getLatestVideoId, saveLatestVideoId } from "@/utils/database/utils";
import { openLinkInBrowser } from "@/utils/helpers";
@@ -17,6 +17,7 @@ import toast, { Toaster } from "react-hot-toast";
import { authFetch } from "@/utils/auth/helpers";
import { appDataDir, join } from "@tauri-apps/api/path";
import { open } from "@tauri-apps/api/shell";
+import { window } from "@tauri-apps/api";
declare global {
interface Window {
@@ -137,6 +138,31 @@ export const Recorder = () => {
return data;
};
+ useEffect(() => {
+ let unlistenFn: UnlistenFn | null = null;
+ const registerListener = async () => {
+ unlistenFn = await listen("tray-on-left-click", (_) => {
+ if (isRecording) {
+ handleStopAllRecordings();
+ }
+
+ const currentWindow = window.getCurrent();
+ if (!currentWindow.isVisible) {
+ currentWindow.show();
+ }
+ currentWindow.setFocus();
+ });
+ };
+
+ registerListener();
+
+ return () => {
+ if (unlistenFn) {
+ unlistenFn();
+ }
+ };
+ }, [isRecording, canStopRecording]);
+
const startDualRecording = async (videoData: {
id: string;
user_id: string;
@@ -167,6 +193,7 @@ export const Recorder = () => {
}).catch((error) => {
console.error("Error invoking start_screen_recording:", error);
});
+ emit("toggle-recording", true);
};
const handleStartAllRecordings = async () => {
@@ -229,6 +256,7 @@ export const Recorder = () => {
setIsRecording(false);
setHasStartedRecording(false);
setStoppingRecording(false);
+ emit("toggle-recording", false);
} catch (error) {
console.error("Error stopping recording:", error);
}