use iced::{
Alignment, Element, Length,
widget::{Space, button, column, container, row, text},
};
use knotra_ui::widget::{self, FONT_BODY, FONT_SMALL, Tokens, icon, icon_button_maybe};
use crate::{
message::{Message, WorkspaceMessage},
state::{
AppState, Screen,
focus::{FocusOrder, FocusTarget},
},
};
mod focus_target {
pub const WORKSPACE_SWITCHER: &str = "shell.workspace_switcher";
pub const NAV_DASHBOARD: &str = "shell.nav.dashboard";
pub const NAV_HISTORY: &str = "shell.nav.history";
pub const REFRESH: &str = "shell.refresh";
pub const PALETTE: &str = "shell.palette";
pub const SETTINGS: &str = "shell.settings";
}
pub fn focus_order(state: &AppState) -> FocusOrder<Message> {
let dashboard_active = matches!(state.screen, Screen::Dashboard);
let history_active = matches!(state.screen, Screen::History);
vec![
(
FocusTarget::control(focus_target::WORKSPACE_SWITCHER),
Some(Message::Workspace(WorkspaceMessage::SwitcherToggled)),
),
(
FocusTarget::control(focus_target::NAV_DASHBOARD),
(!dashboard_active).then_some(Message::Navigate(Screen::Dashboard)),
),
(
FocusTarget::control(focus_target::NAV_HISTORY),
(!history_active).then_some(Message::Navigate(Screen::History)),
),
(
FocusTarget::control(focus_target::REFRESH),
(!state.is_refreshing)
.then_some(Message::Workspace(WorkspaceMessage::RefreshRequested)),
),
(
FocusTarget::control(focus_target::PALETTE),
Some(Message::Palette(crate::message::PaletteMessage::Opened)),
),
(
FocusTarget::control(focus_target::SETTINGS),
Some(Message::Navigate(Screen::Settings)),
),
]
}
fn is_focused(state: &AppState, key: &'static str) -> bool {
state.dashboard_focus.as_ref() == Some(&FocusTarget::control(key))
}
const SHELL_HEIGHT: f32 = 52.0;
const SWITCHER_WIDTH: f32 = 260.0;
pub fn view(state: &AppState) -> Element<'_, Message> {
let tokens = &state.theme.tokens;
let attention = state
.dashboard_display()
.sections
.iter()
.flat_map(|section| section.entries.iter())
.filter(|entry| entry.tier == crate::state::dashboard::DashboardTier::NeedsHelp)
.count();
let workspace_name = state
.workspace
.as_ref()
.map(|ws| ws.name.as_str())
.unwrap_or_else(|| state.t("dashboard.no_workspace"));
let switcher_label = if attention > 0 {
format!("{workspace_name} ({attention})")
} else {
workspace_name.to_owned()
};
let switcher_trigger = button(
row![
text(switcher_label).size(FONT_BODY),
widget::icon::icon_element(&icon::chevron_down()),
]
.spacing(6)
.align_y(Alignment::Center),
)
.on_press(Message::Workspace(WorkspaceMessage::SwitcherToggled))
.style({
let t = tokens.clone();
let focused = is_focused(state, focus_target::WORKSPACE_SWITCHER);
move |_theme, status| {
widget::style::with_focus_ring(&t, focused, widget::style::ghost(&t, status))
}
});
let dashboard_active = matches!(state.screen, Screen::Dashboard);
let history_active = matches!(state.screen, Screen::History);
let nav_button = |label: &str, active: bool, target: Screen, tokens: &Tokens, focused: bool| {
let t = tokens.clone();
button(text(label.to_owned()).size(FONT_BODY))
.on_press_maybe((!active).then_some(Message::Navigate(target)))
.style(move |_theme, status| {
widget::current_or(active, &t, status, focused)
})
};
let status_text: Element<'_, Message> = if state.is_refreshing {
text(state.t("plain.status.checking"))
.size(FONT_SMALL)
.into()
} else {
Space::new().width(Length::Shrink).into()
};
let refresh_button = icon_button_maybe(
tokens,
&icon::refresh(),
state.t("plain.check_now"),
(!state.is_refreshing).then_some(Message::Workspace(WorkspaceMessage::RefreshRequested)),
is_focused(state, focus_target::REFRESH),
);
let palette_button = icon_button_maybe(
tokens,
&icon::command_palette(),
state.t("palette.title"),
Some(Message::Palette(crate::message::PaletteMessage::Opened)),
is_focused(state, focus_target::PALETTE),
);
let settings_button = icon_button_maybe(
tokens,
&icon::settings(),
state.t("nav.settings"),
Some(Message::Navigate(Screen::Settings)),
is_focused(state, focus_target::SETTINGS),
);
let bar = row![
switcher_trigger,
nav_button(
state.t("nav.dashboard"),
dashboard_active,
Screen::Dashboard,
tokens,
is_focused(state, focus_target::NAV_DASHBOARD)
),
nav_button(
state.t("nav.history"),
history_active,
Screen::History,
tokens,
is_focused(state, focus_target::NAV_HISTORY)
),
Space::new().width(Length::Fill),
status_text,
refresh_button,
palette_button,
settings_button,
]
.spacing(8)
.align_y(Alignment::Center)
.padding([0, 12]);
container(bar)
.width(Length::Fill)
.height(Length::Fixed(SHELL_HEIGHT))
.into()
}
pub fn switcher_menu(state: &AppState) -> Option<Element<'_, Message>> {
if !state.workspace_mgr.switcher_open {
return None;
}
let tokens = &state.theme.tokens;
let active_id = state
.all_workspaces
.get(state.active_workspace_idx)
.map(|ws| ws.id.clone());
let mut items = column![].spacing(2);
for ws in &state.all_workspaces {
let is_active = Some(&ws.id) == active_id.as_ref();
let t = tokens.clone();
items = items.push(
button(text(ws.name.clone()).size(FONT_BODY))
.width(Length::Fill)
.on_press_maybe((!is_active).then_some(Message::Workspace(
WorkspaceMessage::WorkspaceSwitched(ws.id.clone()),
)))
.style(move |_theme, status| widget::style::ghost(&t, status)),
);
}
let can_delete = state.all_workspaces.len() > 1;
let menu_action = |label: &str, msg: Message, tokens: &Tokens| {
let t = tokens.clone();
button(text(label.to_owned()).size(FONT_BODY))
.width(Length::Fill)
.on_press(msg)
.style(move |_theme, status| widget::style::ghost(&t, status))
};
let content = column![
items,
menu_action(
state.t("plain.add_workspace"),
Message::Workspace(WorkspaceMessage::CreateWorkspaceDialogOpened),
tokens
),
menu_action(
state.t("workspace.rename.short"),
Message::Workspace(WorkspaceMessage::RenameWorkspaceDialogOpened),
tokens
),
danger_menu_action(
state.t("workspace.delete.short"),
can_delete.then_some(Message::Workspace(
WorkspaceMessage::DeleteWorkspaceRequested
)),
tokens,
),
]
.spacing(2)
.width(Length::Fixed(SWITCHER_WIDTH));
let surface = widget::overlay::raised_card(tokens, content);
Some(
container(surface)
.padding(iced::Padding {
top: SHELL_HEIGHT + 4.0,
left: 8.0,
..iced::Padding::ZERO
})
.into(),
)
}
fn danger_menu_action<'a>(
label: &'a str,
on_press: Option<Message>,
tokens: &Tokens,
) -> Element<'a, Message> {
let t = tokens.clone();
button(text(label).size(FONT_BODY))
.width(Length::Fill)
.on_press_maybe(on_press)
.style(move |_theme, status| widget::style::danger(&t, status))
.into()
}
pub fn page_header<'a>(
title: impl Into<String>,
actions: impl Into<Element<'a, Message>>,
) -> Element<'a, Message> {
row![
text(title.into()).size(20),
Space::new().width(Length::Fill),
actions.into(),
]
.align_y(Alignment::Center)
.padding([8, 14])
.into()
}