use iced::widget::{column, container, scrollable, text};
use iced::{Alignment, Element, Length, Padding};
use knotra_ui::widget::{WIDE_CONTENT_WIDTH, strong};
use crate::{
message::{
ConflictOpsMessage, DashboardMessage, DetailPanelMessage, Message, SelectionMessage,
},
state::{
AppState, LoadPhase,
dashboard::{DashboardCause, DashboardSectionKey, DashboardTier},
focus::{FocusOrder, FocusTarget},
},
};
mod empty;
mod row;
mod section;
mod toolbar;
mod width_mode;
use empty::{empty_workspace, no_matches, view_without_workspace};
use row::{action_key, checkbox_key, name_key};
use section::{focus_key, view_section};
use toolbar::view_toolbar;
pub(crate) use width_mode::WidthMode;
pub fn focus_order(state: &AppState) -> FocusOrder<Message> {
let display = state.dashboard_display();
let mut order = if has_no_projects(state) {
Vec::new()
} else {
toolbar::focus_order(state, state.width_mode)
};
for section in &display.sections {
if let DashboardSectionKey::Tier(tier) = section.key
&& tier != DashboardTier::NeedsHelp
{
order.push((
FocusTarget::control_dynamic(focus_key(tier)),
Some(Message::Dashboard(DashboardMessage::TierToggled(tier))),
));
}
if section.collapsed {
continue;
}
for entry in §ion.entries {
let id = &entry.project.id;
if state.selection_mode {
order.push((
FocusTarget::control_dynamic(checkbox_key(id)),
Some(Message::Selection(SelectionMessage::Toggled(id.clone()))),
));
}
order.push((
FocusTarget::control_dynamic(name_key(id)),
Some(Message::DetailPanel(DetailPanelMessage::Opened(id.clone()))),
));
if entry.tier == DashboardTier::NeedsHelp {
let action_message = if entry.cause == Some(DashboardCause::Conflict) {
(!state.operation_interlock.is_busy()).then_some(Message::ConflictOps(
ConflictOpsMessage::OpenRequested(Some(id.clone())),
))
} else {
Some(Message::DetailPanel(DetailPanelMessage::Opened(id.clone())))
};
order.push((FocusTarget::control_dynamic(action_key(id)), action_message));
}
}
}
order
}
pub fn card_focus_order(state: &AppState) -> FocusOrder<Message> {
let display = state.dashboard_display();
let mut order = Vec::new();
for section in &display.sections {
if section.collapsed {
continue;
}
for entry in §ion.entries {
let id = &entry.project.id;
order.push((
FocusTarget::control_dynamic(name_key(id)),
Some(Message::DetailPanel(DetailPanelMessage::Opened(id.clone()))),
));
}
}
order
}
pub fn view(state: &AppState, mode: WidthMode) -> Element<'_, Message> {
let mut body = column![view_header(state)].height(Length::Fill).spacing(4);
if !has_no_projects(state) {
body = body.push(view_toolbar(state, mode));
}
body = body.push(scrollable(view_body(state, mode)).height(Length::Fill));
if let Some(message) = &state.status_bar {
body = body.push(
container(
text(message)
.size(snora::design::style::text::body_size(&state.theme.tokens))
.line_height(snora::design::style::text::body_line_height(
&state.theme.tokens,
)),
)
.width(Length::Fill)
.padding([3, 12]),
);
}
body.into()
}
fn view_header(state: &AppState) -> Element<'_, Message> {
let tokens = &state.theme.tokens;
iced::widget::row![strong(
text(state.t("nav.dashboard")).size(snora::design::style::text::heading_size(tokens))
)]
.spacing(12)
.align_y(Alignment::Center)
.padding(Padding::new(12.0))
.into()
}
fn has_no_projects(state: &AppState) -> bool {
state
.workspace
.as_ref()
.is_none_or(|workspace| workspace.projects.is_empty())
}
fn view_body(state: &AppState, mode: WidthMode) -> Element<'_, Message> {
if state.workspace.is_none() {
return view_without_workspace(state);
}
if has_no_projects(state) {
return empty_workspace(state);
}
let display = state.dashboard_display();
let mut content: Vec<Element<'_, Message>> = Vec::new();
match &state.load_phase {
LoadPhase::Startup | LoadPhase::Refreshing => content.push(
container(
text(state.t("plain.status.checking"))
.size(snora::design::style::text::body_size(&state.theme.tokens))
.line_height(snora::design::style::text::body_line_height(
&state.theme.tokens,
)),
)
.width(Length::Fill)
.padding([5, 12])
.into(),
),
LoadPhase::Ready => {}
}
if display.sections.is_empty() {
content.push(no_matches(state));
} else {
for section in display.sections {
content.push(view_section(state, section, mode));
}
}
let content_column = column(content)
.spacing(state.theme.tokens.spacing.lg)
.padding(Padding {
top: 4.0,
right: 12.0,
bottom: 16.0,
left: 12.0,
});
match mode {
WidthMode::Wide => container(content_column.width(Length::Fixed(WIDE_CONTENT_WIDTH)))
.align_left(Length::Fill)
.into(),
WidthMode::Compact | WidthMode::Standard => content_column.into(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::AppConfig;
use knotra_vcs::{
ConflictStatus, Project, ProjectStatus, RemoteStatus, RepositoryIdentity, VcsKind,
WorkingTreeStatus, Workspace, WorkspaceStatus,
};
fn needs_help_project(name: &str) -> Project {
Project::new(name, "/tmp")
}
fn clean_status(project_id: knotra_vcs::ProjectId) -> ProjectStatus {
ProjectStatus {
project_id,
identity: RepositoryIdentity {
path: "/tmp".into(),
vcs_kind: VcsKind::Git,
},
context: None,
remote: RemoteStatus::default(),
working_tree: WorkingTreeStatus::default(),
conflict: ConflictStatus::default(),
refreshed_at: jiff::Timestamp::now(),
read_error: None,
}
}
#[test]
fn card_focus_order_contains_only_row_name_targets_in_display_order() {
let mut state = AppState::new(AppConfig::default());
let a = needs_help_project("alpha");
let b = needs_help_project("beta");
let (a_id, b_id) = (a.id.clone(), b.id.clone());
state.workspace = Some(Workspace {
projects: vec![a, b],
..Workspace::new("Test")
});
let order = card_focus_order(&state);
let targets: Vec<_> = order.iter().map(|(target, _)| target.clone()).collect();
assert_eq!(
targets,
vec![
FocusTarget::control_dynamic(name_key(&a_id)),
FocusTarget::control_dynamic(name_key(&b_id)),
],
"only the two row-name targets, in display order — no \
checkboxes, section headers, or row actions"
);
for (_, message) in &order {
assert!(
matches!(
message,
Some(Message::DetailPanel(DetailPanelMessage::Opened(_)))
),
"every card target must activate to opening its detail panel"
);
}
}
#[test]
fn card_focus_order_skips_rows_in_a_collapsed_all_set_section() {
let mut state = AppState::new(AppConfig::default());
assert!(state.config.dashboard_all_set_collapsed);
let needs_help = needs_help_project("alpha");
let all_set = needs_help_project("beta");
let needs_help_id = needs_help.id.clone();
let all_set_id = all_set.id.clone();
state.workspace = Some(Workspace {
projects: vec![needs_help, all_set],
..Workspace::new("Test")
});
state.workspace_status = Some(WorkspaceStatus {
projects: vec![clean_status(all_set_id)],
last_refresh: None,
});
let order = card_focus_order(&state);
let targets: Vec<_> = order.iter().map(|(target, _)| target.clone()).collect();
assert_eq!(
targets,
vec![FocusTarget::control_dynamic(name_key(&needs_help_id))],
"the AllSet-tier project sits in a collapsed section and must \
be excluded, the same way its checkbox/action targets already \
are from focus_order"
);
}
}