f67d38b35cc5
file_dialogs.rs
//! The crash-recovery prompt, the Revert confirmation and the replace prompt for a save dialog's
//! answer that got its extension added (`native_dialogs`). Settings ▸ Auto Save lives in
//! `panels::settings`.
//!
//! Automation ids: `recovery.item.<n>`, `recovery.recover`, `recovery.discard`, `recovery.later`;
//! `revert.yes`, `revert.no`; `overwrite.replace`, `overwrite.cancel`.
use egui::{Color32, CornerRadius, Frame, Margin, RichText, Stroke, Ui, vec2};
use serde_json::json;
use crate::theme::Tokens;
use crate::{Dialog, FilmcraftApp};
/// Dialog-local state.
#[derive(Default)]
pub struct FileDialogState {
pub recovery_choice: usize,
}
pub(crate) fn modal_frame(t: &Tokens) -> Frame {
Frame::new().fill(t.panel_bg).stroke(Stroke::new(1.0, t.separator)).corner_radius(CornerRadius::same(10)).inner_margin(Margin::same(0))
}
pub(crate) fn button(app: &mut FilmcraftApp, ui: &mut Ui, id: &str, label: &str, primary: bool) -> bool {
let t = app.tokens;
let text = RichText::new(label).size(13.0).color(if primary { Color32::WHITE } else { t.text });
let b = egui::Button::new(text)
.min_size(vec2(88.0, 30.0))
.corner_radius(CornerRadius::same(15))
.fill(if primary { t.accent } else { Color32::TRANSPARENT })
.stroke(if primary { Stroke::NONE } else { Stroke::new(1.5, t.text_faint) });
let r = ui.add(b).on_hover_cursor(egui::CursorIcon::PointingHand);
app.auto.add(id, r.rect, label);
r.clicked()
}
pub fn show_recovery(app: &mut FilmcraftApp, ctx: &egui::Context) -> bool {
let t = app.tokens;
let list = app.session.execute("file.recoveryList", json!({})).unwrap_or_default();
let items = list.as_array().cloned().unwrap_or_default();
if items.is_empty() {
return false;
}
let mut choice = app.file_dialogs.recovery_choice.min(items.len() - 1);
let mut open = true;
egui::Modal::new(egui::Id::unique("recovery-modal")).frame(modal_frame(&t)).show(ctx, |ui| {
ui.set_width(520.0);
Frame::new().inner_margin(Margin { left: 28, right: 28, top: 24, bottom: 20 }).show(ui, |ui| {
ui.label(RichText::new(tl!("Recover Unsaved Changes")).size(18.0).strong().color(t.text));
ui.add_space(6.0);
ui.separator();
ui.add_space(6.0);
let c = &items[choice];
let name = c["projectName"].as_str().unwrap_or(tl!("Untitled"));
let when = c["savedAt"].as_str().unwrap_or("");
let why = if c["cleanExit"].as_bool() == Some(true) {
tlf!("FilmCraft was closed while “{name}” had unsaved changes.", name)
} else {
tlf!("FilmCraft quit unexpectedly while “{name}” had unsaved changes.", name)
};
ui.label(RichText::new(why).size(13.5).color(t.text));
ui.add_space(4.0);
ui.label(RichText::new(tlf!("Recover unsaved changes from {when}?", when)).size(13.5).color(t.text));
if let Some(p) = c["projectPath"].as_str() {
ui.label(RichText::new(p).size(11.5).color(t.text_dim));
} else {
ui.label(RichText::new(tl!("The project had not been saved yet.")).size(11.5).color(t.text_dim));
}
if items.len() > 1 {
ui.add_space(8.0);
ui.label(RichText::new(tlf!("{n} sessions have unsaved changes:", n = items.len())).size(12.0).color(t.text_dim));
for (i, it) in items.iter().enumerate() {
let label = format!("{} — {}", it["projectName"].as_str().unwrap_or(tl!("Untitled")), it["savedAt"].as_str().unwrap_or(""));
let r = ui.radio(choice == i, RichText::new(&label).size(12.5));
app.auto.add(&format!("recovery.item.{i}"), r.rect, &label);
if r.clicked() {
choice = i;
}
}
}
ui.add_space(18.0);
let id = items[choice]["id"].as_str().unwrap_or("").to_string();
ui.horizontal(|ui| {
if button(app, ui, "recovery.discard", tl!("Discard"), false) {
if let Err(e) = app.session.execute("file.discardRecovery", json!({ "id": id })) {
app.ui.status = e.to_string();
}
choice = 0;
}
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if button(app, ui, "recovery.recover", tl!("Recover"), true) {
match app.session.execute("file.recover", json!({ "id": id })) {
Ok(_) => open = false,
Err(e) => app.ui.status = e.to_string(),
}
}
if button(app, ui, "recovery.later", tl!("Not Now"), false) {
open = false;
}
});
});
});
});
app.file_dialogs.recovery_choice = choice;
open && !app.session.recovery_candidates().is_empty()
}
pub fn show_revert(app: &mut FilmcraftApp, ctx: &egui::Context) -> bool {
let t = app.tokens;
let file = app.session.path.as_deref().and_then(|p| std::path::Path::new(p).file_name()).map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
let mut open = true;
let r = egui::Modal::new(egui::Id::unique("revert-modal")).frame(modal_frame(&t)).show(ctx, |ui| {
ui.set_width(460.0);
Frame::new().inner_margin(Margin { left: 28, right: 28, top: 24, bottom: 20 }).show(ui, |ui| {
ui.label(RichText::new(tl!("Revert")).size(18.0).strong().color(t.text));
ui.add_space(6.0);
ui.separator();
ui.add_space(6.0);
ui.label(RichText::new(tlf!("Are you sure you want to discard your changes to '{file}'?", file)).size(13.5).color(t.text));
ui.add_space(18.0);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if button(app, ui, "revert.yes", tl!("Yes"), true) {
if let Err(e) = app.session.execute("file.revert", json!({})) {
app.ui.status = e.to_string();
}
open = false;
}
if button(app, ui, "revert.no", tl!("No"), false) {
open = false;
}
});
});
});
// (the modal's own close also fires on Escape: not while a drag is being cancelled, #580)
open && (!r.should_close() || ctx.input(|i| i.pointer.any_down()))
}
/// "“cut.fcproj” already exists in “edits”. Do you want to replace it?" for a save dialog's answer
/// that names an existing file once its extension was added (drawn after every other dialog, so
/// it stays on top). Escape cancels.
pub fn show_overwrite(app: &mut FilmcraftApp, ctx: &egui::Context) {
let Some(path) = app.overwrite_prompt_path().map(std::path::PathBuf::from) else { return };
let t = app.tokens;
let file = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
let folder = path.parent().and_then(|d| d.file_name()).map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
let mut answer = None;
let r = egui::Modal::new(egui::Id::unique("overwrite-modal")).frame(modal_frame(&t)).show(ctx, |ui| {
ui.set_width(460.0);
Frame::new().inner_margin(Margin { left: 28, right: 28, top: 24, bottom: 20 }).show(ui, |ui| {
ui.label(RichText::new(tl!("Replace File")).size(18.0).strong().color(t.text));
ui.add_space(6.0);
ui.separator();
ui.add_space(6.0);
ui.label(RichText::new(tlf!("“{file}” already exists in “{folder}”. Do you want to replace it?", file, folder)).size(13.5).color(t.text));
ui.add_space(18.0);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if button(app, ui, "overwrite.replace", tl!("Replace"), true) {
answer = Some(true);
}
if button(app, ui, "overwrite.cancel", tl!("Cancel"), false) {
answer = Some(false);
}
});
});
});
// (the modal's own close also fires on Escape: not while a drag is being cancelled, #580)
if answer.is_none() && r.should_close() && !ctx.input(|i| i.pointer.any_down()) {
answer = Some(false);
}
if let Some(replace) = answer {
app.answer_overwrite(replace);
}
}
/// Draw the dialog if it is one of ours; returns Some(still open).
pub fn show(app: &mut FilmcraftApp, ctx: &egui::Context, d: Dialog) -> Option<bool> {
match d {
Dialog::Preferences => Some(crate::panels::settings::show(app, ctx)),
Dialog::Recovery => Some(show_recovery(app, ctx)),
Dialog::RevertConfirm => Some(show_revert(app, ctx)),
_ => None,
}
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.