Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export function Screen() {
| `contentInsets` | `ContentInsets` | all 0 | The padding inside the scrollable area. Style padding on the view is ignored. |
| `textPadding` | `number` | `8` | The space between the text and the editor's left and right edges, inside `contentInsets`. |
| `avoidKeyboard` | `boolean` | `false` | Adds the keyboard overlap to the bottom inset on every frame of the keyboard animation. |
| `keyboardOffset` | `number` | `0` | While the keyboard is up, the editor treats its top as this much higher, to keep a toolbar that rides the keyboard clear. It needs `avoidKeyboard`. |
| `decorations` | `Decoration[]` | undefined | Underlines, gaps under lines and line backgrounds. They follow every edit until you pass a new array. |
| `decorationsRevision` | `number` | undefined | The revision the decorations were computed for, from `getRevision()`. The editor moves them through the edits made since then. |
| `onReady` | `() => void` | | Called once after the first draw. Ref methods are valid from here. |
Expand Down
6 changes: 6 additions & 0 deletions android/src/main/cpp/core_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,12 @@ FN(layoutReveal)(JNIEnv *e, jclass, jlong layout, jint left, jint top, jint righ
return moved != 0;
}

FN(pageRevealDelta)(JNIEnv *, jclass, jint row_top, jint row_bottom, jint visible_top, jint visible_bottom,
jint keyboard_top, jint offset, jint range_up, jint range_down) -> jint {
return cp_page_reveal_delta(row_top, row_bottom, visible_top, visible_bottom, keyboard_top, offset, range_up,
range_down);
}

// 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ class HybridCodeEditorView(val context: ThemedReactContext) : HybridCodeEditorVi
editor.setAvoidKeyboard(value ?: false)
}

override var keyboardOffset: Double? = null
set(value) {
field = value
editor.setKeyboardOffset((value ?: 0.0).toFloat())
}

override var suggestions: NativeSuggestions? = null
set(value) {
field = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ internal object CoreNative {
@JvmStatic external fun layoutGeometry(layout: Long, out: IntArray)
@JvmStatic external fun layoutScrollTo(layout: Long, x: Int, y: Int, out: IntArray): Int
@JvmStatic external fun layoutReveal(layout: Long, left: Int, top: Int, right: Int, bottom: Int, out: IntArray): Boolean
@JvmStatic external fun pageRevealDelta(
rowTop: Int, rowBottom: Int, visibleTop: Int, visibleBottom: Int, keyboardTop: Int, offset: Int, rangeUp: Int, rangeDown: Int,
): Int
@JvmStatic external fun layoutMeasure(
layout: Long, pairs: IntArray, pairCount: Int, widest: Int, first: Int, last: Int, geometry: IntArray, topsRows: IntArray,
): Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class CodeEditorView(context: Context) : View(context) {
// Keyboard avoidance: the tracker turns the window's IME overlap into a second bottom inset in px.
private var avoidsKeyboard = false
private var keyboardInset = 0
private var keyboardOffset = 0
private var keyboardTracker: KeyboardInsetTracker? = null
private var contentInsetTop = 0f
private var contentInsetBottom = 0f
Expand Down Expand Up @@ -422,6 +423,14 @@ class CodeEditorView(context: Context) : View(context) {

val isAvoidingKeyboard: Boolean get() = keyboardTracker != null

/** While the keyboard is up, its top counts this much higher, for a toolbar riding the keyboard. */
fun setKeyboardOffset(value: Float) {
val px = dp(value.coerceAtLeast(0f))
if (px == keyboardOffset) return
keyboardOffset = px
applyInsets()
}

private fun applyInsets() {
when {
viewport.setInsets(dp(contentInsetTop), dp(contentInsetBottom) + keyboardInset, dp(contentInsetLeft), dp(contentInsetRight)) -> relayout()
Expand Down
10 changes: 10 additions & 0 deletions android/src/test/java/me/sectiontn/codepad/core/VectorRunner.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.sectiontn.codepad.core

import me.sectiontn.codepad.layout.Viewport
import me.sectiontn.codepad.syntax.CoreNative
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.assertArrayEquals
Expand Down Expand Up @@ -121,6 +122,15 @@ class VectorRunner(private val case: JSONObject) {
"scrollTo" -> lastResult = viewport!!.scrollTo(op.getInt("x"), op.getInt("y"))
"scrollBy" -> lastResult = viewport!!.scrollBy(op.getInt("dx"), op.getInt("dy"))
"reveal" -> lastResult = viewport!!.reveal(op.getInt("left"), op.getInt("top"), op.getInt("right"), op.getInt("bottom"))
"pageReveal" -> {
val row = op.getJSONArray("row")
val visible = op.getJSONArray("visible")
val range = op.getJSONArray("range")
lastResult = CoreNative.pageRevealDelta(
row.getInt(0), row.getInt(1), visible.getInt(0), visible.getInt(1), op.getInt("keyboardTop"), op.getInt("offset"),
range.getInt(0), range.getInt(1),
)
}
"anchor" -> anchor = viewport!!.anchor()
"restore" -> viewport!!.restore(anchor!!)
"resetRows" -> viewport!!.resetRows()
Expand Down
4 changes: 4 additions & 0 deletions cpp/core/include/codepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ CP_API int32_t cp_layout_scroll_to(cp_layout *layout, int32_t x, int32_t y, cp_g
// The smallest scroll that shows a rect in layout coordinates with the text padding around it. Returns 1 when it moved.
CP_API int32_t cp_layout_reveal(cp_layout *layout, int32_t left, int32_t top, int32_t right, int32_t bottom,
cp_geometry *out);
// The page scroll that brings a caret row inside a scroller's visible band and above a raised keyboard. Positive
// scrolls down. keyboard_top is INT32_MAX when no keyboard is up, and offset applies only then.
CP_API int32_t cp_page_reveal_delta(int32_t row_top, int32_t row_bottom, int32_t visible_top, int32_t visible_bottom,
int32_t keyboard_top, int32_t offset, int32_t range_up, int32_t range_down);
// Applies (line, rows) pairs in order, as successive measurements would, then raises the widest line to widest
// when widest >= 0 and the layout does not wrap. Writes the view tops of first_line..last_line + 1, the text rows of
// first_line..last_line, then (line, view top, rows) for every gap in line order, and returns how many ints that
Expand Down
15 changes: 15 additions & 0 deletions cpp/core/src/capi_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ int32_t cp_layout_reveal(cp_layout *layout, int32_t left, int32_t top, int32_t r
return moved ? 1 : 0;
}

int32_t cp_page_reveal_delta(int32_t row_top, int32_t row_bottom, int32_t visible_top, int32_t visible_bottom,
int32_t keyboard_top, int32_t offset, int32_t range_up, int32_t range_down) {
// 64 bits, so a row far off screen against an open band cannot overflow.
int64_t band_bottom = visible_bottom;
if (keyboard_top != INT32_MAX) band_bottom = std::min<int64_t>(band_bottom, int64_t{keyboard_top} - offset);
int64_t delta = 0;
if (row_bottom > band_bottom) {
delta = row_bottom - band_bottom;
} else if (row_top < visible_top) {
delta = int64_t{row_top} - visible_top;
}
delta = std::clamp<int64_t>(delta, -int64_t{std::max(range_up, 0)}, std::max(range_down, 0));
return static_cast<int32_t>(delta);
}

uint32_t cp_layout_measure(cp_layout *layout, const int32_t *pairs, uint32_t pair_count, int32_t widest,
int32_t first_line, int32_t last_line, cp_geometry *out, int32_t *tops_rows,
uint32_t cap) {
Expand Down
5 changes: 4 additions & 1 deletion ios/Codepad/nitro/HybridCodeEditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import UIKit
final class HybridCodeEditorView: HybridCodeEditorViewSpec, @unchecked Sendable {
private enum Prop: CaseIterable {
case text, language, theme, font, lineNumbers, softWrap, readOnly, highlightActiveLine, matchBrackets, placeholder
case scrollEnabled, contentInsets, textPadding, avoidKeyboard, suggestions, autoFocus, settings, decorations
case scrollEnabled, contentInsets, textPadding, avoidKeyboard, keyboardOffset, suggestions, autoFocus, settings
case decorations
}

private let editor: CodeEditorView
Expand Down Expand Up @@ -42,6 +43,7 @@ final class HybridCodeEditorView: HybridCodeEditorViewSpec, @unchecked Sendable
var contentInsets: NativeInsets? { didSet { dirty.insert(.contentInsets) } }
var textPadding: Double? { didSet { dirty.insert(.textPadding) } }
var avoidKeyboard: Bool? { didSet { dirty.insert(.avoidKeyboard) } }
var keyboardOffset: Double? { didSet { dirty.insert(.keyboardOffset) } }
var suggestions: NativeSuggestions? { didSet { dirty.insert(.suggestions) } }
var autoFocus: Bool? { didSet { dirty.insert(.autoFocus) } }
var tabSize: Double? { didSet { dirty.insert(.settings) } }
Expand Down Expand Up @@ -144,6 +146,7 @@ final class HybridCodeEditorView: HybridCodeEditorViewSpec, @unchecked Sendable
right: contentInsets?.right ?? 0)
case .textPadding: editor.setTextPadding(textPadding.map { CGFloat($0) } ?? CodeEditorView.defaultTextPadding)
case .avoidKeyboard: editor.setAvoidKeyboard(avoidKeyboard ?? false)
case .keyboardOffset: editor.setKeyboardOffset(CGFloat(keyboardOffset ?? 0))
case .suggestions: editor.setSuggestions(Self.toSuggestions(suggestions))
case .autoFocus: editor.setAutoFocus(autoFocus ?? false)
case .settings:
Expand Down
9 changes: 9 additions & 0 deletions ios/Codepad/view/CodeEditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ final class CodeEditorView: UIScrollView {
private var contentInsetRight: CGFloat = 0
private(set) var keyboardInset: CGFloat = 0
private var avoidsKeyboard = false
private var keyboardOffset: CGFloat = 0
private(set) var keyboardTracker: KeyboardInsetTracker?
private var ready = false
private var scrollDirty = false
Expand Down Expand Up @@ -334,6 +335,14 @@ final class CodeEditorView: UIScrollView {
if on && window != nil { startKeyboardTracker() } else { stopKeyboardTracker() }
}

/// While the keyboard is up, its top counts this much higher, for a toolbar riding the keyboard.
func setKeyboardOffset(_ points: CGFloat) {
let points = max(0, points)
if points == keyboardOffset { return }
keyboardOffset = points
applyInsets()
}

private func startKeyboardTracker() {
if keyboardTracker != nil { return }
let tracker = KeyboardInsetTracker(view: self) { [weak self] points in self?.applyKeyboardInset(points) }
Expand Down
9 changes: 9 additions & 0 deletions nitrogen/generated/android/c++/JHybridCodeEditorViewSpec.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions nitrogen/generated/android/c++/JHybridCodeEditorViewSpec.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions nitrogen/generated/ios/c++/HybridCodeEditorViewSpecSwift.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions nitrogen/generated/ios/swift/HybridCodeEditorViewSpec_cxx.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions nitrogen/generated/shared/c++/HybridCodeEditorViewSpec.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions nitrogen/generated/shared/c++/HybridCodeEditorViewSpec.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nitrogen/generated/shared/json/CodeEditorViewConfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading