use iced::Task;
use crate::{
message::{ChangelogMessage, ContextMessage, FreezerMessage, Message, SyncMessage},
state::{
AppState, Screen, focus,
freezer::FreezerPhase,
layer::{self, Layer},
sync::SyncPhase,
},
view::{
add_project_modal, command_palette, dashboard,
overlays::{
changelog_layer_focus, conflict_close_message, conflict_layer_focus,
context_switch_layer_focus, freezer_layer_focus, smart_pull_layer_focus,
},
remove_project_dialog, selection_bar, shell, shortcuts_overlay, workspace_manager,
},
};
pub(super) fn overlay_focus_order(state: &AppState) -> Option<focus::FocusOrder<Message>> {
layer::top_layer(state).map(|l| layer_focus(state, &l).order)
}
pub(super) fn layer_focus(state: &AppState, layer: &Layer) -> focus::LayerFocus<Message> {
match layer {
Layer::WorkspaceCreate | Layer::WorkspaceRename | Layer::WorkspaceDelete => {
workspace_manager::layer_focus(state)
}
Layer::Palette => command_palette::layer_focus(state),
Layer::AddProject => add_project_modal::layer_focus(state),
Layer::SaveReleasePoint => freezer_layer_focus(state),
Layer::Resolve(id) => conflict_layer_focus(state, id),
Layer::GenerateNotes => changelog_layer_focus(state),
Layer::ChangeWorkArea => context_switch_layer_focus(state),
Layer::GetLatest => smart_pull_layer_focus(state),
Layer::WorkspaceMenu => shell::switcher_menu_layer_focus(state),
Layer::ShortcutsSheet => shortcuts_overlay::layer_focus(),
Layer::RemoveProject => remove_project_dialog::layer_focus(state),
}
}
pub(super) fn advance_focus(state: &mut AppState, direction: focus::Direction) -> Task<Message> {
if let Some(order) = overlay_focus_order(state) {
advance_in(&order, &mut state.overlay_focus, direction)
} else {
let order = shell_and_dashboard_focus_order(state);
advance_in(&order, &mut state.dashboard_focus, direction)
}
}
pub(super) fn activate_focused(state: &mut AppState) -> Task<Message> {
if let Some(order) = overlay_focus_order(state) {
activate_in(&order, &state.overlay_focus)
} else {
let order = shell_and_dashboard_focus_order(state);
activate_in(&order, &state.dashboard_focus)
}
}
pub(super) fn advance_card_focus(
state: &mut AppState,
direction: focus::Direction,
) -> Task<Message> {
if overlay_focus_order(state).is_some() || !matches!(state.screen, Screen::Dashboard) {
return Task::none();
}
let order = dashboard::card_focus_order(state);
if order.is_empty() {
return Task::none();
}
let previous = state.dashboard_focus.clone();
let already_on_a_card = previous
.as_ref()
.is_some_and(|target| order.iter().any(|(t, _)| t == target));
let next = if already_on_a_card {
focus::advance(&order, previous.as_ref(), direction).cloned()
} else {
order.first().map(|(target, _)| target.clone())
};
state.dashboard_focus = next.clone();
reconciliation_task(focus::reconcile(previous.as_ref(), next.as_ref()))
}
fn shell_and_dashboard_focus_order(state: &AppState) -> focus::FocusOrder<Message> {
let mut order = shell::focus_order(state);
if matches!(state.screen, Screen::Dashboard) {
order.extend(dashboard::focus_order(state));
order.extend(selection_bar::focus_order(state));
}
order
}
pub(crate) fn ring(state: &AppState, target: &focus::FocusTarget) -> bool {
if !state.focus_visible {
return false;
}
match layer::top_layer(state) {
Some(layer) => {
let lf = layer_focus(state, &layer);
focus::resolve(&lf.order, state.overlay_focus.as_ref()) == Some(target)
}
None => {
let order = shell_and_dashboard_focus_order(state);
focus::resolve(&order, state.dashboard_focus.as_ref()) == Some(target)
}
}
}
fn advance_in(
order: &focus::FocusOrder<Message>,
current: &mut Option<focus::FocusTarget>,
direction: focus::Direction,
) -> Task<Message> {
let previous = current.clone();
let next = focus::advance(order, previous.as_ref(), direction).cloned();
*current = next.clone();
reconciliation_task(focus::reconcile(previous.as_ref(), next.as_ref()))
}
fn activate_in(
order: &focus::FocusOrder<Message>,
current: &Option<focus::FocusTarget>,
) -> Task<Message> {
if focus::is_text_input_focused(order, current.as_ref()) {
return Task::none();
}
focus::activation_message(order, current.as_ref())
.map(Task::done)
.unwrap_or_else(Task::none)
}
fn reconciliation_task(reconciliation: focus::Reconciliation) -> Task<Message> {
match reconciliation {
focus::Reconciliation::None => Task::none(),
focus::Reconciliation::FocusTextInput(id) => knotra_ui::widget::focus_input(&id),
focus::Reconciliation::ClearTextInputFocus => knotra_ui::widget::clear_input_focus(),
}
}
pub(super) fn open_overlay_focus(
state: &mut AppState,
target: focus::FocusTarget,
) -> Task<Message> {
let previous = state.overlay_focus.replace(target.clone());
reconciliation_task(focus::reconcile(previous.as_ref(), Some(&target)))
}
pub(super) fn close_overlay_focus(state: &mut AppState) -> Task<Message> {
let previous = state.overlay_focus.take();
reconciliation_task(focus::reconcile(previous.as_ref(), None))
}
pub(super) fn focus_search(state: &mut AppState) -> Task<Message> {
state.screen = Screen::Dashboard;
let target = focus::FocusTarget::text_input(knotra_ui::widget::focus_id::SEARCH.clone());
let previous = state.dashboard_focus.replace(target.clone());
reconciliation_task(focus::reconcile(previous.as_ref(), Some(&target)))
}
pub(super) fn current_target_is_text_input(state: &AppState) -> bool {
let current = if overlay_focus_order(state).is_some() {
&state.overlay_focus
} else {
&state.dashboard_focus
};
matches!(current, Some(focus::FocusTarget::TextInput(_)))
}
pub(super) fn close_topmost_layer(state: &mut AppState) -> Task<Message> {
use crate::message::{PaletteMessage, WorkspaceMessage};
match layer::top_layer(state) {
None => {
if state.selection_mode {
state.clear_selection_mode();
}
Task::none()
}
Some(Layer::ShortcutsSheet) => {
state.keyboard.cheat_sheet_open = false;
Task::none()
}
Some(Layer::Palette) => super::misc::handle_palette(state, PaletteMessage::Closed),
Some(Layer::AddProject) => {
super::workspace::handle_workspace(state, WorkspaceMessage::AddProjectCancelled)
}
Some(Layer::WorkspaceCreate) => {
super::workspace::handle_workspace(state, WorkspaceMessage::CreateWorkspaceCancelled)
}
Some(Layer::WorkspaceRename) => {
super::workspace::handle_workspace(state, WorkspaceMessage::RenameWorkspaceCancelled)
}
Some(Layer::WorkspaceDelete) => {
super::workspace::handle_workspace(state, WorkspaceMessage::DeleteWorkspaceCancelled)
}
Some(Layer::GetLatest) => super::sync::handle_sync(state, SyncMessage::ModalClosed),
Some(Layer::SaveReleasePoint) => {
super::freezer::handle_freezer(state, FreezerMessage::BulkModalClosed)
}
Some(Layer::ChangeWorkArea) => {
super::context::handle_context(state, ContextMessage::BulkModalClosed)
}
Some(Layer::GenerateNotes) => {
super::changelog::handle_changelog(state, ChangelogMessage::ModalClosed)
}
Some(Layer::Resolve(id)) => match conflict_close_message(state, &id) {
Some(msg) => super::conflict_ops::handle_conflict_ops(state, msg),
None => Task::none(),
},
Some(Layer::WorkspaceMenu) => {
super::workspace::handle_workspace(state, WorkspaceMessage::SwitcherToggled)
}
Some(Layer::RemoveProject) => {
super::workspace::handle_workspace(state, WorkspaceMessage::RemoveProjectCancelled)
}
}
}
pub(super) fn settle_layer_focus(state: &mut AppState, before: Option<Layer>) -> Task<Message> {
let Some(current) = layer::top_layer(state) else {
return close_overlay_focus(state);
};
let lf = layer_focus(state, ¤t);
let held_is_stale = match &state.overlay_focus {
None => true,
Some(f) => !lf.order.iter().any(|(t, _)| t == f),
};
let moves_to_entry =
before.as_ref() != Some(¤t) || (!lf.order.is_empty() && held_is_stale);
if moves_to_entry {
match lf.entry {
Some(target) => open_overlay_focus(state, target),
None => close_overlay_focus(state),
}
} else {
Task::none()
}
}
pub(super) fn smart_pull_is_running(state: &AppState) -> bool {
matches!(state.active_modal, crate::state::ActiveModal::Pull)
&& matches!(state.sync.phase, SyncPhase::PullRunning { .. })
}
pub(super) fn freezer_is_running(state: &AppState) -> bool {
matches!(state.freezer.phase, FreezerPhase::Executing)
|| state
.pending_tag_push
.as_ref()
.is_some_and(|push| push.is_pushing)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::AppConfig;
use knotra_vcs::{Project, Workspace};
fn workspace_with_projects(names: &[&str]) -> Workspace {
Workspace {
projects: names.iter().map(|n| Project::new(*n, "/tmp")).collect(),
..Workspace::new("Test")
}
}
#[test]
fn advance_card_focus_moves_through_row_name_targets_only_and_wraps() {
let mut state = AppState::new(AppConfig::default());
state.workspace = Some(workspace_with_projects(&["alpha", "beta", "gamma"]));
let order = dashboard::card_focus_order(&state);
assert_eq!(
order.len(),
3,
"all three NeedsHelp rows should be card targets, nothing else"
);
for target in &order {
let _ = advance_card_focus(&mut state, focus::Direction::Next);
assert_eq!(state.dashboard_focus, Some(target.0.clone()));
}
let _ = advance_card_focus(&mut state, focus::Direction::Next);
assert_eq!(state.dashboard_focus, Some(order[0].0.clone()));
}
#[test]
fn advance_card_focus_from_off_card_focus_lands_on_the_first_card_either_direction() {
let mut state = AppState::new(AppConfig::default());
state.workspace = Some(workspace_with_projects(&["alpha", "beta"]));
let order = dashboard::card_focus_order(&state);
state.dashboard_focus = Some(focus::FocusTarget::control("dashboard.toolbar.select_mode"));
let _ = advance_card_focus(&mut state, focus::Direction::Previous);
assert_eq!(
state.dashboard_focus,
Some(order[0].0.clone()),
"Previous from off-card focus must not land on the second card \
the way resolve-then-advance's stale-target fallback would"
);
state.dashboard_focus = Some(focus::FocusTarget::control("dashboard.toolbar.select_mode"));
let _ = advance_card_focus(&mut state, focus::Direction::Next);
assert_eq!(state.dashboard_focus, Some(order[0].0.clone()));
}
#[test]
fn advance_card_focus_is_a_noop_off_the_dashboard_screen() {
let mut state = AppState::new(AppConfig::default());
state.workspace = Some(workspace_with_projects(&["alpha"]));
state.screen = Screen::History;
let _ = advance_card_focus(&mut state, focus::Direction::Next);
assert_eq!(state.dashboard_focus, None);
}
}