use iced::widget::{Space, button, column, container, row, text, text_input};
use iced::{Alignment, Element, Length, Padding};
use knotra_ui::widget::{BUTTON_HEIGHT, FONT_BODY, FONT_SMALL, Tokens, chip, select, style};
use crate::{
config::{DashboardGrouping, DashboardSort},
message::{DashboardMessage, FilterMessage, Message, SelectionMessage, StatusFilter},
state::{
AppState, SelectionSummary,
focus::{FocusOrder, FocusTarget},
},
};
use super::width_mode::WidthMode;
mod focus_target {
pub const FILTER_NEEDS_HELP: &str = "dashboard.toolbar.filter.needs_help";
pub const FILTER_DIRTY: &str = "dashboard.toolbar.filter.dirty";
pub const FILTER_BEHIND: &str = "dashboard.toolbar.filter.behind";
pub const FILTER_AHEAD: &str = "dashboard.toolbar.filter.ahead";
pub const FILTER_CONFLICT: &str = "dashboard.toolbar.filter.conflict";
pub const FILTER_ALL_SET: &str = "dashboard.toolbar.filter.all_set";
pub const GROUP_SELECT: &str = "dashboard.toolbar.group";
pub const SORT_SELECT: &str = "dashboard.toolbar.sort";
pub const CLEAR_FILTERS: &str = "dashboard.toolbar.clear_filters";
pub const SELECT_MODE: &str = "dashboard.toolbar.select_mode";
pub const TOOLBAR_OVERFLOW: &str = "dashboard.toolbar.overflow";
pub const TOOLBAR_SELECTORS: &str = "dashboard.toolbar.selectors_disclosure";
}
const FILTERS: [StatusFilter; 6] = [
StatusFilter::NeedsHelp,
StatusFilter::Dirty,
StatusFilter::Behind,
StatusFilter::Ahead,
StatusFilter::Conflict,
StatusFilter::AllSet,
];
const COMPACT_VISIBLE_CHIP_COUNT: usize = 3;
fn filter_focus_key(filter: &StatusFilter) -> &'static str {
match filter {
StatusFilter::NeedsHelp => focus_target::FILTER_NEEDS_HELP,
StatusFilter::Dirty => focus_target::FILTER_DIRTY,
StatusFilter::Behind => focus_target::FILTER_BEHIND,
StatusFilter::Ahead => focus_target::FILTER_AHEAD,
StatusFilter::Conflict => focus_target::FILTER_CONFLICT,
StatusFilter::AllSet => focus_target::FILTER_ALL_SET,
}
}
fn is_focused(state: &AppState, key: &'static str) -> bool {
state.dashboard_focus.as_ref() == Some(&FocusTarget::control(key))
}
fn chip_element<'a>(
state: &'a AppState,
tokens: &Tokens,
filter: &StatusFilter,
) -> Element<'a, Message> {
let active = state.filter.has_status_filter(filter);
chip::filter(
tokens,
state.t(filter.label_key()),
active,
is_focused(state, filter_focus_key(filter)),
Some(Message::Filter(FilterMessage::StatusFilterToggled(
filter.clone(),
))),
)
}
fn select_mode_reason(state: &AppState, summary: &SelectionSummary) -> Option<&'static str> {
if !summary.visible_ids.is_empty() {
return None;
}
let no_projects_registered = state
.workspace
.as_ref()
.is_none_or(|workspace| workspace.projects.is_empty());
Some(if no_projects_registered {
state.t("plain.selection.no_projects_registered")
} else {
state.t("plain.selection.no_visible_projects")
})
}
pub(super) fn focus_order(state: &AppState, mode: WidthMode) -> FocusOrder<Message> {
match mode {
WidthMode::Compact => compact_focus_order(state),
WidthMode::Standard | WidthMode::Wide => standard_focus_order(state),
}
}
fn standard_focus_order(state: &AppState) -> FocusOrder<Message> {
let mut order: FocusOrder<Message> = FILTERS
.iter()
.map(|filter| {
(
FocusTarget::control(filter_focus_key(filter)),
Some(Message::Filter(FilterMessage::StatusFilterToggled(
filter.clone(),
))),
)
})
.collect();
order.push((FocusTarget::control(focus_target::GROUP_SELECT), None));
order.push((FocusTarget::control(focus_target::SORT_SELECT), None));
order.push((
FocusTarget::text_input(knotra_ui::widget::focus_id::SEARCH.clone()),
None,
));
if state.filter.is_active() {
order.push((
FocusTarget::control(focus_target::CLEAR_FILTERS),
Some(Message::Filter(FilterMessage::AllFiltersCleared)),
));
}
let summary = state.selection_summary();
let select_message = (!summary.visible_ids.is_empty())
.then_some(Message::Selection(SelectionMessage::ModeEntered));
order.push((
FocusTarget::control(focus_target::SELECT_MODE),
select_message,
));
order
}
fn compact_focus_order(state: &AppState) -> FocusOrder<Message> {
let mut order: FocusOrder<Message> = FILTERS[..COMPACT_VISIBLE_CHIP_COUNT]
.iter()
.map(|filter| {
(
FocusTarget::control(filter_focus_key(filter)),
Some(Message::Filter(FilterMessage::StatusFilterToggled(
filter.clone(),
))),
)
})
.collect();
order.push((
FocusTarget::control(focus_target::TOOLBAR_OVERFLOW),
Some(Message::Dashboard(DashboardMessage::ToolbarOverflowToggled)),
));
if state.dashboard_toolbar_overflow_open {
order.extend(FILTERS[COMPACT_VISIBLE_CHIP_COUNT..].iter().map(|filter| {
(
FocusTarget::control(filter_focus_key(filter)),
Some(Message::Filter(FilterMessage::StatusFilterToggled(
filter.clone(),
))),
)
}));
}
order.push((
FocusTarget::text_input(knotra_ui::widget::focus_id::SEARCH.clone()),
None,
));
order.push((
FocusTarget::control(focus_target::TOOLBAR_SELECTORS),
Some(Message::Dashboard(
DashboardMessage::ToolbarSelectorsToggled,
)),
));
if state.dashboard_toolbar_selectors_open {
order.push((FocusTarget::control(focus_target::GROUP_SELECT), None));
order.push((FocusTarget::control(focus_target::SORT_SELECT), None));
}
if state.filter.is_active() {
order.push((
FocusTarget::control(focus_target::CLEAR_FILTERS),
Some(Message::Filter(FilterMessage::AllFiltersCleared)),
));
}
let summary = state.selection_summary();
let select_message = (!summary.visible_ids.is_empty())
.then_some(Message::Selection(SelectionMessage::ModeEntered));
order.push((
FocusTarget::control(focus_target::SELECT_MODE),
select_message,
));
order
}
pub(super) fn view_toolbar(state: &AppState, mode: WidthMode) -> Element<'_, Message> {
match mode {
WidthMode::Compact => view_compact_toolbar(state),
WidthMode::Standard | WidthMode::Wide => view_standard_toolbar(state),
}
}
fn view_standard_toolbar(state: &AppState) -> Element<'_, Message> {
let tokens = &state.theme.tokens;
let chips = row(FILTERS
.iter()
.map(|filter| chip_element(state, tokens, filter))
.collect::<Vec<_>>())
.spacing(4);
let grouping_options = vec![
(
DashboardGrouping::Attention,
state.t("dashboard.grouping.attention").to_owned(),
),
(
DashboardGrouping::ProjectGroup,
state.t("dashboard.grouping.project_group").to_owned(),
),
(
DashboardGrouping::None,
state.t("dashboard.grouping.none").to_owned(),
),
];
let grouping = row![
text(state.t("dashboard.grouping")).size(12),
select::pick_list(
tokens,
grouping_options,
Some(state.config.dashboard_grouping),
is_focused(state, focus_target::GROUP_SELECT),
|value| Message::Dashboard(DashboardMessage::GroupingChanged(value)),
),
]
.spacing(4)
.align_y(Alignment::Center);
let sorting_options = vec![
(
DashboardSort::Recommended,
state.t("dashboard.sorting.recommended").to_owned(),
),
(
DashboardSort::NameAscending,
state.t("dashboard.sorting.name").to_owned(),
),
];
let sorting = row![
text(state.t("dashboard.sorting")).size(12),
select::pick_list(
tokens,
sorting_options,
Some(state.config.dashboard_sort),
is_focused(state, focus_target::SORT_SELECT),
|value| Message::Dashboard(DashboardMessage::SortChanged(value)),
),
]
.spacing(4)
.align_y(Alignment::Center);
let search = text_input(
state.t("dashboard.search_placeholder"),
&state.filter.search_text,
)
.id(knotra_ui::widget::focus_id::SEARCH.clone())
.on_input(|value| Message::Filter(FilterMessage::SearchChanged(value)))
.width(Length::Fixed(220.0));
let clear: Element<'_, Message> = if state.filter.is_active() {
clear_filters_button(
tokens,
state.t("dashboard.clear_filters"),
is_focused(state, focus_target::CLEAR_FILTERS),
)
} else {
Space::new().width(Length::Shrink).into()
};
let summary = state.selection_summary();
let select_message = (!summary.visible_ids.is_empty())
.then_some(Message::Selection(SelectionMessage::ModeEntered));
let select = select_mode_button(
tokens,
state.t("plain.selection.enter"),
select_message,
select_mode_reason(state, &summary),
is_focused(state, focus_target::SELECT_MODE),
);
container(
column![
chips,
row![grouping, sorting, search, clear, select]
.spacing(6)
.align_y(Alignment::Center),
]
.spacing(5),
)
.width(Length::Fill)
.padding(Padding {
top: 0.0,
right: 12.0,
bottom: 8.0,
left: 12.0,
})
.into()
}
fn view_compact_toolbar(state: &AppState) -> Element<'_, Message> {
let tokens = &state.theme.tokens;
let visible_chips = row(FILTERS[..COMPACT_VISIBLE_CHIP_COUNT]
.iter()
.map(|filter| chip_element(state, tokens, filter))
.collect::<Vec<_>>())
.spacing(4);
let overflow_button = disclosure_button(
tokens,
"⋯",
Message::Dashboard(DashboardMessage::ToolbarOverflowToggled),
is_focused(state, focus_target::TOOLBAR_OVERFLOW),
);
let mut chip_area = column![
row![visible_chips, overflow_button]
.spacing(4)
.align_y(Alignment::Center)
]
.spacing(4);
if state.dashboard_toolbar_overflow_open {
let overflow_chips = row(FILTERS[COMPACT_VISIBLE_CHIP_COUNT..]
.iter()
.map(|filter| chip_element(state, tokens, filter))
.collect::<Vec<_>>())
.spacing(4);
chip_area = chip_area.push(overflow_chips);
}
let search = text_input(
state.t("dashboard.search_placeholder"),
&state.filter.search_text,
)
.id(knotra_ui::widget::focus_id::SEARCH.clone())
.on_input(|value| Message::Filter(FilterMessage::SearchChanged(value)))
.width(Length::Fixed(220.0));
let selectors_button = disclosure_button(
tokens,
"▾",
Message::Dashboard(DashboardMessage::ToolbarSelectorsToggled),
is_focused(state, focus_target::TOOLBAR_SELECTORS),
);
let summary = state.selection_summary();
let select_message = (!summary.visible_ids.is_empty())
.then_some(Message::Selection(SelectionMessage::ModeEntered));
let select = select_mode_button(
tokens,
state.t("plain.selection.enter"),
select_message,
select_mode_reason(state, &summary),
is_focused(state, focus_target::SELECT_MODE),
);
let mut controls = column![
row![search, selectors_button, select]
.spacing(6)
.align_y(Alignment::Center)
]
.spacing(5);
if state.dashboard_toolbar_selectors_open {
controls = controls.push(compact_selectors_row(state, tokens));
}
if state.filter.is_active() {
controls = controls.push(clear_filters_button(
tokens,
state.t("dashboard.clear_filters"),
is_focused(state, focus_target::CLEAR_FILTERS),
));
}
container(column![chip_area, controls].spacing(5))
.width(Length::Fill)
.padding(Padding {
top: 0.0,
right: 12.0,
bottom: 8.0,
left: 12.0,
})
.into()
}
fn compact_selectors_row<'a>(state: &'a AppState, tokens: &Tokens) -> Element<'a, Message> {
let grouping_options = vec![
(
DashboardGrouping::Attention,
state.t("dashboard.grouping.attention").to_owned(),
),
(
DashboardGrouping::ProjectGroup,
state.t("dashboard.grouping.project_group").to_owned(),
),
(
DashboardGrouping::None,
state.t("dashboard.grouping.none").to_owned(),
),
];
let grouping = row![
text(state.t("dashboard.grouping")).size(12),
select::pick_list(
tokens,
grouping_options,
Some(state.config.dashboard_grouping),
is_focused(state, focus_target::GROUP_SELECT),
|value| Message::Dashboard(DashboardMessage::GroupingChanged(value)),
),
]
.spacing(4)
.align_y(Alignment::Center);
let sorting_options = vec![
(
DashboardSort::Recommended,
state.t("dashboard.sorting.recommended").to_owned(),
),
(
DashboardSort::NameAscending,
state.t("dashboard.sorting.name").to_owned(),
),
];
let sorting = row![
text(state.t("dashboard.sorting")).size(12),
select::pick_list(
tokens,
sorting_options,
Some(state.config.dashboard_sort),
is_focused(state, focus_target::SORT_SELECT),
|value| Message::Dashboard(DashboardMessage::SortChanged(value)),
),
]
.spacing(4)
.align_y(Alignment::Center);
row![grouping, sorting]
.spacing(6)
.align_y(Alignment::Center)
.into()
}
fn select_mode_button<'a>(
tokens: &Tokens,
label: &'a str,
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(FONT_BODY))
.height(BUTTON_HEIGHT)
.padding([0, 18])
.on_press_maybe(on_press)
.style(move |_theme, status| {
style::with_focus_ring(&t, is_focused, style::secondary(&t, status))
});
match reason {
Some(r) if show_reason => column![btn, text(r).size(FONT_SMALL)].spacing(6).into(),
_ => btn.into(),
}
}
fn clear_filters_button<'a>(
tokens: &Tokens,
label: &'a str,
is_focused: bool,
) -> Element<'a, Message> {
let t = tokens.clone();
button(text(label).size(12))
.on_press(Message::Filter(FilterMessage::AllFiltersCleared))
.style(move |_theme, status| {
style::with_focus_ring(&t, is_focused, style::secondary(&t, status))
})
.into()
}
fn disclosure_button<'a>(
tokens: &Tokens,
glyph: &'a str,
on_press: Message,
is_focused: bool,
) -> Element<'a, Message> {
let t = tokens.clone();
button(text(glyph).size(FONT_BODY))
.height(BUTTON_HEIGHT)
.padding([0, 12])
.on_press(on_press)
.style(move |_theme, status| {
style::with_focus_ring(&t, is_focused, style::ghost(&t, status))
})
.into()
}