use iced::{
Alignment, Element, Length,
widget::{Space, button, column, container, row, text},
};
use knotra_ui::widget::{
self, BUTTON_HEIGHT, Tokens, centered_button_label, centered_button_row, icon,
icon_button_maybe,
};
use crate::{
message::{Message, WorkspaceMessage},
state::{
AppState, Screen,
focus::{FocusOrder, FocusTarget, LayerFocus},
},
};
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 const ADD_WORKSPACE: &str = "shell.switcher.add_workspace";
pub const RENAME: &str = "shell.switcher.rename";
pub const DELETE: &str = "shell.switcher.delete";
}
pub fn switcher_menu_layer_focus(state: &AppState) -> LayerFocus<Message> {
let active_id = state
.all_workspaces
.get(state.active_workspace_idx)
.map(|ws| ws.id.clone());
let mut order: FocusOrder<Message> = Vec::new();
let mut first_workspace = None;
for ws in &state.all_workspaces {
let is_active = Some(&ws.id) == active_id.as_ref();
let target = FocusTarget::control_dynamic(format!("shell.switcher.workspace.{}", ws.id));
if first_workspace.is_none() {
first_workspace = Some(target.clone());
}
order.push((
target,
(!is_active).then_some(Message::Workspace(WorkspaceMessage::WorkspaceSwitched(
ws.id.clone(),
))),
));
}
let can_delete = state.all_workspaces.len() > 1;
order.push((
FocusTarget::control(focus_target::ADD_WORKSPACE),
Some(Message::Workspace(
WorkspaceMessage::CreateWorkspaceDialogOpened,
)),
));
order.push((
FocusTarget::control(focus_target::RENAME),
Some(Message::Workspace(
WorkspaceMessage::RenameWorkspaceDialogOpened,
)),
));
order.push((
FocusTarget::control(focus_target::DELETE),
can_delete.then_some(Message::Workspace(
WorkspaceMessage::DeleteWorkspaceRequested,
)),
));
LayerFocus {
entry: first_workspace.or_else(|| order.first().map(|(t, _)| t.clone())),
order,
}
}
pub fn focus_order(state: &AppState) -> FocusOrder<Message> {
let dashboard_active = matches!(state.screen, Screen::Dashboard);
let history_active = matches!(state.screen, Screen::History);
let mut order: FocusOrder<Message> = Vec::new();
if state.all_workspaces.len() > 1 {
order.push((
FocusTarget::control(focus_target::WORKSPACE_SWITCHER),
Some(Message::Workspace(WorkspaceMessage::SwitcherToggled)),
));
}
order.extend(vec![
(
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)),
),
]);
order
}
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 workspace_name = state
.workspace
.as_ref()
.map(|ws| ws.name.as_str())
.unwrap_or_else(|| state.t("dashboard.no_workspace"));
let workspace_header: Element<'_, Message> = if state.all_workspaces.len() > 1 {
button(centered_button_row(
row![
text(workspace_name.to_owned())
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
widget::icon::icon_element(&icon::chevron_down()),
]
.spacing(6),
))
.height(Length::Fixed(BUTTON_HEIGHT))
.padding([0, 12])
.on_press(Message::Workspace(WorkspaceMessage::SwitcherToggled))
.style({
let t = tokens.clone();
let focused = crate::app::ring(
state,
&FocusTarget::control(focus_target::WORKSPACE_SWITCHER),
);
move |_theme, status| {
widget::style::with_focus_ring(&t, focused, widget::style::ghost(&t, status))
}
})
.into()
} else {
text(workspace_name.to_owned())
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into()
};
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(centered_button_label(
text(label.to_owned())
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
))
.height(Length::Fixed(BUTTON_HEIGHT))
.padding([0, 12])
.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(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into()
} else {
Space::new().width(Length::Shrink).into()
};
let compact = matches!(state.width_mode, crate::view::dashboard::WidthMode::Compact);
let refresh_button = if compact {
icon_button_maybe(
tokens,
&icon::refresh(),
state.t("plain.check_now"),
(!state.is_refreshing)
.then_some(Message::Workspace(WorkspaceMessage::RefreshRequested)),
crate::app::ring(state, &FocusTarget::control(focus_target::REFRESH)),
)
} else {
widget::header_control(
tokens,
&icon::refresh(),
state.t("plain.check_now"),
(!state.is_refreshing)
.then_some(Message::Workspace(WorkspaceMessage::RefreshRequested)),
crate::app::ring(state, &FocusTarget::control(focus_target::REFRESH)),
)
};
let palette_button = if compact {
icon_button_maybe(
tokens,
&icon::command_palette(),
state.t("palette.title"),
Some(Message::Palette(crate::message::PaletteMessage::Opened)),
crate::app::ring(state, &FocusTarget::control(focus_target::PALETTE)),
)
} else {
widget::header_control(
tokens,
&icon::command_palette(),
state.t("palette.title"),
Some(Message::Palette(crate::message::PaletteMessage::Opened)),
crate::app::ring(state, &FocusTarget::control(focus_target::PALETTE)),
)
};
let settings_button = if compact {
icon_button_maybe(
tokens,
&icon::settings(),
state.t("nav.settings"),
Some(Message::Navigate(Screen::Settings)),
crate::app::ring(state, &FocusTarget::control(focus_target::SETTINGS)),
)
} else {
widget::header_control(
tokens,
&icon::settings(),
state.t("nav.settings"),
Some(Message::Navigate(Screen::Settings)),
crate::app::ring(state, &FocusTarget::control(focus_target::SETTINGS)),
)
};
let bar = row![
workspace_header,
nav_button(
state.t("nav.dashboard"),
dashboard_active,
Screen::Dashboard,
tokens,
crate::app::ring(state, &FocusTarget::control(focus_target::NAV_DASHBOARD))
),
nav_button(
state.t("nav.history"),
history_active,
Screen::History,
tokens,
crate::app::ring(state, &FocusTarget::control(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();
let is_focused = crate::app::ring(
state,
&FocusTarget::control_dynamic(format!("shell.switcher.workspace.{}", ws.id)),
);
items = items.push(
button(centered_button_label(
text(ws.name.clone())
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
))
.width(Length::Fill)
.height(widget::SMALL_BUTTON_HEIGHT)
.padding([0, 12])
.on_press_maybe((!is_active).then_some(Message::Workspace(
WorkspaceMessage::WorkspaceSwitched(ws.id.clone()),
)))
.style(move |_theme, status| {
widget::style::with_focus_ring(&t, is_focused, widget::style::ghost(&t, status))
}),
);
}
let can_delete = state.all_workspaces.len() > 1;
let menu_action = |label: &str, msg: Message, tokens: &Tokens, is_focused: bool| {
let t = tokens.clone();
button(centered_button_label(
text(label.to_owned())
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
))
.width(Length::Fill)
.height(widget::SMALL_BUTTON_HEIGHT)
.padding([0, 12])
.on_press(msg)
.style(move |_theme, status| {
widget::style::with_focus_ring(&t, is_focused, widget::style::ghost(&t, status))
})
};
let content = column![
items,
menu_action(
state.t("plain.add_workspace"),
Message::Workspace(WorkspaceMessage::CreateWorkspaceDialogOpened),
tokens,
crate::app::ring(state, &FocusTarget::control(focus_target::ADD_WORKSPACE)),
),
menu_action(
state.t("workspace.rename.short"),
Message::Workspace(WorkspaceMessage::RenameWorkspaceDialogOpened),
tokens,
crate::app::ring(state, &FocusTarget::control(focus_target::RENAME)),
),
danger_menu_action(
state.t("workspace.delete.short"),
can_delete.then_some(Message::Workspace(
WorkspaceMessage::DeleteWorkspaceRequested
)),
tokens,
crate::app::ring(state, &FocusTarget::control(focus_target::DELETE)),
),
]
.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,
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)),
))
.width(Length::Fill)
.height(widget::SMALL_BUTTON_HEIGHT)
.padding([0, 12])
.on_press_maybe(on_press)
.style(move |_theme, status| {
widget::style::with_focus_ring(&t, is_focused, widget::style::danger(&t, status))
})
.into()
}