use iced::{
Alignment, Element, Length,
widget::{Space, column, container, row, text},
};
use knotra_ui::widget::{
BUTTON_HEIGHT, Tokens, Tone, body_badge_diameter, centered_button_label, guided_field_focused,
list,
overlay::{OverlayWidth, surface},
reasoned, status_badge, status_symbol, style,
};
use knotra_vcs::model::operation::{FreezeBlocker, FreezeNote, FreezeOutcome};
use crate::{
message::{FreezerMessage, Message, TagPushMessage},
state::{
AppState,
focus::{FocusTarget, LayerFocus},
},
};
mod focus_target {
pub const CLOSE: &str = "release.close";
pub const VALIDATE: &str = "release.validate";
pub const SAVE: &str = "release.save";
pub const DETAILS: &str = "release.details";
pub const CANCEL: &str = "release.cancel";
pub const SHARE: &str = "release.share";
pub const SHARE_DECLINE: &str = "release.share_decline";
pub const FOOTER_CLOSE: &str = "release.footer_close";
}
const ICON_COLUMN_WIDTH: Length = Length::Fixed(22.0);
const ICON_COLUMN_SPACING: f32 = 8.0;
pub fn layer_focus(state: &AppState) -> LayerFocus<Message> {
use crate::state::freezer::FreezerPhase;
let freezer = &state.freezer;
let close_target = FocusTarget::control(focus_target::CLOSE);
let name_field = FocusTarget::text_input(knotra_ui::widget::focus_id::RELEASE_NAME.clone());
match &freezer.phase {
FreezerPhase::Idle | FreezerPhase::Validating { .. } => {
let close_msg = Some(Message::Freezer(FreezerMessage::BulkModalClosed));
let note_field =
FocusTarget::text_input(knotra_ui::widget::focus_id::RELEASE_NOTE.clone());
let mut order = vec![
(close_target, close_msg),
(name_field.clone(), None),
(note_field, None),
];
if !matches!(freezer.phase, FreezerPhase::Validating { .. })
&& freezer.freeze_name_is_valid()
{
order.push((
FocusTarget::control(focus_target::VALIDATE),
(!state.operation_interlock.is_busy())
.then_some(Message::Freezer(FreezerMessage::ValidateRequested)),
));
}
LayerFocus {
order,
entry: Some(name_field),
}
}
FreezerPhase::ValidationReady(validation) => {
let close_msg = Some(Message::Freezer(FreezerMessage::BulkModalClosed));
let can_save = validation.all_ready()
&& validation.ready_count() > 0
&& !state.operation_interlock.is_busy();
let has_unreadable = validation.entries.iter().any(|entry| {
entry.blockers.iter().any(|b| {
matches!(
b,
knotra_vcs::model::operation::FreezeBlocker::Unreadable { .. }
)
})
});
let mut order = vec![
(close_target, close_msg.clone()),
(
FocusTarget::control(focus_target::SAVE),
can_save.then_some(Message::Freezer(FreezerMessage::ExecuteConfirmed)),
),
];
let details_target = FocusTarget::control(focus_target::DETAILS);
if has_unreadable {
order.push((details_target.clone(), Some(Message::ToggleOpDetails)));
}
let cancel_target = FocusTarget::control(focus_target::CANCEL);
order.push((cancel_target.clone(), close_msg));
LayerFocus {
order,
entry: Some(if has_unreadable {
details_target
} else {
cancel_target
}),
}
}
FreezerPhase::Executing => LayerFocus {
order: Vec::new(),
entry: None,
},
FreezerPhase::Done(result) => {
let push_is_running = state
.pending_tag_push
.as_ref()
.is_some_and(|push| push.is_pushing);
let close_msg =
(!push_is_running).then_some(Message::Freezer(FreezerMessage::BulkModalClosed));
let mut order = vec![(close_target, close_msg.clone())];
let share_offer_shown = state.pending_tag_push.as_ref().is_some_and(|push| {
push.freeze_name == result.freeze_name
&& !push.project_ids.is_empty()
&& !push.is_pushing
});
let decline_target = FocusTarget::control(focus_target::SHARE_DECLINE);
if share_offer_shown {
order.push((
FocusTarget::control(focus_target::SHARE),
(!state.operation_interlock.is_busy())
.then_some(Message::TagPush(TagPushMessage::PushConfirmed)),
));
order.push((
decline_target.clone(),
Some(Message::TagPush(TagPushMessage::PushDeclined)),
));
}
order.push((
FocusTarget::control(focus_target::DETAILS),
Some(Message::ToggleOpDetails),
));
let footer_close = FocusTarget::control(focus_target::FOOTER_CLOSE);
order.push((footer_close.clone(), close_msg));
LayerFocus {
order,
entry: Some(if share_offer_shown {
decline_target
} else {
footer_close
}),
}
}
}
}
pub fn tag_modal(state: &AppState) -> Element<'_, Message> {
use crate::state::freezer::FreezerPhase;
let tokens = &state.theme.tokens;
let freezer = &state.freezer;
let (inner, footer): (Element<'_, Message>, Element<'_, Message>) = match &freezer.phase {
FreezerPhase::Idle | FreezerPhase::Validating { .. } => {
let name_error: Option<&str> = if freezer.freeze_name.is_empty() {
None } else if !freezer.freeze_name_is_valid() {
Some(state.t("plain.release.name_invalid"))
} else {
None
};
let name_field = guided_field_focused(
tokens,
state.t("plain.release.name_label"),
state.t("plain.release.name_hint"),
&freezer.freeze_name,
|s| Message::Freezer(FreezerMessage::NameChanged(s)),
name_error,
knotra_ui::widget::focus_id::RELEASE_NAME.clone(),
);
let msg_field = guided_field_focused(
tokens,
state.t("plain.release.note_label"),
state.t("plain.release.note_hint"),
&freezer.tag_message,
|s| Message::Freezer(FreezerMessage::TagMessageChanged(s)),
None,
knotra_ui::widget::focus_id::RELEASE_NOTE.clone(),
);
let validate_or_spinner: Element<'_, Message> =
if matches!(freezer.phase, FreezerPhase::Validating { .. }) {
text(state.t("plain.release.checking"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into()
} else if freezer.freeze_name_is_valid() {
reasoned(
tokens,
state.t("plain.release.check_readiness"),
(!state.operation_interlock.is_busy())
.then_some(Message::Freezer(FreezerMessage::ValidateRequested)),
state
.operation_interlock
.is_busy()
.then_some(state.t("plain.activity.busy")),
crate::app::ring(state, &FocusTarget::control(focus_target::VALIDATE)),
style::primary,
)
} else {
Space::new().into()
};
(
column![name_field, msg_field, validate_or_spinner]
.spacing(14)
.into(),
Space::new().into(),
)
}
FreezerPhase::ValidationReady(validation) => {
let blocked_count = validation.blocked_count();
let ready_count = validation.ready_count();
let can_save =
validation.all_ready() && ready_count > 0 && !state.operation_interlock.is_busy();
let has_unreadable = validation.entries.iter().any(|entry| {
entry
.blockers
.iter()
.any(|b| matches!(b, FreezeBlocker::Unreadable { .. }))
});
let val_rows: Vec<Element<'_, Message>> = validation
.entries
.iter()
.map(|entry| {
let (badge, first_line) = if entry.is_blocked() {
(
(Tone::Danger, status_symbol::alert()),
entry
.blockers
.first()
.map(|b| blocker_message(state, b))
.unwrap_or_default(),
)
} else {
(
(Tone::Success, status_symbol::all_set()),
state.t("plain.release.row_ready"),
)
};
let mut row_col = column![
row![
container(status_badge(
tokens,
badge.0,
&badge.1,
body_badge_diameter(tokens)
))
.width(ICON_COLUMN_WIDTH),
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(first_line)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(3)),
]
.spacing(ICON_COLUMN_SPACING)
.align_y(Alignment::Center),
]
.spacing(4);
for blocker in entry.blockers.iter().skip(1) {
row_col = row_col.push(continuation_line(
tokens,
blocker_message(state, blocker),
3,
));
}
if state.show_op_details {
for blocker in &entry.blockers {
if let FreezeBlocker::Unreadable { reason } = blocker {
row_col =
row_col.push(continuation_line(tokens, reason.as_str(), 3));
}
}
}
for note in &entry.notes {
row_col =
row_col.push(continuation_line(tokens, note_message(state, note), 3));
}
row_col.into()
})
.collect();
let save_reason: Option<&str> = if state.operation_interlock.is_busy() {
Some(state.t("plain.activity.busy"))
} else if can_save {
None
} else if ready_count == 0 && blocked_count == 0 {
Some(state.t("plain.disabled.choose_one"))
} else if blocked_count == 1 {
Some(state.t("plain.release.fix_one"))
} else {
Some(state.t("plain.release.fix_some"))
};
let mut footer_row = row![
reasoned(
tokens,
state.t("plain.save_release_point"),
can_save.then_some(Message::Freezer(FreezerMessage::ExecuteConfirmed)),
save_reason,
crate::app::ring(state, &FocusTarget::control(focus_target::SAVE)),
style::primary,
),
Space::new().width(Length::Fill),
]
.spacing(8)
.align_y(Alignment::Center);
if has_unreadable {
let details_label = if state.show_op_details {
state.t("plain.hide_details")
} else {
state.t("plain.show_details")
};
footer_row = footer_row.push(styled_button(
tokens,
details_label,
Some(Message::ToggleOpDetails),
crate::app::ring(state, &FocusTarget::control(focus_target::DETAILS)),
style::ghost,
));
}
let footer = footer_row.push(styled_button(
tokens,
state.t("action.cancel"),
Some(Message::Freezer(FreezerMessage::BulkModalClosed)),
crate::app::ring(state, &FocusTarget::control(focus_target::CANCEL)),
style::ghost,
));
(
column![
text(state.t("plain.release.ready_check"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
list(tokens, val_rows),
impact_warnings_section(tokens, state),
]
.spacing(12)
.into(),
footer.into(),
)
}
FreezerPhase::Executing => (
column![
text(state.t("plain.release.saving"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
text(state.t("plain.release.saving_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(),
),
FreezerPhase::Done(result) => {
let push_is_running = state
.pending_tag_push
.as_ref()
.is_some_and(|push| push.is_pushing);
let outcome_title = match result.outcome {
FreezeOutcome::Success => state.t("plain.release.outcome_success"),
FreezeOutcome::RolledBack => state.t("plain.release.outcome_undone"),
FreezeOutcome::RollbackFailed => state.t("plain.release.outcome_partial"),
FreezeOutcome::NothingDone => state.t("plain.release.outcome_nothing"),
};
let outcome_body = match result.outcome {
FreezeOutcome::Success => state.t("plain.no_next_step"),
FreezeOutcome::RolledBack => state.t("plain.release.outcome_undone_hint"),
FreezeOutcome::RollbackFailed => state.t("plain.release.outcome_partial_hint"),
FreezeOutcome::NothingDone => "",
};
let rows: Vec<Element<'_, Message>> = result
.project_results
.iter()
.map(|pr| {
let icon = if pr.success {
"✓"
} else if pr.rollback_attempted && pr.rollback_succeeded == Some(true) {
"⟲"
} else {
"!"
};
let msg = if pr.success {
state.t("plain.release.row_saved")
} else if pr.rollback_attempted && pr.rollback_succeeded == Some(true) {
state.t("plain.release.row_undone")
} else {
state.t("plain.needs_help")
};
let mut row_col = column![
row![
text(icon)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(ICON_COLUMN_WIDTH),
text(&pr.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(ICON_COLUMN_SPACING),
]
.spacing(4);
if !pr.success
&& state.show_op_details
&& let Some(hint) = &pr.recovery_hint
{
for cmd in &hint.suggested_commands {
row_col = row_col.push(continuation_line(tokens, cmd.as_str(), 2));
}
}
row_col.into()
})
.collect();
let details_label = if state.show_op_details {
state.t("plain.hide_details")
} else {
state.t("plain.show_details")
};
let push_offer: Element<'_, Message> = match &state.pending_tag_push {
Some(push)
if push.freeze_name == result.freeze_name && !push.project_ids.is_empty() =>
{
if push.is_pushing {
text(state.t("plain.release.sharing"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into()
} else {
column![
text(state.t("plain.release.share_offer"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
row![
styled_button(
tokens,
state.t("plain.release.share_action"),
(!state.operation_interlock.is_busy())
.then_some(Message::TagPush(TagPushMessage::PushConfirmed)),
crate::app::ring(
state,
&FocusTarget::control(focus_target::SHARE)
),
style::primary,
),
styled_button(
tokens,
state.t("plain.release.share_decline"),
Some(Message::TagPush(TagPushMessage::PushDeclined)),
crate::app::ring(
state,
&FocusTarget::control(focus_target::SHARE_DECLINE)
),
style::ghost,
),
]
.spacing(8)
.align_y(Alignment::Center),
]
.spacing(8)
.into()
}
}
_ => Space::new().height(Length::Fixed(0.0)).into(),
};
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"),
(!push_is_running).then_some(Message::Freezer(FreezerMessage::BulkModalClosed)),
crate::app::ring(state, &FocusTarget::control(focus_target::FOOTER_CLOSE)),
style::ghost,
),
]
.align_y(Alignment::Center);
(
column![
text(outcome_title).size(snora::design::style::text::title_size(tokens)),
text(outcome_body)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
list(tokens, rows),
push_offer,
]
.spacing(12)
.into(),
footer.into(),
)
}
};
let close_msg = if matches!(freezer.phase, FreezerPhase::Executing)
|| state
.pending_tag_push
.as_ref()
.is_some_and(|push| push.is_pushing)
{
None
} else {
Some(Message::Freezer(FreezerMessage::BulkModalClosed))
};
surface(
tokens,
OverlayWidth::Large.resolve(state.window_width),
state.t("plain.save_release_point"),
close_msg,
crate::app::ring(state, &FocusTarget::control(focus_target::CLOSE)),
inner,
footer,
)
}
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 impact_warnings_section<'a>(tokens: &Tokens, state: &'a AppState) -> Element<'a, Message> {
if !state.freezer.topology_checked {
return text(state.t("plain.release.impact_unchecked"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into();
}
if state.freezer.impact_warnings.is_empty() {
return text(state.t("plain.release.impact_clear"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into();
}
let rows: Vec<Element<'_, Message>> = state
.freezer
.impact_warnings
.iter()
.map(|w| {
row![
text("⚠")
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(ICON_COLUMN_WIDTH),
text(&w.frozen_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(format!(
"{}: {}",
state.t("plain.release.impact_depended_on_by"),
w.dependent_projects.join(", ")
))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(3)),
]
.spacing(ICON_COLUMN_SPACING)
.align_y(Alignment::Center)
.into()
})
.collect();
column![
text(state.t("plain.release.impact_title"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
list(tokens, rows),
]
.spacing(6)
.into()
}
fn continuation_line<'a>(
tokens: &Tokens,
content: impl Into<String>,
content_portion: u16,
) -> Element<'a, Message> {
let content_text: Element<'a, Message> = text(content.into())
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::FillPortion(content_portion))
.into();
row![
Space::new().width(ICON_COLUMN_WIDTH),
Space::new().width(Length::FillPortion(2)),
content_text,
]
.spacing(ICON_COLUMN_SPACING)
.into()
}
fn blocker_message(state: &AppState, blocker: &FreezeBlocker) -> &'static str {
match blocker {
FreezeBlocker::UnresolvedConflict => state.t("plain.release.blocker_needs_choice"),
FreezeBlocker::UncommittedChanges => state.t("plain.release.blocker_unsaved"),
FreezeBlocker::NameInUse => state.t("plain.release.blocker_name_used"),
FreezeBlocker::Unreadable { .. } => state.t("plain.release.blocker_unreadable"),
}
}
fn note_message(state: &AppState, note: &FreezeNote) -> String {
match note {
FreezeNote::UntrackedFiles(n) => format!("{} {n}", state.t("plain.release.note_untracked")),
}
}