| 1 | //! Native macOS file Open/Save dialogs (`osascript`), composition Open/Save/Undo/Redo, |
| 2 | //! Flow toolbar, and top-level keyboard shortcut dispatch. |
| 3 | |
| 4 | use crate::playback_ui::spawn_audio_loader; |
| 5 | use crate::{Studio, flow_pane_id, studio_btn}; |
| 6 | use dock::Command; |
| 7 | use model::{Comp, NodeId}; |
| 8 | use std::path::{Path, PathBuf}; |
| 9 | use std::process::Command as ProcessCommand; |
| 10 | use std::time::Instant; |
| 11 | use winit::event::ElementState; |
| 12 | use winit::keyboard::{Key, KeyCode, NamedKey, PhysicalKey}; |
| 13 | |
| 14 | pub(crate) fn native_open_comp_dialog() -> Option<PathBuf> { |
| 15 | // Use AppKit NSOpenPanel via JXA so `.comp` resolves to Fusion's registered UTI |
| 16 | // (`com.blackmagic-design.fusion.composition`) when installed and to a dynamic UTI otherwise. |
| 17 | // AppleScript's `choose file of type {"comp"}` grays out `.comp` files when Fusion is installed. |
| 18 | let script = r#" |
| 19 | ObjC.import("AppKit"); |
| 20 | const app = $.NSApplication.sharedApplication; |
| 21 | app.setActivationPolicy($.NSApplicationActivationPolicyAccessory); |
| 22 | app.activateIgnoringOtherApps(true); |
| 23 | const panel = $.NSOpenPanel.openPanel; |
| 24 | panel.title = "Open Fusion Composition"; |
| 25 | panel.prompt = "Open"; |
| 26 | panel.canChooseFiles = true; |
| 27 | panel.canChooseDirectories = false; |
| 28 | panel.allowsMultipleSelection = false; |
| 29 | panel.allowedFileTypes = $(["comp", "autocomp"]); |
| 30 | panel.runModal == $.NSModalResponseOK ? ObjC.unwrap(panel.URL.path) : ""; |
| 31 | "#; |
| 32 | let out = ProcessCommand::new("osascript") |
| 33 | .args(["-l", "JavaScript", "-e", script]) |
| 34 | .output() |
| 35 | .ok()?; |
| 36 | if !out.status.success() { |
| 37 | return None; |
| 38 | } |
| 39 | let s = String::from_utf8_lossy(&out.stdout).trim().to_owned(); |
| 40 | if s.is_empty() { None } else { Some(PathBuf::from(s)) } |
| 41 | } |
| 42 | |
| 43 | pub(crate) fn sanitize_filename(name: &str) -> String { |
| 44 | let cleaned: String = name |
| 45 | .chars() |
| 46 | .map(|c| match c { |
| 47 | '/' | '\\' | ':' | '"' | '\n' | '\r' => '_', |
| 48 | other => other, |
| 49 | }) |
| 50 | .collect(); |
| 51 | if cleaned.is_empty() { |
| 52 | "untitled".to_owned() |
| 53 | } else { |
| 54 | cleaned |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn applescript_escape(s: &str) -> String { |
| 59 | s.replace('\\', "\\\\").replace('"', "\\\"") |
| 60 | } |
| 61 | |
| 62 | pub(crate) fn native_save_file_dialog(prompt: &str, default_name: &str) -> Option<PathBuf> { |
| 63 | let safe_prompt = applescript_escape(prompt); |
| 64 | let safe_name = applescript_escape(default_name); |
| 65 | let script = format!( |
| 66 | r#"POSIX path of (choose file name with prompt "{safe_prompt}" default name "{safe_name}")"# |
| 67 | ); |
| 68 | let out = ProcessCommand::new("osascript").args(["-e", &script]).output().ok()?; |
| 69 | if !out.status.success() { |
| 70 | return None; |
| 71 | } |
| 72 | let s = String::from_utf8_lossy(&out.stdout).trim().to_owned(); |
| 73 | if s.is_empty() { None } else { Some(PathBuf::from(s)) } |
| 74 | } |
| 75 | |
| 76 | pub(crate) fn native_save_comp_dialog(default_name: &str) -> Option<PathBuf> { |
| 77 | native_save_file_dialog("Save Fusion Composition As", default_name) |
| 78 | } |
| 79 | |
| 80 | pub(crate) struct FlowToolbarActions { |
| 81 | pub(crate) open_add_tool: bool, |
| 82 | pub(crate) undo: bool, |
| 83 | pub(crate) redo: bool, |
| 84 | pub(crate) open: bool, |
| 85 | pub(crate) save: bool, |
| 86 | pub(crate) save_as: bool, |
| 87 | pub(crate) exit_to_depth: Option<usize>, |
| 88 | pub(crate) commit_rename: Option<(NodeId, String)>, |
| 89 | pub(crate) cancel_rename: bool, |
| 90 | } |
| 91 | |
| 92 | #[allow(clippy::too_many_arguments)] |
| 93 | pub(crate) fn build_flow_toolbar( |
| 94 | ui: &mut ui::Ui, |
| 95 | comp: &Comp, |
| 96 | scope_path: &[NodeId], |
| 97 | can_undo: bool, |
| 98 | can_redo: bool, |
| 99 | comp_unsaved: bool, |
| 100 | renaming_node: &mut Option<NodeId>, |
| 101 | rename_buf: &mut String, |
| 102 | rename_needs_focus: &mut bool, |
| 103 | ) -> FlowToolbarActions { |
| 104 | let mut actions = FlowToolbarActions { |
| 105 | open_add_tool: false, |
| 106 | undo: false, |
| 107 | redo: false, |
| 108 | open: false, |
| 109 | save: false, |
| 110 | save_as: false, |
| 111 | exit_to_depth: None, |
| 112 | commit_rename: None, |
| 113 | cancel_rename: false, |
| 114 | }; |
| 115 | |
| 116 | let bar_bg = ui.theme.strip; |
| 117 | ui.open( |
| 118 | "flow-toolbar", |
| 119 | ui::Spec { |
| 120 | size: [ui::fill(), ui::px(28.0)], |
| 121 | axis: ui::Axis::X, |
| 122 | pad: [6.0, 3.0], |
| 123 | gap: 4.0, |
| 124 | fill: Some(bar_bg), |
| 125 | ..ui::Spec::default() |
| 126 | }, |
| 127 | ); |
| 128 | if studio_btn(ui, "flow-add-btn", "+ Add", false).clicked { |
| 129 | actions.open_add_tool = true; |
| 130 | } |
| 131 | if studio_btn(ui, "flow-undo-btn", "Undo", can_undo).clicked && can_undo { |
| 132 | actions.undo = true; |
| 133 | } |
| 134 | if studio_btn(ui, "flow-redo-btn", "Redo", can_redo).clicked && can_redo { |
| 135 | actions.redo = true; |
| 136 | } |
| 137 | if studio_btn(ui, "flow-open-btn", "Open", false).clicked { |
| 138 | actions.open = true; |
| 139 | } |
| 140 | if studio_btn(ui, "flow-save-btn", if comp_unsaved { "Save*" } else { "Save" }, comp_unsaved).clicked { |
| 141 | actions.save = true; |
| 142 | } |
| 143 | if studio_btn(ui, "flow-saveas-btn", "Save As", false).clicked { |
| 144 | actions.save_as = true; |
| 145 | } |
| 146 | if !scope_path.is_empty() { |
| 147 | let crumb_sep = |ui: &mut ui::Ui, part: usize| { |
| 148 | let ink = ui.theme.text_dim; |
| 149 | ui.leaf( |
| 150 | ("flow-crumb-sep", part), |
| 151 | ui::Spec { |
| 152 | size: [ui::fit(), ui::px(22.0)], |
| 153 | text: Some(">"), |
| 154 | font_size: Some(11.0), |
| 155 | color: Some(ink), |
| 156 | pad: [2.0, 0.0], |
| 157 | center: true, |
| 158 | ..ui::Spec::default() |
| 159 | }, |
| 160 | ); |
| 161 | }; |
| 162 | if studio_btn(ui, "flow-crumb-root", "Root", false).clicked { |
| 163 | actions.exit_to_depth = Some(0); |
| 164 | } |
| 165 | for (depth, &group) in scope_path.iter().enumerate() { |
| 166 | let Some(group_node) = comp.nodes.get(group.0) else { |
| 167 | break; |
| 168 | }; |
| 169 | crumb_sep(ui, depth); |
| 170 | let current = depth + 1 == scope_path.len(); |
| 171 | let crumb = studio_btn(ui, ("flow-crumb", depth), &group_node.name, current); |
| 172 | if crumb.clicked && !current { |
| 173 | actions.exit_to_depth = Some(depth + 1); |
| 174 | } |
| 175 | } |
| 176 | crumb_sep(ui, scope_path.len()); |
| 177 | ui.leaf( |
| 178 | "flow-crumb-hint", |
| 179 | ui::Spec { |
| 180 | size: [ui::fit(), ui::px(22.0)], |
| 181 | text: Some("Esc: back"), |
| 182 | font_size: Some(10.5), |
| 183 | color: Some(ui.theme.text_dim), |
| 184 | center: true, |
| 185 | ..ui::Spec::default() |
| 186 | }, |
| 187 | ); |
| 188 | } |
| 189 | if let Some(rn_id) = *renaming_node { |
| 190 | let rn_field_id = flow_pane_id().child(("rename-field", rn_id.0)); |
| 191 | let rn_sig = ui::text_field( |
| 192 | ui, |
| 193 | rn_field_id, |
| 194 | rename_buf, |
| 195 | "Rename node…", |
| 196 | ui::Spec { |
| 197 | size: [ui::px(160.0), ui::px(22.0)], |
| 198 | font_size: Some(11.5), |
| 199 | fill: Some(ui.theme.base), |
| 200 | border: Some(ui.theme.accent), |
| 201 | radius: 4.0, |
| 202 | pad: [6.0, 1.0], |
| 203 | ..ui::Spec::default() |
| 204 | }, |
| 205 | ); |
| 206 | if *rename_needs_focus { |
| 207 | ui.set_focus(Some(rn_field_id)); |
| 208 | *rename_needs_focus = false; |
| 209 | } else if !rn_sig.focused { |
| 210 | *renaming_node = None; |
| 211 | } |
| 212 | for ev in &rn_sig.events { |
| 213 | if let ui::Event::Key { key, .. } = ev { |
| 214 | match key { |
| 215 | Key::Named(NamedKey::Enter) => { |
| 216 | actions.commit_rename = Some((rn_id, rename_buf.trim().to_owned())); |
| 217 | } |
| 218 | Key::Named(NamedKey::Escape) => { |
| 219 | actions.cancel_rename = true; |
| 220 | } |
| 221 | _ => {} |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | ui.close(); // flow-toolbar |
| 227 | |
| 228 | actions |
| 229 | } |
| 230 | |
| 231 | /// Dispatches global file, history, export, playback, and dock shortcuts. |
| 232 | /// Returns `true` if the event was consumed. |
| 233 | pub(crate) fn handle_global_shortcut(studio: &mut Studio, event: &ui::Event) -> bool { |
| 234 | let typing = studio.ui.typing(); |
| 235 | // Global file & history shortcuts: Cmd+S, Cmd+Shift+S, Cmd+O, Cmd+Z, Cmd+Shift+Z / Cmd+Y, Cmd+Shift+R, Cmd+Shift+E |
| 236 | let key_cmd = match event { |
| 237 | ui::Event::KeyInput { physical, modifiers, state: ElementState::Pressed, repeat: false, .. } => { |
| 238 | let cmd = modifiers.super_key() || modifiers.control_key(); |
| 239 | let shift = modifiers.shift_key(); |
| 240 | if cmd && !shift && *physical == PhysicalKey::Code(KeyCode::KeyS) { |
| 241 | Some("save") |
| 242 | } else if cmd && shift && *physical == PhysicalKey::Code(KeyCode::KeyS) { |
| 243 | Some("save_as") |
| 244 | } else if cmd && !shift && *physical == PhysicalKey::Code(KeyCode::KeyO) { |
| 245 | Some("open") |
| 246 | } else if cmd && !shift && !typing && *physical == PhysicalKey::Code(KeyCode::KeyZ) { |
| 247 | Some("undo") |
| 248 | } else if cmd && shift && !typing && *physical == PhysicalKey::Code(KeyCode::KeyZ) { |
| 249 | Some("redo") |
| 250 | } else if cmd && !shift && !typing && *physical == PhysicalKey::Code(KeyCode::KeyY) { |
| 251 | Some("redo") |
| 252 | } else if cmd && shift && *physical == PhysicalKey::Code(KeyCode::KeyR) { |
| 253 | Some("export") |
| 254 | } else if cmd && shift && *physical == PhysicalKey::Code(KeyCode::KeyE) { |
| 255 | Some("export_preview_mp4") |
| 256 | } else { |
| 257 | None |
| 258 | } |
| 259 | } |
| 260 | ui::Event::Key { key: Key::Character(c), modifiers, .. } => { |
| 261 | let cmd = modifiers.super_key() || modifiers.control_key(); |
| 262 | let shift = modifiers.shift_key(); |
| 263 | if cmd && !shift && c.eq_ignore_ascii_case("s") { |
| 264 | Some("save") |
| 265 | } else if cmd && shift && c.eq_ignore_ascii_case("s") { |
| 266 | Some("save_as") |
| 267 | } else if cmd && !shift && c.eq_ignore_ascii_case("o") { |
| 268 | Some("open") |
| 269 | } else if cmd && !shift && !typing && c.eq_ignore_ascii_case("z") { |
| 270 | Some("undo") |
| 271 | } else if cmd && shift && !typing && c.eq_ignore_ascii_case("z") { |
| 272 | Some("redo") |
| 273 | } else if cmd && !shift && !typing && c.eq_ignore_ascii_case("y") { |
| 274 | Some("redo") |
| 275 | } else if cmd && shift && c.eq_ignore_ascii_case("r") { |
| 276 | Some("export") |
| 277 | } else if cmd && shift && c.eq_ignore_ascii_case("e") { |
| 278 | Some("export_preview_mp4") |
| 279 | } else { |
| 280 | None |
| 281 | } |
| 282 | } |
| 283 | _ => None, |
| 284 | }; |
| 285 | if let Some(action) = key_cmd { |
| 286 | match action { |
| 287 | "save" => { let _ = studio.save_comp(); } |
| 288 | "save_as" => { let _ = studio.save_comp_as(); } |
| 289 | "open" => studio.open_comp(), |
| 290 | "undo" => studio.undo(), |
| 291 | "redo" => studio.redo(), |
| 292 | "export" => { let _ = studio.export_saver_pngs(); } |
| 293 | "export_preview_mp4" => { |
| 294 | let slot = studio.active_viewer_slot(); |
| 295 | let _ = studio.export_viewer_mp4(slot); |
| 296 | } |
| 297 | _ => {} |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | // Canvas panes draw their own popups, which `popups_open` doesn't see. |
| 303 | let popup_open = studio.ui.popups_open() |
| 304 | || studio.add_tool.open |
| 305 | || studio.spline.is_popup_open() |
| 306 | || studio.viewers.iter().any(|v| v.is_context_menu_open()); |
| 307 | // Shift+Space opens Add Tool launcher (when not typing in a text field) |
| 308 | if !typing && !popup_open { |
| 309 | let is_shift_space = match event { |
| 310 | ui::Event::KeyInput { key: Key::Named(NamedKey::Space), modifiers, state: ElementState::Pressed, repeat: false, .. } |
| 311 | | ui::Event::Key { key: Key::Named(NamedKey::Space), modifiers, .. } => { |
| 312 | modifiers.shift_key() && !modifiers.super_key() && !modifiers.control_key() && !modifiers.alt_key() |
| 313 | } |
| 314 | _ => false, |
| 315 | }; |
| 316 | if is_shift_space { |
| 317 | studio.open_add_tool(); |
| 318 | return true; |
| 319 | } |
| 320 | // Global Space toggles playback (synced with audio) and `` ` `` clears both viewers from any focused pane! |
| 321 | let is_space = match event { |
| 322 | ui::Event::KeyInput { key: Key::Named(NamedKey::Space), modifiers, state: ElementState::Pressed, repeat: false, .. } |
| 323 | | ui::Event::Key { key: Key::Named(NamedKey::Space), modifiers, .. } => { |
| 324 | !modifiers.shift_key() && !modifiers.super_key() && !modifiers.control_key() && !modifiers.alt_key() |
| 325 | } |
| 326 | _ => false, |
| 327 | }; |
| 328 | if is_space { |
| 329 | studio.playback.toggle_play_with_comp(&studio.comp, Instant::now()); |
| 330 | studio.sync_audio(true, false); |
| 331 | return true; |
| 332 | } |
| 333 | let is_backquote = match event { |
| 334 | ui::Event::KeyInput { physical: PhysicalKey::Code(KeyCode::Backquote), modifiers, state: ElementState::Pressed, repeat: false, .. } => { |
| 335 | !modifiers.super_key() && !modifiers.control_key() && !modifiers.alt_key() |
| 336 | } |
| 337 | ui::Event::Key { key: Key::Character(c), modifiers, .. } => { |
| 338 | (c.as_str() == "`" || c.as_str() == "~") && !modifiers.super_key() && !modifiers.control_key() && !modifiers.alt_key() |
| 339 | } |
| 340 | _ => false, |
| 341 | }; |
| 342 | if is_backquote { |
| 343 | studio.clear_viewers(); |
| 344 | return true; |
| 345 | } |
| 346 | if !studio.flow.is_modal_active() && !studio.spline.is_modal_active() { |
| 347 | let viewer_slot = match event { |
| 348 | ui::Event::KeyInput { key: Key::Character(c), modifiers, state: ElementState::Pressed, repeat: false, .. } |
| 349 | | ui::Event::Key { key: Key::Character(c), modifiers, .. } |
| 350 | if !modifiers.shift_key() |
| 351 | && !modifiers.super_key() |
| 352 | && !modifiers.control_key() |
| 353 | && !modifiers.alt_key() => |
| 354 | { |
| 355 | match c.as_str() { |
| 356 | "1" => Some(0), |
| 357 | "2" => Some(1), |
| 358 | _ => None, |
| 359 | } |
| 360 | } |
| 361 | _ => None, |
| 362 | }; |
| 363 | if let Some(slot) = viewer_slot { |
| 364 | studio.toggle_viewer_slot(slot); |
| 365 | return true; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | // Canvas panes receive `KeyInput`; every other pane receives `Key`. |
| 370 | if !studio.ui.popups_open() |
| 371 | && !studio.add_tool.open |
| 372 | && let ui::Event::KeyInput { key, modifiers, state: ElementState::Pressed, repeat: false, .. } |
| 373 | | ui::Event::Key { key, modifiers, .. } = event |
| 374 | && matches!(key, Key::Named(NamedKey::Tab)) |
| 375 | && modifiers.control_key() |
| 376 | { |
| 377 | let cmd = if modifiers.shift_key() { Command::Previous } else { Command::Next }; |
| 378 | let _ = studio.dock.at(&mut studio.layout, &mut studio.seen, &String::clone).run(cmd); |
| 379 | return true; |
| 380 | } |
| 381 | false |
| 382 | } |
| 383 | |
| 384 | impl Studio { |
| 385 | pub(crate) fn undo(&mut self) { |
| 386 | let label = self.history.undo_label().unwrap_or("Edit").to_owned(); |
| 387 | let prev_viewed = self.viewed_names(); |
| 388 | if self.history.undo(&mut self.comp) { |
| 389 | self.reconcile_node_refs(prev_viewed); |
| 390 | self.invalidate_node(None); |
| 391 | self.set_notice(format!("Undo: {label}")); |
| 392 | } else { |
| 393 | self.set_notice("Nothing to undo"); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | pub(crate) fn redo(&mut self) { |
| 398 | let label = self.history.redo_label().unwrap_or("Edit").to_owned(); |
| 399 | let prev_viewed = self.viewed_names(); |
| 400 | if self.history.redo(&mut self.comp) { |
| 401 | self.reconcile_node_refs(prev_viewed); |
| 402 | self.invalidate_node(None); |
| 403 | self.set_notice(format!("Redo: {label}")); |
| 404 | } else { |
| 405 | self.set_notice("Nothing to redo"); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | pub(crate) fn save_comp(&mut self) -> Result<String, String> { |
| 410 | let Some(path) = self.comp.path.clone() else { |
| 411 | return self.save_comp_as(); |
| 412 | }; |
| 413 | self.save_to_path(&path) |
| 414 | } |
| 415 | |
| 416 | pub(crate) fn save_to_path(&mut self, path: &Path) -> Result<String, String> { |
| 417 | self.comp.current_time = self.playback.current_frame; |
| 418 | match self.comp.save_to_file(path) { |
| 419 | Ok(backup) => { |
| 420 | self.comp.path = Some(path.to_path_buf()); |
| 421 | if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) { |
| 422 | self.comp.name = stem.to_owned(); |
| 423 | } |
| 424 | self.history.mark_saved(&self.comp); |
| 425 | let msg = match backup { |
| 426 | Some(bak) => { |
| 427 | eprintln!("Saved {} (backup -> {})", path.display(), bak.display()); |
| 428 | "Saved (backup in temp)".to_owned() |
| 429 | } |
| 430 | None => { |
| 431 | eprintln!("Saved {}", path.display()); |
| 432 | format!("Saved {}", self.comp.name) |
| 433 | } |
| 434 | }; |
| 435 | self.set_notice(msg.clone()); |
| 436 | Ok(msg) |
| 437 | } |
| 438 | Err(e) => { |
| 439 | let err = format!("Save failed: {e}"); |
| 440 | eprintln!("{err}"); |
| 441 | self.set_notice(err.clone()); |
| 442 | Err(err) |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | pub(crate) fn save_comp_as(&mut self) -> Result<String, String> { |
| 448 | let default_name = format!("{}.comp", self.comp.name); |
| 449 | let Some(mut path) = native_save_comp_dialog(&default_name) else { |
| 450 | return Err("Save As cancelled".to_owned()); |
| 451 | }; |
| 452 | if path.extension().is_none() { |
| 453 | path.set_extension("comp"); |
| 454 | } |
| 455 | self.save_to_path(&path) |
| 456 | } |
| 457 | |
| 458 | pub(crate) fn open_comp(&mut self) { |
| 459 | let Some(path) = native_open_comp_dialog() else { |
| 460 | return; |
| 461 | }; |
| 462 | match Comp::from_fusion_file(&path) { |
| 463 | Ok(new_comp) => { |
| 464 | self.reload_comp(new_comp); |
| 465 | self.set_notice(format!("Opened {}", self.comp.name)); |
| 466 | } |
| 467 | Err(e) => { |
| 468 | let msg = format!("Open failed: {e}"); |
| 469 | eprintln!("{msg}"); |
| 470 | self.set_notice(msg); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | pub(crate) fn reload_comp(&mut self, new_comp: Comp) { |
| 476 | if self.playback.playing { |
| 477 | self.playback.toggle_play(Instant::now()); |
| 478 | self.sync_audio(true, false); |
| 479 | } |
| 480 | self.comp = new_comp; |
| 481 | // The old viewed nodes mean nothing in the new comp; viewers fall back to its output below. |
| 482 | self.reconcile_node_refs([None, None]); |
| 483 | self.history.clear(); |
| 484 | self.history.mark_saved(&self.comp); |
| 485 | self.spline.hidden_splines.clear(); |
| 486 | self.engine.bust_all_caches(); |
| 487 | self.playback.init_from_comp(&self.comp); |
| 488 | |
| 489 | let out_node = self.comp.default_output_node(); |
| 490 | for (slot, v) in self.viewers.iter_mut().enumerate() { |
| 491 | v.viewed_node = out_node; |
| 492 | v.cached_frames.clear(); |
| 493 | v.dirty = true; |
| 494 | if slot == 0 { |
| 495 | v.fit(); |
| 496 | } |
| 497 | } |
| 498 | self.flow.reset_for_comp(&self.comp); |
| 499 | self.flow.viewed_nodes = self.viewers.each_ref().map(|v| v.viewed_node); |
| 500 | let default_sel = self |
| 501 | .comp |
| 502 | .nodes |
| 503 | .iter() |
| 504 | .find(|n| !self.comp.node_splines(n.id).is_empty()) |
| 505 | .map(|n| n.id) |
| 506 | .or(out_node); |
| 507 | if let Some(id) = default_sel { |
| 508 | self.flow.selected.insert(id); |
| 509 | self.flow.primary = Some(id); |
| 510 | } |
| 511 | self.spline.refresh_for_comp(&self.comp, default_sel, true); |
| 512 | self.keyframes.audio_peaks.clear(); |
| 513 | self.keyframes.cached_ranges.clear(); |
| 514 | self.keyframes.fit_comp(&self.comp); |
| 515 | self.keyframes.sync_tracks(&self.comp, default_sel); |
| 516 | for v in &mut self.viewers { |
| 517 | v.audio_peaks.clear(); |
| 518 | } |
| 519 | self.ram_cache.clear(); |
| 520 | self.cancel_prefetch(); |
| 521 | self.loaded_audio_track = None; |
| 522 | self.last_audio_offset = self.comp.audio_offset; |
| 523 | self.last_fps = self.comp.fps; |
| 524 | self.last_audio_filename = self.comp.audio_filename.clone(); |
| 525 | |
| 526 | if self.enable_audio { |
| 527 | if let Some(player) = &mut self.audio_player { |
| 528 | player.set_track(None); |
| 529 | } |
| 530 | self.audio_rx = spawn_audio_loader(&self.comp); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | pub(crate) fn copy_nodes(&mut self, ids: &[NodeId]) { |
| 535 | self.ui.clipboard.set_text(self.comp.export_fusion_clipboard(ids)); |
| 536 | let n = ids.len(); |
| 537 | self.set_notice(format!("Copied {n} node{}", if n == 1 { "" } else { "s" })); |
| 538 | } |
| 539 | |
| 540 | pub(crate) fn paste_nodes(&mut self) { |
| 541 | if self.reject_in_group("pasting") { |
| 542 | return; |
| 543 | } |
| 544 | let Some(text) = self.ui.clipboard.text() else { |
| 545 | self.set_notice("Clipboard is empty"); |
| 546 | return; |
| 547 | }; |
| 548 | match self.history.paste_fusion_clipboard(&mut self.comp, &text, (60.0, 40.0)) { |
| 549 | Ok(ids) => { |
| 550 | self.flow.selected = ids.iter().copied().collect(); |
| 551 | self.flow.primary = ids.first().copied(); |
| 552 | self.spline.set_target_node(&self.comp, self.flow.primary); |
| 553 | self.invalidate_node(None); |
| 554 | let n = ids.len(); |
| 555 | self.set_notice(format!("Pasted {n} node{}", if n == 1 { "" } else { "s" })); |
| 556 | } |
| 557 | Err(e) => self.set_notice(format!("Nothing pasted: {e}")), |
| 558 | } |
| 559 | } |
| 560 | } |