use iced::{
Alignment, Element, Length,
widget::{Space, button, column, container, row, text},
};
use knotra_ui::widget::{BUTTON_HEIGHT, Tokens, centered_button_label, style};
use crate::{
message::{ContextMessage, FreezerMessage, Message, SelectionMessage, SyncMessage},
state::{
AppState,
focus::{FocusOrder, FocusTarget},
},
view::dashboard::WidthMode,
};
mod focus_target {
pub const FETCH: &str = "dashboard.selection_bar.fetch";
pub const PULL: &str = "dashboard.selection_bar.pull";
pub const TAG: &str = "dashboard.selection_bar.tag";
pub const SWITCH: &str = "dashboard.selection_bar.switch";
pub const EXIT: &str = "dashboard.selection_bar.exit";
}
pub fn focus_order(state: &AppState) -> FocusOrder<Message> {
if !state.selection_mode {
return Vec::new();
}
let summary = state.selection_summary();
let count = summary.selected_count;
let busy = state.operation_interlock.is_busy();
let can_act = count > 0 && !busy;
let fetch_msg = (can_act && !summary.fetchable_ids.is_empty())
.then_some(Message::Sync(SyncMessage::BulkFetchRequested));
let pull_msg =
(can_act && summary.has_upstream).then_some(Message::Sync(SyncMessage::BulkPullRequested));
let tag_msg = can_act.then_some(Message::Freezer(FreezerMessage::BulkOpenRequested));
let switch_msg =
(count == 1 && !busy).then_some(Message::Context(ContextMessage::BulkOpenRequested));
vec![
(FocusTarget::control(focus_target::FETCH), fetch_msg),
(FocusTarget::control(focus_target::PULL), pull_msg),
(FocusTarget::control(focus_target::TAG), tag_msg),
(FocusTarget::control(focus_target::SWITCH), switch_msg),
(
FocusTarget::control(focus_target::EXIT),
Some(Message::Selection(SelectionMessage::ModeExited)),
),
]
}
pub fn view(state: &AppState, mode: WidthMode) -> Option<Element<'_, Message>> {
if !state.selection_mode {
return None;
}
let summary = state.selection_summary();
let count = summary.selected_count;
let label = if count == 0 {
state.t("plain.selection.none").to_owned()
} else {
format!(
"✓ {} {}",
count,
state.t("plain.selection.selected_suffix")
)
};
let busy = state.operation_interlock.is_busy();
let group_reason = if busy {
Some(state.t("plain.activity.busy"))
} else if count == 0 {
Some(state.t("plain.disabled.choose_one"))
} else {
None
};
let can_act = count > 0 && !busy;
let fetch_msg = (can_act && !summary.fetchable_ids.is_empty())
.then_some(Message::Sync(SyncMessage::BulkFetchRequested));
let pull_msg =
(can_act && summary.has_upstream).then_some(Message::Sync(SyncMessage::BulkPullRequested));
let tag_msg = can_act.then_some(Message::Freezer(FreezerMessage::BulkOpenRequested));
let switch_msg =
(count == 1 && !busy).then_some(Message::Context(ContextMessage::BulkOpenRequested));
let mut contextual_reasons: Vec<String> = Vec::new();
if can_act {
if summary.fetchable_ids.is_empty() {
contextual_reasons.push(format!(
"{}: {}",
state.t("plain.check_for_updates"),
state.t("plain.selection.none_fetchable")
));
}
if !summary.has_upstream {
contextual_reasons.push(format!(
"{}: {}",
state.t("plain.get_latest"),
state.t("plain.disabled.no_upstream")
));
}
if count > 1 {
contextual_reasons.push(format!(
"{}: {}",
state.t("plain.change_work_area"),
state.t("plain.selection.choose_one_work_area")
));
}
}
let tokens = &state.theme.tokens;
let fetch_btn = action_button(
tokens,
state.t("plain.check_for_updates"),
fetch_msg,
crate::app::ring(state, &FocusTarget::control(focus_target::FETCH)),
);
let pull_btn = action_button(
tokens,
state.t("plain.get_latest"),
pull_msg,
crate::app::ring(state, &FocusTarget::control(focus_target::PULL)),
);
let tag_btn = action_button(
tokens,
state.t("plain.save_release_point"),
tag_msg,
crate::app::ring(state, &FocusTarget::control(focus_target::TAG)),
);
let switch_btn = action_button(
tokens,
state.t("plain.change_work_area"),
switch_msg,
crate::app::ring(state, &FocusTarget::control(focus_target::SWITCH)),
);
let clear_btn = exit_button(
tokens,
state.t("plain.exit_selection"),
crate::app::ring(state, &FocusTarget::control(focus_target::EXIT)),
);
let command_row = row![
text(label)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
Space::new().width(Length::Fill),
clear_btn,
]
.align_y(Alignment::Center);
let actions: Element<'_, Message> = match mode {
WidthMode::Compact => column![
row![
container(fetch_btn).width(Length::FillPortion(1)),
container(pull_btn).width(Length::FillPortion(1)),
]
.spacing(8)
.align_y(Alignment::Start),
row![
container(tag_btn).width(Length::FillPortion(1)),
container(switch_btn).width(Length::FillPortion(1)),
]
.spacing(8)
.align_y(Alignment::Start),
]
.spacing(8)
.into(),
WidthMode::Standard | WidthMode::Wide => row![
container(fetch_btn).width(Length::FillPortion(1)),
container(pull_btn).width(Length::FillPortion(1)),
container(tag_btn).width(Length::FillPortion(1)),
container(switch_btn).width(Length::FillPortion(1)),
]
.spacing(8)
.align_y(Alignment::Start)
.into(),
};
let mut content = column![command_row].spacing(6);
if let Some(reason) = group_reason {
content = content.push(
text(reason)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
);
}
content = content.push(actions);
for reason in contextual_reasons {
content = content.push(
text(reason)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
);
}
let bar = container(content.padding([6, 12])).width(Length::Fill);
Some(bar.into())
}
fn action_button<'a>(
tokens: &Tokens,
label: &'a str,
on_press: Option<Message>,
is_focused: bool,
) -> Element<'a, Message> {
let t = tokens.clone();
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, 18])
.on_press_maybe(on_press)
.style(move |_theme, status| {
style::with_focus_ring(&t, is_focused, style::secondary(&t, status))
})
.into()
}
fn exit_button<'a>(tokens: &Tokens, label: &'a str, is_focused: bool) -> Element<'a, Message> {
let t = tokens.clone();
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)
.on_press(Message::Selection(SelectionMessage::ModeExited))
.style(move |_theme, status| style::with_focus_ring(&t, is_focused, style::ghost(&t, status)))
.into()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::AppConfig;
use crate::state::AppState;
#[test]
fn focus_order_is_empty_when_not_in_selection_mode() {
let state = AppState::new(AppConfig::default());
assert!(!state.selection_mode);
assert!(focus_order(&state).is_empty());
}
#[test]
fn focus_order_has_five_targets_with_only_exit_active_when_nothing_selected() {
let mut state = AppState::new(AppConfig::default());
state.selection_mode = true;
let order = focus_order(&state);
assert_eq!(order.len(), 5);
let live: Vec<_> = order
.iter()
.filter(|(_, message)| message.is_some())
.collect();
assert_eq!(live.len(), 1);
assert_eq!(live[0].0, FocusTarget::control(focus_target::EXIT));
}
}