use std::collections::HashSet;
use arc_flow_core::{Graph, NodeId, NodeStatus};
use arc_tokens::Locale;
fn t(locale: Locale, zh: &'static str, en: &'static str) -> &'static str {
match locale {
Locale::ZhCn => zh,
Locale::EnUs => en,
}
}
pub fn status_label(status: NodeStatus, locale: Locale) -> &'static str {
match status {
NodeStatus::Idle => t(locale, "空闲", "idle"),
NodeStatus::Queued => t(locale, "排队中", "queued"),
NodeStatus::Running => t(locale, "运行中", "running"),
NodeStatus::Done => t(locale, "已完成", "done"),
NodeStatus::Failed => t(locale, "失败", "failed"),
NodeStatus::Skipped => t(locale, "已跳过", "skipped"),
}
}
pub fn reading_order(graph: &Graph) -> Vec<NodeId> {
graph
.topo_order()
.unwrap_or_else(|_| graph.node_ids().to_vec())
}
pub fn neighbours(graph: &Graph, id: NodeId, downstream: bool) -> Vec<NodeId> {
let mut seen = HashSet::new();
let mut out: Vec<NodeId> = graph
.edges()
.filter_map(|e| {
let from = graph.port(e.from)?.node;
let to = graph.port(e.to)?.node;
match downstream {
true if from == id => Some(to),
false if to == id => Some(from),
_ => None,
}
})
.filter(|n| *n != id && seen.insert(*n))
.collect();
out.sort_by(|a, b| {
let pa = graph.world_rect(*a).map(|r| r.min);
let pb = graph.world_rect(*b).map(|r| r.min);
match (pa, pb) {
(Some(a), Some(b)) => a.y.total_cmp(&b.y).then(a.x.total_cmp(&b.x)),
_ => std::cmp::Ordering::Equal,
}
});
out
}
fn join_labels(graph: &Graph, ids: &[NodeId], locale: Locale) -> String {
let sep = t(locale, "、", ", ");
ids.iter()
.filter_map(|id| graph.node(*id).map(|n| n.label.to_string()))
.collect::<Vec<_>>()
.join(sep)
}
pub fn describe_node(graph: &Graph, id: NodeId, locale: Locale) -> String {
let Some(node) = graph.node(id) else {
return String::new();
};
let mut parts = vec![format!(
"{}{}",
t(locale, "状态:", "Status: "),
status_label(node.status, locale)
)];
let children = graph
.node_ids()
.iter()
.filter(|c| graph.node(**c).and_then(|n| n.parent) == Some(id))
.count();
if children > 0 {
parts.push(match locale {
Locale::ZhCn => format!("包含 {children} 个子节点"),
Locale::EnUs => format!("contains {children} child nodes"),
});
}
let ups = neighbours(graph, id, false);
let downs = neighbours(graph, id, true);
parts.push(if ups.is_empty() {
t(locale, "没有输入连线", "no incoming edges").to_string()
} else {
format!(
"{}{}",
t(locale, "来自 ", "from "),
join_labels(graph, &ups, locale)
)
});
parts.push(if downs.is_empty() {
t(locale, "没有输出连线", "no outgoing edges").to_string()
} else {
format!(
"{}{}",
t(locale, "连到 ", "to "),
join_labels(graph, &downs, locale)
)
});
let sep = t(locale, ";", "; ");
let end = t(locale, "。", ".");
format!("{}{end}", parts.join(sep))
}
pub fn describe_canvas(graph: &Graph, locale: Locale) -> String {
let (n, e) = (graph.node_count(), graph.edge_count());
match locale {
Locale::ZhCn => format!(
"{n} 个节点,{e} 条连线。Tab 切换节点,Alt+左右方向键沿连线移动,Alt+上下方向键切换相邻节点。"
),
Locale::EnUs => format!(
"{n} nodes, {e} edges. Tab moves between nodes, Alt+Left/Right follows edges, Alt+Up/Down switches between neighbours."
),
}
}
pub const KEY_SHORTCUTS: &str =
"Tab Shift+Tab Alt+ArrowLeft Alt+ArrowRight Alt+ArrowUp Alt+ArrowDown";
#[cfg(test)]
mod tests {
use super::*;
use arc_flow_core::{Size2, Vec2};
fn chain() -> (Graph, Vec<NodeId>) {
let mut g = Graph::new();
let sz = Size2::new(100.0, 40.0);
let (a, _, ao) = g.add_node_with("t", Vec2::new(0.0, 0.0), sz, 0, 1);
let (b, bi, bo) = g.add_node_with("t", Vec2::new(200.0, 0.0), sz, 1, 1);
let (c, ci, _) = g.add_node_with("t", Vec2::new(400.0, 100.0), sz, 1, 0);
let (d, di, _) = g.add_node_with("t", Vec2::new(400.0, -100.0), sz, 1, 0);
g.connect(ao[0], bi[0]).unwrap();
g.connect(bo[0], ci[0]).unwrap();
g.connect(bo[0], di[0]).unwrap();
for (id, name) in [(a, "A"), (b, "B"), (c, "C"), (d, "D")] {
g.apply(arc_flow_core::GraphOp::SetNodeLabel {
id,
label: name.into(),
})
.unwrap();
}
(g, vec![a, b, c, d])
}
#[test]
fn describes_status_and_connections_in_both_locales() {
let (mut g, ids) = chain();
g.set_status(ids[1], NodeStatus::Running);
assert_eq!(
describe_node(&g, ids[1], Locale::ZhCn),
"状态:运行中;来自 A;连到 D、C。"
);
assert_eq!(
describe_node(&g, ids[1], Locale::EnUs),
"Status: running; from A; to D, C."
);
assert_eq!(
describe_node(&g, ids[0], Locale::ZhCn),
"状态:空闲;没有输入连线;连到 B。"
);
assert!(describe_canvas(&g, Locale::ZhCn).starts_with("4 个节点,3 条连线。"));
}
#[test]
fn reading_order_and_neighbours() {
let (g, ids) = chain();
let order = reading_order(&g);
assert_eq!(order[0], ids[0]);
assert_eq!(order[1], ids[1]);
assert_eq!(neighbours(&g, ids[1], true), vec![ids[3], ids[2]]);
assert_eq!(neighbours(&g, ids[1], false), vec![ids[0]]);
assert!(neighbours(&g, ids[2], true).is_empty());
}
}