Skip to main content

arc_graph/
lib.rs

1//! # arc-graph — GPUI 知识图谱画布(本地设计,方案 `docs/graph/knowledge-graph-plan.md`)
2//!
3//! 2D 知识图谱视图:Barnes–Hut 力导向布局在**进程内后台线程**跑(纯 UI 组件,
4//! 无任何后端服务),本视图每帧只取最新坐标快照渲染。
5//!
6//! # 使用方法
7//!
8//! ```
9//! use arc_graph::Graph2D;
10//! use arc_graph_core::KnowledgeGraph;
11//! use gpui_kit::{Context, Render, Window, div, prelude::*};
12//!
13//! struct Page {
14//!     canvas: gpui_kit::Entity<Graph2D>,
15//! }
16//!
17//! impl Page {
18//!     pub fn new(cx: &mut Context<Self>) -> Self {
19//!         let mut g = KnowledgeGraph::new();
20//!         let person = g.add_kind("人物");
21//!         let a = g.add_node("张三", Some(person));
22//!         let b = g.add_node("李四", Some(person));
23//!         g.add_edge(a, b, 1.0, None).unwrap();
24//!         // 力导向布局在后台线程自动迭代;已有坐标可用 `new_static` 跳过布局。
25//!         let canvas = cx.new(|cx| Graph2D::new("graph", g, cx).fit_on_mount());
26//!         Self { canvas }
27//!     }
28//! }
29//!
30//! impl Render for Page {
31//!     fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
32//!         div().size_full().child(self.canvas.clone())
33//!     }
34//! }
35//! ```
36//!
37//! # 交互与接入(G4,无后端)
38//!
39//! - 滚轮 / 背景拖动平移;Ctrl/⌘+滚轮或捏合缩放。
40//! - 悬停高亮一度邻居,其余节点和边淡化(所有 LOD 一致)。
41//! - 点击选择,Shift+点击切换多选,Shift+背景拖动框选。
42//! - 拖动节点后保持固定,用 `set_pinned(id, false, cx)` 解除。
43//! - `search(query)` 返回当前可见匹配项;点击搜索结果后用 `fly_to(id, cx)` 弹簧定位。
44//! - `set_kind_filter(Some(kind), cx)` 筛选类型,`None` 恢复全部;
45//!   筛选后双击节点或调用 `expand(id, cx)` 揭示一度邻居。
46//! - 搜索框、类型按钮由宿主页组合;完整示例见 gallery 的 `graph-canvas` 页。
47//!
48//! ```no_run
49//! # use arc_graph::{Graph2D, NodeId};
50//! # use gpui_kit::{App, Entity};
51//! fn locate(canvas: &Entity<Graph2D>, cx: &mut App) {
52//!     if let Some(id) = canvas.read(cx).search("张").first().copied() {
53//!         canvas.update(cx, |view, cx| view.fly_to(id, cx));
54//!     }
55//!     canvas.update(cx, |view, cx| {
56//!         view.set_kind_filter(None, cx);
57//!         view.set_pinned(NodeId(1), false, cx);
58//!     });
59//! }
60//! ```
61//!
62//! LOD(方案 §5):缩得很小时节点批量画 quad 点;中等画外壳;
63//! 放大才渲染 div 卡片(含文字)。缩小模式下按度数优先级显示浮动标签,
64//! 屏幕网格碰撞剔除,同屏最多 200 个。
65
66pub mod motion;
67pub mod view;
68
69pub use arc_graph_core::{
70    Edge, EdgeId, ForceLayout2D, GraphError, GraphPayload, KnowledgeGraph, LayoutConfig, Node,
71    NodeId, NodeKind,
72};
73pub use motion::{Appear, GraphMotion};
74pub use view::{Graph2D, Lod, RenderCounts};