Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
af959a6
chore(example): update the Podfile lock to 0.1.10
SectionTN Nov 13, 2026
68a3725
perf: log draw time in microseconds
SectionTN Nov 13, 2026
5ed3f4f
test: pin the layout in shared vectors
SectionTN Nov 13, 2026
2a4e077
feat(core): move the row heights and the viewport into the core
SectionTN Nov 13, 2026
4efdd79
perf(ios): lay out a draw with one frame call and one measure call
SectionTN Nov 13, 2026
6146e73
fix(ios): skip underlines that start below the window in a draw
SectionTN Nov 13, 2026
ad04797
perf(android): run the viewport on the core with two calls per draw
SectionTN Nov 13, 2026
1bb2c7f
docs: describe the layout in the core and the two calls per draw
SectionTN Nov 13, 2026
d1e9d45
fix: read no top of the last draw before the measure call
SectionTN Nov 13, 2026
f9f9c82
fix(core): clamp the scroll when a measured visible line shrinks
SectionTN Nov 13, 2026
7d4f7f8
fix: keep the frame's cached layouts through a draw's inserts
SectionTN Nov 13, 2026
df39dea
fix: lay out the lines a clamp in the measure call shows
SectionTN Nov 13, 2026
a293bfe
fix(ios): move the scroll view when an edit clamps the core scroll
SectionTN Nov 13, 2026
d360bee
docs(core): note that the caller clamps after a config or widest change
SectionTN Nov 13, 2026
066d810
test: measure a batch with a growth above and a shrink at the top
SectionTN Nov 13, 2026
2d05742
fix: keep taking the frame until it reaches the first visible line
SectionTN Nov 13, 2026
cb2dd01
fix: tell scroll listeners when an edit clamps the scroll
SectionTN Nov 13, 2026
b981d4c
test(ios): make the scroll tests fail without their fixes
SectionTN Nov 13, 2026
87d1dd8
fix(ios): send the scroll event when an edit moves the scroll
SectionTN Nov 13, 2026
8ea2ba9
fix(ios): bring back the push and send the scroll event after a relayout
SectionTN Nov 13, 2026
5a4aa26
fix: tell scroll listeners when the draw's measure call moves the scroll
SectionTN Nov 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 104 additions & 8 deletions android/src/main/cpp/core_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <jni.h>
#include <time.h>

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <string>
Expand Down Expand Up @@ -554,23 +555,118 @@ FN(searchFind)(JNIEnv *e, jclass, jlong search, jlong text, jintArray scan, jint
return n;
}

// Main thread, once per draw. No Kotlin runs inside, so both outputs stay critical. text_len[0] gets the units needed.
FN(editorFrame)(JNIEnv *e, jclass, jlong editor, jlong snapshot, jint first, jint last, jint applied,
jboolean use_changed, jintArray entries, jintArray out, jcharArray text, jintArray text_len) -> jint {
jsize n = e->GetArrayLength(entries);
std::vector<int32_t> revisions(static_cast<size_t>(n));
if (n > 0) e->GetIntArrayRegion(entries, 0, n, reinterpret_cast<jint *>(revisions.data()));
// Main thread, once per draw. No Kotlin runs inside, so the arrays stay critical. The first pair_count (line,
// revision) pairs of pairs are read. text_len[0] gets the units needed.
FN(editorFrame)(JNIEnv *e, jclass, jlong editor, jlong layout, jlong snapshot, jint applied, jboolean use_changed,
jintArray pairs, jint pair_count, jintArray out, jcharArray text, jintArray text_len) -> jint {
auto count = static_cast<uint32_t>(std::max(0, std::min(pair_count, e->GetArrayLength(pairs) / 2)));
jsize cap = e->GetArrayLength(out);
jsize text_cap = e->GetArrayLength(text);
uint32_t units = 0;
auto *p = static_cast<jint *>(e->GetPrimitiveArrayCritical(pairs, nullptr));
auto *o = static_cast<jint *>(e->GetPrimitiveArrayCritical(out, nullptr));
auto *t = static_cast<jchar *>(e->GetPrimitiveArrayCritical(text, nullptr));
uint32_t needed = cp_editor_frame(ed(editor), ptr<cp_snapshot>(snapshot), first, last, applied, use_changed ? 1 : 0,
revisions.data(), reinterpret_cast<int32_t *>(o), static_cast<uint32_t>(cap),
uint32_t needed = cp_editor_frame(ed(editor), ptr<cp_layout>(layout), ptr<cp_snapshot>(snapshot), applied,
use_changed ? 1 : 0, reinterpret_cast<const int32_t *>(p), count,
reinterpret_cast<int32_t *>(o), static_cast<uint32_t>(cap),
reinterpret_cast<cp_unit *>(t), static_cast<uint32_t>(text_cap), &units);
e->ReleasePrimitiveArrayCritical(text, t, 0);
e->ReleasePrimitiveArrayCritical(out, o, 0);
e->ReleasePrimitiveArrayCritical(pairs, p, JNI_ABORT);
auto length = static_cast<jint>(units);
e->SetIntArrayRegion(text_len, 0, 1, &length);
return static_cast<jint>(needed);
}

// The layout. Main thread only. Geometry crosses as the 17 ints of cp_geometry, in its field order.
static_assert(sizeof(cp_geometry) == 17 * sizeof(int32_t), "cp_geometry is 17 packed ints");
static_assert(sizeof(cp_layout_config) == 10 * sizeof(int32_t), "cp_layout_config is 10 packed ints");

namespace {

cp_layout *lay(jlong v) { return ptr<cp_layout>(v); }

void put(JNIEnv *e, jintArray out, const cp_geometry &g) {
if (out != nullptr) e->SetIntArrayRegion(out, 0, 17, reinterpret_cast<const jint *>(&g));
}

} // namespace

FN(layoutNew)(JNIEnv *, jclass) -> jlong { return reinterpret_cast<jlong>(cp_layout_new()); }
FN(layoutFree)(JNIEnv *, jclass, jlong layout) -> void { cp_layout_free(lay(layout)); }

FN(editorAttachLayout)(JNIEnv *, jclass, jlong editor, jlong layout) -> void {
cp_editor_attach_layout(ed(editor), layout != 0 ? lay(layout) : nullptr);
}

FN(layoutConfigure)(JNIEnv *e, jclass, jlong layout, jintArray config) -> jboolean {
cp_layout_config c;
e->GetIntArrayRegion(config, 0, 10, reinterpret_cast<jint *>(&c));
return cp_layout_configure(lay(layout), &c) != 0;
}

FN(layoutGeometry)(JNIEnv *e, jclass, jlong layout, jintArray out) -> void {
cp_geometry g;
cp_layout_geometry(lay(layout), &g);
put(e, out, g);
}

FN(layoutScrollTo)(JNIEnv *e, jclass, jlong layout, jint x, jint y, jintArray out) -> jint {
cp_geometry g;
int32_t axes = cp_layout_scroll_to(lay(layout), x, y, &g);
put(e, out, g);
return axes;
}

FN(layoutReveal)(JNIEnv *e, jclass, jlong layout, jint left, jint top, jint right, jint bottom, jintArray out)
-> jboolean {
cp_geometry g;
int32_t moved = cp_layout_reveal(lay(layout), left, top, right, bottom, &g);
put(e, out, g);
return moved != 0;
}

// The draw's measure call. The first pair_count (line, rows) pairs of pairs are applied. tops_rows may be short, then
// it gets nothing and the return says how many ints it needs.
FN(layoutMeasure)(JNIEnv *e, jclass, jlong layout, jintArray pairs, jint pair_count, jint widest, jint first,
jint last, jintArray geometry, jintArray tops_rows) -> jint {
auto count = static_cast<uint32_t>(std::max(0, std::min(pair_count, e->GetArrayLength(pairs) / 2)));
jsize cap = e->GetArrayLength(tops_rows);
cp_geometry g;
auto *p = static_cast<jint *>(e->GetPrimitiveArrayCritical(pairs, nullptr));
auto *o = static_cast<jint *>(e->GetPrimitiveArrayCritical(tops_rows, nullptr));
uint32_t needed = cp_layout_measure(lay(layout), reinterpret_cast<const int32_t *>(p), count, widest, first, last,
&g, reinterpret_cast<int32_t *>(o), static_cast<uint32_t>(cap));
e->ReleasePrimitiveArrayCritical(tops_rows, o, 0);
e->ReleasePrimitiveArrayCritical(pairs, p, JNI_ABORT);
put(e, geometry, g);
return static_cast<jint>(needed);
}

FN(layoutSetWidest)(JNIEnv *, jclass, jlong layout, jint widest) -> void { cp_layout_set_widest(lay(layout), widest); }
FN(layoutRows)(JNIEnv *, jclass, jlong layout, jint line) -> jint { return cp_layout_rows(lay(layout), line); }
FN(layoutGapRows)(JNIEnv *, jclass, jlong layout, jint line) -> jint { return cp_layout_gap_rows(lay(layout), line); }
FN(layoutRowStart)(JNIEnv *, jclass, jlong layout, jint line) -> jint {
return cp_layout_row_start(lay(layout), line);
}
FN(layoutLineAtRow)(JNIEnv *, jclass, jlong layout, jint row) -> jint {
return cp_layout_line_at_row(lay(layout), row);
}
FN(layoutIsGapRow)(JNIEnv *, jclass, jlong layout, jint row) -> jboolean {
return cp_layout_is_gap_row(lay(layout), row) != 0;
}
FN(layoutRowTop)(JNIEnv *, jclass, jlong layout, jint row) -> jint { return cp_layout_row_top(lay(layout), row); }
FN(layoutLineTop)(JNIEnv *, jclass, jlong layout, jint line) -> jint { return cp_layout_line_top(lay(layout), line); }
FN(layoutRowAt)(JNIEnv *, jclass, jlong layout, jint y) -> jint { return cp_layout_row_at(lay(layout), y); }

FN(layoutAnchor)(JNIEnv *e, jclass, jlong layout, jintArray out) -> void {
int32_t anchor[2] = {0, 0};
cp_layout_anchor(lay(layout), &anchor[0], &anchor[1]);
e->SetIntArrayRegion(out, 0, 2, reinterpret_cast<const jint *>(anchor));
}

FN(layoutRestore)(JNIEnv *, jclass, jlong layout, jint line, jint offset) -> jboolean {
return cp_layout_restore(lay(layout), line, offset) != 0;
}

FN(layoutResetRows)(JNIEnv *, jclass, jlong layout) -> void { cp_layout_reset_rows(lay(layout)); }
3 changes: 3 additions & 0 deletions android/src/main/java/me/sectiontn/codepad/core/EditorCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class EditorCore(initialText: String = "", clock: (() -> Long)? = null) : AutoCl
val canUndo: Boolean get() = life.read { CoreNative.editorHistory(handle()) and 1 != 0 }
val canRedo: Boolean get() = life.read { CoreNative.editorHistory(handle()) and 2 != 0 }

/** False once [close] freed the core. Main thread. */
internal val isOpen: Boolean get() = native != 0L

internal fun handle(): Long {
val h = native
check(h != 0L) { "the editor core is closed" }
Expand Down
25 changes: 13 additions & 12 deletions android/src/main/java/me/sectiontn/codepad/core/EditorFrame.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.sectiontn.codepad.core

import me.sectiontn.codepad.layout.Viewport
import me.sectiontn.codepad.syntax.CoreNative

/**
Expand Down Expand Up @@ -48,11 +49,6 @@ class EditorFrame internal constructor(
val i = runLines.indexOf(line)
return if (i < 0) null else texts[i]
}

companion object {
/** CP_NO_ENTRY: a line with no layout. */
const val NO_ENTRY = Int.MIN_VALUE
}
}

/** Makes the frame call, with buffers kept across draws and grown when the core asks for more. */
Expand All @@ -61,17 +57,22 @@ class FrameReader(ints: Int = 4096, units: Int = 16384) {
private var units = CharArray(maxOf(units, 1))
private val textLength = IntArray(1)

/** [snapshot] is a snapshot pointer or 0. [entries] holds the cached revision of each line of [first, last]. */
fun read(core: EditorCore, snapshot: Long, first: Int, last: Int, applied: Int, useChanged: Boolean, entries: IntArray): EditorFrame {
/**
* The draw's frame: the layout picks the lines. [pairs] holds [pairCount] (line, revision) pairs of the view's cache
* in any order. [snapshot] is a snapshot pointer or 0.
*/
fun read(core: EditorCore, layout: Viewport, snapshot: Long, applied: Int, useChanged: Boolean, pairs: IntArray, pairCount: Int): EditorFrame {
while (true) {
val needed = CoreNative.editorFrame(core.handle(), snapshot, first, last, applied, useChanged, entries, ints, units, textLength)
if (needed <= ints.size && textLength[0] <= units.size) return decode(first, last)
val needed = CoreNative.editorFrame(
core.handle(), layout.nativeHandle(), snapshot, applied, useChanged, pairs, pairCount, ints, units, textLength,
)
if (needed <= ints.size && textLength[0] <= units.size) return decode()
if (needed > ints.size) ints = IntArray(needed)
if (textLength[0] > units.size) units = CharArray(textLength[0])
}
}

private fun decode(first: Int, last: Int): EditorFrame {
private fun decode(): EditorFrame {
var i = 0
fun next(): Int = ints[i++]
val revision = next()
Expand All @@ -82,8 +83,8 @@ class FrameReader(ints: Int = 4096, units: Int = 16384) {
val caretLine = next()
val decorationsVersion = next()
val history = next()
val lo = first.coerceIn(0, lineCount - 1)
val hi = last.coerceIn(lo, lineCount - 1)
val lo = next()
val hi = next()
val starts = IntArray(hi - lo + 2) { next() }
val runs = IntArray(next() * 2) { next() }
var count = 0
Expand Down
119 changes: 0 additions & 119 deletions android/src/main/java/me/sectiontn/codepad/layout/HeightMap.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class LayoutCache<T : Any>(capacity: Int) {
trim()
}

/** Every entry, least recently used first. The eviction order stays as it is. */
fun forEachEntry(body: (line: Int, value: T) -> Unit) {
for ((line, value) in map) body(line, value)
}

fun clear() = map.clear()

/** Lines [first, first + oldCount) changed. Their entries drop and later entries move with their text. */
Expand Down
Loading
Loading