From 8a0fdc7610c42b1e4723444ab0875b3d8b27caf5 Mon Sep 17 00:00:00 2001 From: Dasemu Date: Mon, 26 Oct 2026 06:55:22 +0100 Subject: [PATCH] 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 --- app/lib/src/graph/tree_canvas.dart | 25 ++++++++-- app/lib/src/state/app_state.dart | 40 +++++++++++---- app/lib/src/views/graph_view.dart | 2 + app/test/tags_types_test.dart | 79 ++++++++++++++++++++++++++++++ docs/app.md | 7 +-- 5 files changed, 136 insertions(+), 17 deletions(-) diff --git a/app/lib/src/graph/tree_canvas.dart b/app/lib/src/graph/tree_canvas.dart index bee4391..2f0601e 100644 --- a/app/lib/src/graph/tree_canvas.dart +++ b/app/lib/src/graph/tree_canvas.dart @@ -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 { 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!]; diff --git a/app/lib/src/state/app_state.dart b/app/lib/src/state/app_state.dart index 0729fc9..fc75c80 100644 --- a/app/lib/src/state/app_state.dart +++ b/app/lib/src/state/app_state.dart @@ -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 loadTypeIcons() async { - final types = await backend.typeCatalog(); + final List 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 = {}; @@ -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(); } diff --git a/app/lib/src/views/graph_view.dart b/app/lib/src/views/graph_view.dart index 34df677..d4ca975 100644 --- a/app/lib/src/views/graph_view.dart +++ b/app/lib/src/views/graph_view.dart @@ -443,6 +443,8 @@ class _GraphViewState extends State { 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; diff --git a/app/test/tags_types_test.dart b/app/test/tags_types_test.dart index 5ba3d69..4f3e455 100644 --- a/app/test/tags_types_test.dart +++ b/app/test/tags_types_test.dart @@ -293,4 +293,83 @@ void main() { final data = await b.graphData(); expect(data.nodes.firstWhere((n) => n.title == 'Rome').pageType, 'lugar'); }); + + testWidgets('the tree picks up icons loaded or edited after it was built', ( + tester, + ) async { + final b = await demo(); + tester.view.physicalSize = const Size(1600, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + // Cold start: the icon cache is still empty when the tree is built. + final app = AppState(b)..go(const GraphRoute(GraphMode.tree)); + await tester.pumpWidget( + AppScope( + state: app, + child: MaterialApp( + theme: buildTheme(Brightness.light), + home: const Scaffold(body: GraphView(mode: GraphMode.tree)), + ), + ), + ); + await settle(tester); + TreeItem ada() { + final items = []; + void walk(TreeItem t) { + items.add(t); + t.children.forEach(walk); + } + + walk(tester.state(find.byType(TreeCanvas)).root); + return items.firstWhere((t) => t.label == 'Ada'); + } + + expect(ada().icon, isNull); + await app.loadTypeIcons(); + await settle(tester); + expect(ada().icon, '🧑'); + await b.setTypeIcon('persona', '👤'); + await app.loadTypeIcons(); + await settle(tester); + expect(ada().icon, '👤'); + }); + + test('reload refreshes the icon cache; failures are logged', () async { + final b = await demo(); + final app = AppState(b); + await app.loadTypeIcons(); + expect(app.typeIcon('persona'), '🧑'); + final v = app.iconsVersion; + await app.loadTypeIcons(); + expect(app.iconsVersion, v, reason: 'unchanged icons do not notify'); + // Changed elsewhere (sync, restore): a reload picks it up. + await b.setTypeIcon('persona', '👤'); + app.reload(); + await Future.delayed(Duration.zero); + expect(app.typeIcon('persona'), '👤'); + await AppState(_FailingBackend()).loadTypeIcons(); + }); + + testWidgets( + 'renaming a tag from its view moves the type emoji in the cache', + (tester) async { + final b = await demo(); + final app = await pump(tester, b, route: const TagRoute('persona')); + await tester.tap(find.byKey(const ValueKey('tag-rename'))); + await settle(tester); + await tester.enterText( + find.byKey(const ValueKey('tag-rename-input')), + 'gente', + ); + await tester.tap(find.byKey(const ValueKey('tag-rename-confirm'))); + await settle(tester); + expect(app.typeIcon('gente'), '🧑'); + expect(app.typeIcon('persona'), isNull); + }, + ); +} + +class _FailingBackend extends MemoryBackend { + @override + Future> typeCatalog() async => throw Exception('boom'); } diff --git a/docs/app.md b/docs/app.md index 807f6c5..54f00bd 100644 --- a/docs/app.md +++ b/docs/app.md @@ -151,9 +151,10 @@ Both are plain names with no page, shown as one row of pills under the title. shown before the names of pages of that type: page title, All pages, quick switcher, recents, tabs, hub members, tag view (also its title when the tag is a type), linked references, related pages, the type pill and picker, and the tree view labels. Never in the - network graph. `AppState.typeIcons` caches type → emoji (`loadTypeIcons`, at start and - after edits); list DTOs carry `pageType` and `TypeIcon` (`widgets/type_icon.dart`) looks - it up. + network graph. `AppState.typeIcons` caches type → emoji (`loadTypeIcons`: at start, after + edits and on every view reload, so synced or restored changes show; `iconsVersion` makes + the tree view redraw its labels without a new layout); list DTOs carry `pageType` and + `TypeIcon` (`widgets/type_icon.dart`) looks it up. - **Tags & types view** (sidebar, under All pages; also a tab route): "Types" lists every type with its emoji, page count and seeded suggestions; the emoji button opens a picker (curated grid by category, a field to paste any single emoji, "Remove emoji"); "New type" creates a