Skip to content

Commit

Permalink
add tauri-specta
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendonovich committed Jun 12, 2024
1 parent 1732b44 commit ebe1383
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 84 deletions.
4 changes: 2 additions & 2 deletions apps/desktop/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { withSentryConfig } from "@sentry/nextjs";
/** @type {import('next').NextConfig} */

import("dotenv").then(({ config }) => config({ path: "../../.env" }));

import fs from "fs";
import path from "path";

fs.copyFileSync(path.resolve("../../.env"), path.resolve("./.env"));

const packageJson = JSON.parse(
fs.readFileSync(path.resolve("./package.json"), "utf8")
);
Expand Down
107 changes: 106 additions & 1 deletion apps/desktop/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions apps/desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ jpeg-encoder = "0.6.0"
nix = "0.20.0"
urlencoding = "2.1.2"
bytes = "1.0"
tauri-specta = { version = "1", features = ["javascript", "typescript"] }
specta = "1"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
44 changes: 33 additions & 11 deletions apps/desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use regex::Regex;
use tokio::sync::Mutex;
use std::sync::atomic::{AtomicBool};
use std::{vec};
use tauri::{command, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTraySubmenu, Window};
use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTraySubmenu, Window};
use window_vibrancy::{apply_blur, apply_vibrancy, NSVisualEffectMaterial};
use window_shadows::set_shadow;
use tauri_plugin_positioner::{WindowExt, Position};
Expand All @@ -34,9 +34,9 @@ use ffmpeg_sidecar::{
use winit::monitor::{MonitorHandle, VideoMode};


fn main() {
fn main() {
let _ = fix_path_env::fix();

std::panic::set_hook(Box::new(|info| {
eprintln!("Thread panicked: {:?}", info);
}));
Expand Down Expand Up @@ -71,7 +71,8 @@ fn main() {

handle_ffmpeg_installation().expect("Failed to install FFmpeg");

#[command]
#[tauri::command]
#[specta::specta]
async fn start_server(window: Window) -> Result<u16, String> {
start(move |url| {
let _ = window.emit("redirect_uri", url);
Expand All @@ -80,6 +81,7 @@ fn main() {
}

#[tauri::command]
#[specta::specta]
fn open_screen_capture_preferences() {
#[cfg(target_os = "macos")]
std::process::Command::new("open")
Expand All @@ -89,6 +91,7 @@ fn main() {
}

#[tauri::command]
#[specta::specta]
fn open_mic_preferences() {
#[cfg(target_os = "macos")]
std::process::Command::new("open")
Expand All @@ -98,6 +101,7 @@ fn main() {
}

#[tauri::command]
#[specta::specta]
fn open_camera_preferences() {
#[cfg(target_os = "macos")]
std::process::Command::new("open")
Expand All @@ -107,6 +111,7 @@ fn main() {
}

#[tauri::command]
#[specta::specta]
fn reset_screen_permissions() {
#[cfg(target_os = "macos")]
std::process::Command::new("tccutil")
Expand All @@ -118,6 +123,7 @@ fn main() {
}

#[tauri::command]
#[specta::specta]
fn reset_microphone_permissions() {
#[cfg(target_os = "macos")]
std::process::Command::new("tccutil")
Expand All @@ -129,6 +135,7 @@ fn main() {
}

#[tauri::command]
#[specta::specta]
fn reset_camera_permissions() {
#[cfg(target_os = "macos")]
std::process::Command::new("tccutil")
Expand Down Expand Up @@ -191,20 +198,35 @@ fn main() {

let tray = SystemTray::new().with_menu(create_tray_menu(None)).with_menu_on_left_click(false).with_title("Cap");

#[cfg(debug_assertions)]
tauri_specta::ts::export(specta::collect_types![
start_dual_recording,
stop_all_recordings,
enumerate_audio_devices,
start_server,
open_screen_capture_preferences,
open_mic_preferences,
open_camera_preferences,
has_screen_capture_access,
reset_screen_permissions,
reset_microphone_permissions,
reset_camera_permissions,
], "../src/utils/commands.ts").unwrap();

tauri::Builder::default()
.plugin(tauri_plugin_oauth::init())
.plugin(tauri_plugin_positioner::init())
.setup(move |app| {
let handle = app.handle();

if let Some(options_window) = app.get_window("main") {
if let Some(options_window) = app.get_window("main") {
let _ = options_window.move_window(Position::Center);
#[cfg(target_os = "macos")]
apply_vibrancy(&options_window, NSVisualEffectMaterial::MediumLight, None, Some(16.0)).expect("Unsupported platform! 'apply_vibrancy' is only supported on macOS");

#[cfg(target_os = "windows")]
apply_blur(&options_window, Some((255, 255, 255, 255))).expect("Unsupported platform! 'apply_blur' is only supported on Windows");

set_shadow(&options_window, true).expect("Unsupported platform!");
}

Expand Down Expand Up @@ -249,7 +271,7 @@ fn main() {
}
}
});

let tray_handle = app.tray_handle();
app.listen_global("media-devices-set", move|event| {
#[derive(serde::Deserialize)]
Expand All @@ -265,7 +287,7 @@ fn main() {
let id_prefix = if kind == DeviceKind::Video {
"video"
} else {
"audio"
"audio"
};
let mut none_item = CustomMenuItem::new(format!("in_{}_none", id_prefix), "None");
if selected_device.is_none() {
Expand All @@ -277,13 +299,13 @@ fn main() {
.filter(|device| device.kind == kind)
.fold(initial, |tray_items, device| {
let mut menu_item = CustomMenuItem::new(format!("in_{}_{}", id_prefix, device.id), &device.label);

if let Some(selected) = selected_device {
if selected.label == device.label {
menu_item = menu_item.selected();
}
}

tray_items.add_item(menu_item)
})
}
Expand Down Expand Up @@ -348,7 +370,7 @@ fn main() {
let kind = if item_id.contains("video") { "videoinput" } else { "audioinput" };

app.emit_all("tray-set-device-id", SetDevicePayload {
device_type: kind.to_string(),
device_type: kind.to_string(),
id: if device_id == "none" { None } else { Some(device_id) }
}).expect("Failed to emit tray set media device event to windows");
}
Expand Down
Loading

0 comments on commit ebe1383

Please sign in to comment.