Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
343842a
Walk the note once for its semantics, not once for every widget in it
sanjaydotpro Nov 16, 2026
3783457
Read TeX between dollar signs as math, by pandoc's rules
sanjaydotpro Nov 16, 2026
245c8f6
Draw TeX formulas in place of their source
sanjaydotpro Nov 16, 2026
bfc127d
Colour fenced code in the language its info string names
sanjaydotpro Nov 16, 2026
86a3ab8
Underline, raise and lower the words in <u>, <ins>, <sup> and <sub>
sanjaydotpro Nov 16, 2026
ec81adc
Break a table cell's line at <br>
sanjaydotpro Nov 16, 2026
8dd95bf
Add light and dark goldens of formulas, code and underline
sanjaydotpro Nov 16, 2026
e4609b0
Add a profile benchmark for a long note with formulas and code
sanjaydotpro Nov 16, 2026
b67f7ea
Read clipboard HTML into a paste tree
sanjaydotpro Nov 16, 2026
9d53d0b
Add paste fixture corpus and capture tooling
sanjaydotpro Nov 16, 2026
3749aa8
Write pastes as markdown, or in the plain note format
sanjaydotpro Nov 16, 2026
c620cff
Fit a paste to the line it lands on
sanjaydotpro Nov 16, 2026
917ab85
Keep a pasted picture's place while it downloads
sanjaydotpro Nov 16, 2026
2003476
Paste rich content into notes as one edit
sanjaydotpro Nov 16, 2026
27b972f
Read every clipboard representation in one call on macOS
sanjaydotpro Nov 16, 2026
8e54e5a
Read every clipboard representation in one call on Windows
sanjaydotpro Nov 16, 2026
f3b6b54
Test reading a clipboard snapshot from the channel
sanjaydotpro Nov 16, 2026
b27326e
Read every clipboard representation in one call on iOS
sanjaydotpro Nov 16, 2026
c2d99ee
Read the primary clip in one call on Android
sanjaydotpro Nov 16, 2026
7742447
Read VS Code's data from Chromium's custom data on macOS
sanjaydotpro Nov 16, 2026
83c010f
Read every macOS paste fixture through the native clipboard
sanjaydotpro Nov 16, 2026
781945d
Add macOS clipboard snapshots for the paste fixtures
sanjaydotpro Nov 16, 2026
69edea6
Let paste fixtures carry their macOS snapshot
sanjaydotpro Nov 16, 2026
e45313c
Read RTFD pictures that a Cocoa app's own HTML shows
sanjaydotpro Nov 16, 2026
c23ced8
Add the Apple Notes fixture's macOS clipboard snapshot
sanjaydotpro Nov 16, 2026
86d21d9
Read ballot-box paragraphs as to-dos
sanjaydotpro Nov 16, 2026
854e521
Check every paste fixture against what it should paste as
sanjaydotpro Nov 16, 2026
ec825fb
Give back every picture one edit dropped
sanjaydotpro Nov 16, 2026
6e4115a
Keep the lines around a pasted markdown picture tidy
sanjaydotpro Nov 16, 2026
9910dcf
Test the paste reader, writers, TeX and picture downloads
sanjaydotpro Nov 16, 2026
85f403c
Write stacked conditions under a sum as a substack
sanjaydotpro Nov 16, 2026
5896db5
Read pastes over 16 KB off the UI isolate
sanjaydotpro Nov 16, 2026
dbffa54
Test rich and web-picture pastes through the editor
sanjaydotpro Nov 16, 2026
248a3c7
Pin down that a pasted picture's address never syncs
sanjaydotpro Nov 16, 2026
471c0e8
Offer the link of a pasted picture that would not load
sanjaydotpro Nov 16, 2026
c94872f
Write KaTeX's non-breaking spaces in text as spaces
sanjaydotpro Nov 16, 2026
73e6c33
Read picture sizes given in rem, em and pt
sanjaydotpro Nov 16, 2026
b4f65fe
Leave avatars out of a paste
sanjaydotpro Nov 16, 2026
f94162c
Read - and -- under words as typing, and escapes as plain characters
sanjaydotpro Nov 16, 2026
79c169b
Type markdown shortcuts the way Notion does
sanjaydotpro Nov 16, 2026
9d31e24
Measure only the lines an edit changed
sanjaydotpro Nov 16, 2026
c53031d
Add a typing benchmark for long notes
sanjaydotpro Nov 16, 2026
9d3a061
Write paste blocks out as HTML for documents and mail
sanjaydotpro Nov 16, 2026
1d38563
Render and convert a note's text the way its editor reads it
sanjaydotpro Nov 16, 2026
7894f8c
Carry a whole selection in Kapy Notes' own clipboard type
sanjaydotpro Nov 16, 2026
885b20f
Copy, cut and paste every selection through the rich clipboard
sanjaydotpro Nov 16, 2026
d85aef8
Let go of the clipboard before converting a pasted bitmap
sanjaydotpro Nov 16, 2026
1d057d7
Turn markdown on by default
sanjaydotpro Nov 16, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import android.content.Context
import android.net.Uri
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.io.ByteArrayOutputStream
import java.io.File
import kotlin.text.Charsets.UTF_8

/** Publishes one selection as text, HTML, and a native image at the same time. */
object RichClipboard {
private const val channelName = "kapynotes/rich_clipboard"
private const val MAX_IMAGE_BYTES = 25L * 1024 * 1024

fun register(engine: FlutterEngine, context: Context) {
val appContext = context.applicationContext
Expand All @@ -29,6 +31,14 @@ object RichClipboard {
}
}

"read" -> {
try {
result.success(read(appContext))
} catch (error: Exception) {
result.error("clipboard_read", error.message, null)
}
}

"readHtml" -> {
try {
result.success(readHtml(appContext))
Expand Down Expand Up @@ -103,6 +113,59 @@ object RichClipboard {
}
}

/**
* Everything the primary clip offers a paste, in the shape
* `ClipboardSnapshot.fromChannel` reads: the first item's HTML and text,
* and its content URI's bytes when that URI is a picture.
*/
private fun read(context: Context): Map<String, Any> {
val manager = context.getSystemService(ClipboardManager::class.java)
val clip = manager.primaryClip ?: return emptyMap()
val description = clip.description
val snapshot = mutableMapOf<String, Any>(
"types" to List(description.mimeTypeCount) { description.getMimeType(it) },
)
if (clip.itemCount == 0) return snapshot
val item = clip.getItemAt(0)
val uri = item.uri
val image = uri
?.let { runCatching { context.contentResolver.getType(it) }.getOrNull() }
?.takeIf { it.startsWith("image/") }
val html = item.htmlText
?: uri?.let { ClipboardImageProvider.htmlFileFor(context, it) }
?.takeIf(File::isFile)
?.readText(UTF_8)
html?.let { snapshot["html"] = it }
val text = if (image == null) item.coerceToText(context) else item.text
text?.toString()?.takeIf { it.isNotEmpty() }?.let { snapshot["text"] = it }
if (uri != null && image != null) {
readCapped(context, uri)?.let {
snapshot["image"] = it
snapshot["imageMime"] = image
}
}
return snapshot
}

/** The bytes behind [uri], or null when it can't be read or is too large to paste. */
private fun readCapped(context: Context, uri: Uri): ByteArray? = try {
context.contentResolver.openInputStream(uri)?.use { input ->
val output = ByteArrayOutputStream()
val buffer = ByteArray(64 * 1024)
var total = 0L
while (true) {
val read = input.read(buffer)
if (read < 0) break
total += read
if (total > MAX_IMAGE_BYTES) return@use null
output.write(buffer, 0, read)
}
output.toByteArray().takeIf { it.isNotEmpty() }
}
} catch (error: Exception) {
null
}

private fun readHtml(context: Context): String? {
val manager = context.getSystemService(ClipboardManager::class.java)
val clip = manager.primaryClip ?: return null
Expand Down
10 changes: 10 additions & 0 deletions assets/fonts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ with the new ones, from the upstream files, or the change will not show.

License: [`ShantellSans-OFL.txt`](ShantellSans-OFL.txt)

## Formula fonts

Formulas written in TeX are drawn in KaTeX's fonts, which come with
flutter_math_fork rather than living here: twenty files, about 660 KB in
every install. They are MIT licensed by Khan Academy, and that notice ships
as `packages/flutter_math_fork/katex_fonts/LICENSE`, listed in
`pubspec.yaml`, since the package's own LICENSE does not carry it.

Source: <https://github.com/KaTeX/katex-fonts>

## Brand font

The `Kapy Notes` wordmark uses Odin Rounded Bold by Frank Hemmekam.
Expand Down
246 changes: 246 additions & 0 deletions benchmark/editor_typing.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
// How fast does typing stay in a long markdown note?
//
// Five thousand lines of everything a markdown note holds — headings, lists,
// tasks, quotes, code, tables and sums — with markdown on, typed into the way
// a keyboard types: one editing-value update per key, about thirty a second,
// two hundred keys at the end of the note and two hundred in the middle.
// Then prints how long those frames took to build and to draw, and quits.
//
// Other sizes, plain notes and fewer keys come from defines, several sizes
// and modes in one run:
//
// --dart-define=EDITOR_BENCHMARK_LINES=500,1000,5000
// --dart-define=EDITOR_BENCHMARK_MODES=markdown,plain
// --dart-define=EDITOR_BENCHMARK_KEYS=30
//
// A plain app rather than an integration test, because only profile mode
// says anything about frame times, and Flutter 3.47.4's AOT compiler fails on
// an entrypoint that leaves out the app's own windowing code ("Class with
// illegal cid" in _window_macos.dart). This one keeps the app's `main`
// reachable, never calling it, so it compiles the way the app does. Run it
// under a bundle id of its own, never the real app's:
//
// FLUTTER_XCODE_PRODUCT_BUNDLE_IDENTIFIER=com.kapybara.kapynotes.typingperf \
// flutter run --profile -d macos -t benchmark/editor_typing.dart
//
// The figures are the line starting EDITOR_TYPING_PERF in the log.
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/scheduler.dart';
import 'package:kapy_notes/calc/engine.dart';
import 'package:kapy_notes/calc/highlight.dart';
import 'package:kapy_notes/core/editor_font.dart';
import 'package:kapy_notes/core/theme.dart';
import 'package:kapy_notes/data/local_store.dart';
import 'package:kapy_notes/data/shortcut_prefs.dart';
import 'package:kapy_notes/main.dart' as app;
import 'package:kapy_notes/ui/editor/note_editor.dart';
import 'package:material_ui/material_ui.dart';

class _MemoryStore extends LocalStore {
_MemoryStore() : super(fileName: 'editor-typing-benchmark.json');

@override
Future<void> load() async {}

@override
Future<void> flush() async {}

@override
void put(String key, Object? value) => data[key] = value;
}

/// A long note in the shapes people write: every kind of block, over and
/// over, [lines] lines of it.
String longNote({int lines = 5000}) {
const pattern = [
'## Section {n}',
'Plain words about item {n}, with **bold**, *italic* and a '
'[link](https://example.com/{n}).',
'- groceries {n}: 3 * 4.50',
'- [ ] call about invoice {n}',
'- [x] paid {n} = 120 + 35',
' - nested detail {n}',
'1. first step {n}',
'2. second step',
'> a quote worth keeping {n}',
'rent {n} = 1200 / 2',
'```dart',
'final total = items.fold(0, (a, b) => a + b);',
'```',
'',
'| Item | Cost |',
'| --- | --- |',
'| Taxi | 14 |',
'',
'Words with `inline code` and ~~struck~~ text and https://example.org/{n}',
'',
];
return [
for (var i = 0; i < lines; i++)
pattern[i % pattern.length].replaceAll('{n}', '$i'),
].join('\n');
}

/// [values] at the [p]th percentile, by nearest rank.
double percentile(List<double> values, double p) {
final sorted = [...values]..sort();
final rank = (p / 100 * sorted.length).ceil().clamp(1, sorted.length);
return sorted[rank - 1];
}

Map<String, Object> summary(List<FrameTiming> timings) {
double ms(Duration duration) => duration.inMicroseconds / 1000;
const budget = 1000 / 60;
final build = [for (final t in timings) ms(t.buildDuration)];
final raster = [for (final t in timings) ms(t.rasterDuration)];
Map<String, Object> stats(List<double> values) => {
'p50': percentile(values, 50),
'p90': percentile(values, 90),
'p99': percentile(values, 99),
'max': percentile(values, 100),
'over_16_7ms': values.where((value) => value > budget).length,
};
return {
'frames': timings.length,
'build_ms': stats(build),
'raster_ms': stats(raster),
'frames_over_16_7ms': [
for (var i = 0; i < timings.length; i++)
if (build[i] > budget || raster[i] > budget) i,
].length,
};
}

void main() {
WidgetsFlutterBinding.ensureInitialized();
unawaited(_runAll());
}

Future<void> _runAll() async {
const lines = String.fromEnvironment(
'EDITOR_BENCHMARK_LINES',
defaultValue: '5000',
);
const modes = String.fromEnvironment(
'EDITOR_BENCHMARK_MODES',
defaultValue: 'markdown',
);
final reports = [
for (final mode in modes.split(','))
for (final size in lines.split(','))
await _run(lines: int.parse(size), markdown: mode == 'markdown'),
];
// ignore: avoid_print
print('EDITOR_TYPING_PERF ${jsonEncode(reports)}');
if (reports.isEmpty) await app.main();
exit(0);
}

void _show(String body, {required bool markdown}) {
final engine = CalcEngine();
runApp(
MaterialApp(
key: UniqueKey(),
theme: KapyTheme.dark(),
home: Scaffold(
body: NoteEditor(
noteId: 'benchmark',
initialBody: body,
engine: engine,
highlighter: Highlighter(engine.registry),
gutterWidth: 200,
resultsVisible: true,
markdownEnabled: markdown,
autofocus: true,
onDocumentChanged: (_, _, _) {},
onGutterWidthChanged: (_) {},
onResultsVisibilityChanged: (_) {},
onGutterWidthReset: () {},
onSettingsPressed: () {},
writingFont: WritingFont.handwritten,
shortcuts: ShortcutPrefs(_MemoryStore())..load(),
),
),
),
);
}

EditableTextState _field() {
EditableTextState? found;
void visit(Element element) {
if (found != null) return;
if (element is StatefulElement && element.state is EditableTextState) {
found = element.state as EditableTextState;
return;
}
element.visitChildElements(visit);
}

WidgetsBinding.instance.rootElement!.visitChildElements(visit);
return found!;
}

Future<Map<String, Object>> _run({
required int lines,
required bool markdown,
}) async {
final body = longNote(lines: lines);
_show(body, markdown: markdown);
await Future<void>.delayed(const Duration(seconds: 3));
final field = _field();
const sentence = 'The quick brown fox jumps over the lazy dog, 12 * 3. ';
final keys = (sentence * 4).substring(
0,
const int.fromEnvironment('EDITOR_BENCHMARK_KEYS', defaultValue: 200),
);

Future<Map<String, Object>> typeAt(int caret) async {
field.userUpdateTextEditingValue(
field.textEditingValue.copyWith(
selection: TextSelection.collapsed(offset: caret),
),
SelectionChangedCause.tap,
);
await Future<void>.delayed(const Duration(seconds: 1));
final timings = <FrameTiming>[];
void record(List<FrameTiming> batch) => timings.addAll(batch);
SchedulerBinding.instance.addTimingsCallback(record);
for (final key in keys.split('')) {
final value = field.textEditingValue;
final at = value.selection.start;
// What the platform's text input sends for a key.
field.updateEditingValue(
TextEditingValue(
text: value.text.replaceRange(at, at, key),
selection: TextSelection.collapsed(offset: at + 1),
),
);
await Future<void>.delayed(const Duration(milliseconds: 33));
}
// Timings arrive in batches, about once a second in profile mode.
await Future<void>.delayed(const Duration(seconds: 2));
SchedulerBinding.instance.removeTimingsCallback(record);
return summary(timings);
}

final atEnd = await typeAt(field.textEditingValue.text.length);
final middle = field.textEditingValue.text
.split('\n')
.take(lines ~/ 2)
.join('\n')
.length;
final inMiddle = await typeAt(middle);
return {
'lines': lines,
'markdown': markdown,
'characters': body.length,
'keys_typed': keys.length,
'typed_correctly':
field.textEditingValue.text.length == body.length + keys.length * 2,
'at_end': atEnd,
'in_middle': inMiddle,
};
}
Loading
Loading