mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 23:36:20 +00:00
fix(app): type icon cache refreshes on reload and reaches the tree view
loadTypeIcons logs failures, only notifies on change and bumps iconsVersion; every generation bump reloads it. The tree re-resolves label icons when iconsVersion changes, without re-running the layout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1a8bca5deb
commit
8a0fdc7610
5 files changed
+136
-17
No files matched your search
@@ -257,12 +257,16 @@ class TreeCanvas extends StatefulWidget {
|
||||
required this.onTap,
|
||||
this.name = 'Graph',
|
||||
this.typeIcon,
|
||||
this.iconsVersion = 0,
|
||||
});
|
||||
final GraphModel model;
|
||||
|
||||
/// Emoji of a page type, shown before page labels.
|
||||
final String? Function(String? type)? typeIcon;
|
||||
|
||||
/// Changes when the type icons change: labels re-resolve their emoji (no new layout).
|
||||
final int iconsVersion;
|
||||
|
||||
/// Label of the centre: the name of the notes store.
|
||||
final String name;
|
||||
final void Function(int global) onTap;
|
||||
@@ -313,9 +317,21 @@ class TreeCanvasState extends State<TreeCanvas> {
|
||||
if (!identical(oldWidget.model, widget.model) ||
|
||||
oldWidget.name != widget.name) {
|
||||
_build();
|
||||
} else if (oldWidget.iconsVersion != widget.iconsVersion) {
|
||||
_refreshIcons(_root);
|
||||
_repaint.value++;
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-reads the type emoji of every page item (labels redraw with them).
|
||||
void _refreshIcons(TreeItem t) {
|
||||
final g = t.global;
|
||||
if (g != null) {
|
||||
t.icon = widget.typeIcon?.call(widget.model.node(g).pageType);
|
||||
}
|
||||
t.children.forEach(_refreshIcons);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_generation++;
|
||||
@@ -739,8 +755,8 @@ class _TreePainter extends CustomPainter {
|
||||
final Brightness brightness;
|
||||
final TextScaler textScaler;
|
||||
|
||||
/// Laid-out labels per item, invalidated when the text colours change.
|
||||
static final _cache = Expando<(Color, TextPainter)>();
|
||||
/// Laid-out labels per item, invalidated when the text colour or the icon changes.
|
||||
static final _cache = Expando<(Color, String?, TextPainter)>();
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
@@ -868,9 +884,10 @@ class _TreePainter extends CustomPainter {
|
||||
final branch = t.children.isNotEmpty;
|
||||
final ink = branch || t.parent == null ? colors.ink : colors.muted;
|
||||
var entry = _cache[t];
|
||||
if (entry == null || entry.$1 != ink) {
|
||||
if (entry == null || entry.$1 != ink || entry.$2 != t.icon) {
|
||||
entry = _cache[t] = (
|
||||
ink,
|
||||
t.icon,
|
||||
TextPainter(
|
||||
text: TextSpan(
|
||||
text: withTypeIcon(t.icon, t.label),
|
||||
@@ -890,7 +907,7 @@ class _TreePainter extends CustomPainter {
|
||||
)..layout(maxWidth: 170),
|
||||
);
|
||||
}
|
||||
final tp = entry.$2;
|
||||
final tp = entry.$3;
|
||||
final r = s._radius(t) * scale.clamp(0.6, 1.6);
|
||||
// Labels on the outer side of the node, away from its parent.
|
||||
final par = t.parent == null ? null : s._index[t.parent!];
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dart:ui' show PlatformDispatcher;
|
||||
|
||||
import 'package:flutter/foundation.dart' show mapEquals;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart' show HardwareKeyboard;
|
||||
|
||||
@@ -362,18 +363,37 @@ class AppState extends ChangeNotifier {
|
||||
String? tabTypeIcon(AppTab t) =>
|
||||
t.route is PageTarget ? typeIcon(t.pageType) : null;
|
||||
|
||||
/// Reloads [typeIcons] from the type catalogue (at start and after type edits).
|
||||
/// Bumped whenever [typeIcons] changes (lets painters such as the tree view redraw).
|
||||
int iconsVersion = 0;
|
||||
|
||||
/// Reloads [typeIcons] from the type catalogue (at start, after type edits and on every
|
||||
/// [reload]); listeners are only notified when something changed. Errors are logged.
|
||||
Future<void> loadTypeIcons() async {
|
||||
final types = await backend.typeCatalog();
|
||||
final List<TagDto> types;
|
||||
try {
|
||||
types = await backend.typeCatalog();
|
||||
} catch (e, st) {
|
||||
debugPrint('loading type icons failed: $e\n$st');
|
||||
return;
|
||||
}
|
||||
final next = {
|
||||
for (final t in types)
|
||||
if (t.icon != null) t.name.toLowerCase(): t.icon!,
|
||||
};
|
||||
if (mapEquals(next, typeIcons)) return;
|
||||
typeIcons
|
||||
..clear()
|
||||
..addAll({
|
||||
for (final t in types)
|
||||
if (t.icon != null) t.name.toLowerCase(): t.icon!,
|
||||
});
|
||||
..addAll(next);
|
||||
iconsVersion++;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Views reload from the core: so do the type icons (they may have changed elsewhere).
|
||||
void _bumpGeneration() {
|
||||
generation++;
|
||||
loadTypeIcons().ignore();
|
||||
}
|
||||
|
||||
/// Hubs expanded in the sidebar tree (kept here so it survives sidebar rebuilds).
|
||||
final expandedHubs = <String>{};
|
||||
|
||||
@@ -405,7 +425,7 @@ class AppState extends ChangeNotifier {
|
||||
|
||||
/// Rebuilds the current view from the core (after queued writes have run).
|
||||
void reload() {
|
||||
generation++;
|
||||
_bumpGeneration();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -495,7 +515,7 @@ class AppState extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
}
|
||||
generation++;
|
||||
_bumpGeneration();
|
||||
_saveSettings();
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -547,7 +567,7 @@ class AppState extends ChangeNotifier {
|
||||
t.label = null;
|
||||
}
|
||||
}
|
||||
generation++;
|
||||
_bumpGeneration();
|
||||
_saveSettings();
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -557,7 +577,7 @@ class AppState extends ChangeNotifier {
|
||||
tab
|
||||
..route = r
|
||||
..label = null;
|
||||
generation++;
|
||||
_bumpGeneration();
|
||||
_saveSettings();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -443,6 +443,8 @@ class _GraphViewState extends State<GraphView> {
|
||||
onTap: _open,
|
||||
name: AppScope.read(context).backend.graphName,
|
||||
typeIcon: AppScope.read(context).typeIcon,
|
||||
// Subscribes: icon edits (or icons loaded after a cold start) reach the labels.
|
||||
iconsVersion: AppScope.of(context).iconsVersion,
|
||||
);
|
||||
}
|
||||
final sub = _sub;
|
||||
|
||||
Reference in new issue
Block a user