use crate::AppModel;
use crate::cli_outbound::Cli;
use crate::nav::menu::{self, Action, Entry, Seat, Verb};
use crate::nav::tabs::Tab;
use crate::nav::ws_key;
use std::path::PathBuf;
use super::ShellState;
pub(super) enum Target {
Tab(Tab),
Conversation { ws: PathBuf, agent: String },
Ball(BallRef),
}
pub(super) struct BallRef {
pub project: PathBuf,
pub id: String,
pub owner: String,
}
pub(super) fn attach(
response: &egui::Response,
seat: Seat,
target: &Target,
model: &mut AppModel,
state: &mut ShellState,
lernie: &Cli,
bl: &Cli,
) {
let entries = menu::entries(seat);
if entries.is_empty() {
return;
}
response.context_menu(|ui| paint(ui, &entries, target, model, state, lernie, bl));
}
fn paint(
ui: &mut egui::Ui,
entries: &[Entry],
target: &Target,
model: &mut AppModel,
state: &mut ShellState,
lernie: &Cli,
bl: &Cli,
) {
for entry in entries {
let hint = accelerates(&entry.carrier);
match &entry.action {
Action::Fire(verb) => {
if ui.button(&entry.label).on_hover_text(hint).clicked() {
fire(verb, target, model, state, lernie, bl);
ui.close_menu();
}
}
Action::Submenu(children) => {
ui.menu_button(&entry.label, |ui| {
paint(ui, children, target, model, state, lernie, bl);
})
.response
.on_hover_text(hint);
}
}
}
}
fn accelerates(carrier: &str) -> String {
format!(
"Does the same as {carrier}, aimed at this row instead of the selection. A menu \
is not on the Tab path, so that carrier is this verb's keyboard spelling."
)
}
fn fire(
verb: &Verb,
target: &Target,
model: &mut AppModel,
state: &mut ShellState,
lernie: &Cli,
bl: &Cli,
) {
match (verb, target) {
(Verb::DeleteWorkspace, Target::Tab(tab)) => super::delete::open(state, &tab.ws),
(Verb::Unpin, Target::Tab(tab)) => model.toggle_pin(&ws_key(&tab.ws)),
(Verb::Stop { children }, Target::Conversation { ws, agent }) => {
super::dispatch::stop_agent(model, lernie, bl, ws, agent, *children);
}
(Verb::Flush, Target::Conversation { ws, .. }) => {
super::dispatch::scan_ws(model, lernie, bl, ws);
}
(Verb::DeleteAgent, Target::Conversation { ws, agent }) => {
super::delete_agent::open(state, lernie, ws, agent);
}
(Verb::Assign(to), Target::Ball(ball)) => {
super::ball_bar::assign_ball(model, lernie, bl, &ball.project, &ball.id, to);
}
(Verb::MoveTo(to), Target::Ball(ball)) => {
super::ball_bar::move_ball(model, lernie, bl, &ball.project, &ball.id, &ball.owner, to);
}
(Verb::Release, Target::Ball(ball)) => {
super::ball_bar::release_ball(model, lernie, bl, &ball.project, &ball.id, &ball.owner);
}
(Verb::CloseBall, Target::Ball(ball)) => {
super::ball_bar::close_ball(model, lernie, bl, &ball.project, &ball.id, &ball.owner);
}
_ => {}
}
}