use iced::widget::{Space, button, column, container, row, text};
use iced::{Alignment, Element, Length};
use knotra_ui::widget::{BUTTON_HEIGHT, FONT_SMALL, Tokens, checkbox, style};
use knotra_vcs::model::project::Project;
use crate::{
message::{ConflictOpsMessage, DetailPanelMessage, Message, SelectionMessage},
state::{
AppState,
dashboard::{DashboardCause, DashboardEntry, DashboardTier, ProgressKind},
focus::FocusTarget,
},
};
use super::width_mode::WidthMode;
const IDENTITY_TRACK_WIDTH: f32 = 280.0;
const MIDDLE_TRACK_WIDTH: f32 = 320.0;
const ACTION_TRACK_WIDTH: f32 = 100.0;
pub(super) fn checkbox_key(id: &knotra_vcs::ProjectId) -> String {
format!("dashboard.row.{id}.checkbox")
}
pub(super) fn name_key(id: &knotra_vcs::ProjectId) -> String {
format!("dashboard.row.{id}.name")
}
pub(super) fn action_key(id: &knotra_vcs::ProjectId) -> String {
format!("dashboard.row.{id}.action")
}
fn is_focused(state: &AppState, key: &str) -> bool {
state.dashboard_focus.as_ref() == Some(&FocusTarget::control_dynamic(key.to_owned()))
}
pub(super) fn view_project_row<'a>(
state: &'a AppState,
entry: DashboardEntry<'a>,
mode: WidthMode,
) -> Element<'a, Message> {
let tokens = &state.theme.tokens;
let project = entry.project;
let checkbox_element = selection_checkbox(state, tokens, project);
let name = name_button(state, tokens, project);
let action = row_action(state, tokens, &entry, project);
let work_area = entry
.status
.and_then(|status| status.context.as_ref())
.map(|context| context.label.as_str())
.unwrap_or(state.t("dashboard.work_area_unknown"));
match mode {
WidthMode::Compact => {
view_compact_row(state, &entry, checkbox_element, name, action, work_area)
}
WidthMode::Standard | WidthMode::Wide => {
view_standard_row(state, &entry, checkbox_element, name, action, work_area)
}
}
}
fn selection_checkbox<'a>(
state: &'a AppState,
tokens: &Tokens,
project: &'a Project,
) -> Option<Element<'a, Message>> {
if !state.selection_mode {
return None;
}
let id = project.id.clone();
let is_checked = state.selection.contains(&project.id);
Some(checkbox(
tokens,
format!(
"{} — {}",
state.t("plain.selection.select_project"),
project.name
),
is_checked,
move |_checked| Message::Selection(SelectionMessage::Toggled(id.clone())),
is_focused(state, &checkbox_key(&project.id)),
))
}
fn name_button<'a>(
state: &'a AppState,
tokens: &Tokens,
project: &'a Project,
) -> Element<'a, Message> {
let focused = is_focused(state, &name_key(&project.id));
let t = tokens.clone();
button(text(project.name.as_str()).size(13))
.on_press(Message::DetailPanel(DetailPanelMessage::Opened(
project.id.clone(),
)))
.style(move |_theme, status| style::with_focus_ring(&t, focused, style::ghost(&t, status)))
.into()
}
fn row_action<'a>(
state: &'a AppState,
tokens: &Tokens,
entry: &DashboardEntry<'a>,
project: &'a Project,
) -> Element<'a, Message> {
if entry.tier != DashboardTier::NeedsHelp {
return Space::new().into();
}
let focused = is_focused(state, &action_key(&project.id));
if entry.cause == Some(DashboardCause::Conflict) {
row_action_button(
tokens,
state.t("dashboard.resolve"),
style::secondary,
(!state.operation_interlock.is_busy()).then_some(Message::ConflictOps(
ConflictOpsMessage::OpenRequested(Some(project.id.clone())),
)),
None,
focused,
)
} else {
row_action_button(
tokens,
state.t("plain.show_details"),
style::ghost,
Some(Message::DetailPanel(DetailPanelMessage::Opened(
project.id.clone(),
))),
None,
focused,
)
}
}
fn view_standard_row<'a>(
state: &'a AppState,
entry: &DashboardEntry<'a>,
checkbox_element: Option<Element<'a, Message>>,
name: Element<'a, Message>,
action: Element<'a, Message>,
work_area: &'a str,
) -> Element<'a, Message> {
let mut identity = row![].spacing(4).align_y(Alignment::Center);
if let Some(checkbox_element) = checkbox_element {
identity = identity.push(checkbox_element);
}
let mut identity_details = column![name].spacing(2);
if entry.tier == DashboardTier::NeedsHelp {
let vcs = entry
.status
.map(|status| status.identity.vcs_kind.to_string())
.unwrap_or_else(|| state.t("status.unknown").to_owned());
identity_details = identity_details.push(text(vcs).size(11));
}
identity = identity.push(identity_details);
let middle: Element<'_, Message> = match entry.tier {
DashboardTier::NeedsHelp => text(cause_label(state, entry.cause)).size(12).into(),
DashboardTier::InProgress => {
let count = entry
.relevant_count
.map(|count| format!("{}: {}", progress_label(state, count.kind), count.value))
.unwrap_or_else(|| state.t("plain.status.unsaved_work").to_owned());
column![text(work_area).size(12), text(count).size(11)]
.spacing(2)
.into()
}
DashboardTier::AllSet => text(work_area).size(12).into(),
};
container(
row![
container(identity).width(Length::Fixed(IDENTITY_TRACK_WIDTH)),
container(middle).width(Length::Fixed(MIDDLE_TRACK_WIDTH)),
container(action).width(Length::Fixed(ACTION_TRACK_WIDTH)),
]
.spacing(8)
.align_y(Alignment::Center),
)
.width(Length::Fill)
.padding([7, 8])
.into()
}
fn view_compact_row<'a>(
state: &'a AppState,
entry: &DashboardEntry<'a>,
checkbox_element: Option<Element<'a, Message>>,
name: Element<'a, Message>,
action: Element<'a, Message>,
work_area: &'a str,
) -> Element<'a, Message> {
let mut line_one = row![].spacing(4).align_y(Alignment::Center);
if let Some(checkbox_element) = checkbox_element {
line_one = line_one.push(checkbox_element);
}
line_one = line_one.push(name);
line_one = line_one.push(Space::new().width(Length::Fill));
line_one = line_one.push(action);
let line_two = compact_status_line(state, entry, work_area);
container(column![line_one, text(line_two).size(11)].spacing(2))
.width(Length::Fill)
.padding([7, 8])
.into()
}
fn compact_status_line(state: &AppState, entry: &DashboardEntry<'_>, work_area: &str) -> String {
match entry.tier {
DashboardTier::NeedsHelp => {
let vcs = entry
.status
.map(|status| status.identity.vcs_kind.to_string())
.unwrap_or_else(|| state.t("status.unknown").to_owned());
format!("{vcs} · {}", cause_label(state, entry.cause))
}
DashboardTier::InProgress => {
let count = entry
.relevant_count
.map(|count| format!("{}: {}", progress_label(state, count.kind), count.value))
.unwrap_or_else(|| state.t("plain.status.unsaved_work").to_owned());
format!("{work_area} · {count}")
}
DashboardTier::AllSet => work_area.to_owned(),
}
}
fn row_action_button<'a>(
tokens: &Tokens,
label: &'a str,
style_fn: fn(&Tokens, iced::widget::button::Status) -> iced::widget::button::Style,
on_press: Option<Message>,
reason: Option<&'a str>,
is_focused: bool,
) -> Element<'a, Message> {
let t = tokens.clone();
let show_reason = on_press.is_none();
let btn = button(text(label).size(12))
.height(BUTTON_HEIGHT)
.padding([0, 12])
.on_press_maybe(on_press)
.style(move |_theme, status| style::with_focus_ring(&t, is_focused, style_fn(&t, status)));
match reason {
Some(r) if show_reason => column![btn, text(r).size(FONT_SMALL)].spacing(4).into(),
_ => btn.into(),
}
}
fn cause_label(state: &AppState, cause: Option<DashboardCause>) -> &'static str {
match cause {
Some(DashboardCause::MissingPath) => state.t("dashboard.cause.missing_path"),
Some(DashboardCause::Conflict) => state.t("dashboard.cause.conflict"),
Some(DashboardCause::ConflictDetectionUnavailable) => {
state.t("dashboard.cause.conflict_detection_unavailable")
}
Some(DashboardCause::ReadUnavailable) => state.t("dashboard.cause.read_unavailable"),
Some(DashboardCause::DetachedContext) => state.t("dashboard.cause.detached_context"),
Some(DashboardCause::StatusUnknown) | None => state.t("dashboard.cause.status_unknown"),
}
}
fn progress_label(state: &AppState, kind: ProgressKind) -> &'static str {
match kind {
ProgressKind::Uncommitted => state.t("dashboard.progress.uncommitted"),
ProgressKind::Untracked => state.t("dashboard.progress.untracked"),
ProgressKind::Ahead => state.t("dashboard.progress.ahead"),
ProgressKind::Behind => state.t("dashboard.progress.behind"),
}
}