mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 15:26:17 +00:00
feat(app): Flutter outliner with journals, page view and timeline
Logseq-like outliner (render/edit Markdown per block, Enter/Tab/Shift-Tab, backspace merge, arrow navigation, drag to reorder, collapse, [[ # (( autocomplete, TODO/DONE), journals feed with prev/next links, page view with linked references and related panel, virtualised CustomPainter timeline, Ctrl+K quick switcher, sidebar with recents, light/dark theme with bundled Inter, Noto Serif and Source Code Pro. Edits are optimistic and serialised to the core through one write queue. Cargokit builds synapse-ffi; its build tool is patched to use the library name (synapse_ffi) of hyphenated crates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c3d6636606
commit
df42d4010f
135 files changed
+17689
No files matched your search
@@ -0,0 +1,111 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../backend/graph_backend.dart';
|
||||
import '../state/app_state.dart';
|
||||
import '../theme/theme.dart';
|
||||
import '../widgets/common.dart';
|
||||
import '../widgets/dates.dart';
|
||||
|
||||
/// Every non-journal page, most recently edited first, loaded in pages of 100.
|
||||
class AllPagesView extends StatefulWidget {
|
||||
const AllPagesView({super.key});
|
||||
|
||||
@override
|
||||
State<AllPagesView> createState() => _AllPagesViewState();
|
||||
}
|
||||
|
||||
class _AllPagesViewState extends State<AllPagesView> {
|
||||
final _pages = <PageDto>[];
|
||||
bool _loading = false;
|
||||
bool _done = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_more();
|
||||
}
|
||||
|
||||
Future<void> _more() async {
|
||||
if (_loading || _done) return;
|
||||
_loading = true;
|
||||
final batch = await AppScope.read(context).backend
|
||||
.listPages(limit: 100, offset: _pages.length);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_pages.addAll(batch);
|
||||
_done = batch.length < 100;
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final app = AppScope.read(context);
|
||||
final colors = context.colors;
|
||||
return LayoutBuilder(
|
||||
builder: (context, box) => NotificationListener<ScrollNotification>(
|
||||
onNotification: (n) {
|
||||
if (n.metrics.extentAfter < 600) _more();
|
||||
return false;
|
||||
},
|
||||
child: ListView.builder(
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
sidePadding(box.maxWidth),
|
||||
36,
|
||||
sidePadding(box.maxWidth),
|
||||
80,
|
||||
),
|
||||
itemCount: _pages.length + 1,
|
||||
itemBuilder: (context, i) {
|
||||
if (i == 0) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 20),
|
||||
child: Text(
|
||||
'All pages',
|
||||
style: Theme.of(context).textTheme.headlineLarge,
|
||||
),
|
||||
);
|
||||
}
|
||||
final p = _pages[i - 1];
|
||||
final edited = DateTime.fromMillisecondsSinceEpoch(
|
||||
p.updatedAt.toInt(),
|
||||
);
|
||||
return InkWell(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
onTap: () => app.openId(p.id),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 9, horizontal: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
p.title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 14.5, color: colors.ink),
|
||||
),
|
||||
),
|
||||
if (p.originJournal != null)
|
||||
Text(
|
||||
'born ${shortDate(parseDay(p.originJournal!))}',
|
||||
style: TextStyle(fontSize: 12, color: colors.faint),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
SizedBox(
|
||||
width: 70,
|
||||
child: Text(
|
||||
shortDate(edited),
|
||||
textAlign: TextAlign.right,
|
||||
style: TextStyle(fontSize: 12, color: colors.muted),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../backend/graph_backend.dart';
|
||||
import '../editor/outline_view.dart';
|
||||
import '../state/app_state.dart';
|
||||
import '../state/outline_controller.dart';
|
||||
import '../theme/theme.dart';
|
||||
import '../widgets/common.dart';
|
||||
import '../widgets/dates.dart';
|
||||
|
||||
/// Continuous feed of journals, today on top, loading older days as you scroll.
|
||||
class JournalsView extends StatefulWidget {
|
||||
const JournalsView({super.key});
|
||||
|
||||
@override
|
||||
State<JournalsView> createState() => _JournalsViewState();
|
||||
}
|
||||
|
||||
class _Day {
|
||||
_Day(this.view, this.controller);
|
||||
final PageData view;
|
||||
final OutlineController controller;
|
||||
}
|
||||
|
||||
class _JournalsViewState extends State<JournalsView> {
|
||||
static const _batch = 7;
|
||||
final _days = <_Day>[];
|
||||
bool _loading = false;
|
||||
bool _exhausted = false;
|
||||
Object? _error;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadMore();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (final d in _days) {
|
||||
d.controller.dispose();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _loadMore() async {
|
||||
if (_loading || _exhausted) return;
|
||||
setState(() => _loading = true);
|
||||
final app = AppScope.read(context);
|
||||
try {
|
||||
final before = _days.isEmpty ? null : _days.last.view.page.journalDate;
|
||||
final views = await app.backend.journals(before: before, limit: _batch);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
for (final v in views) {
|
||||
_days.add(
|
||||
_Day(
|
||||
v,
|
||||
OutlineController(
|
||||
backend: app.backend,
|
||||
queue: app.queue,
|
||||
pageId: v.page.id,
|
||||
blocks: v.blocks,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
_exhausted = views.length < _batch;
|
||||
_loading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_error = e;
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final app = AppScope.read(context);
|
||||
final today = app.backend.today();
|
||||
return LayoutBuilder(
|
||||
builder: (context, box) {
|
||||
final pad = sidePadding(box.maxWidth);
|
||||
return NotificationListener<ScrollNotification>(
|
||||
onNotification: (n) {
|
||||
if (n.metrics.extentAfter < 800) _loadMore();
|
||||
return false;
|
||||
},
|
||||
child: CustomScrollView(
|
||||
slivers: [
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 28)),
|
||||
for (final d in _days)
|
||||
SliverPadding(
|
||||
padding: EdgeInsets.symmetric(horizontal: pad),
|
||||
sliver: SliverMainAxisGroup(
|
||||
slivers: [
|
||||
SliverToBoxAdapter(
|
||||
child: _JournalHeader(view: d.view, today: today),
|
||||
),
|
||||
OutlineSliver(controller: d.controller),
|
||||
SliverToBoxAdapter(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 36),
|
||||
child: Divider(height: 1, color: context.colors.line),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(bottom: 80),
|
||||
child: Center(
|
||||
child: _error != null
|
||||
? Text('Could not load journals: $_error')
|
||||
: _exhausted
|
||||
? Text(
|
||||
'The beginning of your journals',
|
||||
style: TextStyle(
|
||||
color: context.colors.faint,
|
||||
fontSize: 13,
|
||||
),
|
||||
)
|
||||
: const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _JournalHeader extends StatelessWidget {
|
||||
const _JournalHeader({required this.view, required this.today});
|
||||
final PageData view;
|
||||
final String today;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final app = AppScope.read(context);
|
||||
final date = view.page.journalDate!;
|
||||
final colors = context.colors;
|
||||
final rel = relativeDay(date, today);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 14),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Flexible(
|
||||
child: MouseRegion(
|
||||
cursor: SystemMouseCursors.click,
|
||||
child: GestureDetector(
|
||||
onTap: () => app.openId(view.page.id),
|
||||
child: Text(
|
||||
longDate(date),
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 4),
|
||||
child: Text(
|
||||
rel == null ? weekday(date) : '$rel · ${weekday(date)}',
|
||||
style: TextStyle(color: colors.muted, fontSize: 13),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
JournalChain(view: view),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Previous / next journal links (the episodic chain).
|
||||
class JournalChain extends StatelessWidget {
|
||||
const JournalChain({super.key, required this.view});
|
||||
final PageData view;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final app = AppScope.read(context);
|
||||
final prev = view.prevJournal;
|
||||
final next = view.nextJournal;
|
||||
if (prev == null && next == null) return const SizedBox.shrink();
|
||||
return Row(
|
||||
children: [
|
||||
if (prev != null)
|
||||
TextLink(
|
||||
shortDate(parseDay(prev)),
|
||||
icon: Icons.west_rounded,
|
||||
onTap: () => app.openTitle(prev),
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
if (prev != null && next != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10),
|
||||
child: Text('·', style: TextStyle(color: context.colors.faint)),
|
||||
),
|
||||
if (next != null)
|
||||
TextLink(
|
||||
shortDate(parseDay(next)),
|
||||
icon: Icons.east_rounded,
|
||||
iconAfter: true,
|
||||
onTap: () => app.openTitle(next),
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,476 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../backend/graph_backend.dart';
|
||||
import '../editor/outline_view.dart';
|
||||
import '../state/app_state.dart';
|
||||
import '../state/outline_controller.dart';
|
||||
import '../theme/theme.dart';
|
||||
import '../widgets/common.dart';
|
||||
import '../widgets/dates.dart';
|
||||
import 'journals_view.dart';
|
||||
|
||||
/// A single page: title, aliases, outline, linked references and related notes.
|
||||
class PageScreen extends StatefulWidget {
|
||||
const PageScreen({super.key, required this.target});
|
||||
final PageTarget target;
|
||||
|
||||
@override
|
||||
State<PageScreen> createState() => _PageScreenState();
|
||||
}
|
||||
|
||||
class _PageScreenState extends State<PageScreen> {
|
||||
PageData? _view;
|
||||
OutlineController? _outline;
|
||||
List<BacklinkGroup>? _backlinks;
|
||||
List<RelatedDto>? _related;
|
||||
Object? _error;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_outline?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
final app = AppScope.read(context);
|
||||
try {
|
||||
final t = widget.target;
|
||||
PageData? v;
|
||||
if (t.id != null) {
|
||||
v = await app.backend.page(t.id!);
|
||||
} else {
|
||||
v = await app.backend.openPage(t.title!);
|
||||
}
|
||||
if (!mounted) return;
|
||||
if (v == null) {
|
||||
setState(() => _error = 'This page no longer exists.');
|
||||
return;
|
||||
}
|
||||
final view = v;
|
||||
setState(() {
|
||||
_view = view;
|
||||
_outline = OutlineController(
|
||||
backend: app.backend,
|
||||
queue: app.queue,
|
||||
pageId: view.page.id,
|
||||
blocks: view.blocks,
|
||||
);
|
||||
});
|
||||
app.pageShown(view.page);
|
||||
// Make sure pending writes (e.g. a link just typed elsewhere) are visible.
|
||||
await app.queue.idle;
|
||||
final results = await Future.wait([
|
||||
app.backend.backlinks(view.page.id),
|
||||
app.backend.related(view.page.id),
|
||||
]);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_backlinks = results[0] as List<BacklinkGroup>;
|
||||
_related = results[1] as List<RelatedDto>;
|
||||
});
|
||||
} catch (e) {
|
||||
if (mounted) setState(() => _error = e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_error != null) {
|
||||
return Center(
|
||||
child: Text('$_error', style: TextStyle(color: context.colors.muted)),
|
||||
);
|
||||
}
|
||||
final view = _view;
|
||||
if (view == null) return const SizedBox.shrink();
|
||||
return LayoutBuilder(
|
||||
builder: (context, box) {
|
||||
final wide = box.maxWidth >= 1180;
|
||||
final mainWidth = wide ? box.maxWidth - 300 : box.maxWidth;
|
||||
final pad = sidePadding(mainWidth);
|
||||
final scroll = CustomScrollView(
|
||||
slivers: [
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 36)),
|
||||
SliverPadding(
|
||||
padding: EdgeInsets.symmetric(horizontal: pad),
|
||||
sliver: SliverMainAxisGroup(
|
||||
slivers: [
|
||||
SliverToBoxAdapter(child: _Header(view: view)),
|
||||
OutlineSliver(controller: _outline!),
|
||||
SliverToBoxAdapter(
|
||||
child: _LinkedReferences(groups: _backlinks),
|
||||
),
|
||||
if (!wide)
|
||||
SliverToBoxAdapter(child: RelatedPanel(related: _related)),
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 120)),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
if (!wide) return scroll;
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(child: scroll),
|
||||
Container(
|
||||
width: 300,
|
||||
decoration: BoxDecoration(
|
||||
border: Border(left: BorderSide(color: context.colors.line)),
|
||||
),
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.fromLTRB(20, 44, 20, 40),
|
||||
child: RelatedPanel(related: _related, compact: true),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Header extends StatelessWidget {
|
||||
const _Header({required this.view});
|
||||
final PageData view;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final page = view.page;
|
||||
final colors = context.colors;
|
||||
final journal = page.journalDate;
|
||||
final app = AppScope.read(context);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 18),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (journal != null) ...[
|
||||
Text(
|
||||
longDate(journal),
|
||||
style: Theme.of(context).textTheme.headlineLarge,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
weekday(journal),
|
||||
style: TextStyle(color: colors.muted, fontSize: 13),
|
||||
),
|
||||
const SizedBox(width: 14),
|
||||
JournalChain(view: view),
|
||||
],
|
||||
),
|
||||
] else ...[
|
||||
_TitleField(page: page),
|
||||
const SizedBox(height: 6),
|
||||
Wrap(
|
||||
spacing: 14,
|
||||
runSpacing: 4,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
if (page.originJournal != null)
|
||||
TextLink(
|
||||
'Born ${longDate(page.originJournal!)}',
|
||||
icon: Icons.spa_outlined,
|
||||
onTap: () => app.openTitle(page.originJournal!),
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
_Aliases(page: page, initial: view.aliases),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Page title; editing it renames the page (and rewrites every link to it).
|
||||
class _TitleField extends StatefulWidget {
|
||||
const _TitleField({required this.page});
|
||||
final PageDto page;
|
||||
|
||||
@override
|
||||
State<_TitleField> createState() => _TitleFieldState();
|
||||
}
|
||||
|
||||
class _TitleFieldState extends State<_TitleField> {
|
||||
late final _text = TextEditingController(text: widget.page.title);
|
||||
late final _focus = FocusNode()..addListener(_blur);
|
||||
late String _committed = widget.page.title;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_text.dispose();
|
||||
_focus.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _blur() {
|
||||
if (!_focus.hasFocus) _commit();
|
||||
}
|
||||
|
||||
Future<void> _commit() async {
|
||||
final title = _text.text.trim();
|
||||
if (title.isEmpty || title == _committed) {
|
||||
_text.text = _committed;
|
||||
return;
|
||||
}
|
||||
final app = AppScope.read(context);
|
||||
final messenger = ScaffoldMessenger.of(context);
|
||||
try {
|
||||
await app.queue.run(() => app.backend.renamePage(widget.page.id, title));
|
||||
_committed = title;
|
||||
app.renamed(widget.page.id, title);
|
||||
} catch (e) {
|
||||
_text.text = _committed;
|
||||
messenger.showSnackBar(SnackBar(content: Text('Rename failed: $e')));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => TextField(
|
||||
controller: _text,
|
||||
focusNode: _focus,
|
||||
style: Theme.of(context).textTheme.headlineLarge,
|
||||
decoration: const InputDecoration.collapsed(hintText: 'Untitled'),
|
||||
maxLines: null,
|
||||
onSubmitted: (_) => _focus.unfocus(),
|
||||
textInputAction: TextInputAction.done,
|
||||
);
|
||||
}
|
||||
|
||||
class _Aliases extends StatefulWidget {
|
||||
const _Aliases({required this.page, required this.initial});
|
||||
final PageDto page;
|
||||
final List<String> initial;
|
||||
|
||||
@override
|
||||
State<_Aliases> createState() => _AliasesState();
|
||||
}
|
||||
|
||||
class _AliasesState extends State<_Aliases> {
|
||||
late final List<String> _aliases = List.of(widget.initial);
|
||||
bool _adding = false;
|
||||
final _text = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_text.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _add() async {
|
||||
final alias = _text.text.trim();
|
||||
setState(() => _adding = false);
|
||||
_text.clear();
|
||||
if (alias.isEmpty || _aliases.contains(alias)) return;
|
||||
final app = AppScope.read(context);
|
||||
await app.queue.run(() => app.backend.addAlias(widget.page.id, alias));
|
||||
if (mounted) setState(() => _aliases.add(alias));
|
||||
}
|
||||
|
||||
Future<void> _remove(String alias) async {
|
||||
final app = AppScope.read(context);
|
||||
await app.queue.run(() => app.backend.removeAlias(alias));
|
||||
if (mounted) setState(() => _aliases.remove(alias));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = context.colors;
|
||||
return Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 4,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
for (final a in _aliases)
|
||||
Container(
|
||||
padding: const EdgeInsets.only(left: 8, right: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(a, style: TextStyle(fontSize: 12, color: colors.muted)),
|
||||
InkResponse(
|
||||
radius: 10,
|
||||
onTap: () => _remove(a),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(3),
|
||||
child: Icon(
|
||||
Icons.close_rounded,
|
||||
size: 12,
|
||||
color: colors.faint,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (_adding)
|
||||
SizedBox(
|
||||
width: 140,
|
||||
child: TextField(
|
||||
controller: _text,
|
||||
autofocus: true,
|
||||
style: TextStyle(fontSize: 12, color: colors.ink),
|
||||
decoration: const InputDecoration.collapsed(hintText: 'alias'),
|
||||
onSubmitted: (_) => _add(),
|
||||
onTapOutside: (_) => _add(),
|
||||
),
|
||||
)
|
||||
else
|
||||
TextLink(
|
||||
_aliases.isEmpty ? 'Add alias' : 'Alias',
|
||||
icon: Icons.add_rounded,
|
||||
onTap: () => setState(() => _adding = true),
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LinkedReferences extends StatelessWidget {
|
||||
const _LinkedReferences({required this.groups});
|
||||
final List<BacklinkGroup>? groups;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final g = groups;
|
||||
if (g == null || g.isEmpty) return const SizedBox(height: 40);
|
||||
final app = AppScope.read(context);
|
||||
final colors = context.colors;
|
||||
final count = g.fold<int>(0, (n, x) => n + x.blocks.length);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 56),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SectionLabel(
|
||||
'Linked references',
|
||||
trailing: Text(
|
||||
'$count',
|
||||
style: TextStyle(fontSize: 11, color: colors.faint),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
for (final group in g)
|
||||
Container(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
padding: const EdgeInsets.fromLTRB(14, 10, 14, 10),
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surface,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
TextLink(
|
||||
group.isJournal
|
||||
? longDate(group.pageTitle)
|
||||
: group.pageTitle,
|
||||
onTap: () => app.openId(group.pageId),
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.ink,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
for (final b in group.blocks)
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Dot(),
|
||||
const SizedBox(width: 4),
|
||||
Expanded(
|
||||
child: StaticBlock(
|
||||
content: b.content,
|
||||
onTap: () => app.openId(group.pageId),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Notes related by spreading activation over links, synapses and time.
|
||||
class RelatedPanel extends StatelessWidget {
|
||||
const RelatedPanel({super.key, required this.related, this.compact = false});
|
||||
final List<RelatedDto>? related;
|
||||
final bool compact;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final r = related;
|
||||
final colors = context.colors;
|
||||
final app = AppScope.read(context);
|
||||
final top = (r == null || r.isEmpty) ? 1.0 : r.first.score;
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(top: compact ? 0 : 40),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SectionLabel('Related'),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'Strengthened by links, visits and shared days',
|
||||
style: TextStyle(fontSize: 12, color: colors.faint),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
if (r == null)
|
||||
const SizedBox.shrink()
|
||||
else if (r.isEmpty)
|
||||
Text(
|
||||
'Nothing yet. Link this note to others.',
|
||||
style: TextStyle(fontSize: 13, color: colors.muted),
|
||||
)
|
||||
else
|
||||
for (final x in r)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 10),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
TextLink(
|
||||
x.page.isJournal ? longDate(x.page.title) : x.page.title,
|
||||
icon: x.page.isJournal
|
||||
? Icons.calendar_today_outlined
|
||||
: Icons.description_outlined,
|
||||
onTap: () => app.openId(x.page.id),
|
||||
style: TextStyle(fontSize: 13.5, color: colors.ink),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
child: LinearProgressIndicator(
|
||||
value: (x.score / top).clamp(0.05, 1.0),
|
||||
minHeight: 3,
|
||||
backgroundColor: colors.line,
|
||||
color: colors.accent.withAlpha(0x99),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../backend/graph_backend.dart';
|
||||
import '../state/app_state.dart';
|
||||
import '../theme/theme.dart';
|
||||
|
||||
/// Ctrl+K: jump to a page by title or alias, or create it.
|
||||
Future<void> showQuickSwitcher(BuildContext context) => showDialog<void>(
|
||||
context: context,
|
||||
barrierColor: Colors.black.withAlpha(0x33),
|
||||
builder: (_) =>
|
||||
AppScope(state: AppScope.read(context), child: const QuickSwitcher()),
|
||||
);
|
||||
|
||||
class QuickSwitcher extends StatefulWidget {
|
||||
const QuickSwitcher({super.key});
|
||||
|
||||
@override
|
||||
State<QuickSwitcher> createState() => _QuickSwitcherState();
|
||||
}
|
||||
|
||||
class _QuickSwitcherState extends State<QuickSwitcher> {
|
||||
final _text = TextEditingController();
|
||||
List<TitleHit> _hits = const [];
|
||||
int _selected = 0;
|
||||
int _seq = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_search('');
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_text.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String get _query => _text.text.trim();
|
||||
|
||||
bool get _offerCreate =>
|
||||
_query.isNotEmpty &&
|
||||
!_hits.any((h) => h.title.toLowerCase() == _query.toLowerCase());
|
||||
|
||||
int get _count => _hits.length + (_offerCreate ? 1 : 0);
|
||||
|
||||
Future<void> _search(String q) async {
|
||||
final seq = ++_seq;
|
||||
final hits = await AppScope.read(context).backend
|
||||
.searchTitles(q, limit: 12);
|
||||
if (!mounted || seq != _seq) return;
|
||||
setState(() {
|
||||
_hits = hits;
|
||||
_selected = 0;
|
||||
});
|
||||
}
|
||||
|
||||
void _open(int i) {
|
||||
final app = AppScope.read(context);
|
||||
Navigator.of(context).pop();
|
||||
if (i < _hits.length) {
|
||||
app.openId(_hits[i].pageId);
|
||||
} else if (_query.isNotEmpty) {
|
||||
app.openTitle(_query);
|
||||
}
|
||||
}
|
||||
|
||||
KeyEventResult _key(FocusNode _, KeyEvent e) {
|
||||
if (e is! KeyDownEvent && e is! KeyRepeatEvent) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
if (_count == 0) return KeyEventResult.ignored;
|
||||
if (e.logicalKey == LogicalKeyboardKey.arrowDown) {
|
||||
setState(() => _selected = (_selected + 1) % _count);
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
if (e.logicalKey == LogicalKeyboardKey.arrowUp) {
|
||||
setState(() => _selected = (_selected - 1 + _count) % _count);
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = context.colors;
|
||||
return Align(
|
||||
alignment: const Alignment(0, -0.55),
|
||||
child: Material(
|
||||
color: colors.paper,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: Container(
|
||||
width: 560,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: colors.line),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(18, 14, 18, 12),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.search_rounded, size: 18, color: colors.muted),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Focus(
|
||||
onKeyEvent: _key,
|
||||
child: TextField(
|
||||
controller: _text,
|
||||
autofocus: true,
|
||||
style: TextStyle(fontSize: 16, color: colors.ink),
|
||||
decoration: const InputDecoration.collapsed(
|
||||
hintText: 'Search or create a page',
|
||||
),
|
||||
onChanged: (q) => _search(q),
|
||||
onSubmitted: (_) => _open(_selected),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (_count > 0) Divider(height: 1, color: colors.line),
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxHeight: 380),
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
padding: const EdgeInsets.all(6),
|
||||
children: [
|
||||
for (var i = 0; i < _count; i++)
|
||||
_Item(
|
||||
selected: i == _selected,
|
||||
onTap: () => _open(i),
|
||||
icon: i < _hits.length
|
||||
? Icons.description_outlined
|
||||
: Icons.add_rounded,
|
||||
title: i < _hits.length
|
||||
? _hits[i].title
|
||||
: 'Create page “$_query”',
|
||||
detail: i < _hits.length && _hits[i].isAlias
|
||||
? 'alias'
|
||||
: null,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Item extends StatelessWidget {
|
||||
const _Item({
|
||||
required this.selected,
|
||||
required this.onTap,
|
||||
required this.icon,
|
||||
required this.title,
|
||||
this.detail,
|
||||
});
|
||||
final bool selected;
|
||||
final VoidCallback onTap;
|
||||
final IconData icon;
|
||||
final String title;
|
||||
final String? detail;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = context.colors;
|
||||
return MouseRegion(
|
||||
cursor: SystemMouseCursors.click,
|
||||
child: GestureDetector(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 9),
|
||||
decoration: BoxDecoration(
|
||||
color: selected ? colors.accentSoft : null,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
size: 16,
|
||||
color: selected ? colors.accent : colors.muted,
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Text(
|
||||
title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 14, color: colors.ink),
|
||||
),
|
||||
),
|
||||
if (detail != null)
|
||||
Text(
|
||||
detail!,
|
||||
style: TextStyle(fontSize: 12, color: colors.faint),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,390 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../state/app_state.dart';
|
||||
import '../theme/theme.dart';
|
||||
import '../widgets/dates.dart';
|
||||
import 'all_pages_view.dart';
|
||||
import 'journals_view.dart';
|
||||
import 'page_screen.dart';
|
||||
import 'quick_switcher.dart';
|
||||
import 'timeline_view.dart';
|
||||
|
||||
/// App frame: sidebar (navigation, recents, theme), top bar and the current view.
|
||||
class Shell extends StatefulWidget {
|
||||
const Shell({super.key});
|
||||
|
||||
@override
|
||||
State<Shell> createState() => _ShellState();
|
||||
}
|
||||
|
||||
class _ShellState extends State<Shell> {
|
||||
bool _sidebar = true;
|
||||
final _scaffold = GlobalKey<ScaffoldState>();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final app = AppScope.of(context);
|
||||
final colors = context.colors;
|
||||
final route = app.route;
|
||||
return CallbackShortcuts(
|
||||
bindings: {
|
||||
const SingleActivator(LogicalKeyboardKey.keyK, control: true): () =>
|
||||
showQuickSwitcher(context),
|
||||
const SingleActivator(LogicalKeyboardKey.keyK, meta: true): () =>
|
||||
showQuickSwitcher(context),
|
||||
const SingleActivator(
|
||||
LogicalKeyboardKey.keyJ,
|
||||
control: true,
|
||||
shift: true,
|
||||
): () =>
|
||||
app.go(const JournalsRoute()),
|
||||
const SingleActivator(
|
||||
LogicalKeyboardKey.keyT,
|
||||
control: true,
|
||||
shift: true,
|
||||
): () =>
|
||||
app.go(const TimelineRoute()),
|
||||
const SingleActivator(
|
||||
LogicalKeyboardKey.backslash,
|
||||
control: true,
|
||||
): () =>
|
||||
setState(() => _sidebar = !_sidebar),
|
||||
const SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true):
|
||||
app.back,
|
||||
const SingleActivator(LogicalKeyboardKey.arrowRight, alt: true):
|
||||
app.forward,
|
||||
},
|
||||
child: Focus(
|
||||
autofocus: true,
|
||||
child: LayoutBuilder(
|
||||
builder: (context, box) {
|
||||
final narrow = box.maxWidth < 760;
|
||||
final sidebar = _Sidebar(
|
||||
onNavigate: () {
|
||||
if (narrow) _scaffold.currentState?.closeDrawer();
|
||||
},
|
||||
);
|
||||
return Scaffold(
|
||||
key: _scaffold,
|
||||
drawer: narrow
|
||||
? Drawer(backgroundColor: colors.surface, child: sidebar)
|
||||
: null,
|
||||
body: Row(
|
||||
children: [
|
||||
if (!narrow)
|
||||
AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 180),
|
||||
curve: Curves.easeOutCubic,
|
||||
width: _sidebar ? 248 : 0,
|
||||
child: ClipRect(
|
||||
child: OverflowBox(
|
||||
alignment: Alignment.topLeft,
|
||||
minWidth: 248,
|
||||
maxWidth: 248,
|
||||
child: sidebar,
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Column(
|
||||
children: [
|
||||
_TopBar(
|
||||
onMenu: () => narrow
|
||||
? _scaffold.currentState?.openDrawer()
|
||||
: setState(() => _sidebar = !_sidebar),
|
||||
),
|
||||
Expanded(
|
||||
child: KeyedSubtree(
|
||||
key: ValueKey(route),
|
||||
child: switch (route) {
|
||||
JournalsRoute() => const JournalsView(),
|
||||
TimelineRoute() => const TimelineView(),
|
||||
AllPagesRoute() => const AllPagesView(),
|
||||
PageTarget() => PageScreen(target: route),
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TopBar extends StatelessWidget {
|
||||
const _TopBar({required this.onMenu});
|
||||
final VoidCallback onMenu;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final app = AppScope.of(context);
|
||||
final colors = context.colors;
|
||||
final brightness = Theme.of(context).brightness;
|
||||
Widget icon(IconData i, String tip, VoidCallback? f) => IconButton(
|
||||
tooltip: tip,
|
||||
onPressed: f,
|
||||
iconSize: 18,
|
||||
color: colors.muted,
|
||||
disabledColor: colors.line,
|
||||
icon: Icon(i),
|
||||
);
|
||||
return Container(
|
||||
height: 48,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
icon(Icons.menu_rounded, 'Toggle sidebar (Ctrl+\\)', onMenu),
|
||||
icon(
|
||||
Icons.arrow_back_rounded,
|
||||
'Back (Alt+←)',
|
||||
app.canGoBack ? app.back : null,
|
||||
),
|
||||
icon(
|
||||
Icons.arrow_forward_rounded,
|
||||
'Forward (Alt+→)',
|
||||
app.canGoForward ? app.forward : null,
|
||||
),
|
||||
const Spacer(),
|
||||
_SearchPill(onTap: () => showQuickSwitcher(context)),
|
||||
const SizedBox(width: 4),
|
||||
icon(
|
||||
brightness == Brightness.dark
|
||||
? Icons.light_mode_outlined
|
||||
: Icons.dark_mode_outlined,
|
||||
'Toggle theme',
|
||||
() => app.toggleTheme(brightness),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SearchPill extends StatelessWidget {
|
||||
const _SearchPill({required this.onTap});
|
||||
final VoidCallback onTap;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = context.colors;
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: colors.line),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.search_rounded, size: 16, color: colors.muted),
|
||||
const SizedBox(width: 8),
|
||||
Text('Search', style: TextStyle(fontSize: 13, color: colors.muted)),
|
||||
const SizedBox(width: 18),
|
||||
Text('Ctrl K', style: TextStyle(fontSize: 11, color: colors.faint)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Sidebar extends StatelessWidget {
|
||||
const _Sidebar({required this.onNavigate});
|
||||
final VoidCallback onNavigate;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final app = AppScope.of(context);
|
||||
final colors = context.colors;
|
||||
final route = app.route;
|
||||
void go(AppRoute r) {
|
||||
app.go(r);
|
||||
onNavigate();
|
||||
}
|
||||
|
||||
return Container(
|
||||
color: colors.surface,
|
||||
child: SafeArea(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(20, 18, 20, 22),
|
||||
child: Row(
|
||||
children: [
|
||||
CustomPaint(
|
||||
size: const Size(20, 20),
|
||||
painter: _Logo(colors.accent),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Text(
|
||||
'Synapse',
|
||||
style: TextStyle(
|
||||
fontFamily: titleFont,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: colors.ink,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
_NavItem(
|
||||
icon: Icons.calendar_today_outlined,
|
||||
label: 'Journals',
|
||||
selected: route is JournalsRoute,
|
||||
onTap: () => go(const JournalsRoute()),
|
||||
),
|
||||
_NavItem(
|
||||
icon: Icons.timeline_rounded,
|
||||
label: 'Timeline',
|
||||
selected: route is TimelineRoute,
|
||||
onTap: () => go(const TimelineRoute()),
|
||||
),
|
||||
_NavItem(
|
||||
icon: Icons.description_outlined,
|
||||
label: 'All pages',
|
||||
selected: route is AllPagesRoute,
|
||||
onTap: () => go(const AllPagesRoute()),
|
||||
),
|
||||
const SizedBox(height: 22),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
child: Text(
|
||||
'RECENT',
|
||||
style: Theme.of(context).textTheme.labelSmall,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
Expanded(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
children: [
|
||||
for (final p in app.recent)
|
||||
_NavItem(
|
||||
label: p.isJournal ? longDate(p.title) : p.title,
|
||||
selected: route is PageTarget && route.id == p.id,
|
||||
dense: true,
|
||||
onTap: () => go(PageTarget(id: p.id)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _NavItem extends StatefulWidget {
|
||||
const _NavItem({
|
||||
this.icon,
|
||||
required this.label,
|
||||
required this.selected,
|
||||
required this.onTap,
|
||||
this.dense = false,
|
||||
});
|
||||
final IconData? icon;
|
||||
final String label;
|
||||
final bool selected;
|
||||
final VoidCallback onTap;
|
||||
final bool dense;
|
||||
|
||||
@override
|
||||
State<_NavItem> createState() => _NavItemState();
|
||||
}
|
||||
|
||||
class _NavItemState extends State<_NavItem> {
|
||||
bool _hover = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = context.colors;
|
||||
final active = widget.selected;
|
||||
return MouseRegion(
|
||||
cursor: SystemMouseCursors.click,
|
||||
onEnter: (_) => setState(() => _hover = true),
|
||||
onExit: (_) => setState(() => _hover = false),
|
||||
child: GestureDetector(
|
||||
onTap: widget.onTap,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 1),
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: 10,
|
||||
vertical: widget.dense ? 6 : 8,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: active
|
||||
? colors.accentSoft
|
||||
: _hover
|
||||
? colors.line.withAlpha(0x66)
|
||||
: null,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
if (widget.icon != null) ...[
|
||||
Icon(
|
||||
widget.icon,
|
||||
size: 16,
|
||||
color: active ? colors.accent : colors.muted,
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
],
|
||||
Expanded(
|
||||
child: Text(
|
||||
widget.label,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: widget.dense ? 13 : 14,
|
||||
fontWeight: active ? FontWeight.w600 : FontWeight.w500,
|
||||
color: active
|
||||
? colors.ink
|
||||
: (widget.dense ? colors.muted : colors.ink),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Two linked nodes: the mark.
|
||||
class _Logo extends CustomPainter {
|
||||
_Logo(this.color);
|
||||
final Color color;
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final p = Paint()..color = color;
|
||||
final a = Offset(size.width * 0.25, size.height * 0.68);
|
||||
final b = Offset(size.width * 0.75, size.height * 0.32);
|
||||
canvas.drawLine(
|
||||
a,
|
||||
b,
|
||||
Paint()
|
||||
..color = color.withAlpha(0x99)
|
||||
..strokeWidth = 2,
|
||||
);
|
||||
canvas.drawCircle(a, size.width * 0.18, p);
|
||||
canvas.drawCircle(b, size.width * 0.14, p);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(_Logo old) => old.color != color;
|
||||
}
|
||||
@@ -0,0 +1,507 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../backend/graph_backend.dart';
|
||||
import '../state/app_state.dart';
|
||||
import '../theme/theme.dart';
|
||||
import '../widgets/dates.dart';
|
||||
|
||||
/// Loaded timeline data, fetched in fixed chunks of days as the view pans.
|
||||
class TimelineModel extends ChangeNotifier {
|
||||
TimelineModel(this.backend, this.today);
|
||||
|
||||
static const chunkDays = 32;
|
||||
final GraphBackend backend;
|
||||
|
||||
/// Day index 0.
|
||||
final DateTime today;
|
||||
|
||||
final _loaded = <int>{};
|
||||
final days = <int, TimelineDayDto>{};
|
||||
final titles = <String, PageBrief>{};
|
||||
|
||||
/// Where each page hangs: (day index, row).
|
||||
final born = <String, (int, int)>{};
|
||||
|
||||
DateTime dayAt(int index) => today.add(Duration(days: index));
|
||||
int indexOf(String ymd) => parseDay(ymd).difference(today).inDays;
|
||||
|
||||
void ensure(int from, int to) {
|
||||
final c0 = (from / chunkDays).floor();
|
||||
final c1 = (to / chunkDays).floor();
|
||||
for (var c = c0; c <= c1; c++) {
|
||||
if (_loaded.add(c)) _load(c);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _load(int chunk) async {
|
||||
final start = chunk * chunkDays;
|
||||
try {
|
||||
final t = await backend.timeline(
|
||||
formatDay(dayAt(start)),
|
||||
formatDay(dayAt(start + chunkDays - 1)),
|
||||
);
|
||||
for (final p in t.pages) {
|
||||
titles[p.id] = p;
|
||||
}
|
||||
for (final d in t.days) {
|
||||
final i = indexOf(d.date);
|
||||
days[i] = d;
|
||||
for (var k = 0; k < d.created.length; k++) {
|
||||
born[d.created[k]] = (i, k);
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
_loaded.remove(chunk);
|
||||
debugPrint('timeline chunk $chunk failed: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Journals on a horizontal line, each linked to the next; pages hang below the journal
|
||||
/// they were born in, with faint edges to the days they were edited. Pan by dragging, zoom
|
||||
/// with the wheel or a pinch, tap a node to open it.
|
||||
class TimelineView extends StatefulWidget {
|
||||
const TimelineView({super.key});
|
||||
|
||||
@override
|
||||
State<TimelineView> createState() => _TimelineViewState();
|
||||
}
|
||||
|
||||
class _TimelineViewState extends State<TimelineView> {
|
||||
late final TimelineModel _model;
|
||||
final _view = _Viewport();
|
||||
final _hits = <(Rect, String)>[];
|
||||
double _startDayWidth = 0;
|
||||
bool _placed = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final backend = AppScope.read(context).backend;
|
||||
_model = TimelineModel(backend, parseDay(backend.today()));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_model.dispose();
|
||||
_view.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _zoom(double factor, double focalX) {
|
||||
final dw = (_view.dayWidth * factor).clamp(4.0, 260.0);
|
||||
// Keep the day under the focal point fixed.
|
||||
final day = (focalX - _view.x) / _view.dayWidth;
|
||||
_view.update(dayWidth: dw, x: focalX - day * dw);
|
||||
}
|
||||
|
||||
void _tap(Offset p) {
|
||||
for (final (r, id) in _hits.reversed) {
|
||||
if (r.inflate(3).contains(p)) {
|
||||
AppScope.read(context).openId(id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = context.colors;
|
||||
return LayoutBuilder(
|
||||
builder: (context, box) {
|
||||
if (!_placed) {
|
||||
_placed = true;
|
||||
_view.x = box.maxWidth - 140;
|
||||
}
|
||||
return Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: ClipRect(
|
||||
child: Listener(
|
||||
onPointerSignal: (e) {
|
||||
if (e is PointerScrollEvent) {
|
||||
if (HardwareKeyboard.instance.isShiftPressed) {
|
||||
_view.update(x: _view.x - e.scrollDelta.dy);
|
||||
} else {
|
||||
_zoom(
|
||||
math.pow(1.0015, -e.scrollDelta.dy).toDouble(),
|
||||
e.localPosition.dx,
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTapUp: (d) => _tap(d.localPosition),
|
||||
onScaleStart: (_) => _startDayWidth = _view.dayWidth,
|
||||
onScaleUpdate: (d) {
|
||||
if (d.scale != 1.0) {
|
||||
final target = _startDayWidth * d.scale;
|
||||
_zoom(target / _view.dayWidth, d.localFocalPoint.dx);
|
||||
}
|
||||
_view.update(
|
||||
x: _view.x + d.focalPointDelta.dx,
|
||||
y: math.min(0, _view.y + d.focalPointDelta.dy),
|
||||
);
|
||||
},
|
||||
child: CustomPaint(
|
||||
painter: _TimelinePainter(
|
||||
model: _model,
|
||||
view: _view,
|
||||
colors: colors,
|
||||
hits: _hits,
|
||||
textScaler: MediaQuery.textScalerOf(context),
|
||||
),
|
||||
size: Size.infinite,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
right: 20,
|
||||
top: 16,
|
||||
child: _Toolbar(
|
||||
onToday: () => _view.update(x: box.maxWidth - 140, y: 0),
|
||||
onZoomIn: () => _zoom(1.4, box.maxWidth / 2),
|
||||
onZoomOut: () => _zoom(1 / 1.4, box.maxWidth / 2),
|
||||
),
|
||||
),
|
||||
Positioned(left: 24, bottom: 18, child: _Legend(colors: colors)),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Viewport extends ChangeNotifier {
|
||||
double dayWidth = 64;
|
||||
|
||||
/// Screen x of day 0 (today).
|
||||
double x = 0;
|
||||
|
||||
/// Vertical scroll of the hanging pages (<= 0).
|
||||
double y = 0;
|
||||
|
||||
void update({double? dayWidth, double? x, double? y}) {
|
||||
this.dayWidth = dayWidth ?? this.dayWidth;
|
||||
this.x = x ?? this.x;
|
||||
this.y = y ?? this.y;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class _TimelinePainter extends CustomPainter {
|
||||
_TimelinePainter({
|
||||
required this.model,
|
||||
required this.view,
|
||||
required this.colors,
|
||||
required this.hits,
|
||||
required this.textScaler,
|
||||
}) : super(repaint: Listenable.merge([model, view]));
|
||||
|
||||
final TimelineModel model;
|
||||
final _Viewport view;
|
||||
final SynapseColors colors;
|
||||
final List<(Rect, String)> hits;
|
||||
final TextScaler textScaler;
|
||||
|
||||
static const axisY = 120.0;
|
||||
static const rowH = 28.0;
|
||||
|
||||
double xOf(int day) => view.x + day * view.dayWidth;
|
||||
|
||||
TextPainter _label(String text, TextStyle style, double maxWidth) {
|
||||
return TextPainter(
|
||||
text: TextSpan(text: text, style: style),
|
||||
textDirection: TextDirection.ltr,
|
||||
maxLines: 1,
|
||||
ellipsis: '…',
|
||||
textScaler: textScaler,
|
||||
)..layout(maxWidth: math.max(0, maxWidth));
|
||||
}
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
hits.clear();
|
||||
final dw = view.dayWidth;
|
||||
final first = ((0 - view.x) / dw).floor() - 1;
|
||||
final last = ((size.width - view.x) / dw).ceil() + 1;
|
||||
// Load a margin around the viewport so edges to nearby days can be drawn.
|
||||
final margin = (size.width / dw).ceil();
|
||||
model.ensure(first - margin, last + margin);
|
||||
|
||||
// Axis.
|
||||
canvas.drawLine(
|
||||
Offset(0, axisY),
|
||||
Offset(size.width, axisY),
|
||||
Paint()
|
||||
..color = colors.line
|
||||
..strokeWidth = 1,
|
||||
);
|
||||
|
||||
// Month and day ticks.
|
||||
final labelStyle = TextStyle(
|
||||
fontFamily: uiFont,
|
||||
fontSize: 11,
|
||||
color: colors.muted,
|
||||
);
|
||||
for (var d = first; d <= last; d++) {
|
||||
final date = model.dayAt(d);
|
||||
final x = xOf(d);
|
||||
if (date.day == 1) {
|
||||
canvas.drawLine(
|
||||
Offset(x, axisY - 40),
|
||||
Offset(x, axisY - 8),
|
||||
Paint()..color = colors.line,
|
||||
);
|
||||
final tp = _label(
|
||||
'${monthName(date.month)} ${date.year}',
|
||||
labelStyle.copyWith(fontWeight: FontWeight.w600, color: colors.ink),
|
||||
200,
|
||||
);
|
||||
tp.paint(canvas, Offset(x + 6, axisY - 44));
|
||||
}
|
||||
if (dw >= 22) {
|
||||
final tp = _label(
|
||||
'${date.day}',
|
||||
labelStyle.copyWith(color: colors.faint),
|
||||
dw,
|
||||
);
|
||||
tp.paint(canvas, Offset(x - tp.width / 2, axisY - 24));
|
||||
}
|
||||
}
|
||||
|
||||
// Month of the first visible day, pinned to the left edge.
|
||||
final firstDate = model.dayAt(first + 1);
|
||||
final sticky = _label(
|
||||
'${monthName(firstDate.month)} ${firstDate.year}',
|
||||
labelStyle.copyWith(fontWeight: FontWeight.w600, color: colors.ink),
|
||||
200,
|
||||
);
|
||||
final nextMonth = DateTime.utc(firstDate.year, firstDate.month + 1);
|
||||
final nextX = xOf(nextMonth.difference(model.today).inDays);
|
||||
final stickyX = math.min(24.0, nextX - sticky.width - 12);
|
||||
canvas.drawRect(
|
||||
Rect.fromLTWH(stickyX - 6, axisY - 48, sticky.width + 12, 22),
|
||||
Paint()..color = colors.paper,
|
||||
);
|
||||
sticky.paint(canvas, Offset(stickyX, axisY - 44));
|
||||
|
||||
// Today marker.
|
||||
final tx = xOf(0);
|
||||
if (tx >= 0 && tx <= size.width) {
|
||||
canvas.drawLine(
|
||||
Offset(tx, axisY - 30),
|
||||
Offset(tx, size.height),
|
||||
Paint()
|
||||
..color = colors.accent.withAlpha(0x33)
|
||||
..strokeWidth = 1,
|
||||
);
|
||||
}
|
||||
|
||||
final journalDays =
|
||||
model.days.entries
|
||||
.where((e) => e.value.journalId != null)
|
||||
.map((e) => e.key)
|
||||
.toList()
|
||||
..sort();
|
||||
|
||||
// Episodic chain: each journal linked to the next.
|
||||
final chain = Paint()
|
||||
..color = colors.accent.withAlpha(0x88)
|
||||
..strokeWidth = 2
|
||||
..strokeCap = StrokeCap.round;
|
||||
for (var k = 0; k + 1 < journalDays.length; k++) {
|
||||
final a = journalDays[k], b = journalDays[k + 1];
|
||||
if (b < first || a > last) continue;
|
||||
canvas.drawLine(Offset(xOf(a), axisY), Offset(xOf(b), axisY), chain);
|
||||
}
|
||||
|
||||
final y0 = axisY + 44 + view.y;
|
||||
final showLabels = dw >= 34;
|
||||
// Labels may run until the next day that also has pages hanging from it.
|
||||
final withPages =
|
||||
model.days.entries
|
||||
.where((e) => e.value.created.isNotEmpty)
|
||||
.map((e) => e.key)
|
||||
.toList()
|
||||
..sort();
|
||||
double labelWidth(int d) {
|
||||
final next = withPages.where((x) => x > d).firstOrNull;
|
||||
final room = next == null ? 220.0 : (next - d) * dw - 18;
|
||||
return room.clamp(40.0, 220.0);
|
||||
}
|
||||
|
||||
// Edges from pages to the days they were edited.
|
||||
final edge = Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1;
|
||||
for (final e in model.days.entries) {
|
||||
final d = e.key;
|
||||
for (final ed in e.value.edited) {
|
||||
final b = model.born[ed.pageId];
|
||||
if (b == null || b.$1 == d) continue;
|
||||
if ((b.$1 < first && d < first) || (b.$1 > last && d > last)) continue;
|
||||
final from = Offset(xOf(b.$1) + 4, y0 + b.$2 * rowH);
|
||||
final to = Offset(xOf(d), axisY);
|
||||
edge.color = colors.ref.withAlpha(
|
||||
(30 + 20 * math.min(ed.edits, 6)).toInt(),
|
||||
);
|
||||
final mid = Offset(
|
||||
(from.dx + to.dx) / 2,
|
||||
from.dy + 24 + (from.dx - to.dx).abs() * 0.08,
|
||||
);
|
||||
canvas.drawPath(
|
||||
Path()
|
||||
..moveTo(from.dx, from.dy)
|
||||
..quadraticBezierTo(mid.dx, mid.dy, to.dx, to.dy),
|
||||
edge,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Pages hanging from their birth day, and journal nodes.
|
||||
for (var d = first; d <= last; d++) {
|
||||
final day = model.days[d];
|
||||
if (day == null) continue;
|
||||
final x = xOf(d);
|
||||
if (day.created.isNotEmpty) {
|
||||
canvas.drawLine(
|
||||
Offset(x, axisY),
|
||||
Offset(x, y0 + (day.created.length - 1) * rowH),
|
||||
Paint()
|
||||
..color = colors.line
|
||||
..strokeWidth = 1,
|
||||
);
|
||||
}
|
||||
for (var k = 0; k < day.created.length; k++) {
|
||||
final id = day.created[k];
|
||||
final c = Offset(x, y0 + k * rowH);
|
||||
if (c.dy < axisY + 8) continue;
|
||||
canvas.drawCircle(c, 4, Paint()..color = colors.paper);
|
||||
canvas.drawCircle(
|
||||
c,
|
||||
4,
|
||||
Paint()
|
||||
..color = colors.muted
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.5,
|
||||
);
|
||||
var hit = Rect.fromCircle(center: c, radius: 6);
|
||||
if (showLabels) {
|
||||
final title = model.titles[id]?.title ?? '…';
|
||||
final tp = _label(
|
||||
title,
|
||||
TextStyle(fontFamily: uiFont, fontSize: 12, color: colors.ink),
|
||||
labelWidth(d),
|
||||
);
|
||||
final at = Offset(c.dx + 9, c.dy - tp.height / 2);
|
||||
tp.paint(canvas, at);
|
||||
hit = hit.expandToInclude(at & tp.size);
|
||||
}
|
||||
hits.add((hit, id));
|
||||
}
|
||||
final jid = day.journalId;
|
||||
if (jid != null) {
|
||||
final c = Offset(x, axisY);
|
||||
final r = day.created.isNotEmpty || day.edited.isNotEmpty ? 6.0 : 4.5;
|
||||
canvas.drawCircle(c, r + 2, Paint()..color = colors.paper);
|
||||
canvas.drawCircle(c, r, Paint()..color = colors.accent);
|
||||
hits.add((Rect.fromCircle(center: c, radius: r + 6), jid));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(_TimelinePainter old) =>
|
||||
old.colors != colors || old.model != model || old.view != view;
|
||||
}
|
||||
|
||||
class _Toolbar extends StatelessWidget {
|
||||
const _Toolbar({
|
||||
required this.onToday,
|
||||
required this.onZoomIn,
|
||||
required this.onZoomOut,
|
||||
});
|
||||
final VoidCallback onToday;
|
||||
final VoidCallback onZoomIn;
|
||||
final VoidCallback onZoomOut;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = context.colors;
|
||||
Widget btn(IconData i, String tip, VoidCallback f) => IconButton(
|
||||
tooltip: tip,
|
||||
onPressed: f,
|
||||
icon: Icon(i, size: 18, color: colors.muted),
|
||||
);
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: colors.paper,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: colors.line),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
btn(Icons.today_outlined, 'Today', onToday),
|
||||
btn(Icons.remove_rounded, 'Zoom out', onZoomOut),
|
||||
btn(Icons.add_rounded, 'Zoom in', onZoomIn),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Legend extends StatelessWidget {
|
||||
const _Legend({required this.colors});
|
||||
final SynapseColors colors;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget item(Widget mark, String text) => Padding(
|
||||
padding: const EdgeInsets.only(right: 16),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
mark,
|
||||
const SizedBox(width: 6),
|
||||
Text(text, style: TextStyle(fontSize: 12, color: colors.muted)),
|
||||
],
|
||||
),
|
||||
);
|
||||
return Row(
|
||||
children: [
|
||||
item(
|
||||
Container(
|
||||
width: 9,
|
||||
height: 9,
|
||||
decoration: BoxDecoration(
|
||||
color: colors.accent,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
),
|
||||
'Journal',
|
||||
),
|
||||
item(
|
||||
Container(
|
||||
width: 9,
|
||||
height: 9,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: colors.muted, width: 1.5),
|
||||
),
|
||||
),
|
||||
'Page born that day',
|
||||
),
|
||||
item(Container(width: 16, height: 1.5, color: colors.ref), 'Edited on'),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user