862 lines
31 KiB
Rust
862 lines
31 KiB
Rust
//! Application-owned planning, confirmation, and task-queue integration.
|
|
//!
|
|
//! Planning requests are descriptions of read-only preflight work. Execution requests are emitted
|
|
//! only after explicit confirmation and only for the task which is currently active in the manager.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::fmt;
|
|
use std::path::PathBuf;
|
|
|
|
use crate::domain::{EntryId, EntryKind, Location};
|
|
use crate::fs::FsError;
|
|
pub use crate::operations::{CollisionPolicy, OperationKind};
|
|
use crate::operations::{
|
|
OperationEvent, OperationPlan, OperationStart, PlanGeneration, PlanId, PreparedOperation,
|
|
TaskId, TaskStatus, WorkKind, WorkPlan,
|
|
};
|
|
|
|
use super::{
|
|
AppState, Effect, Generation, Overlay, PaneId, PaneMode, PaneState, PendingOperation,
|
|
SessionEpoch, next_generation,
|
|
};
|
|
|
|
/// The confirmed pane state against which a plan and its result must be validated.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PaneSnapshot {
|
|
pub pane: PaneId,
|
|
pub location: Location,
|
|
pub listing_generation: Generation,
|
|
pub session_epoch: Option<SessionEpoch>,
|
|
}
|
|
|
|
impl PaneSnapshot {
|
|
pub(crate) fn capture(pane: PaneId, state: &PaneState) -> Self {
|
|
Self {
|
|
pane,
|
|
location: state.location.clone(),
|
|
listing_generation: state.listing_generation,
|
|
session_epoch: state.session_epoch,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// An explicit per-source collision decision. No decision is inferred from a previous plan.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct CollisionResolution {
|
|
pub source: EntryId,
|
|
pub policy: CollisionPolicy,
|
|
}
|
|
|
|
/// Read-only preflight request. The source and optional destination snapshots make late results
|
|
/// harmless, including when a pane has since navigated or reconnected.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PlanOperationRequest {
|
|
pub plan_id: PlanId,
|
|
pub generation: PlanGeneration,
|
|
pub source: PaneSnapshot,
|
|
pub sources: Vec<EntryId>,
|
|
pub destination: Location,
|
|
/// Present for dual-pane operations. The destination is this pane's confirmed location, not a
|
|
/// pending navigation target, and planning never changes the active pane.
|
|
pub destination_pane: Option<PaneSnapshot>,
|
|
pub kind: OperationKind,
|
|
pub policy: CollisionPolicy,
|
|
pub resolutions: Vec<CollisionResolution>,
|
|
}
|
|
|
|
impl PlanOperationRequest {
|
|
pub fn into_result(self, result: Result<PlanOperationOutput, FsError>) -> PlanOperationResult {
|
|
PlanOperationResult {
|
|
request: self,
|
|
result,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Opaque preflight fingerprint. Runtime may encode metadata, listing identity, or a digest; the
|
|
/// reducer only compares it for equality and never interprets its contents.
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
pub struct DestinationFingerprint(pub String);
|
|
|
|
impl DestinationFingerprint {
|
|
pub fn new(value: impl Into<String>) -> Self {
|
|
Self(value.into())
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct OperationCollision {
|
|
pub source: EntryId,
|
|
pub destination: Location,
|
|
pub existing_kind: EntryKind,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct ItemDiagnostic {
|
|
pub source: Option<EntryId>,
|
|
pub message: String,
|
|
pub partial: Option<Location>,
|
|
}
|
|
|
|
/// Preflight output before it is bound to the request which produced it.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PlanOperationOutput {
|
|
pub plan: OperationPlan,
|
|
/// Complete confirmed nodes and source/destination fingerprints. Collision placeholders do not
|
|
/// have an executable prepared snapshot and can never be confirmed.
|
|
pub prepared: Option<PreparedOperation>,
|
|
pub destination_fingerprint: DestinationFingerprint,
|
|
pub collisions: Vec<OperationCollision>,
|
|
pub item_diagnostics: Vec<ItemDiagnostic>,
|
|
}
|
|
|
|
/// App-owned prepared operation. This is the adaptation point for a future operations::preflight
|
|
/// `PreparedOperation`: it carries the immutable plan and all diagnostics needed by UI/runtime.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PlannedOperation {
|
|
pub request: PlanOperationRequest,
|
|
pub plan: OperationPlan,
|
|
pub prepared: Option<PreparedOperation>,
|
|
pub destination_fingerprint: DestinationFingerprint,
|
|
pub collisions: Vec<OperationCollision>,
|
|
pub item_diagnostics: Vec<ItemDiagnostic>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct PlanOperationResult {
|
|
pub request: PlanOperationRequest,
|
|
pub result: Result<PlanOperationOutput, FsError>,
|
|
}
|
|
|
|
impl PlanOperationResult {
|
|
pub fn plan_id(&self) -> PlanId {
|
|
self.request.plan_id
|
|
}
|
|
}
|
|
|
|
/// State retained while destination entry, planning, collision resolution, or confirmation is open.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct OperationSession {
|
|
pub source: PaneSnapshot,
|
|
pub destination: Option<Location>,
|
|
pub destination_pane: Option<PaneSnapshot>,
|
|
pub destination_input: String,
|
|
pub policy: CollisionPolicy,
|
|
pub resolutions: BTreeMap<EntryId, CollisionPolicy>,
|
|
pub request: Option<PlanOperationRequest>,
|
|
/// Clipboard sources are detached from later navigation; direct F5/F6 sources remain live.
|
|
pub detached_sources: bool,
|
|
pub collision_index: usize,
|
|
pub notice: Option<String>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum CollisionChoice {
|
|
Skip,
|
|
Overwrite,
|
|
Cancel,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum QuitDecision {
|
|
Stay,
|
|
CancelAndWait,
|
|
ExitWithWarning,
|
|
}
|
|
|
|
/// Executor diagnostics which are not representable by TaskManager's item outcome model.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct TaskDiagnostic {
|
|
pub task_id: TaskId,
|
|
pub message: String,
|
|
pub partial: Option<Location>,
|
|
}
|
|
|
|
/// Full execution context for an active start. `start` must still be active in the reducer when
|
|
/// this effect is created; the runtime must not recycle it for a queued or stale operation.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct ExecuteOperationRequest {
|
|
pub start: OperationStart,
|
|
pub operation: PlannedOperation,
|
|
}
|
|
|
|
impl AppState {
|
|
pub(crate) fn plan_active_selection(&mut self, kind: OperationKind) -> Vec<Effect> {
|
|
let sources = self.active_operation_sources();
|
|
self.begin_operation(kind, self.active, sources, None)
|
|
}
|
|
|
|
pub(crate) fn plan_paste(&mut self) -> Vec<Effect> {
|
|
let Some(clipboard) = self.clipboard.clone() else {
|
|
self.active_pane_mut().error =
|
|
Some(FsError::Other("Cannot paste an empty clipboard".into()));
|
|
self.pending_operation = None;
|
|
self.operation_session = None;
|
|
return Vec::new();
|
|
};
|
|
if clipboard.sources.is_empty() {
|
|
self.active_pane_mut().error =
|
|
Some(FsError::Other("Cannot paste an empty clipboard".into()));
|
|
self.pending_operation = None;
|
|
self.operation_session = None;
|
|
return Vec::new();
|
|
}
|
|
self.begin_operation(
|
|
clipboard.kind,
|
|
clipboard.source_pane,
|
|
clipboard.sources,
|
|
Some(clipboard.source),
|
|
)
|
|
}
|
|
|
|
fn begin_operation(
|
|
&mut self,
|
|
kind: OperationKind,
|
|
source_pane: PaneId,
|
|
sources: Vec<EntryId>,
|
|
detached_source: Option<PaneSnapshot>,
|
|
) -> Vec<Effect> {
|
|
self.pending_operation = None;
|
|
self.operation_session = None;
|
|
self.planned_operation = None;
|
|
if sources.is_empty() {
|
|
self.active_pane_mut().error = Some(FsError::Other(
|
|
"Cannot plan an operation without a hovered or selected entry".into(),
|
|
));
|
|
return Vec::new();
|
|
}
|
|
|
|
let detached_sources = detached_source.is_some();
|
|
let source = detached_source
|
|
.unwrap_or_else(|| PaneSnapshot::capture(source_pane, self.pane(source_pane)));
|
|
self.pending_operation = Some(PendingOperation {
|
|
kind,
|
|
source_pane,
|
|
sources,
|
|
});
|
|
let mut session = OperationSession {
|
|
source,
|
|
destination: None,
|
|
destination_pane: None,
|
|
destination_input: String::new(),
|
|
policy: CollisionPolicy::Ask,
|
|
resolutions: BTreeMap::new(),
|
|
request: None,
|
|
detached_sources,
|
|
collision_index: 0,
|
|
notice: None,
|
|
};
|
|
|
|
if self.mode == PaneMode::Dual {
|
|
let destination_pane = source_pane.other();
|
|
let snapshot = PaneSnapshot::capture(destination_pane, self.pane(destination_pane));
|
|
session.destination = Some(snapshot.location.clone());
|
|
session.destination_pane = Some(snapshot);
|
|
self.operation_session = Some(session);
|
|
self.request_operation_plan().into_iter().collect()
|
|
} else {
|
|
// In single-pane mode the destination is never guessed. The typed path remains visible
|
|
// until Enter creates a read-only planner request.
|
|
session.destination_input = self
|
|
.pane(source_pane)
|
|
.location
|
|
.path
|
|
.to_string_lossy()
|
|
.into_owned();
|
|
self.operation_session = Some(session);
|
|
self.set_overlay(Overlay::DestinationPrompt);
|
|
Vec::new()
|
|
}
|
|
}
|
|
|
|
pub(crate) fn submit_destination(&mut self) -> Vec<Effect> {
|
|
if self.overlay != Overlay::DestinationPrompt {
|
|
return Vec::new();
|
|
}
|
|
let Some(session) = &mut self.operation_session else {
|
|
self.set_overlay(Overlay::None);
|
|
return Vec::new();
|
|
};
|
|
let input = session.destination_input.trim();
|
|
if input.is_empty() {
|
|
session.notice = Some("Destination path cannot be empty".into());
|
|
return Vec::new();
|
|
}
|
|
let location = Location {
|
|
source: session.source.location.source.clone(),
|
|
path: PathBuf::from(input),
|
|
};
|
|
session.destination = Some(location);
|
|
session.notice = None;
|
|
self.request_operation_plan().into_iter().collect()
|
|
}
|
|
|
|
fn request_operation_plan(&mut self) -> Option<Effect> {
|
|
if !self.operation_session_is_current() {
|
|
if let Some(session) = &mut self.operation_session {
|
|
session.notice =
|
|
Some("Source or destination changed; start a new operation".into());
|
|
}
|
|
self.planned_operation = None;
|
|
self.set_overlay(Overlay::ConfirmOperation);
|
|
return None;
|
|
}
|
|
|
|
let plan_id = PlanId::new(self.next_plan_id);
|
|
self.next_plan_id = self
|
|
.next_plan_id
|
|
.checked_add(1)
|
|
.expect("operation plan identifier space exhausted");
|
|
let generation = PlanGeneration::new(self.next_plan_generation);
|
|
self.next_plan_generation = next_generation(self.next_plan_generation);
|
|
let pending = self.pending_operation.as_ref()?;
|
|
let session = self.operation_session.as_mut()?;
|
|
let request = PlanOperationRequest {
|
|
plan_id,
|
|
generation,
|
|
source: session.source.clone(),
|
|
sources: pending.sources.clone(),
|
|
destination: session.destination.clone()?,
|
|
destination_pane: session.destination_pane.clone(),
|
|
kind: pending.kind,
|
|
policy: session.policy,
|
|
resolutions: session
|
|
.resolutions
|
|
.iter()
|
|
.map(|(source, policy)| CollisionResolution {
|
|
source: source.clone(),
|
|
policy: *policy,
|
|
})
|
|
.collect(),
|
|
};
|
|
session.request = Some(request.clone());
|
|
self.planned_operation = None;
|
|
self.set_overlay(Overlay::ConfirmOperation);
|
|
Some(Effect::PlanOperation(request))
|
|
}
|
|
|
|
pub(crate) fn finish_operation_plan(&mut self, result: PlanOperationResult) -> Vec<Effect> {
|
|
let Some(session) = &self.operation_session else {
|
|
return Vec::new();
|
|
};
|
|
if session.request.as_ref() != Some(&result.request) {
|
|
return Vec::new();
|
|
}
|
|
if !self.operation_source_is_current(session)
|
|
|| session
|
|
.destination_pane
|
|
.as_ref()
|
|
.is_some_and(|snapshot| !self.snapshot_is_current(snapshot))
|
|
{
|
|
return Vec::new();
|
|
}
|
|
if !self.operation_session_is_current() {
|
|
self.planned_operation = None;
|
|
if let Some(session) = &mut self.operation_session {
|
|
session.notice = Some("A source disappeared; the plan cannot be confirmed".into());
|
|
}
|
|
return Vec::new();
|
|
}
|
|
|
|
match result.result {
|
|
Err(error) => {
|
|
self.planned_operation = None;
|
|
if let Some(session) = &mut self.operation_session {
|
|
session.notice = Some(format!("Planning failed: {}", error.user_message()));
|
|
}
|
|
self.set_overlay(Overlay::ConfirmOperation);
|
|
}
|
|
Ok(output) => {
|
|
if !plan_matches_request(&output.plan, &result.request) {
|
|
if let Some(session) = &mut self.operation_session {
|
|
session.notice = Some("Planner returned a mismatched plan".into());
|
|
}
|
|
self.planned_operation = None;
|
|
return Vec::new();
|
|
}
|
|
let has_collisions = !output.collisions.is_empty();
|
|
if let Some(session) = &mut self.operation_session {
|
|
session.notice = None;
|
|
}
|
|
let planned = PlannedOperation {
|
|
request: result.request,
|
|
plan: output.plan,
|
|
prepared: output.prepared,
|
|
destination_fingerprint: output.destination_fingerprint,
|
|
collisions: output.collisions,
|
|
item_diagnostics: output.item_diagnostics,
|
|
};
|
|
self.planned_operation = Some(planned);
|
|
self.set_overlay(
|
|
if has_collisions
|
|
&& self
|
|
.operation_session
|
|
.as_ref()
|
|
.is_some_and(|session| session.policy == CollisionPolicy::Ask)
|
|
{
|
|
Overlay::Collision
|
|
} else {
|
|
Overlay::ConfirmOperation
|
|
},
|
|
);
|
|
}
|
|
}
|
|
Vec::new()
|
|
}
|
|
|
|
pub(crate) fn confirm_operation(&mut self, expected: Option<PlanId>) -> Vec<Effect> {
|
|
if self.overlay != Overlay::ConfirmOperation {
|
|
return Vec::new();
|
|
}
|
|
if !self.operation_session_is_current() {
|
|
self.planned_operation = None;
|
|
if let Some(session) = &mut self.operation_session {
|
|
session.notice = Some("The plan is stale; start a new operation".into());
|
|
}
|
|
return Vec::new();
|
|
}
|
|
let Some(planned) = self.planned_operation.clone() else {
|
|
return Vec::new();
|
|
};
|
|
if expected.is_some_and(|id| id != planned.plan.id())
|
|
|| !planned.collisions.is_empty()
|
|
&& self
|
|
.operation_session
|
|
.as_ref()
|
|
.is_some_and(|session| session.policy == CollisionPolicy::Ask)
|
|
{
|
|
return Vec::new();
|
|
}
|
|
if self.confirmed_plans.contains(&planned.plan.id()) {
|
|
return Vec::new();
|
|
}
|
|
|
|
self.confirmed_plans.insert(planned.plan.id());
|
|
self.planned_operations
|
|
.insert(planned.plan.id(), planned.clone());
|
|
self.task_manager
|
|
.start_work(WorkPlan::from_operation(planned.plan.clone()));
|
|
self.pending_operation = None;
|
|
self.operation_session = None;
|
|
self.planned_operation = None;
|
|
self.set_overlay(Overlay::None);
|
|
self.next_execution_effect().into_iter().collect()
|
|
}
|
|
|
|
pub(crate) fn resolve_collision(
|
|
&mut self,
|
|
choice: CollisionChoice,
|
|
apply_to_all: bool,
|
|
) -> Vec<Effect> {
|
|
if self.overlay != Overlay::Collision {
|
|
return Vec::new();
|
|
}
|
|
if choice == CollisionChoice::Cancel {
|
|
self.cancel_pending_operation();
|
|
return Vec::new();
|
|
}
|
|
let policy = match choice {
|
|
CollisionChoice::Skip => CollisionPolicy::Skip,
|
|
CollisionChoice::Overwrite => CollisionPolicy::Overwrite,
|
|
CollisionChoice::Cancel => unreachable!(),
|
|
};
|
|
let Some(planned) = &self.planned_operation else {
|
|
return Vec::new();
|
|
};
|
|
if planned.collisions.is_empty() {
|
|
return Vec::new();
|
|
}
|
|
let Some(session) = &mut self.operation_session else {
|
|
return Vec::new();
|
|
};
|
|
session.collision_index = session.collision_index.min(planned.collisions.len() - 1);
|
|
let selected = planned.collisions[session.collision_index].clone();
|
|
if selected.existing_kind == EntryKind::Directory && choice == CollisionChoice::Overwrite {
|
|
session.notice = Some("Directory merge/overwrite is not supported".into());
|
|
return Vec::new();
|
|
}
|
|
let collisions = if apply_to_all {
|
|
planned.collisions.clone()
|
|
} else {
|
|
vec![selected]
|
|
};
|
|
if apply_to_all
|
|
&& choice == CollisionChoice::Overwrite
|
|
&& collisions
|
|
.iter()
|
|
.any(|collision| collision.existing_kind == EntryKind::Directory)
|
|
{
|
|
session.notice = Some("Directory merge/overwrite is not supported".into());
|
|
return Vec::new();
|
|
}
|
|
for collision in collisions {
|
|
session.resolutions.insert(collision.source, policy);
|
|
}
|
|
if apply_to_all {
|
|
session.policy = policy;
|
|
}
|
|
session.notice = Some("Collision choice recorded; a fresh plan is required".into());
|
|
self.planned_operation = None;
|
|
self.request_operation_plan().into_iter().collect()
|
|
}
|
|
|
|
pub(crate) fn select_collision(&mut self, delta: isize) {
|
|
let Some(planned) = &self.planned_operation else {
|
|
return;
|
|
};
|
|
let Some(session) = &mut self.operation_session else {
|
|
return;
|
|
};
|
|
session.collision_index =
|
|
wrapped_index(session.collision_index, planned.collisions.len(), delta);
|
|
}
|
|
|
|
pub(crate) fn destination_fingerprint_changed(
|
|
&mut self,
|
|
plan_id: PlanId,
|
|
fingerprint: DestinationFingerprint,
|
|
) -> Vec<Effect> {
|
|
let Some(planned) = &self.planned_operation else {
|
|
return Vec::new();
|
|
};
|
|
if planned.plan.id() != plan_id
|
|
|| planned.destination_fingerprint == fingerprint
|
|
|| !self.operation_session_is_current()
|
|
{
|
|
return Vec::new();
|
|
}
|
|
self.planned_operation = None;
|
|
if let Some(session) = &mut self.operation_session {
|
|
session.notice = Some("Destination changed; the old plan is stale".into());
|
|
}
|
|
self.request_operation_plan().into_iter().collect()
|
|
}
|
|
|
|
pub(crate) fn cancel_pending_operation(&mut self) {
|
|
self.pending_operation = None;
|
|
self.operation_session = None;
|
|
self.planned_operation = None;
|
|
self.set_overlay(Overlay::None);
|
|
}
|
|
|
|
fn operation_session_is_current(&self) -> bool {
|
|
let Some(session) = &self.operation_session else {
|
|
return false;
|
|
};
|
|
self.operation_source_is_current(session)
|
|
&& session
|
|
.destination_pane
|
|
.as_ref()
|
|
.is_none_or(|snapshot| self.snapshot_is_current(snapshot))
|
|
&& self.pending_operation.as_ref().is_some_and(|pending| {
|
|
pending.source_pane == session.source.pane
|
|
&& (session.detached_sources
|
|
|| pending.sources.iter().all(|source| {
|
|
let pane = self.pane(pending.source_pane);
|
|
pane.entries
|
|
.iter()
|
|
.any(|entry| pane.location.entry_id(&entry.name) == *source)
|
|
|| pane.selection_set.contains(source)
|
|
}))
|
|
})
|
|
}
|
|
|
|
fn operation_source_is_current(&self, session: &OperationSession) -> bool {
|
|
if !session.detached_sources {
|
|
return self.snapshot_is_current(&session.source);
|
|
}
|
|
let current = self.pane(session.source.pane);
|
|
// Navigation and listing refreshes do not invalidate a detached clipboard. A replacement
|
|
// session on the same endpoint still does; another endpoint leaves the original typed
|
|
// host/epoch in the request, where the connection manager enforces its availability.
|
|
current.location.source != session.source.location.source
|
|
|| current.session_epoch == session.source.session_epoch
|
|
}
|
|
|
|
fn snapshot_is_current(&self, snapshot: &PaneSnapshot) -> bool {
|
|
let pane = self.pane(snapshot.pane);
|
|
pane.location == snapshot.location
|
|
&& pane.listing_generation == snapshot.listing_generation
|
|
&& pane.session_epoch == snapshot.session_epoch
|
|
}
|
|
|
|
pub(crate) fn open_tasks(&mut self) {
|
|
if self.overlay == Overlay::Tasks {
|
|
self.set_overlay(Overlay::None);
|
|
return;
|
|
}
|
|
self.task_details = None;
|
|
self.selected_task = self
|
|
.task_manager
|
|
.active_operation()
|
|
.map(|start| start.task_id)
|
|
.or_else(|| {
|
|
self.task_manager
|
|
.tasks()
|
|
.find(|task| !task.status().is_terminal())
|
|
.or_else(|| self.task_manager.tasks().last())
|
|
.map(|task| task.id())
|
|
});
|
|
self.set_overlay(Overlay::Tasks);
|
|
}
|
|
|
|
pub(crate) fn select_task(&mut self, delta: isize) {
|
|
let tasks: Vec<_> = self.task_manager.tasks().map(|task| task.id()).collect();
|
|
if tasks.is_empty() {
|
|
self.selected_task = None;
|
|
return;
|
|
}
|
|
let current = self
|
|
.selected_task
|
|
.and_then(|selected| tasks.iter().position(|id| *id == selected))
|
|
.unwrap_or(0);
|
|
self.selected_task = Some(tasks[wrapped_index(current, tasks.len(), delta)]);
|
|
}
|
|
|
|
pub(crate) fn open_task_details(&mut self) {
|
|
if self.selected_task.is_some() {
|
|
self.task_details = self.selected_task;
|
|
self.set_overlay(Overlay::TaskDetails);
|
|
}
|
|
}
|
|
|
|
pub(crate) fn cancel_selected_task(&mut self) -> Vec<Effect> {
|
|
let task_id = self.selected_task.or_else(|| {
|
|
self.task_manager
|
|
.active_operation()
|
|
.map(|start| start.task_id)
|
|
});
|
|
task_id
|
|
.map(|task_id| self.cancel_task(task_id))
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
fn cancel_task(&mut self, task_id: TaskId) -> Vec<Effect> {
|
|
let Some(task) = self.task_manager.task(task_id) else {
|
|
return Vec::new();
|
|
};
|
|
let status = task.status();
|
|
let operation_id = task.operation_id();
|
|
let editor_workspace = (status == TaskStatus::Queued
|
|
&& task.plan().work_kind() == WorkKind::EditUpload)
|
|
.then(|| self.edit_upload_workspaces.get(&task.plan().id()).copied())
|
|
.flatten();
|
|
let queued_undo = (status == TaskStatus::Queued
|
|
&& task.plan().work_kind() == WorkKind::Undo)
|
|
.then(|| task.plan().id());
|
|
if !self.task_manager.request_cancellation(task_id) {
|
|
return Vec::new();
|
|
}
|
|
if status == TaskStatus::Queued {
|
|
// Queued cancellation is acknowledged locally; its owned editor bytes can be dropped now.
|
|
let mut effects = editor_workspace
|
|
.map(Effect::CleanupEditorWorkspace)
|
|
.into_iter()
|
|
.chain(queued_undo.map(Effect::DiscardUndo))
|
|
.collect::<Vec<_>>();
|
|
effects.extend(self.next_execution_effect());
|
|
effects
|
|
} else if self
|
|
.task_manager
|
|
.active_operation()
|
|
.is_some_and(|start| start.task_id == task_id)
|
|
{
|
|
vec![Effect::CancelOperation(operation_id)]
|
|
} else {
|
|
Vec::new()
|
|
}
|
|
}
|
|
|
|
pub(crate) fn apply_operation_event(&mut self, event: OperationEvent) -> Vec<Effect> {
|
|
use crate::operations::EventDisposition;
|
|
|
|
let editor_workspace = self.terminal_editor_workspace(&event);
|
|
if self.task_manager.apply_event(event) != EventDisposition::Applied {
|
|
return Vec::new();
|
|
}
|
|
self.finish_cancel_wait();
|
|
let mut effects = editor_workspace
|
|
.map(Effect::CleanupEditorWorkspace)
|
|
.into_iter()
|
|
.collect::<Vec<_>>();
|
|
effects.extend(self.next_execution_effect());
|
|
effects
|
|
}
|
|
|
|
fn terminal_editor_workspace(
|
|
&self,
|
|
event: &OperationEvent,
|
|
) -> Option<super::EditorWorkspaceId> {
|
|
if !matches!(
|
|
event.kind,
|
|
crate::operations::OperationEventKind::Completed
|
|
| crate::operations::OperationEventKind::CancellationAcknowledged
|
|
) {
|
|
return None;
|
|
}
|
|
let active = self.task_manager.active_operation()?;
|
|
if active.operation_id != event.operation_id || active.plan_id != event.plan_id {
|
|
return None;
|
|
}
|
|
let task = self.task_manager.task(active.task_id)?;
|
|
(task.plan().work_kind() == WorkKind::EditUpload)
|
|
.then(|| self.edit_upload_workspaces.get(&event.plan_id).copied())
|
|
.flatten()
|
|
}
|
|
|
|
pub(crate) fn add_task_diagnostic(&mut self, diagnostic: TaskDiagnostic) {
|
|
self.task_diagnostics
|
|
.entry(diagnostic.task_id)
|
|
.or_default()
|
|
.push(diagnostic);
|
|
}
|
|
|
|
pub(crate) fn next_execution_effect(&mut self) -> Option<Effect> {
|
|
let start = self.task_manager.active_operation()?;
|
|
let task = self.task_manager.task(start.task_id)?;
|
|
if task.status() != TaskStatus::Running
|
|
|| self.dispatched_operations.contains(&start.operation_id)
|
|
{
|
|
return None;
|
|
}
|
|
let effect = match task.plan().work_kind() {
|
|
WorkKind::CopyMove => {
|
|
let operation = self.planned_operations.get(&start.plan_id)?.clone();
|
|
Effect::ExecuteOperation(ExecuteOperationRequest { start, operation })
|
|
}
|
|
WorkKind::Create | WorkKind::Rename | WorkKind::Trash | WorkKind::Delete => {
|
|
let plan = self.entry_action_plans.get(&start.plan_id)?.clone();
|
|
let pane = *self.entry_action_panes.get(&start.plan_id)?;
|
|
let trash_records = self
|
|
.entry_action_records
|
|
.get(&start.plan_id)
|
|
.cloned()
|
|
.unwrap_or_default();
|
|
Effect::ExecuteEntryAction {
|
|
start,
|
|
plan,
|
|
pane,
|
|
trash_records,
|
|
}
|
|
}
|
|
WorkKind::EditUpload => {
|
|
let plan = task.plan().clone();
|
|
let workspace_id = *self.edit_upload_workspaces.get(&start.plan_id)?;
|
|
Effect::ExecuteEditUpload {
|
|
start,
|
|
plan,
|
|
workspace_id,
|
|
}
|
|
}
|
|
WorkKind::Undo => {
|
|
let (plan, entry) = self.undo_plans.get(&start.plan_id)?.clone();
|
|
Effect::ExecuteUndo { start, plan, entry }
|
|
}
|
|
};
|
|
self.dispatched_operations.insert(start.operation_id);
|
|
Some(effect)
|
|
}
|
|
|
|
pub(crate) fn request_quit(&mut self) {
|
|
if self.has_unfinished_tasks() {
|
|
self.set_overlay(Overlay::Quit);
|
|
} else {
|
|
self.should_quit = true;
|
|
}
|
|
}
|
|
|
|
pub(crate) fn decide_quit(&mut self, decision: QuitDecision) -> Vec<Effect> {
|
|
if self.overlay != Overlay::Quit {
|
|
return Vec::new();
|
|
}
|
|
match decision {
|
|
QuitDecision::Stay => {
|
|
self.quit_after_tasks = false;
|
|
self.set_overlay(Overlay::None);
|
|
Vec::new()
|
|
}
|
|
QuitDecision::ExitWithWarning => {
|
|
self.should_quit = true;
|
|
Vec::new()
|
|
}
|
|
QuitDecision::CancelAndWait => {
|
|
self.quit_after_tasks = true;
|
|
let tasks: Vec<_> = self
|
|
.task_manager
|
|
.tasks()
|
|
.filter(|task| {
|
|
matches!(task.status(), TaskStatus::Queued | TaskStatus::Running)
|
|
})
|
|
.map(|task| task.id())
|
|
.collect();
|
|
let mut effects = Vec::new();
|
|
for task in tasks {
|
|
effects.extend(self.cancel_task(task));
|
|
}
|
|
self.finish_cancel_wait();
|
|
effects
|
|
}
|
|
}
|
|
}
|
|
|
|
fn finish_cancel_wait(&mut self) {
|
|
if self.quit_after_tasks && !self.has_unfinished_tasks() {
|
|
self.should_quit = true;
|
|
self.set_overlay(Overlay::None);
|
|
}
|
|
}
|
|
|
|
fn has_unfinished_tasks(&self) -> bool {
|
|
self.task_manager.tasks().any(|task| {
|
|
matches!(
|
|
task.status(),
|
|
TaskStatus::Queued | TaskStatus::Running | TaskStatus::Cancelling
|
|
)
|
|
})
|
|
}
|
|
|
|
pub fn operation_error(&self) -> Option<&str> {
|
|
self.operation_session
|
|
.as_ref()
|
|
.and_then(|session| session.notice.as_deref())
|
|
}
|
|
|
|
pub fn diagnostics_for(&self, task_id: TaskId) -> &[TaskDiagnostic] {
|
|
self.task_diagnostics
|
|
.get(&task_id)
|
|
.map(Vec::as_slice)
|
|
.unwrap_or(&[])
|
|
}
|
|
}
|
|
|
|
fn plan_matches_request(plan: &OperationPlan, request: &PlanOperationRequest) -> bool {
|
|
// Preflight may canonicalize a relative path or symlink. Endpoint identity is immutable; the
|
|
// separately returned destination fingerprint, rather than the typed path, detects changes.
|
|
plan.id() == request.plan_id
|
|
&& plan.generation() == request.generation
|
|
&& plan.kind() == request.kind
|
|
&& plan.destination().source == request.destination.source
|
|
&& plan.policy() == request.policy
|
|
&& !plan.sources().is_empty()
|
|
&& plan.sources().iter().all(|source| {
|
|
request.sources.iter().any(|requested| {
|
|
requested.source == source.location().source
|
|
&& requested.path == source.location().path
|
|
})
|
|
})
|
|
}
|
|
|
|
fn wrapped_index(current: usize, len: usize, delta: isize) -> usize {
|
|
if len == 0 {
|
|
return 0;
|
|
}
|
|
let len = len.min(isize::MAX as usize) as isize;
|
|
let current = current.min(len as usize - 1) as isize;
|
|
(current + delta).rem_euclid(len) as usize
|
|
}
|
|
|
|
impl fmt::Display for DestinationFingerprint {
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
formatter.write_str(&self.0)
|
|
}
|
|
}
|