use iced::widget::{button, column, container, row, text};
use iced::{Color, Element, Length};
use knotra_ui::widget::{BUTTON_HEIGHT, Tokens, centered_button_row, icon, list, strong, style};
use crate::{
message::{DashboardMessage, Message},
state::{
AppState,
dashboard::{DashboardSection, DashboardSectionKey, DashboardTier},
focus::FocusTarget,
},
};
use super::row::view_project_row;
use super::width_mode::WidthMode;
pub(super) fn view_section<'a>(
state: &'a AppState,
section: DashboardSection<'a>,
mode: WidthMode,
) -> Element<'a, Message> {
let header = section_header(state, section.key, section.entries.len(), section.collapsed);
if section.collapsed {
return header;
}
let rows: Vec<Element<'a, Message>> = section
.entries
.into_iter()
.map(|entry| view_project_row(state, entry, mode))
.collect();
column![header, list(&state.theme.tokens, rows)]
.spacing(state.theme.tokens.spacing.sm)
.into()
}
pub(super) fn focus_key(tier: DashboardTier) -> String {
format!("dashboard.section.{tier:?}")
}
fn is_focused(state: &AppState, key: &str) -> bool {
state.dashboard_focus.as_ref() == Some(&FocusTarget::control_dynamic(key.to_owned()))
}
fn group_toggle_style(tokens: &Tokens, status: button::Status) -> button::Style {
let text_secondary = snora::design::style::color::to_iced_color(tokens.palette.text_secondary);
let text_color = match status {
button::Status::Disabled => Color {
a: text_secondary.a * 0.5,
..text_secondary
},
_ => text_secondary,
};
button::Style {
background: None,
text_color,
..button::Style::default()
}
}
fn section_header<'a>(
state: &'a AppState,
key: DashboardSectionKey,
entry_count: usize,
collapsed: bool,
) -> Element<'a, Message> {
let (label, toggle) = match key {
DashboardSectionKey::Tier(tier) => {
let label = match tier {
DashboardTier::NeedsHelp => state.t("tier.needs_attention").to_owned(),
DashboardTier::InProgress => state.t("tier.active").to_owned(),
DashboardTier::AllSet => state.t("tier.clean").to_owned(),
};
let toggle = (tier != DashboardTier::NeedsHelp).then_some(tier);
(label, toggle)
}
DashboardSectionKey::ProjectGroup(Some(group)) => (group, None),
DashboardSectionKey::ProjectGroup(None) => (state.t("group.ungrouped").to_owned(), None),
DashboardSectionKey::Flat => (state.t("dashboard.all_projects").to_owned(), None),
};
let label = format!("{label} ({entry_count})");
if let Some(tier) = toggle {
let tokens = state.theme.tokens.clone();
let focused = is_focused(state, &focus_key(tier));
let chevron = if collapsed {
icon::chevron_right()
} else {
icon::chevron_down()
};
button(centered_button_row(
row![
strong(text(label).size(snora::design::style::text::title_size(&tokens))),
icon::icon_element(&chevron)
]
.spacing(6),
))
.height(Length::Fixed(BUTTON_HEIGHT))
.width(Length::Fill)
.on_press(Message::Dashboard(DashboardMessage::TierToggled(tier)))
.style(move |_theme, status| {
style::with_focus_ring(&tokens, focused, group_toggle_style(&tokens, status))
})
.into()
} else {
container(strong(text(label).size(
snora::design::style::text::title_size(&state.theme.tokens),
)))
.width(Length::Fill)
.padding([5, 8])
.into()
}
}