Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 96 additions & 1 deletion crates/app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::sync_view::{SyncRequest, SyncView};
use crate::theme::{self, pal, radius, space, text};
use crate::toasts::{ToastKind, Toasts};
use crate::transfers::{self, TransfersView};
use crate::undo::Op;
use crate::util::{self, same_fs};
use crate::widgets;
use crate::worker::{self, ConnectRequest, WorkResult, Worker};
Expand Down Expand Up @@ -164,6 +165,9 @@ pub enum Action {
TrashExternal(Vec<PathBuf>),
/// "Empty Trash…" (asks first).
EmptyTrash,
/// ⌘Z / ⇧⌘Z: the next step of the file operation history.
Undo,
Redo,
/// Show the Trash in Finder (explicitly Finder: Manta may be the
/// default file viewer).
ShowTrashInFinder,
Expand Down Expand Up @@ -451,6 +455,8 @@ pub struct MantaApp {
pub(crate) trasher: crate::trash::SharedTrasher,
/// The sidebar's Trash: its folder and full / empty state.
pub(crate) trash: trash_place::TrashPlace,
/// Undo and Redo (⌘Z, ⇧⌘Z) of Manta's own file operations.
pub(crate) history: crate::undo::History,
/// Where the Trash row was drawn in the last frame.
pub(crate) trash_rect: Option<egui::Rect>,
/// Secrets of favorites: the login Keychain (tests: memory).
Expand Down Expand Up @@ -608,7 +614,10 @@ pub(crate) fn can_trash(fs: &SharedVfs) -> bool {

/// "New File": an empty file at `path`. `open_write` truncates, so a file
/// that appeared since the folder was listed is left alone.
fn create_empty_file(fs: &dyn manta_core::Vfs, path: &VfsPath) -> manta_core::VfsResult<()> {
pub(crate) fn create_empty_file(
fs: &dyn manta_core::Vfs,
path: &VfsPath,
) -> manta_core::VfsResult<()> {
if fs.stat(path).is_ok() {
return Err(manta_core::VfsError::AlreadyExists(path.to_string()));
}
Expand Down Expand Up @@ -977,6 +986,7 @@ impl MantaApp {
folder_views_path: None,
trasher: init.trasher.clone().unwrap_or_else(crate::trash::system),
trash: trash_place::TrashPlace::default(),
history: Default::default(),
trash_rect: None,
secrets,
live: crate::live_refresh::LiveRefresh::new(
Expand Down Expand Up @@ -1819,6 +1829,11 @@ impl MantaApp {
refresh,
outcome,
} => self.on_trashed(action, refresh, outcome),
WorkResult::Undone {
redo,
refresh,
outcome,
} => self.on_undone(redo, refresh, outcome),
WorkResult::TrashMeasured(summary) => self.on_trash_measured(summary),
WorkResult::TrashEmptied(result) => self.on_trash_emptied(result),
WorkResult::Ejected { path, name, result } => self.on_ejected(path, name, result),
Expand Down Expand Up @@ -1850,11 +1865,15 @@ impl MantaApp {
select,
action,
result,
undo,
} => {
if let Some(id) = action {
let outcome = result.clone().map(|()| 0);
self.finish_action(id, outcome);
}
if let (Ok(()), Some(op)) = (&result, undo) {
self.history.record(op);
}
match result {
Ok(()) => self.set_success(description),
Err(e) => self.set_error_detail(failure, e),
Expand Down Expand Up @@ -1914,6 +1933,9 @@ impl MantaApp {
outcome: crate::trash::TrashOutcome,
) {
self.trash.stale();
if !outcome.put_back.is_empty() {
self.history.record(Op::PutBack(outcome.put_back.clone()));
}
let name_of = |p: &std::path::Path| {
p.file_name()
.map(|n| n.to_string_lossy().into_owned())
Expand Down Expand Up @@ -1944,6 +1966,54 @@ impl MantaApp {
}
}

/// ⌘Z (⇧⌘Z: `redo`): run the next step of the history in the
/// background. Nothing happens while a step still runs.
fn undo_redo(&mut self, redo: bool) {
if self.history.busy {
return;
}
let Some(op) = self.history.take(redo) else {
self.set_info(if redo {
t!("status.nothing_to_redo")
} else {
t!("status.nothing_to_undo")
});
return;
};
let refresh = op.refresh(&self.local);
let trasher = self.trasher.clone();
self.worker.spawn(
if redo { "redo" } else { "undo" },
move || Ok(op.run(trasher.as_ref())),
move |result: Result<crate::undo::Outcome, String>| WorkResult::Undone {
redo,
refresh,
outcome: result.unwrap_or_else(|e| crate::undo::Outcome {
error: Some((t!("error.undo_failed").to_string(), e)),
..Default::default()
}),
},
);
}

fn on_undone(
&mut self,
redo: bool,
refresh: Vec<(SharedVfs, VfsPath)>,
mut outcome: crate::undo::Outcome,
) {
self.history.finish(redo, &mut outcome);
self.trash.stale();
match (outcome.error, outcome.done) {
(Some((title, detail)), _) => self.set_error_detail(title, detail),
(None, Some(done)) => self.set_success(done),
(None, None) => {}
}
for (fs, dir) in refresh {
self.refresh_matching(&fs, &dir);
}
}

/// Show the one-time "what's new" notice after an update, or mark it
/// seen right away on a new install.
fn check_whats_new(&mut self, existing_install: bool) {
Expand Down Expand Up @@ -2762,6 +2832,8 @@ impl MantaApp {
Action::DropOnTrash { from, fs, paths } => self.drop_on_trash(from, fs, paths),
Action::TrashExternal(paths) => self.trash_external(paths),
Action::EmptyTrash => self.ask_empty_trash(),
Action::Undo => self.undo_redo(false),
Action::Redo => self.undo_redo(true),
Action::ShowTrashInFinder => self.show_trash_in_finder(),
Action::OpenFullDiskAccess => self.open_full_disk_access(),
Action::ToggleHidden(side) => {
Expand Down Expand Up @@ -3437,6 +3509,8 @@ impl MantaApp {
Cmd::MoveToTrash => Action::Delete(side),
Cmd::DeletePermanently => Action::DeletePermanently(side),
Cmd::EmptyTrash => Action::EmptyTrash,
Cmd::Undo => Action::Undo,
Cmd::Redo => Action::Redo,
Cmd::SelectAll => Action::SelectAll(side),
Cmd::ToggleHidden => Action::ToggleHidden(side),
Cmd::NewTab => Action::NewTab(side),
Expand Down Expand Up @@ -3528,6 +3602,8 @@ impl MantaApp {
Cmd::Open | Cmd::CopyToOther | Cmd::OpenWithApp | Cmd::GetInfo => has_sel,
Cmd::MoveToOther | Cmd::MoveToTrash | Cmd::DeletePermanently => has_sel && !ro,
Cmd::EmptyTrash => self.trash.dir.is_some() && self.trash.status.full() != Some(false),
Cmd::Undo => self.history.can_undo(),
Cmd::Redo => self.history.can_redo(),
Cmd::NewFolder | Cmd::NewFile => !ro,
Cmd::Compress => has_sel && !ro,
// Asked for every menu item every frame: no copies of the
Expand Down Expand Up @@ -4109,6 +4185,7 @@ impl MantaApp {
select: None,
action,
result,
undo: None,
},
);
}
Expand Down Expand Up @@ -4622,6 +4699,7 @@ impl MantaApp {
None,
);
let action = Some(self.start_action(a));
let undo = Some(Op::remove_new(fs.clone(), target.clone(), false));
self.worker.spawn(
"touch",
move || create_empty_file(&*fs, &target).map_err(|e| e.to_string()),
Expand All @@ -4632,6 +4710,7 @@ impl MantaApp {
select,
action,
result,
undo,
},
);
}
Expand All @@ -4644,6 +4723,7 @@ impl MantaApp {
None,
);
let action = Some(self.start_action(a));
let undo = Some(Op::remove_new(fs.clone(), target.clone(), true));
self.worker.spawn(
"mkdir",
move || fs.create_dir(&target).map_err(|e| e.to_string()),
Expand All @@ -4654,6 +4734,7 @@ impl MantaApp {
select,
action,
result,
undo,
},
);
}
Expand All @@ -4669,6 +4750,11 @@ impl MantaApp {
);
a.destination = Some(target.clone());
let action = Some(self.start_action(a));
let undo = Some(Op::Rename {
fs: fs.clone(),
from: target.clone(),
to: from.clone(),
});
self.worker.spawn(
"rename",
move || fs.rename(&from, &target).map_err(|e| e.to_string()),
Expand All @@ -4679,6 +4765,7 @@ impl MantaApp {
select,
action,
result,
undo,
},
);
}
Expand Down Expand Up @@ -6153,6 +6240,14 @@ impl MantaApp {
if !panel_focus && key(Modifiers::COMMAND, Key::Backspace) {
actions.push(Action::Delete(side));
}
// ⇧⌘Z before ⌘Z (a pattern without ⇧ also matches with it). A
// focused text field keeps both for its text (see `text_focus`).
if !panel_focus && key(cmd_shift, Key::Z) {
actions.push(Action::Redo);
}
if !panel_focus && key(Modifiers::COMMAND, Key::Z) {
actions.push(Action::Undo);
}
if !panel_focus && key(Modifiers::COMMAND, Key::A) {
self.pane_mut(side).tab_mut().select_all();
}
Expand Down
6 changes: 6 additions & 0 deletions crates/app/src/app/finder_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ impl MantaApp {
a.destination = Some(target.clone());
let action = Some(self.start_action(a));
let refresh = vec![(fs.clone(), dir)];
let undo = Some(crate::undo::Op::Rename {
fs: fs.clone(),
from: target.clone(),
to: from.clone(),
});
self.worker.spawn(
"rename",
move || {
Expand All @@ -350,6 +355,7 @@ impl MantaApp {
select: None,
action,
result,
undo,
},
);
}
Expand Down
12 changes: 11 additions & 1 deletion crates/app/src/app_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,15 @@ pub fn command_menus() -> Vec<MenuDef> {
),
m(
t!("menu.edit"),
&[c(SelectAll), None, c(Filter), c(EditPath)],
&[
c(Undo),
c(Redo),
None,
c(SelectAll),
None,
c(Filter),
c(EditPath),
],
),
m(
t!("menu.view"),
Expand Down Expand Up @@ -1029,6 +1037,8 @@ mod tests {
("\u{8}".into(), OPTION | COMMAND)
);
assert_eq!(k(Cmd::EmptyTrash), ("\u{8}".into(), SHIFT | COMMAND));
assert_eq!(k(Cmd::Undo), ("z".into(), COMMAND));
assert_eq!(k(Cmd::Redo), ("z".into(), SHIFT | COMMAND));
assert_eq!(k(Cmd::AddToFavorites), ("t".into(), CONTROL | COMMAND));
assert_eq!(k(Cmd::ToggleHidden), (".".into(), SHIFT | COMMAND));
assert_eq!(k(Cmd::Rename), ("\u{F705}".into(), 0), "F2");
Expand Down
16 changes: 16 additions & 0 deletions crates/app/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub enum Cmd {
DeletePermanently,
/// ⇧⌘⌫: delete everything in the Trash after asking (Finder).
EmptyTrash,
/// ⌘Z: revert the last file operation (Trash, rename, new item).
Undo,
/// ⇧⌘Z: do the last undone operation again.
Redo,
SelectAll,
ToggleHidden,
NewTab,
Expand Down Expand Up @@ -96,6 +100,8 @@ pub const ALL: &[Cmd] = &[
Cmd::MoveToTrash,
Cmd::DeletePermanently,
Cmd::EmptyTrash,
Cmd::Undo,
Cmd::Redo,
Cmd::SelectAll,
Cmd::ToggleHidden,
Cmd::NewTab,
Expand Down Expand Up @@ -166,6 +172,8 @@ impl Cmd {
Cmd::MoveToTrash => t!("cmd.move_to_trash"),
Cmd::DeletePermanently => t!("cmd.delete_permanently"),
Cmd::EmptyTrash => t!("cmd.empty_trash"),
Cmd::Undo => t!("cmd.undo"),
Cmd::Redo => t!("cmd.redo"),
Cmd::SelectAll => t!("cmd.select_all"),
Cmd::ToggleHidden => t!("cmd.toggle_hidden"),
Cmd::NewTab => t!("cmd.new_tab"),
Expand Down Expand Up @@ -224,6 +232,8 @@ impl Cmd {
Cmd::MoveToTrash => "⌘⌫",
Cmd::DeletePermanently => "⌥⌘⌫",
Cmd::EmptyTrash => "⇧⌘⌫",
Cmd::Undo => "⌘Z",
Cmd::Redo => "⇧⌘Z",
Cmd::SelectAll => "⌘A",
Cmd::ToggleHidden => "⇧⌘.",
Cmd::NewTab => "⌘T",
Expand Down Expand Up @@ -279,6 +289,8 @@ impl Cmd {
Cmd::MoveToTrash => i::TRASH,
Cmd::DeletePermanently => i::TRASH,
Cmd::EmptyTrash => i::TRASH,
Cmd::Undo => i::ARROW_COUNTER_CLOCKWISE,
Cmd::Redo => i::ARROW_CLOCKWISE,
Cmd::SelectAll => i::CHECK_CIRCLE,
Cmd::ToggleHidden => i::FILE_DASHED,
Cmd::NewTab => i::PLUS,
Expand Down Expand Up @@ -338,6 +350,8 @@ impl Cmd {
| Cmd::MoveToTrash
| Cmd::DeletePermanently
| Cmd::EmptyTrash
| Cmd::Undo
| Cmd::Redo
| Cmd::Compress
| Cmd::GetInfo
| Cmd::ExtractHere => t!("cmd.cat.file"),
Expand Down Expand Up @@ -392,6 +406,8 @@ impl Cmd {
Cmd::MoveToTrash => t!("cmd.kw.move_to_trash"),
Cmd::DeletePermanently => t!("cmd.kw.delete_permanently"),
Cmd::EmptyTrash => t!("cmd.kw.empty_trash"),
Cmd::Undo => t!("cmd.kw.undo"),
Cmd::Redo => t!("cmd.kw.redo"),
Cmd::SelectAll => t!("cmd.kw.select_all"),
Cmd::ToggleHidden => t!("cmd.kw.toggle_hidden"),
Cmd::NewTab => t!("cmd.kw.new_tab"),
Expand Down
13 changes: 13 additions & 0 deletions crates/app/src/i18n/catalog/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ pub const ENTRIES: Entries = &[
("status.created_folder", "Created folder “{name}”", "Ordner „{name}“ erstellt"),
("status.created_file", "Created file “{name}”", "Datei „{name}“ erstellt"),
("status.renamed", "Renamed “{old}” to “{name}”", "„{old}“ in „{name}“ umbenannt"),
("status.put_back.single", "Put back “{name}”", "„{name}“ zurückgelegt"),
("status.put_back.one", "Put back {count} item", "{count} Objekt zurückgelegt"),
("status.put_back.other", "Put back {count} items", "{count} Objekte zurückgelegt"),
("status.removed", "Deleted “{name}”", "„{name}“ gelöscht"),
("status.nothing_to_undo", "Nothing to undo", "Nichts zu widerrufen"),
("status.nothing_to_redo", "Nothing to redo", "Nichts zu wiederholen"),
// Error toasts
("error.load_favorites", "Could not load favorites: {error}", "Die Favoriten konnten nicht geladen werden: {error}"),
("error.save_favorites", "Couldn't save favorites", "Die Favoriten konnten nicht gesichert werden"),
Expand All @@ -108,6 +114,13 @@ pub const ENTRIES: Entries = &[
("error.create_folder", "Couldn't create folder “{name}”", "Der Ordner „{name}“ konnte nicht erstellt werden"),
("error.create_file", "Couldn't create file “{name}”", "Die Datei „{name}“ konnte nicht erstellt werden"),
("error.rename", "Couldn't rename “{name}”", "„{name}“ konnte nicht umbenannt werden"),
("error.put_back.single", "Couldn't put back “{name}”", "„{name}“ konnte nicht zurückgelegt werden"),
("error.put_back.one", "Couldn't put back {count} item", "{count} Objekt konnte nicht zurückgelegt werden"),
("error.put_back.other", "Couldn't put back {count} items", "{count} Objekte konnten nicht zurückgelegt werden"),
("error.remove", "Couldn't delete “{name}”", "„{name}“ konnte nicht gelöscht werden"),
("error.undo_failed", "Couldn't undo", "Widerrufen nicht möglich"),
("error.undo_exists", "An item named “{name}” already exists there.", "Dort gibt es bereits ein Objekt mit dem Namen „{name}“."),
("error.undo_not_empty", "It isn't empty anymore. Delete it yourself if you no longer need it.", "Es ist nicht mehr leer. Lösche es selbst, wenn du es nicht mehr brauchst."),
("error.is_folder", "“{name}” is a folder", "„{name}“ ist ein Ordner"),
("error.internal", "internal error: {detail}", "Interner Fehler: {detail}"),
("toast.finished_with_issues", "Finished with issues: {title}", "Mit Problemen abgeschlossen: {title}"),
Expand Down
Loading
Loading