mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 15:26:17 +00:00
feat(app): hubs, types and tags UI, themes, key bindings and slash commands
Settings screen (themes incl. Sweet, rebindable shortcuts), rich text escapes, page sidebar with the local graph, sortable All pages, tree view changes and the Type/Tags rows in the page header. Docs updated. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e18fd2c27e
commit
88ef4a97a5
32 files changed
+5431
-375
No files matched your search
@@ -11,7 +11,9 @@ enum EdgeKind {
|
||||
blockRef('Block ref'),
|
||||
embed('Embed'),
|
||||
mention('Mention'),
|
||||
synapse('Synapse');
|
||||
synapse('Synapse'),
|
||||
contains('Contains'),
|
||||
tagged('Shared tag');
|
||||
|
||||
const EdgeKind(this.label);
|
||||
final String label;
|
||||
@@ -51,6 +53,123 @@ class GraphModel {
|
||||
|
||||
bool isJournal(int i) => node(i).journalDate != null;
|
||||
|
||||
bool isHub(int i) => node(i).isHub;
|
||||
|
||||
/// Whether the graph has any hub (folder-like page).
|
||||
late final bool hasHubs = data.nodes.any((n) => n.isHub);
|
||||
|
||||
/// Hubs that are the home hub of the most pages first (journals excluded); ties by index.
|
||||
/// A hub's position here is its colour rank in the hub grouping.
|
||||
late final List<int> hubOrder = () {
|
||||
final count = <int, int>{};
|
||||
for (var i = 0; i < nodeCount; i++) {
|
||||
final h = node(i).homeHub;
|
||||
if (h != null && !isJournal(i)) count[h] = (count[h] ?? 0) + 1;
|
||||
}
|
||||
return count.keys.toList()..sort((a, b) {
|
||||
final c = count[b]!.compareTo(count[a]!);
|
||||
return c != 0 ? c : a.compareTo(b);
|
||||
});
|
||||
}();
|
||||
|
||||
/// Hub node index → colour rank (see [hubOrder]).
|
||||
late final Map<int, int> hubRank = {
|
||||
for (var r = 0; r < hubOrder.length; r++) hubOrder[r]: r,
|
||||
};
|
||||
|
||||
/// Members of each hub: the targets of its `CONTAINS` edges (hub -> member), in edge order.
|
||||
late final Map<int, Set<int>> hubMembers = () {
|
||||
final m = <int, Set<int>>{};
|
||||
for (var e = 0; e < edgeCount; e++) {
|
||||
if (kind(e) != EdgeKind.contains) continue;
|
||||
final hub = source(e), page = target(e);
|
||||
if (hub == page) continue;
|
||||
(m[hub] ??= <int>{}).add(page);
|
||||
}
|
||||
return m;
|
||||
}();
|
||||
|
||||
/// Layout-only springs from every page in `nodes` to its home hub, so folders cluster.
|
||||
List<(int, int, double)> hubSprings(
|
||||
Iterable<int> nodes, {
|
||||
double strength = 0.6,
|
||||
}) => [
|
||||
for (final i in nodes)
|
||||
if (node(i).homeHub case final h? when h != i) (i, h, strength),
|
||||
];
|
||||
|
||||
/// The tag that groups page `i` in the "by tags" clusters: the page's most widely used tag
|
||||
/// (ties: alphabetical). Keys are lower-case; journals and untagged pages have none.
|
||||
String? homeTag(int i) {
|
||||
if (isJournal(i)) return null;
|
||||
String? best;
|
||||
for (final t in node(i).tags) {
|
||||
final k = t.toLowerCase();
|
||||
if (best == null ||
|
||||
_tagUse[k]! > _tagUse[best]! ||
|
||||
(_tagUse[k] == _tagUse[best] && k.compareTo(best) < 0)) {
|
||||
best = k;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/// Pages per tag key and the first spelling met, over every non-journal page.
|
||||
late final Map<String, int> _tagUse = () {
|
||||
final m = <String, int>{};
|
||||
for (var i = 0; i < nodeCount; i++) {
|
||||
if (isJournal(i)) continue;
|
||||
for (final t in node(i).tags) {
|
||||
final k = t.toLowerCase();
|
||||
m[k] = (m[k] ?? 0) + 1;
|
||||
_tagName.putIfAbsent(k, () => t);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}();
|
||||
final _tagName = <String, String>{};
|
||||
|
||||
/// Tag keys that are the home tag of the most pages first; a tag's position is its colour
|
||||
/// rank in the tag grouping.
|
||||
late final List<String> tagGroups = () {
|
||||
final count = <String, int>{};
|
||||
for (var i = 0; i < nodeCount; i++) {
|
||||
final t = homeTag(i);
|
||||
if (t != null) count[t] = (count[t] ?? 0) + 1;
|
||||
}
|
||||
return count.keys.toList()..sort((a, b) {
|
||||
final c = count[b]!.compareTo(count[a]!);
|
||||
return c != 0 ? c : a.compareTo(b);
|
||||
});
|
||||
}();
|
||||
|
||||
late final Map<String, int> tagGroupRank = {
|
||||
for (var r = 0; r < tagGroups.length; r++) tagGroups[r]: r,
|
||||
};
|
||||
|
||||
/// Display name of a tag key.
|
||||
String tagLabel(String key) => _tagName[key] ?? key;
|
||||
|
||||
/// Layout-only springs from every page in `nodes` to the best connected page of its home
|
||||
/// tag, so pages sharing a tag cluster spatially.
|
||||
List<(int, int, double)> tagSprings(
|
||||
Iterable<int> nodes, {
|
||||
double strength = 0.6,
|
||||
}) {
|
||||
final anchor = <String, int>{};
|
||||
for (final i in nodes) {
|
||||
final t = homeTag(i);
|
||||
if (t == null) continue;
|
||||
final a = anchor[t];
|
||||
if (a == null || node(i).degree > node(a).degree) anchor[t] = i;
|
||||
}
|
||||
return [
|
||||
for (final i in nodes)
|
||||
if (homeTag(i) case final t? when anchor[t] != i)
|
||||
(i, anchor[t]!, strength),
|
||||
];
|
||||
}
|
||||
|
||||
/// Tag pages actually used as tags, most used first.
|
||||
late final List<int> tags = () {
|
||||
final count = <int, int>{};
|
||||
@@ -112,7 +231,7 @@ class GraphModel {
|
||||
/// Filters of the network modes.
|
||||
class GraphFilter {
|
||||
const GraphFilter({
|
||||
this.journals = false,
|
||||
this.journals = true,
|
||||
this.orphans = true,
|
||||
this.minDegree = 0,
|
||||
this.tag,
|
||||
@@ -126,6 +245,8 @@ class GraphFilter {
|
||||
EdgeKind.blockRef,
|
||||
EdgeKind.embed,
|
||||
EdgeKind.mention,
|
||||
EdgeKind.contains,
|
||||
EdgeKind.tagged,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import '../state/app_state.dart';
|
||||
import '../theme/theme.dart';
|
||||
import 'graph_model.dart';
|
||||
|
||||
enum ColorBy { cluster, tag, none }
|
||||
enum ColorBy { cluster, tag, hub, tagGroup, none }
|
||||
|
||||
/// Calm categorical palette (cluster / tag colours), tuned for each theme.
|
||||
List<Color> graphPalette(Brightness b) => b == Brightness.light
|
||||
@@ -119,6 +119,7 @@ class NetworkCanvasState extends State<NetworkCanvas>
|
||||
// Reused point buffers (no per-frame allocation or boxing).
|
||||
final _edgeBufs = <EdgeKind, _Buf>{};
|
||||
final _nodeBufs = <(int, int), _Buf>{};
|
||||
final _hubBufs = <int, _Buf>{};
|
||||
Float32List _meshPos = Float32List(0);
|
||||
Int32List _meshCol = Int32List(0);
|
||||
double _startScale = 1;
|
||||
@@ -189,6 +190,8 @@ class NetworkCanvasState extends State<NetworkCanvas>
|
||||
3.0,
|
||||
22.0,
|
||||
);
|
||||
// Hubs are a little larger; the painter adds a ring around them.
|
||||
if (n.isHub) _radius[k] = _radius[k] * 1.2 + 1.5;
|
||||
_colorIndex[k] = switch (widget.colorBy) {
|
||||
ColorBy.cluster => n.cluster < 10 ? n.cluster : -1,
|
||||
ColorBy.tag =>
|
||||
@@ -198,6 +201,18 @@ class NetworkCanvasState extends State<NetworkCanvas>
|
||||
final i = g.tags.indexOf(n.primaryTag!);
|
||||
return i < 10 ? i : -1;
|
||||
})(),
|
||||
ColorBy.hub =>
|
||||
n.homeHub == null
|
||||
? -1
|
||||
: (() {
|
||||
final r = g.hubRank[n.homeHub!] ?? -1;
|
||||
return r < 10 ? r : -1;
|
||||
})(),
|
||||
ColorBy.tagGroup => (() {
|
||||
final t = g.homeTag(sub.nodes[k]);
|
||||
final r = t == null ? -1 : (g.tagGroupRank[t] ?? -1);
|
||||
return r < 10 ? r : -1;
|
||||
})(),
|
||||
ColorBy.none => -1,
|
||||
};
|
||||
}
|
||||
@@ -451,6 +466,8 @@ class _NetworkPainter extends CustomPainter {
|
||||
|
||||
/// Laid-out labels per subgraph, invalidated when the text colour changes.
|
||||
static final _labels = Expando<(Color, Map<int, TextPainter>)>();
|
||||
static final _hullLabels = Expando<(Color, Map<int, TextPainter>)>();
|
||||
static const _ringWidth = 1.6;
|
||||
|
||||
Color _edgeColor(EdgeKind k) => switch (k) {
|
||||
EdgeKind.link => colors.muted,
|
||||
@@ -460,6 +477,8 @@ class _NetworkPainter extends CustomPainter {
|
||||
EdgeKind.embed => colors.ref,
|
||||
EdgeKind.mention => colors.faint,
|
||||
EdgeKind.synapse => palette[3],
|
||||
EdgeKind.contains => colors.ink,
|
||||
EdgeKind.tagged => palette[1],
|
||||
};
|
||||
|
||||
@override
|
||||
@@ -494,7 +513,9 @@ class _NetworkPainter extends CustomPainter {
|
||||
canvas.scale(scale);
|
||||
|
||||
// Cluster hulls.
|
||||
if (s.widget.hulls) _hulls(canvas, sub, pos, scale);
|
||||
final hullLabels = s.widget.hulls
|
||||
? _hulls(canvas, sub, pos, scale)
|
||||
: const <_HullLabel>[];
|
||||
|
||||
// Edges, batched by kind.
|
||||
final pulse = s._pulse.value;
|
||||
@@ -585,15 +606,35 @@ class _NetworkPainter extends CustomPainter {
|
||||
b.n = 0;
|
||||
}
|
||||
final journalColor = colors.faint;
|
||||
for (final b in s._hubBufs.values) {
|
||||
b.n = 0;
|
||||
}
|
||||
for (var k = 0; k < sub.length; k++) {
|
||||
if (!visible(k)) continue;
|
||||
final r = brain == null
|
||||
? s._radius[k]
|
||||
: 3 + 9 * (brain.activation[sub.nodes[k]] ?? 0);
|
||||
if (g.isHub(sub.nodes[k])) {
|
||||
s._hubBufs
|
||||
.putIfAbsent(((r + _ringWidth) * 2).round(), _Buf.new)
|
||||
.add2(pos[2 * k], pos[2 * k + 1]);
|
||||
}
|
||||
final ci = g.isJournal(sub.nodes[k]) ? -2 : s._colorIndex[k];
|
||||
final key = (ci, (r * 2).round());
|
||||
buckets.putIfAbsent(key, _Buf.new).add2(pos[2 * k], pos[2 * k + 1]);
|
||||
}
|
||||
// Hub rings: a slightly larger disc under the node (one call per size bucket).
|
||||
for (final MapEntry(key: d2, value: pts) in s._hubBufs.entries) {
|
||||
if (pts.n == 0) continue;
|
||||
canvas.drawRawPoints(
|
||||
ui.PointMode.points,
|
||||
pts.view,
|
||||
Paint()
|
||||
..color = colors.ink.withValues(alpha: dim ? 0.2 : 0.8)
|
||||
..strokeWidth = d2.toDouble()
|
||||
..strokeCap = StrokeCap.round,
|
||||
);
|
||||
}
|
||||
for (final MapEntry(key: (ci, d2), value: pts) in buckets.entries) {
|
||||
if (pts.n == 0) continue;
|
||||
var c = ci == -2 ? journalColor : (ci < 0 ? colors.muted : palette[ci]);
|
||||
@@ -656,6 +697,7 @@ class _NetworkPainter extends CustomPainter {
|
||||
}
|
||||
canvas.restore();
|
||||
|
||||
_paintHullLabels(canvas, size, sub, hullLabels);
|
||||
_paintLabels(canvas, size, sub, pos, highlight, focusLocal);
|
||||
}
|
||||
|
||||
@@ -748,18 +790,39 @@ class _NetworkPainter extends CustomPainter {
|
||||
}
|
||||
}
|
||||
|
||||
void _hulls(Canvas canvas, SubGraph sub, Float32List pos, double scale) {
|
||||
final byCluster = <int, List<Offset>>{};
|
||||
/// Draws the soft hulls (by hub when colouring by hub, else by community) and returns
|
||||
/// the labels to draw on screen afterwards.
|
||||
List<_HullLabel> _hulls(
|
||||
Canvas canvas,
|
||||
SubGraph sub,
|
||||
Float32List pos,
|
||||
double scale,
|
||||
) {
|
||||
final g = sub.model;
|
||||
final by = state.widget.colorBy;
|
||||
final byHub = by == ColorBy.hub;
|
||||
final byTag = by == ColorBy.tagGroup;
|
||||
final groups = <int, List<Offset>>{};
|
||||
for (var k = 0; k < sub.length; k++) {
|
||||
final c = sub.model.node(sub.nodes[k]).cluster;
|
||||
if (c >= 10) continue;
|
||||
byCluster
|
||||
.putIfAbsent(c, () => [])
|
||||
.add(Offset(pos[2 * k], pos[2 * k + 1]));
|
||||
final n = g.node(sub.nodes[k]);
|
||||
final int c;
|
||||
if (byHub) {
|
||||
final h = n.homeHub;
|
||||
c = h == null ? -1 : (g.hubRank[h] ?? -1);
|
||||
} else if (byTag) {
|
||||
final t = g.homeTag(sub.nodes[k]);
|
||||
c = t == null ? -1 : (g.tagGroupRank[t] ?? -1);
|
||||
} else {
|
||||
c = n.cluster;
|
||||
}
|
||||
if (c < 0 || c >= 10) continue;
|
||||
groups.putIfAbsent(c, () => []).add(Offset(pos[2 * k], pos[2 * k + 1]));
|
||||
}
|
||||
for (final MapEntry(key: c, value: pts) in byCluster.entries) {
|
||||
final labels = <_HullLabel>[];
|
||||
for (final MapEntry(key: c, value: pts) in groups.entries) {
|
||||
if (pts.length < 3) continue;
|
||||
final hull = convexHull(corePoints(pts));
|
||||
final core = corePoints(pts);
|
||||
final hull = convexHull(core);
|
||||
if (hull.length < 3) continue;
|
||||
final path = Path()..addPolygon(hull, true);
|
||||
final color = palette[c];
|
||||
@@ -777,6 +840,65 @@ class _NetworkPainter extends CustomPainter {
|
||||
..strokeWidth = 28 / scale
|
||||
..strokeJoin = StrokeJoin.round,
|
||||
);
|
||||
if (byHub || byTag) {
|
||||
var cx = 0.0, cy = 0.0, x0 = double.infinity, x1 = -double.infinity;
|
||||
for (final p in core) {
|
||||
cx += p.dx;
|
||||
cy += p.dy;
|
||||
x0 = math.min(x0, p.dx);
|
||||
x1 = math.max(x1, p.dx);
|
||||
}
|
||||
labels.add(
|
||||
_HullLabel(
|
||||
c,
|
||||
byHub ? g.node(g.hubOrder[c]).title : g.tagLabel(g.tagGroups[c]),
|
||||
Offset(cx / core.length, cy / core.length),
|
||||
(x1 - x0) * scale,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
return labels;
|
||||
}
|
||||
|
||||
/// Hub titles at the hull centroids, only when the hull is wide enough on screen.
|
||||
void _paintHullLabels(
|
||||
Canvas canvas,
|
||||
Size size,
|
||||
SubGraph sub,
|
||||
List<_HullLabel> labels,
|
||||
) {
|
||||
if (labels.isEmpty) return;
|
||||
final s = state;
|
||||
var entry = _hullLabels[sub];
|
||||
if (entry == null || entry.$1 != colors.muted) {
|
||||
entry = _hullLabels[sub] = (colors.muted, <int, TextPainter>{});
|
||||
}
|
||||
for (final l in labels) {
|
||||
if (l.screenWidth < 90) continue;
|
||||
final at = l.center * s._scale + s._offset;
|
||||
if (at.dx < -100 ||
|
||||
at.dy < -30 ||
|
||||
at.dx > size.width + 100 ||
|
||||
at.dy > size.height + 30) {
|
||||
continue;
|
||||
}
|
||||
final tp = entry.$2[l.group] ??= TextPainter(
|
||||
text: TextSpan(
|
||||
text: l.title,
|
||||
style: TextStyle(
|
||||
fontFamily: uiFont,
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.muted,
|
||||
),
|
||||
),
|
||||
textDirection: TextDirection.ltr,
|
||||
maxLines: 1,
|
||||
ellipsis: '…',
|
||||
textScaler: textScaler,
|
||||
)..layout(maxWidth: 200);
|
||||
tp.paint(canvas, at - Offset(tp.width / 2, tp.height / 2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -813,6 +935,7 @@ class _NetworkPainter extends CustomPainter {
|
||||
k == focus ||
|
||||
(brainAct != null && brainAct > 0.15) ||
|
||||
screenR >= (compact ? 7 : (sub.length > 200 ? 8.5 : 5.5)) ||
|
||||
(!compact && sub.model.isHub(sub.nodes[k]) && screenR >= 5) ||
|
||||
(sub.length <= 40 && !compact)) {
|
||||
candidates.add(k);
|
||||
}
|
||||
@@ -885,6 +1008,15 @@ class _NetworkPainter extends CustomPainter {
|
||||
old.colors != colors || !identical(old.state, state);
|
||||
}
|
||||
|
||||
/// A hub title to draw at a hull centroid (world position, screen width of the hull).
|
||||
class _HullLabel {
|
||||
const _HullLabel(this.group, this.title, this.center, this.screenWidth);
|
||||
final int group;
|
||||
final String title;
|
||||
final Offset center;
|
||||
final double screenWidth;
|
||||
}
|
||||
|
||||
/// Growable float buffer reused across frames.
|
||||
class _Buf {
|
||||
Float32List data = Float32List(256);
|
||||
|
||||
@@ -4,13 +4,15 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import '../theme/theme.dart';
|
||||
import 'graph_model.dart';
|
||||
import 'network_canvas.dart';
|
||||
|
||||
/// A node of the namespace / tag tree.
|
||||
/// A node of the hub / namespace / tag tree.
|
||||
class TreeItem {
|
||||
TreeItem(this.label, {this.global});
|
||||
TreeItem(this.label, {this.global, this.isHub = false});
|
||||
final String label;
|
||||
|
||||
/// Drawn with the folder look.
|
||||
final bool isHub;
|
||||
|
||||
/// Page index when the item is a page.
|
||||
int? global;
|
||||
final children = <TreeItem>[];
|
||||
@@ -19,6 +21,12 @@ class TreeItem {
|
||||
double angle = 0;
|
||||
Offset pos = Offset.zero;
|
||||
|
||||
/// Colour-space position: centre hue (degrees) of this branch's sector and its width.
|
||||
/// Siblings split their parent's sector evenly, so they are equally far apart in hue.
|
||||
double hue = 0;
|
||||
double span = 360;
|
||||
int sibling = 0;
|
||||
|
||||
TreeItem child(String label) {
|
||||
for (final c in children) {
|
||||
if (c.label == label) return c;
|
||||
@@ -29,14 +37,75 @@ class TreeItem {
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds a tree with two branches: namespaces (`a/b/c` titles split on `/`) and tags (each
|
||||
/// tag with the pages using it, most used tags first).
|
||||
/// Hub branches: root hubs (hubs that are no other hub's member) with their members,
|
||||
/// recursively, plus the notes that live in no hub (orphans included). A hub is expanded once (the first time it is reached) and never inside
|
||||
/// itself, so cycles and shared sub-hubs stay bounded; later occurrences are leaves.
|
||||
List<TreeItem> _hubBranches(GraphModel g, int maxPerHub, int maxLoose) {
|
||||
final branches = <TreeItem>[];
|
||||
final members = g.hubMembers;
|
||||
final nested = {for (final m in members.values) ...m};
|
||||
final expanded = <int>{};
|
||||
TreeItem build(int hub) {
|
||||
final item = TreeItem(g.node(hub).title, global: hub, isHub: true);
|
||||
if (!expanded.add(hub)) return item;
|
||||
var shown = 0;
|
||||
final all = members[hub] ?? const <int>{};
|
||||
// Sub-hubs are always listed; plain pages are capped.
|
||||
for (final m in all) {
|
||||
if (g.isHub(m)) {
|
||||
item.children.add(build(m));
|
||||
} else if (shown < maxPerHub) {
|
||||
shown++;
|
||||
item.children.add(TreeItem(g.node(m).title, global: m));
|
||||
}
|
||||
}
|
||||
final hidden = all.where((m) => !g.isHub(m)).length - shown;
|
||||
if (hidden > 0) item.children.add(TreeItem('… $hidden more'));
|
||||
return item;
|
||||
}
|
||||
|
||||
final hubs = [
|
||||
for (var i = 0; i < g.nodeCount; i++)
|
||||
if (g.isHub(i)) i,
|
||||
];
|
||||
for (final h in hubs) {
|
||||
if (!nested.contains(h)) branches.add(build(h));
|
||||
}
|
||||
// Hubs only reachable through a cycle still need a place.
|
||||
for (final h in hubs) {
|
||||
if (!expanded.contains(h)) branches.add(build(h));
|
||||
}
|
||||
// Notes under no container hang straight off the centre (namespaced ones keep their
|
||||
// namespace branch).
|
||||
final loose = [
|
||||
for (var i = 0; i < g.nodeCount; i++)
|
||||
if (!g.isHub(i) &&
|
||||
!g.isJournal(i) &&
|
||||
!nested.contains(i) &&
|
||||
!g.node(i).title.contains('/'))
|
||||
i,
|
||||
];
|
||||
for (final i in loose.take(maxLoose)) {
|
||||
branches.add(TreeItem(g.node(i).title, global: i));
|
||||
}
|
||||
if (loose.length > maxLoose) {
|
||||
branches.add(TreeItem('… ${loose.length - maxLoose} more'));
|
||||
}
|
||||
return branches;
|
||||
}
|
||||
|
||||
/// Builds a tree from hubs (folders, straight off the centre, with loose notes), namespaces (`a/b/c` titles split on `/`, attached
|
||||
/// directly to the centre) and tags (each tag with the pages using it, most used tags first).
|
||||
TreeItem buildNamespaceTree(
|
||||
GraphModel g, {
|
||||
String name = 'Graph',
|
||||
int maxTags = 24,
|
||||
int maxPerTag = 30,
|
||||
int maxPerHub = 40,
|
||||
int maxLoose = 80,
|
||||
}) {
|
||||
final root = TreeItem('Graph');
|
||||
final root = TreeItem(name);
|
||||
root.children.addAll(_hubBranches(g, maxPerHub, maxLoose));
|
||||
final ns = TreeItem('Namespaces');
|
||||
final byTitle = <String, int>{};
|
||||
for (var i = 0; i < g.nodeCount; i++) {
|
||||
@@ -53,7 +122,8 @@ TreeItem buildNamespaceTree(
|
||||
at.global ??= byTitle[path];
|
||||
}
|
||||
}
|
||||
if (ns.children.isNotEmpty) root.children.add(ns);
|
||||
// No "Namespaces" node: top-level namespaces hang straight off the centre.
|
||||
root.children.addAll(ns.children);
|
||||
final tags = TreeItem('Tags');
|
||||
for (final tag in g.tags.take(maxTags)) {
|
||||
final item = TreeItem('#${g.node(tag).title}', global: tag);
|
||||
@@ -68,11 +138,32 @@ TreeItem buildNamespaceTree(
|
||||
tags.children.add(item);
|
||||
}
|
||||
if (tags.children.isNotEmpty) root.children.add(tags);
|
||||
// With a single branch, that branch is the centre.
|
||||
final top = root.children.length == 1 ? root.children.single : root;
|
||||
_measure(top, 0);
|
||||
_place(top, 0, 2 * math.pi);
|
||||
return top;
|
||||
_measure(root, 0);
|
||||
_place(root, 0, 2 * math.pi);
|
||||
_colorize(root);
|
||||
return root;
|
||||
}
|
||||
|
||||
/// Gives every arm of `t` its own sector of the hue circle (the centre's arms share it all),
|
||||
/// and every sub-branch an equal slice of its parent's sector.
|
||||
void _colorize(TreeItem t) {
|
||||
final k = t.children.length;
|
||||
for (var j = 0; j < k; j++) {
|
||||
final c = t.children[j];
|
||||
c.span = t.span / k;
|
||||
c.hue = t.hue - t.span / 2 + (j + 0.5) * c.span;
|
||||
c.sibling = j;
|
||||
_colorize(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// Branch colour: hue from the sector, alternating lightness so neighbours stay apart even
|
||||
/// when the slices get thin.
|
||||
Color treeColor(TreeItem t, Brightness b) {
|
||||
final dark = b == Brightness.dark;
|
||||
final l =
|
||||
(dark ? 0.66 : 0.48) + (t.sibling.isOdd ? (dark ? -0.08 : 0.08) : 0);
|
||||
return HSLColor.fromAHSL(1, (t.hue % 360 + 360) % 360, 0.62, l).toColor();
|
||||
}
|
||||
|
||||
int _measure(TreeItem t, int depth) {
|
||||
@@ -98,6 +189,16 @@ void _place(TreeItem t, double a0, double a1) {
|
||||
}
|
||||
}
|
||||
|
||||
void _shift(TreeItem t, Offset d) {
|
||||
for (final x in _walk(t)) {
|
||||
x.pos += d;
|
||||
}
|
||||
}
|
||||
|
||||
class _Tick extends ChangeNotifier {
|
||||
void bump() => notifyListeners();
|
||||
}
|
||||
|
||||
Iterable<TreeItem> _walk(TreeItem t) sync* {
|
||||
yield t;
|
||||
for (final c in t.children) {
|
||||
@@ -105,10 +206,18 @@ Iterable<TreeItem> _walk(TreeItem t) sync* {
|
||||
}
|
||||
}
|
||||
|
||||
/// Radial tree of namespaces and tags; pan/zoom with the GPU transform (no repaint).
|
||||
/// Radial tree of hubs, namespaces and tags; pan/zoom with the GPU transform (no repaint).
|
||||
class TreeCanvas extends StatefulWidget {
|
||||
const TreeCanvas({super.key, required this.model, required this.onTap});
|
||||
const TreeCanvas({
|
||||
super.key,
|
||||
required this.model,
|
||||
required this.onTap,
|
||||
this.name = 'Graph',
|
||||
});
|
||||
final GraphModel model;
|
||||
|
||||
/// Label of the centre: the name of the notes store.
|
||||
final String name;
|
||||
final void Function(int global) onTap;
|
||||
|
||||
@override
|
||||
@@ -116,21 +225,26 @@ class TreeCanvas extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _TreeCanvasState extends State<TreeCanvas> {
|
||||
late TreeItem _root = buildNamespaceTree(widget.model);
|
||||
late TreeItem _root = buildNamespaceTree(widget.model, name: widget.name);
|
||||
final _tick = _Tick();
|
||||
TreeItem? _drag;
|
||||
bool _pan = true;
|
||||
final _transform = TransformationController();
|
||||
bool _placed = false;
|
||||
|
||||
@override
|
||||
void didUpdateWidget(TreeCanvas oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (!identical(oldWidget.model, widget.model)) {
|
||||
_root = buildNamespaceTree(widget.model);
|
||||
if (!identical(oldWidget.model, widget.model) ||
|
||||
oldWidget.name != widget.name) {
|
||||
_root = buildNamespaceTree(widget.model, name: widget.name);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_transform.dispose();
|
||||
_tick.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -139,18 +253,36 @@ class _TreeCanvasState extends State<TreeCanvas> {
|
||||
return maxDepth * _ring + 170;
|
||||
}
|
||||
|
||||
/// Nodes can be dragged away from the rings, so the scene is larger than the layout.
|
||||
double get _half => _extent * 1.6;
|
||||
|
||||
TreeItem? _hit(Offset scene, double half) {
|
||||
final p = scene - Offset(half, half);
|
||||
TreeItem? best;
|
||||
var d = 12.0;
|
||||
for (final t in _walk(_root)) {
|
||||
final x = (t.pos - p).distance;
|
||||
if (x < d) {
|
||||
d = x;
|
||||
best = t;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = context.colors;
|
||||
if (_root.children.isEmpty) {
|
||||
return Center(
|
||||
child: Text(
|
||||
'No namespaces (a/b titles) or tags yet.',
|
||||
'No hubs, namespaces (a/b titles) or tags yet.',
|
||||
style: TextStyle(color: colors.muted),
|
||||
),
|
||||
);
|
||||
}
|
||||
final extent = _extent;
|
||||
final half = _half;
|
||||
return LayoutBuilder(
|
||||
builder: (context, box) {
|
||||
if (!_placed) {
|
||||
@@ -161,8 +293,8 @@ class _TreeCanvasState extends State<TreeCanvas> {
|
||||
);
|
||||
_transform.value = Matrix4.identity()
|
||||
..translateByDouble(
|
||||
box.maxWidth / 2 - extent * s,
|
||||
box.maxHeight / 2 - extent * s,
|
||||
box.maxWidth / 2 - half * s,
|
||||
box.maxHeight / 2 - half * s,
|
||||
0,
|
||||
1,
|
||||
)
|
||||
@@ -171,26 +303,41 @@ class _TreeCanvasState extends State<TreeCanvas> {
|
||||
return InteractiveViewer(
|
||||
transformationController: _transform,
|
||||
constrained: false,
|
||||
panEnabled: _pan,
|
||||
minScale: 0.05,
|
||||
maxScale: 6,
|
||||
boundaryMargin: const EdgeInsets.all(double.infinity),
|
||||
child: GestureDetector(
|
||||
onTapUp: (d) {
|
||||
final p = d.localPosition - Offset(extent, extent);
|
||||
for (final t in _walk(_root)) {
|
||||
if (t.global != null && (t.pos - p).distance < 10) {
|
||||
widget.onTap(t.global!);
|
||||
return;
|
||||
}
|
||||
child: Listener(
|
||||
// Dragging a node moves it with its whole branch; empty space pans the view.
|
||||
onPointerDown: (e) {
|
||||
final t = _hit(e.localPosition, half);
|
||||
if (t != null) {
|
||||
_drag = t;
|
||||
setState(() => _pan = false);
|
||||
}
|
||||
},
|
||||
child: CustomPaint(
|
||||
size: Size(2 * extent, 2 * extent),
|
||||
painter: _TreePainter(
|
||||
_root,
|
||||
extent,
|
||||
colors,
|
||||
graphPalette(Theme.of(context).brightness),
|
||||
onPointerMove: (e) {
|
||||
final t = _drag;
|
||||
if (t == null) return;
|
||||
_shift(t, e.delta / _transform.value.getMaxScaleOnAxis());
|
||||
_tick.bump();
|
||||
},
|
||||
onPointerUp: (_) => _release(),
|
||||
onPointerCancel: (_) => _release(),
|
||||
child: GestureDetector(
|
||||
onTapUp: (d) {
|
||||
final t = _hit(d.localPosition, half);
|
||||
if (t?.global != null) widget.onTap(t!.global!);
|
||||
},
|
||||
child: CustomPaint(
|
||||
size: Size(2 * half, 2 * half),
|
||||
painter: _TreePainter(
|
||||
_root,
|
||||
half,
|
||||
colors,
|
||||
Theme.of(context).brightness,
|
||||
_tick,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -198,29 +345,38 @@ class _TreeCanvasState extends State<TreeCanvas> {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _release() {
|
||||
if (_drag == null) return;
|
||||
_drag = null;
|
||||
setState(() => _pan = true);
|
||||
}
|
||||
}
|
||||
|
||||
class _TreePainter extends CustomPainter {
|
||||
_TreePainter(this.root, this.extent, this.colors, this.palette);
|
||||
_TreePainter(
|
||||
this.root,
|
||||
this.extent,
|
||||
this.colors,
|
||||
this.brightness,
|
||||
Listenable tick,
|
||||
) : super(repaint: tick);
|
||||
final TreeItem root;
|
||||
final double extent;
|
||||
final SynapseColors colors;
|
||||
final List<Color> palette;
|
||||
final Brightness brightness;
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
canvas.translate(extent, extent);
|
||||
final link = Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.2
|
||||
..color = colors.line;
|
||||
..strokeWidth = 1.4;
|
||||
for (final t in _walk(root)) {
|
||||
for (final c in t.children) {
|
||||
// Curve from parent to child bending through the child's angle.
|
||||
final mid = Offset.fromDirection(
|
||||
c.angle,
|
||||
t.depth * _ring + _ring * 0.5,
|
||||
);
|
||||
// Curve from parent to child bending through the child's ray.
|
||||
final mid = c.pos - Offset.fromDirection(c.angle, _ring * 0.5);
|
||||
link.color = treeColor(c, brightness).withValues(alpha: 0.7);
|
||||
canvas.drawPath(
|
||||
Path()
|
||||
..moveTo(t.pos.dx, t.pos.dy)
|
||||
@@ -229,43 +385,80 @@ class _TreePainter extends CustomPainter {
|
||||
);
|
||||
}
|
||||
}
|
||||
var branch = 0;
|
||||
// Colour per first-level branch; with a single-branch tree each subtree gets its own.
|
||||
for (final b in root.children) {
|
||||
final color = palette[branch++ % palette.length];
|
||||
for (final t in _walk(b)) {
|
||||
final r = t.children.isEmpty ? 3.5 : 5.5;
|
||||
canvas.drawCircle(
|
||||
t.pos,
|
||||
r,
|
||||
Paint()..color = t.global == null ? colors.faint : color,
|
||||
);
|
||||
final tp = TextPainter(
|
||||
text: TextSpan(
|
||||
text: t.label,
|
||||
style: TextStyle(
|
||||
fontFamily: uiFont,
|
||||
fontSize: t.children.isEmpty ? 11 : 12.5,
|
||||
fontWeight: t.children.isEmpty
|
||||
? FontWeight.w400
|
||||
: FontWeight.w600,
|
||||
color: t.children.isEmpty ? colors.muted : colors.ink,
|
||||
),
|
||||
),
|
||||
textDirection: TextDirection.ltr,
|
||||
maxLines: 1,
|
||||
ellipsis: '…',
|
||||
)..layout(maxWidth: 150);
|
||||
// Labels outside the circle, left of the node on the left half.
|
||||
final left = math.cos(t.angle) < 0;
|
||||
final at = t.pos + Offset(left ? -tp.width - 8 : 8, -tp.height / 2);
|
||||
tp.paint(canvas, at);
|
||||
void node(TreeItem t, Color color) {
|
||||
final r = t.children.isEmpty ? 3.5 : 5.5;
|
||||
final fill = Paint()
|
||||
..color = t.global == null ? color.withValues(alpha: 0.55) : color;
|
||||
if (t.isHub) {
|
||||
_folder(canvas, t.pos, fill);
|
||||
} else {
|
||||
canvas.drawCircle(t.pos, r, fill);
|
||||
}
|
||||
final tp = TextPainter(
|
||||
text: TextSpan(
|
||||
text: t.label,
|
||||
style: TextStyle(
|
||||
fontFamily: uiFont,
|
||||
fontSize: t.children.isEmpty ? 11 : 12.5,
|
||||
fontWeight: t.children.isEmpty ? FontWeight.w400 : FontWeight.w600,
|
||||
color: t.children.isEmpty ? colors.muted : colors.ink,
|
||||
),
|
||||
),
|
||||
textDirection: TextDirection.ltr,
|
||||
maxLines: 1,
|
||||
ellipsis: '…',
|
||||
)..layout(maxWidth: 150);
|
||||
// Labels outside the circle, left of the node on the left half.
|
||||
final left = math.cos(t.angle) < 0;
|
||||
tp.paint(
|
||||
canvas,
|
||||
t.pos + Offset(left ? -tp.width - 8 : 8, -tp.height / 2),
|
||||
);
|
||||
}
|
||||
canvas.drawCircle(Offset.zero, 7, Paint()..color = colors.accent);
|
||||
|
||||
for (final t in _walk(root).skip(1)) {
|
||||
node(t, treeColor(t, brightness));
|
||||
}
|
||||
// The centre carries the name of the notes store.
|
||||
final c = TextPainter(
|
||||
text: TextSpan(
|
||||
text: root.label,
|
||||
style: TextStyle(
|
||||
fontFamily: uiFont,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: colors.ink,
|
||||
),
|
||||
),
|
||||
textDirection: TextDirection.ltr,
|
||||
maxLines: 1,
|
||||
ellipsis: '…',
|
||||
)..layout(maxWidth: 180);
|
||||
c.paint(canvas, root.pos + Offset(-c.width / 2, -c.height - 12));
|
||||
canvas.drawCircle(root.pos, 7, Paint()..color = colors.accent);
|
||||
}
|
||||
|
||||
/// Small folder glyph (body plus tab) centred on `c`.
|
||||
void _folder(Canvas canvas, Offset c, Paint paint) {
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(
|
||||
Rect.fromCenter(center: c.translate(0, 1), width: 13, height: 9),
|
||||
const Radius.circular(2),
|
||||
),
|
||||
paint,
|
||||
);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(
|
||||
Rect.fromLTWH(c.dx - 6.5, c.dy - 5, 6, 3),
|
||||
const Radius.circular(1.2),
|
||||
),
|
||||
paint,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(_TreePainter old) =>
|
||||
!identical(old.root, root) || old.colors != colors;
|
||||
!identical(old.root, root) ||
|
||||
old.colors != colors ||
|
||||
old.brightness != brightness;
|
||||
}
|
||||
Reference in new issue
Block a user