use iced::widget::{Space, button, column, container, rich_text, row, span, text};
use iced::{Alignment, Element, Length, Padding};
use knotra_ui::widget::{
BUTTON_HEIGHT, STRONG_FONT, Tokens, body_badge_diameter, centered_button_label,
centered_button_row, checkbox, status_badge, style,
};
use knotra_vcs::model::project::Project;
use crate::{
message::{ConflictOpsMessage, DetailPanelMessage, Message, SelectionMessage},
state::{
AppState,
dashboard::{
DashboardCause, DashboardEntry, DashboardTier, ProgressKind, badge_for_all_set,
badge_for_cause, badge_for_progress, workspace_has_mixed_vcs_kinds,
},
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 = 160.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()))
}
fn project_name_style(
tokens: &Tokens,
status: iced::widget::button::Status,
focused: bool,
) -> iced::widget::button::Style {
use iced::widget::button::Status;
let text_primary = snora::design::style::color::to_iced_color(tokens.palette.text_primary);
let accent = snora::design::style::color::to_iced_color(tokens.palette.accent);
let text_color = if focused || matches!(status, Status::Hovered | Status::Pressed) {
accent
} else {
text_primary
};
iced::widget::button::Style {
background: None,
text_color,
..iced::widget::button::Style::default()
}
}
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| {
if context.unborn {
format!(
"{} · {}",
context.label,
state.t("dashboard.no_commits_yet")
)
} else {
context.label.clone()
}
})
.unwrap_or_else(|| state.t("dashboard.work_area_unknown").to_owned());
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();
let mut name_span: iced::advanced::text::Span<'_, ()> =
span(project.name.as_str()).font(STRONG_FONT);
if focused {
name_span = name_span
.color(snora::design::style::color::to_iced_color(
tokens.palette.accent,
))
.underline(true);
}
let label = rich_text![name_span]
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens));
let cue = knotra_ui::widget::icon::colored_icon_element(
&knotra_ui::widget::icon::chevron_right(),
snora::design::style::text::body_small_size(tokens).0,
snora::design::style::color::to_iced_color(tokens.palette.text_secondary),
);
button(centered_button_row(row![label, cue].spacing(2)))
.height(Length::Fixed(BUTTON_HEIGHT))
.padding(Padding {
left: 0.0,
..button::DEFAULT_PADDING
})
.on_press(Message::DetailPanel(DetailPanelMessage::Opened(
project.id.clone(),
)))
.style(move |_theme, status| {
style::with_focus_ring(&t, focused, project_name_style(&t, status, focused))
})
.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 entry_badge(entry: &DashboardEntry<'_>) -> (knotra_ui::widget::Tone, snora::Icon) {
match entry.tier {
DashboardTier::NeedsHelp => {
badge_for_cause(entry.cause.unwrap_or(DashboardCause::StatusUnknown))
}
DashboardTier::InProgress => badge_for_progress(
entry
.relevant_count
.map(|c| c.kind)
.unwrap_or(ProgressKind::Uncommitted),
),
DashboardTier::AllSet => badge_for_all_set(),
}
}
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: String,
) -> Element<'a, Message> {
let tokens = &state.theme.tokens;
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
&& workspace_has_mixed_vcs_kinds(state.workspace_status.as_ref())
{
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(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens)),
);
}
identity = identity.push(identity_details);
let (tone, glyph) = entry_badge(entry);
let diameter = body_badge_diameter(tokens);
let middle: Element<'_, Message> = match entry.tier {
DashboardTier::NeedsHelp => row![
status_badge(tokens, tone, &glyph, diameter),
text(cause_label(state, entry.cause))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
]
.spacing(6)
.align_y(Alignment::Center)
.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());
row![
status_badge(tokens, tone, &glyph, diameter),
text(format!("{work_area} · {count}"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
]
.spacing(6)
.align_y(Alignment::Center)
.into()
}
DashboardTier::AllSet => row![
status_badge(tokens, tone, &glyph, diameter),
text(work_area)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
]
.spacing(6)
.align_y(Alignment::Center)
.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: String,
) -> 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 tokens = &state.theme.tokens;
let line_two = compact_status_line(state, entry, &work_area);
let (tone, glyph) = entry_badge(entry);
container(
column![
line_one,
row![
status_badge(tokens, tone, &glyph, body_badge_diameter(tokens)),
text(line_two)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::Fill),
]
.spacing(6)
.align_y(Alignment::Center),
]
.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
if workspace_has_mixed_vcs_kinds(state.workspace_status.as_ref()) =>
{
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::NeedsHelp => cause_label(state, entry.cause).to_owned(),
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(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, 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(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
]
.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"),
}
}