use iced::{
Alignment, Element, Length,
widget::{Space, column, container, row, text},
};
use knotra_ui::widget::{
BUTTON_HEIGHT, SMALL_BUTTON_HEIGHT, Tokens, body_badge_diameter, centered_button_label,
current_or, list,
overlay::{OverlayWidth, surface},
reasoned, status_badge, style,
};
use knotra_vcs::{
ProjectId, model::operation::ProjectOperationOutcome, model::operation::SmartPullDisposition,
};
use crate::{
message::{Message, SyncMessage},
state::{
AppState,
dashboard::badge_for_operation_outcome,
focus::{FocusTarget, LayerFocus},
},
};
mod focus_target {
pub const CLOSE: &str = "get_latest.close";
pub const REVIEW_RETRY: &str = "get_latest.review_retry";
pub const RETRY_FAILED_CLOSE: &str = "get_latest.retry_failed_close";
pub const START: &str = "get_latest.start";
pub const CANCEL: &str = "get_latest.cancel";
pub const DETAILS: &str = "get_latest.details";
pub const DONE_CLOSE: &str = "get_latest.done_close";
}
pub fn layer_focus(state: &AppState) -> LayerFocus<Message> {
use crate::state::sync::SyncPhase;
let sync = &state.sync;
let close_target = FocusTarget::control(focus_target::CLOSE);
match &sync.phase {
SyncPhase::Idle
| SyncPhase::Planning
| SyncPhase::RetryPreparing
| SyncPhase::FetchRunning { .. }
| SyncPhase::PullRunning { .. } => LayerFocus {
order: Vec::new(),
entry: None,
},
SyncPhase::RetryPreparationFailed => {
let close_msg = Some(Message::Sync(SyncMessage::ModalClosed));
let retry_message = match &state.activity.latest {
crate::state::LatestOpState::Completed {
log,
retry:
crate::state::RetryAvailability::Available(
crate::state::ActivityRetryAction::ReviewSmartPull { .. },
),
} => Some(Message::Activity(
crate::message::ActivityMessage::RetryRequested {
source_operation_id: log.result.operation_id.clone(),
},
)),
_ => None,
};
let review_target = FocusTarget::control(focus_target::REVIEW_RETRY);
let close_target_footer = FocusTarget::control(focus_target::RETRY_FAILED_CLOSE);
LayerFocus {
order: vec![
(close_target, close_msg.clone()),
(review_target.clone(), retry_message.clone()),
(close_target_footer.clone(), close_msg),
],
entry: Some(if retry_message.is_some() {
review_target
} else {
close_target_footer
}),
}
}
SyncPhase::AwaitingConfirm(plan) => {
let close_msg = Some(Message::Sync(SyncMessage::ModalClosed));
let mut order = vec![(close_target, close_msg.clone())];
let mut first_choice = None;
for entry in &plan.entries {
if entry.is_dirty && !entry.has_conflict && entry.skip_reason.is_none() {
let fetch_target = FocusTarget::control_dynamic(format!(
"get_latest.{}.fetch_only",
entry.project_id
));
if first_choice.is_none() {
first_choice = Some(fetch_target.clone());
}
order.push((
fetch_target,
(entry.disposition
!= knotra_vcs::model::operation::SmartPullDisposition::FetchOnly)
.then_some(Message::Sync(SyncMessage::DispositionChanged(
entry.project_id.clone(),
knotra_vcs::model::operation::SmartPullDisposition::FetchOnly,
))),
));
let stash_target = FocusTarget::control_dynamic(format!(
"get_latest.{}.stash_and_pull",
entry.project_id
));
order.push((
stash_target,
(entry.disposition
!= knotra_vcs::model::operation::SmartPullDisposition::StashAndPull)
.then_some(Message::Sync(SyncMessage::DispositionChanged(
entry.project_id.clone(),
knotra_vcs::model::operation::SmartPullDisposition::StashAndPull,
))),
));
}
}
let has_work = plan
.entries
.iter()
.any(|e| !matches!(e.disposition, SmartPullDisposition::Excluded));
let can_start = has_work && !state.operation_interlock.is_busy();
order.push((
FocusTarget::control(focus_target::START),
can_start.then_some(Message::Sync(SyncMessage::ExecuteRequested)),
));
let cancel_target = FocusTarget::control(focus_target::CANCEL);
order.push((
cancel_target.clone(),
Some(Message::Sync(SyncMessage::Cancelled)),
));
LayerFocus {
order,
entry: Some(first_choice.unwrap_or(cancel_target)),
}
}
SyncPhase::Done(_) => {
let close_msg = Some(Message::Sync(SyncMessage::ModalClosed));
let footer_close = FocusTarget::control(focus_target::DONE_CLOSE);
LayerFocus {
order: vec![
(close_target, close_msg.clone()),
(
FocusTarget::control(focus_target::DETAILS),
Some(Message::ToggleOpDetails),
),
(footer_close.clone(), close_msg),
],
entry: Some(footer_close),
}
}
}
}
pub fn pull_modal(state: &AppState) -> Element<'_, Message> {
use crate::state::sync::SyncPhase;
let tokens = &state.theme.tokens;
let sync = &state.sync;
let (inner, footer): (Element<'_, Message>, Element<'_, Message>) = match &sync.phase {
SyncPhase::Idle | SyncPhase::Planning => (
column![
text(state.t("plain.get_latest.preparing"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
text(state.t("plain.get_latest.preparing_hint"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
]
.spacing(8)
.into(),
Space::new().into(),
),
SyncPhase::RetryPreparing => (
column![
text(state.t("plain.activity.retry_preparing"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
text(state.t("plain.get_latest.preparing_hint"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
]
.spacing(8)
.into(),
Space::new().into(),
),
SyncPhase::RetryPreparationFailed => {
let retry_message = match &state.activity.latest {
crate::state::LatestOpState::Completed {
log,
retry:
crate::state::RetryAvailability::Available(
crate::state::ActivityRetryAction::ReviewSmartPull { .. },
),
} => Some(Message::Activity(
crate::message::ActivityMessage::RetryRequested {
source_operation_id: log.result.operation_id.clone(),
},
)),
_ => None,
};
let footer = row![
styled_button(
tokens,
state.t("plain.activity.review_retry"),
retry_message,
crate::app::ring(state, &FocusTarget::control(focus_target::REVIEW_RETRY)),
style::primary,
),
Space::new().width(Length::Fill),
styled_button(
tokens,
state.t("action.close"),
Some(Message::Sync(SyncMessage::ModalClosed)),
crate::app::ring(
state,
&FocusTarget::control(focus_target::RETRY_FAILED_CLOSE)
),
style::ghost,
),
]
.align_y(Alignment::Center);
(
column![
text(state.t("plain.activity.retry_prepare_failed"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
]
.into(),
footer.into(),
)
}
SyncPhase::AwaitingConfirm(plan) => {
let mut rows: Vec<Element<'_, Message>> = Vec::new();
let header_row: Element<'_, Message> = row![
text(state.t("plain.project"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(3)),
text(state.t("plain.what_will_happen"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(2)),
text(state.t("plain.note"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(3)),
]
.spacing(8)
.into();
for entry in &plan.entries {
let (action_label, note) = disposition_plain(
state,
entry.disposition.clone(),
entry.is_dirty,
entry.has_conflict,
entry.skip_reason.as_ref().map(|reason| reason.i18n_key()),
);
let action_cell: Element<'_, Message> =
if entry.is_dirty && !entry.has_conflict && entry.skip_reason.is_none() {
let curr = &entry.disposition;
row![
pick_disposition_btn(
tokens,
&entry.project_id,
SmartPullDisposition::FetchOnly,
state.t("plain.get_latest.check_only"),
curr == &SmartPullDisposition::FetchOnly,
crate::app::ring(
state,
&FocusTarget::control_dynamic(format!(
"get_latest.{}.fetch_only",
entry.project_id
))
)
),
pick_disposition_btn(
tokens,
&entry.project_id,
SmartPullDisposition::StashAndPull,
state.t("plain.get_latest.get_anyway"),
curr == &SmartPullDisposition::StashAndPull,
crate::app::ring(
state,
&FocusTarget::control_dynamic(format!(
"get_latest.{}.stash_and_pull",
entry.project_id
))
)
),
]
.spacing(4)
.into()
} else {
text(action_label)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(2))
.into()
};
rows.push(
row![
text(&entry.project_name)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(3)),
action_cell,
text(note)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(3)),
]
.spacing(8)
.align_y(Alignment::Center)
.into(),
);
}
for exclusion in &sync.retry_exclusions {
let name = state
.workspace
.as_ref()
.and_then(|workspace| {
workspace
.projects
.iter()
.find(|project| project.id == exclusion.project_id)
})
.map(|project| project.name.as_str())
.unwrap_or_else(|| state.t("plain.project"));
rows.push(
row![
text(name)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(3)),
text(state.t("plain.activity.skipped"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(2)),
text(state.t(exclusion.reason.i18n_key()))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(3)),
]
.spacing(8)
.into(),
);
}
let has_work = plan
.entries
.iter()
.any(|e| !matches!(e.disposition, SmartPullDisposition::Excluded));
let can_start = has_work && !state.operation_interlock.is_busy();
let start_reason = if can_start {
None
} else if state.operation_interlock.is_busy() {
Some(state.t("plain.activity.busy"))
} else {
Some(state.t("plain.disabled.choose_one"))
};
let footer = row![
reasoned(
tokens,
state.t("plain.get_latest.start"),
can_start.then_some(Message::Sync(SyncMessage::ExecuteRequested)),
start_reason,
crate::app::ring(state, &FocusTarget::control(focus_target::START)),
style::primary,
),
Space::new().width(Length::Fill),
styled_button(
tokens,
state.t("action.cancel"),
Some(Message::Sync(SyncMessage::Cancelled)),
crate::app::ring(state, &FocusTarget::control(focus_target::CANCEL)),
style::ghost,
),
]
.align_y(Alignment::Center);
(
column![
text(state.t("plain.get_latest.review_heading"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
header_row,
list(tokens, rows),
]
.spacing(12)
.into(),
footer.into(),
)
}
SyncPhase::FetchRunning { done, total, .. } => {
let progress_text = format!(
"{} — {} {} {}",
state.t("plain.get_latest.working"),
done,
state.t("plain.of"),
total
);
(
column![
text(progress_text)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
]
.spacing(8)
.into(),
Space::new().into(),
)
}
SyncPhase::PullRunning {
completed, plan, ..
} => {
let total = plan.entries.len();
let done = completed.len();
let mut result_rows: Vec<Element<'_, Message>> = completed
.iter()
.map(|p| {
let (tone, glyph) = badge_for_operation_outcome(p.result.effective_outcome());
let msg = match p.result.effective_outcome() {
ProjectOperationOutcome::Succeeded => state.t("plain.get_latest.done_row"),
ProjectOperationOutcome::Failed => {
state.t("plain.get_latest.needs_help_row")
}
ProjectOperationOutcome::Skipped => state.t("plain.get_latest.skipped_row"),
};
row![
container(status_badge(
tokens,
tone,
&glyph,
body_badge_diameter(tokens)
))
.width(Length::Fixed(20.0)),
text(&p.project_name)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(2)),
text(msg)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(2)),
]
.spacing(8)
.into()
})
.collect();
for entry in plan.entries.iter().skip(done) {
result_rows.push(
row![
text("○")
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::Fixed(20.0)),
text(&entry.project_name)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(2)),
text(state.t("plain.waiting"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(2)),
]
.spacing(8)
.into(),
);
}
let progress_label = format!("{} of {} done", done, total);
(
column![
text(state.t("plain.get_latest.working"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
text(progress_label)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
list(tokens, result_rows),
]
.spacing(12)
.into(),
Space::new().into(),
)
}
SyncPhase::Done(result) => pull_result_view(tokens, state, result),
};
let close_msg = if matches!(sync.phase, SyncPhase::PullRunning { .. }) {
None
} else {
Some(Message::Sync(SyncMessage::ModalClosed))
};
surface(
tokens,
OverlayWidth::Large.resolve(state.window_width),
state.t("plain.get_latest"),
close_msg,
crate::app::ring(state, &FocusTarget::control(focus_target::CLOSE)),
inner,
footer,
)
}
fn pull_result_view<'a>(
tokens: &Tokens,
state: &'a AppState,
result: &'a crate::state::sync::SyncResult,
) -> (Element<'a, Message>, Element<'a, Message>) {
let ok = result.success_count();
let fail = result.fail_count();
let skipped = result.skipped_count();
let summary = if fail == 0 && skipped == 0 {
format!(
"{} {} {}.",
state.t("plain.get_latest.all_done_prefix"),
ok,
state.t("plain.get_latest.all_done_suffix")
)
} else if fail == 0 {
format!(
"{} {}. {} {}.",
ok,
state.t("plain.get_latest.done_count"),
skipped,
state.t("plain.get_latest.skipped_count")
)
} else {
format!(
"{} {}. {} {}. {} {}.",
ok,
state.t("plain.get_latest.done_count"),
fail,
state.t("plain.get_latest.needs_help_count"),
skipped,
state.t("plain.get_latest.skipped_count")
)
};
let body_text = if fail == 0 {
state.t("plain.no_next_step")
} else {
state.t("plain.get_latest.review_help_rows")
};
let rows: Vec<Element<'_, Message>> = result
.per_project
.iter()
.map(|pp| {
let (tone, glyph) = badge_for_operation_outcome(pp.outcome.clone());
let msg = match pp.outcome {
ProjectOperationOutcome::Succeeded => state.t("plain.get_latest.done_row"),
ProjectOperationOutcome::Failed => state.t("plain.needs_help"),
ProjectOperationOutcome::Skipped =>
{
pp.skip_reason
.as_deref()
.map(|reason| crate::view::skip_reason_display(state, reason))
.unwrap_or(state.t("plain.get_latest.skipped_row"))
}
};
let mut row_col = column![
row![
container(status_badge(
tokens,
tone,
&glyph,
body_badge_diameter(tokens)
))
.width(Length::Fixed(20.0)),
text(&pp.project_name)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(2)),
text(msg)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(2)),
]
.spacing(8),
]
.spacing(4);
if pp.outcome == ProjectOperationOutcome::Failed
&& !pp.commands_executed.is_empty()
&& state.show_op_details
{
for cmd in &pp.commands_executed {
row_col = row_col.push(
text(format!(" {}", cmd))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
);
}
if !pp.stderr.is_empty() {
row_col = row_col.push(
text(format!(" {}", pp.stderr.lines().next().unwrap_or("")))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
);
}
}
row_col.into()
})
.collect();
let details_label = if state.show_op_details {
state.t("plain.hide_details")
} else {
state.t("plain.show_details")
};
let footer = row![
styled_button(
tokens,
details_label,
Some(Message::ToggleOpDetails),
crate::app::ring(state, &FocusTarget::control(focus_target::DETAILS)),
style::ghost,
),
Space::new().width(Length::Fill),
styled_button(
tokens,
state.t("action.close"),
Some(Message::Sync(SyncMessage::ModalClosed)),
crate::app::ring(state, &FocusTarget::control(focus_target::DONE_CLOSE)),
style::ghost,
),
]
.align_y(Alignment::Center);
let body = column![
text(summary).size(snora::design::style::text::title_size(tokens)),
text(body_text)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
list(tokens, rows),
]
.spacing(12);
(body.into(), footer.into())
}
fn styled_button<'a>(
tokens: &Tokens,
label: &'a str,
on_press: Option<Message>,
is_focused: bool,
style_fn: fn(&Tokens, iced::widget::button::Status) -> iced::widget::button::Style,
) -> Element<'a, Message> {
let t = tokens.clone();
iced::widget::button(centered_button_label(
text(label)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
))
.height(BUTTON_HEIGHT)
.padding([0, 18])
.on_press_maybe(on_press)
.style(move |_theme, status| style::with_focus_ring(&t, is_focused, style_fn(&t, status)))
.into()
}
fn pick_disposition_btn<'a>(
tokens: &Tokens,
project_id: &'a ProjectId,
disposition: SmartPullDisposition,
label: &'a str,
selected: bool,
is_focused: bool,
) -> Element<'a, Message> {
let t = tokens.clone();
let msg = (!selected).then_some(Message::Sync(SyncMessage::DispositionChanged(
project_id.clone(),
disposition,
)));
iced::widget::button(centered_button_label(
text(label)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
))
.height(SMALL_BUTTON_HEIGHT)
.padding([0, 10])
.on_press_maybe(msg)
.style(move |_theme, status| current_or(selected, &t, status, is_focused))
.into()
}
fn disposition_plain(
state: &AppState,
d: SmartPullDisposition,
is_dirty: bool,
has_conflict: bool,
skip_reason_key: Option<&'static str>,
) -> (&'static str, &'static str) {
match d {
SmartPullDisposition::Pull => (state.t("plain.get_latest.action_get"), ""),
SmartPullDisposition::FetchOnly => (
state.t("plain.get_latest.action_check"),
if is_dirty {
state.t("plain.get_latest.note_unsaved")
} else {
""
},
),
SmartPullDisposition::StashAndPull => (
state.t("plain.get_latest.action_get_anyway"),
state.t("plain.get_latest.note_save_restore"),
),
SmartPullDisposition::Excluded => (
state.t("plain.get_latest.action_skip"),
if let Some(key) = skip_reason_key {
state.t(key)
} else if has_conflict {
state.t("plain.get_latest.note_needs_choice")
} else {
""
},
),
}
}