Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

dispose editor instance on close

Brendan Allan5d37ec3f60d4
diff --git a/apps/desktop-solid/src-tauri/capabilities/default.json b/apps/desktop-solid/src-tauri/capabilities/default.json
index 2e31ea154..5010d0dcc 100644
--- a/apps/desktop-solid/src-tauri/capabilities/default.json
+++ b/apps/desktop-solid/src-tauri/capabilities/default.json
@@ -2,7 +2,7 @@
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main window",
- "windows": ["main", "camera", "prev-recordings", "editor"],
+ "windows": ["*"],
"permissions": [
"core:default",
"core:window:allow-close",
diff --git a/apps/desktop-solid/src-tauri/src/display.rs b/apps/desktop-solid/src-tauri/src/display.rs
index c4092e87e..3b524b1d2 100644
--- a/apps/desktop-solid/src-tauri/src/display.rs
+++ b/apps/desktop-solid/src-tauri/src/display.rs
@@ -68,7 +68,7 @@ pub fn list_capture_windows() -> Vec<CaptureWindow> {
.collect::<Vec<_>>()
}
-pub const FPS: u32 = 30;
+pub const FPS: u32 = 60;
pub async fn start_capturing(
pipe_path: PathBuf,
@@ -97,7 +97,7 @@ pub async fn start_capturing(
fps: FPS,
show_highlight: true,
output_type: FrameType::BGRAFrame,
- output_resolution: Resolution::Captured,
+ output_resolution: Resolution::_2160p,
crop_area,
..Default::default()
};
diff --git a/apps/desktop-solid/src-tauri/src/lib.rs b/apps/desktop-solid/src-tauri/src/lib.rs
index 85823ebd6..22143c3c3 100644
--- a/apps/desktop-solid/src-tauri/src/lib.rs
+++ b/apps/desktop-solid/src-tauri/src/lib.rs
@@ -34,7 +34,7 @@ use std::{
time::Duration,
};
use std::{fs::File, pin};
-use tauri::{AppHandle, Manager, State, WebviewWindow};
+use tauri::{AppHandle, Manager, State, WebviewWindow, WindowEvent};
use tauri_nspanel::{cocoa::appkit::NSMainMenuWindowLevel, ManagerExt};
use tauri_plugin_decorum::WebviewWindowExt;
use tauri_specta::Event;
@@ -420,6 +420,7 @@ struct AudioData {
struct EditorInstance {
app: AppHandle,
pub path: PathBuf,
+ pub id: String,
pub screen_decoder: VideoDecoderActor,
pub camera_decoder: Option<VideoDecoderActor>,
pub audio: Option<AudioData>,
@@ -596,6 +597,7 @@ impl EditorInstance {
Self {
app: app.clone(),
+ id: video_id,
path: project_path,
screen_decoder,
camera_decoder,
@@ -611,16 +613,46 @@ impl EditorInstance {
}
pub async fn get(app: &AppHandle, video_id: String) -> Arc<Self> {
- match app.try_state::<Arc<EditorInstance>>() {
- Some(state) => (*state).clone(),
+ let map = match app.try_state::<Arc<Mutex<HashMap<String, Arc<Self>>>>>() {
+ Some(s) => (*s).clone(),
None => {
- let instance = Arc::new(EditorInstance::new(app, video_id).await);
- app.manage(instance.clone());
+ let map = Arc::new(Mutex::new(HashMap::new()));
+ app.manage(map.clone());
+ map
+ }
+ };
+
+ let mut map = map.lock().await;
+
+ use std::collections::hash_map::Entry;
+ match map.entry(video_id.clone()) {
+ Entry::Occupied(o) => o.get().clone(),
+ Entry::Vacant(v) => {
+ let instance = Arc::new(Self::new(app, video_id).await);
+ v.insert(instance.clone());
instance
}
}
}
+ pub async fn dispose(&self) {
+ let Some(map) = self
+ .app
+ .try_state::<Arc<Mutex<HashMap<String, Arc<Self>>>>>()
+ else {
+ return;
+ };
+
+ let mut state = self.state.lock().await;
+ println!("got state");
+ if let Some(sender) = state.playback_task.take() {
+ println!("stopping playback");
+ sender.send(true).ok();
+ };
+
+ map.lock().await.remove(&self.id);
+ }
+
pub async fn modify_and_emit_state(&self, modify: impl Fn(&mut EditorState)) {
let mut state = self.state.lock().await;
modify(&mut state);
@@ -1305,6 +1337,20 @@ pub fn run() {
Ok(())
})
+ .on_window_event(|window, event| {
+ let label = window.label();
+ if label.starts_with("editor-") {
+ if let WindowEvent::CloseRequested {..} = event {
+ let id = label.strip_prefix("editor-").unwrap().to_string();
+
+ let app = window.app_handle().clone();
+
+ tokio::spawn(async move {
+ EditorInstance::get(&app, id).await.dispose().await;
+ });
+ }
+ }
+ })
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
diff --git a/apps/desktop-solid/src/routes/prev-recordings.tsx b/apps/desktop-solid/src/routes/prev-recordings.tsx
index a4603d1c1..949a6efdb 100644
--- a/apps/desktop-solid/src/routes/prev-recordings.tsx
+++ b/apps/desktop-solid/src/routes/prev-recordings.tsx
@@ -178,11 +178,17 @@ export default function () {
tooltipText="Edit"
tooltipPlacement="right"
onClick={() => {
- new WebviewWindow("editor", {
- width: 1150,
- height: 800,
- url: `/editor?path=${recording}`,
- });
+ new WebviewWindow(
+ `editor-${recording
+ .split("/")
+ .at(-1)
+ ?.split(".")[0]!}`,
+ {
+ width: 1150,
+ height: 800,
+ url: `/editor?path=${recording}`,
+ }
+ );
}}
>
<IconCapEditor class="size-[1rem]" />
diff --git a/apps/desktop/src-tauri/src/main.rs b/apps/desktop/src-tauri/src/main.rs
index 748ac856f..cfe0c9b9b 100644
--- a/apps/desktop/src-tauri/src/main.rs
+++ b/apps/desktop/src-tauri/src/main.rs
@@ -182,15 +182,18 @@ fn main() {
app.manage(Arc::new(Mutex::new(recording_state)));
if let Some(main_tray) = app.tray_by_id("cap_main") {
- main_tray.on_tray_icon_event(move |tray, event| if let tauri::tray::TrayIconEvent::Click {
+ main_tray.on_tray_icon_event(move |tray, event| {
+ if let tauri::tray::TrayIconEvent::Click {
button,
button_state,
..
- } = event {
- if button == MouseButton::Left && button_state == MouseButtonState::Down {
- if let Err(err) = tray.app_handle().emit("cap://tray/clicked", ()) {
- eprintln!("Failed to emit event for tray {}", err);
- };
+ } = event
+ {
+ if button == MouseButton::Left && button_state == MouseButtonState::Down {
+ if let Err(err) = tray.app_handle().emit("cap://tray/clicked", ()) {
+ eprintln!("Failed to emit event for tray {}", err);
+ };
+ }
}
});
}