From e987d1d50a8b7ddbbf6ceeee6be6d7ea8ccbc4bf Mon Sep 17 00:00:00 2001 From: Ajit Kumar Date: Thu, 3 Dec 2026 20:48:56 +0530 Subject: [PATCH 1/2] feat: cursor-controller --- .../foxdebug/acode/cursor/CursorController.kt | 67 +++++ .../com/foxdebug/acode/runtime/ServiceName.kt | 1 + .../foxdebug/acode/runtime/ServiceRegistry.kt | 2 + src/cm/cursorController/caretDrag.ts | 138 +++++++++++ src/cm/cursorController/feedback.ts | 45 ++++ src/cm/touchSelectionMenu.js | 19 +- src/components/cursorController/index.js | 231 ++++++++++++++++++ src/components/cursorController/indicator.js | 75 ++++++ src/components/cursorController/knob.js | 100 ++++++++ src/components/cursorController/style.scss | 184 ++++++++++++++ src/handlers/quickTools.js | 2 +- src/handlers/quickToolsInit.js | 2 + src/lang/ar-ye.json | 6 +- src/lang/az-az.json | 6 +- src/lang/be-by.json | 6 +- src/lang/bn-bd.json | 6 +- src/lang/cs-cz.json | 6 +- src/lang/de-de.json | 6 +- src/lang/en-us.json | 6 +- src/lang/es-sv.json | 6 +- src/lang/fr-fr.json | 6 +- src/lang/he-il.json | 6 +- src/lang/hi-in.json | 6 +- src/lang/hu-hu.json | 6 +- src/lang/id-id.json | 6 +- src/lang/index.d.ts | 4 + src/lang/ir-fa.json | 6 +- src/lang/it-it.json | 6 +- src/lang/ja-jp.json | 6 +- src/lang/ko-kr.json | 6 +- src/lang/ln-ln.json | 6 +- src/lang/ml-in.json | 6 +- src/lang/mm-unicode.json | 6 +- src/lang/mm-zawgyi.json | 6 +- src/lang/pl-pl.json | 6 +- src/lang/pt-br.json | 6 +- src/lang/pu-in.json | 6 +- src/lang/ru-ru.json | 6 +- src/lang/tl-ph.json | 6 +- src/lang/tr-tr.json | 6 +- src/lang/uk-ua.json | 6 +- src/lang/uz-uz.json | 6 +- src/lang/vi-vn.json | 6 +- src/lang/zh-cn.json | 6 +- src/lang/zh-hant.json | 6 +- src/lang/zh-tw.json | 6 +- src/lib/settings.js | 2 + src/settings/appSettings.js | 14 ++ tests/unit/cursorControllerCaretDrag.test.ts | 192 +++++++++++++++ tests/unit/quickToolsAdapterUi.test.js | 5 + 50 files changed, 1246 insertions(+), 35 deletions(-) create mode 100644 platforms/android/app/src/main/java/com/foxdebug/acode/cursor/CursorController.kt create mode 100644 src/cm/cursorController/caretDrag.ts create mode 100644 src/cm/cursorController/feedback.ts create mode 100644 src/components/cursorController/index.js create mode 100644 src/components/cursorController/indicator.js create mode 100644 src/components/cursorController/knob.js create mode 100644 src/components/cursorController/style.scss create mode 100644 tests/unit/cursorControllerCaretDrag.test.ts diff --git a/platforms/android/app/src/main/java/com/foxdebug/acode/cursor/CursorController.kt b/platforms/android/app/src/main/java/com/foxdebug/acode/cursor/CursorController.kt new file mode 100644 index 0000000000..81a86c839c --- /dev/null +++ b/platforms/android/app/src/main/java/com/foxdebug/acode/cursor/CursorController.kt @@ -0,0 +1,67 @@ +package com.foxdebug.acode.cursor + +import android.os.Build +import android.view.HapticFeedbackConstants +import androidx.annotation.Keep +import com.foxdebug.acode.runtime.Callback +import com.foxdebug.acode.runtime.Service +import org.json.JSONArray + +/** + * Plays the Quick Tools cursor controller's haptics through the platform's + * own feedback constants, so they feel like the rest of the system and follow + * the user's touch feedback setting. + */ +@Keep +class CursorController : Service() { + override fun execute(action: String, args: JSONArray, callback: Callback): Boolean { + if (action != ACTION_FEEDBACK) return false + val view = webView + val constant = feedbackConstant(args.optString(0)) + if (view != null && constant != null) { + view.post { view.performHapticFeedback(constant) } + } + callback.success() + return true + } + + private fun feedbackConstant(kind: String): Int? = when (kind) { + "tick" -> tick() + "edge" -> reject() + "select" -> HapticFeedbackConstants.LONG_PRESS + "start" -> HapticFeedbackConstants.VIRTUAL_KEY + "end" -> sinceOMr1(HapticFeedbackConstants.VIRTUAL_KEY_RELEASE) + "confirm" -> confirm() + else -> null + } + + // TEXT_HANDLE_MOVE and CLOCK_TICK are silent on many devices, so caret + // steps use the slider tick, falling back to the context-click tick. + private fun tick(): Int = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + HapticFeedbackConstants.SEGMENT_TICK + } else { + HapticFeedbackConstants.CONTEXT_CLICK + } + + private fun reject(): Int = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + HapticFeedbackConstants.REJECT + } else { + HapticFeedbackConstants.LONG_PRESS + } + + private fun confirm(): Int = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + HapticFeedbackConstants.CONFIRM + } else { + HapticFeedbackConstants.VIRTUAL_KEY + } + + private fun sinceOMr1(constant: Int): Int? = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) constant else null + + private companion object { + const val ACTION_FEEDBACK = "feedback" + } +} diff --git a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/ServiceName.kt b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/ServiceName.kt index 0c767084c1..5a43a83812 100644 --- a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/ServiceName.kt +++ b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/ServiceName.kt @@ -9,6 +9,7 @@ enum class ServiceName(val key: String) { NATIVE_HTTP("NativeHttpPlugin"), BUILD_INFO("BuildInfo"), CRASH_HANDLER("CrashHandler"), + CURSOR_CONTROLLER("CursorController"), CUSTOM_TABS("CustomTabs"), DEVICE("Device"), FILE("File"), diff --git a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/ServiceRegistry.kt b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/ServiceRegistry.kt index 2e1e97b58a..88db7755bc 100644 --- a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/ServiceRegistry.kt +++ b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/ServiceRegistry.kt @@ -4,6 +4,7 @@ import com.foxdebug.acode.auth.Authenticator import com.foxdebug.acode.browser.Plugin import com.foxdebug.acode.buildinfo.BuildInfo import com.foxdebug.acode.crashhandler.CrashHandler +import com.foxdebug.acode.cursor.CursorController import com.foxdebug.acode.customtabs.CustomTabsPlugin import com.foxdebug.acode.device.Device import com.foxdebug.acode.file.FileUtils @@ -28,6 +29,7 @@ object ServiceRegistry { ServiceDefinition(ServiceName.NATIVE_HTTP) { NativeHttpPlugin() }, ServiceDefinition(ServiceName.BUILD_INFO) { BuildInfo() }, ServiceDefinition(ServiceName.CRASH_HANDLER, loadOnStart = true) { CrashHandler() }, + ServiceDefinition(ServiceName.CURSOR_CONTROLLER) { CursorController() }, ServiceDefinition(ServiceName.CUSTOM_TABS) { CustomTabsPlugin() }, ServiceDefinition(ServiceName.DEVICE) { Device() }, ServiceDefinition(ServiceName.FILE, loadOnStart = true) { FileUtils() }, diff --git a/src/cm/cursorController/caretDrag.ts b/src/cm/cursorController/caretDrag.ts new file mode 100644 index 0000000000..a5e47fc9a6 --- /dev/null +++ b/src/cm/cursorController/caretDrag.ts @@ -0,0 +1,138 @@ +import { + EditorSelection, + type EditorState, + type SelectionRange, + type TransactionSpec, +} from "@codemirror/state"; + +interface Coords { + left: number; + right: number; + top: number; + bottom: number; +} + +export interface CaretDragView { + state: EditorState; + scrollDOM: { scrollLeft: number }; + defaultLineHeight: number; + dispatch(spec: TransactionSpec): void; + moveVertically(start: SelectionRange, forward: boolean): SelectionRange; + coordsAtPos(pos: number, side?: -1 | 1): Coords | null; + posAtCoords(coords: { x: number; y: number }, precise: false): number | null; +} + +export interface CaretDragResult { + moved: boolean; + blocked: boolean; + changedRow: boolean; +} + +/** + * The caret follows the finger 1:1, with no speed curve. A position at a + * soft-wrap break belongs to two visual rows, so ranges keep their `assoc` + * to stay on the row the finger is on. + */ +export default class CaretDrag { + #view: CaretDragView; + #anchor: number | null = null; + #startX: number | null = null; + #startScrollLeft = 0; + #originY = 0; + #lineOffset = 0; + + constructor(view: CaretDragView) { + this.#view = view; + this.#captureStart(0); + } + + set extend(value: boolean) { + this.#anchor = value ? this.#view.state.selection.main.anchor : null; + } + + update(offsetX: number, offsetY: number): CaretDragResult { + const view = this.#view; + if (this.#startX == null) this.#captureStart(offsetX); + const { main } = view.state.selection; + const lineHeight = view.defaultLineHeight || 1; + const targetLines = Math.round((offsetY - this.#originY) / lineHeight); + let range = EditorSelection.cursor(main.head, main.assoc); + let blocked = false; + let changedRow = false; + + while (this.#lineOffset !== targetLines) { + const forward = targetLines > this.#lineOffset; + const next = view.moveVertically(range, forward); + if (this.#sameRow(range, next)) { + blocked = true; + this.#originY = offsetY - this.#lineOffset * lineHeight; + break; + } + range = next; + changedRow = true; + this.#lineOffset += forward ? 1 : -1; + } + + const target = this.#cursorAtX(range, offsetX) ?? range; + const { head, assoc } = target; + const anchor = Math.min(this.#anchor ?? head, view.state.doc.length); + const sameSide = anchor !== head || assoc < 0 === main.assoc < 0; + if (anchor === main.anchor && head === main.head && sameSide) { + return { moved: false, blocked, changedRow }; + } + view.dispatch({ + selection: EditorSelection.create([ + EditorSelection.range(anchor, head, undefined, undefined, assoc), + ]), + scrollIntoView: true, + userEvent: "select", + }); + return { moved: true, blocked, changedRow }; + } + + #captureStart(offsetX: number): void { + const view = this.#view; + const { main } = view.state.selection; + const coords = this.#coords(main.head, main.assoc); + if (!coords) return; + this.#startX = coords.left - offsetX; + this.#startScrollLeft = view.scrollDOM.scrollLeft; + } + + #sameRow(from: SelectionRange, to: SelectionRange): boolean { + if (from.head === to.head && from.assoc < 0 === to.assoc < 0) return true; + const before = this.#coords(from.head, from.assoc); + const after = this.#coords(to.head, to.assoc); + if (!before || !after) { + const { doc } = this.#view.state; + return doc.lineAt(from.head).number === doc.lineAt(to.head).number; + } + return this.#sameTop(before, after); + } + + #cursorAtX(range: SelectionRange, offsetX: number): SelectionRange | null { + const view = this.#view; + const row = this.#coords(range.head, range.assoc); + if (!row || this.#startX == null) return null; + const scrolled = view.scrollDOM.scrollLeft - this.#startScrollLeft; + const pos = view.posAtCoords( + { + x: this.#startX + offsetX - scrolled, + y: (row.top + row.bottom) / 2, + }, + false, + ); + if (pos == null) return null; + const after = view.coordsAtPos(pos, 1); + const endOfRow = !!after && !this.#sameTop(after, row); + return EditorSelection.cursor(pos, endOfRow ? -1 : 0); + } + + #coords(pos: number, assoc: number): Coords | null { + return this.#view.coordsAtPos(pos, assoc < 0 ? -1 : 1); + } + + #sameTop(a: Coords, b: Coords): boolean { + return Math.abs(a.top - b.top) < (this.#view.defaultLineHeight || 1) / 2; + } +} diff --git a/src/cm/cursorController/feedback.ts b/src/cm/cursorController/feedback.ts new file mode 100644 index 0000000000..9b27b987e3 --- /dev/null +++ b/src/cm/cursorController/feedback.ts @@ -0,0 +1,45 @@ +import config from "lib/config"; +import appSettings from "lib/settings"; +import exec from "native/exec"; +import haptic from "utils/haptic"; + +export type CursorFeedback = + | "tick" + | "edge" + | "select" + | "start" + | "end" + | "confirm"; + +const TICK_INTERVAL_MS = 35; +const STRONG_FEEDBACK: CursorFeedback[] = ["edge", "select", "confirm"]; + +let lastTickTime = 0; + +/** + * Android plays the matching system haptic constant natively. Elsewhere the + * app's generic haptic stands in, with ticks throttled so quick drags don't buzz. + */ +export default function cursorFeedback(kind: CursorFeedback): void { + const { vibrateOnTap, cursorControllerHaptics } = appSettings.value; + if (!vibrateOnTap || !cursorControllerHaptics) return; + + const platformId = window.Bridge?.platformId; + if (platformId === "android") { + try { + exec(null, null, "CursorController", "feedback", [kind]); + } catch (error) { + console.error("Cursor controller feedback failed", error); + } + return; + } + + if (kind === "start" || kind === "end") return; + if (platformId !== "ios" && !STRONG_FEEDBACK.includes(kind)) return; + if (kind === "tick") { + const now = performance.now(); + if (now - lastTickTime < TICK_INTERVAL_MS) return; + lastTickTime = now; + } + haptic(config.VIBRATION_TIME); +} diff --git a/src/cm/touchSelectionMenu.js b/src/cm/touchSelectionMenu.js index 3b430abc85..f6adb68324 100644 --- a/src/cm/touchSelectionMenu.js +++ b/src/cm/touchSelectionMenu.js @@ -196,6 +196,7 @@ class TouchSelectionMenuController { #stateSyncRaf = 0; #isScrolling = false; #isPointerInteracting = false; + #isMenuHeld = false; #pointerSelectionSession = null; #readOnlyTapSession = null; #pendingPointerSelectionClick = null; @@ -311,6 +312,7 @@ class TouchSelectionMenuController { this.#menuRequested = false; this.#isPointerInteracting = false; this.#isScrolling = false; + this.#isMenuHeld = false; this.#clearMenuShowTimer(); cancelAnimationFrame(this.#stateSyncRaf); this.#stateSyncRaf = 0; @@ -365,6 +367,18 @@ class TouchSelectionMenuController { this.#hideMenu(); } + holdMenu(held) { + if (!this.#enabled || this.#canvas) return; + this.#isMenuHeld = !!held; + if (held) { + this.#clearMenuShowTimer(); + this.#cancelReadOnlyTap(); + this.#hideMenu(); + return; + } + if (this.#shouldShowMenu()) this.#scheduleMenuShow(MENU_SHOW_DELAY); + } + onScrollEnd() { if (!this.#enabled || this.#canvas || !this.#isScrolling) return; this.#isScrolling = false; @@ -405,6 +419,7 @@ class TouchSelectionMenuController { this.#menuRequested = false; this.#isPointerInteracting = false; this.#isScrolling = false; + this.#isMenuHeld = false; this.#clearMenuShowTimer(); this.#hideMenu(true); if (this.#canvas) this.#view.touchSelection?.hideMenu(); @@ -689,7 +704,9 @@ class TouchSelectionMenuController { } #shouldShowMenu() { - if (this.#isScrolling || this.#isPointerInteracting) return false; + if (this.#isScrolling || this.#isPointerInteracting || this.#isMenuHeld) { + return false; + } if (!this.#view.hasFocus && !this.#isReadOnly()) return false; if ( !this.#menuRequested && diff --git a/src/components/cursorController/index.js b/src/components/cursorController/index.js new file mode 100644 index 0000000000..bef996619b --- /dev/null +++ b/src/components/cursorController/index.js @@ -0,0 +1,231 @@ +import "./style.scss"; +import CaretDrag from "cm/cursorController/caretDrag"; +import cursorFeedback from "cm/cursorController/feedback"; +import keyboardHandler from "handlers/keyboard"; +import { key, setQuicktoolsUsed } from "handlers/quickTools"; +import appSettings from "lib/settings"; +import CursorIndicator from "./indicator"; +import CursorKnob from "./knob"; + +const HOLD_TO_SELECT_MS = 350; +const HOLD_SLOP_PX = 6; +const EDGE_FEEDBACK_MS = 170; +const GUARDED_EVENTS = ["touchstart", "click", "contextmenu"]; +const PLACEMENT_EVENTS = ["switch-file", "file-loaded"]; + +/** + * A pointing-stick knob on the Quick Tools seam. Once a touch starts on it, + * the knob captures the pointer, so the finger keeps steering the caret + * wherever it travels, including over the soft keyboard. + */ +export default class CursorController { + #knob = new CursorKnob(); + #indicator = new CursorIndicator(); + #gesture = null; + #frame = 0; + + mount($footer) { + this.#knob.mount($footer); + const $knob = this.#knob.element; + $knob.addEventListener("pointerdown", this.#begin); + for (const type of GUARDED_EVENTS) { + $knob.addEventListener(type, stopEvent, { passive: false }); + } + + new MutationObserver(this.#place).observe($footer, { childList: true }); + editorManager.on(PLACEMENT_EVENTS, this.#onFileChange); + appSettings.on("update:cursorController", this.#place); + keyboardHandler.on("keyboardShow", this.#place); + keyboardHandler.on("keyboardHide", this.#place); + $footer.addEventListener("transitionend", this.#onFooterTransition); + new ResizeObserver(this.#place).observe(root); + document.addEventListener("focusin", this.#onFocusChange); + document.addEventListener("focusout", this.#onFocusChange); + key.on("shift", this.#place); + this.#place(); + } + + #place = () => { + cancelAnimationFrame(this.#frame); + this.#frame = requestAnimationFrame(() => { + this.#knob.place(this.#shouldShow()); + }); + }; + + #onFooterTransition = (event) => { + if (event.target === event.currentTarget) this.#place(); + }; + + #onFocusChange = (event) => { + if (getEditorView()?.dom.contains(event.target)) this.#place(); + }; + + #onFileChange = () => { + if (this.#gesture) this.#finish(); + this.#place(); + }; + + #shouldShow() { + if (this.#gesture) return true; + if (!appSettings.value.cursorController) return false; + const view = getEditorView(); + return !!view && (view.hasFocus || key.shift); + } + + #begin = (event) => { + stopEvent(event); + if (this.#gesture) return; + + const view = getEditorView(); + if (!view) { + cursorFeedback("edge"); + this.#knob.bump(); + return; + } + + const $knob = this.#knob.element; + $knob.setPointerCapture(event.pointerId); + const gesture = { + view, + drag: new CaretDrag(view), + touchSelection: getTouchSelectionController(), + pointerId: event.pointerId, + startX: event.clientX, + startY: event.clientY, + offsetX: 0, + offsetY: 0, + dirty: false, + selecting: key.shift, + selectionMade: false, + edgeLatched: false, + edgeAt: 0, + holdTimer: 0, + frame: 0, + }; + this.#gesture = gesture; + if (gesture.selecting) { + gesture.drag.extend = true; + } else { + gesture.holdTimer = setTimeout(this.#startSelection, HOLD_TO_SELECT_MS); + } + + $knob.addEventListener("pointermove", this.#onMove); + $knob.addEventListener("pointerup", this.#onEnd); + $knob.addEventListener("pointercancel", this.#onEnd); + $knob.addEventListener("lostpointercapture", this.#onEnd); + this.#knob.setActive(true); + this.#knob.setSelecting(gesture.selecting); + gesture.touchSelection?.holdMenu(true); + this.#indicator.show(view, gesture.selecting); + cursorFeedback("start"); + }; + + #onMove = (event) => { + const gesture = this.#gesture; + if (!gesture || event.pointerId !== gesture.pointerId) return; + gesture.offsetX = event.clientX - gesture.startX; + gesture.offsetY = event.clientY - gesture.startY; + gesture.dirty = true; + this.#knob.lean(event.clientX, event.clientY); + if ( + gesture.holdTimer && + Math.hypot(gesture.offsetX, gesture.offsetY) > HOLD_SLOP_PX + ) { + clearTimeout(gesture.holdTimer); + gesture.holdTimer = 0; + } + this.#requestStep(gesture); + }; + + #requestStep(gesture) { + if (!gesture.frame) gesture.frame = requestAnimationFrame(this.#tick); + } + + #tick = () => { + const gesture = this.#gesture; + if (!gesture) return; + gesture.frame = 0; + if (!gesture.dirty || gesture.holdTimer) return; + gesture.dirty = false; + this.#step(gesture); + }; + + #step(gesture) { + const { view } = gesture; + setQuicktoolsUsed(); + const { moved, blocked, changedRow } = gesture.drag.update( + gesture.offsetX, + gesture.offsetY, + ); + + if (changedRow) gesture.edgeLatched = false; + const hitEdge = blocked && !gesture.edgeLatched; + if (hitEdge) { + gesture.edgeLatched = true; + gesture.edgeAt = performance.now(); + cursorFeedback("edge"); + this.#knob.bump(); + } + if (!moved) return; + + if (gesture.selecting) gesture.selectionMade = true; + if (performance.now() - gesture.edgeAt > EDGE_FEEDBACK_MS) { + cursorFeedback("tick"); + } + this.#indicator.show(view, gesture.selecting); + } + + #startSelection = () => { + const gesture = this.#gesture; + if (!gesture) return; + gesture.holdTimer = 0; + gesture.selecting = true; + gesture.drag.extend = true; + cursorFeedback("select"); + this.#knob.setSelecting(true); + this.#indicator.show(gesture.view, true); + this.#requestStep(gesture); + }; + + #onEnd = (event) => { + if (event.pointerId === this.#gesture?.pointerId) this.#finish(); + }; + + #finish() { + const gesture = this.#gesture; + this.#gesture = null; + + const { view, pointerId } = gesture; + const $knob = this.#knob.element; + cancelAnimationFrame(gesture.frame); + clearTimeout(gesture.holdTimer); + $knob.removeEventListener("pointermove", this.#onMove); + $knob.removeEventListener("pointerup", this.#onEnd); + $knob.removeEventListener("pointercancel", this.#onEnd); + $knob.removeEventListener("lostpointercapture", this.#onEnd); + if ($knob.hasPointerCapture(pointerId)) + $knob.releasePointerCapture(pointerId); + + this.#knob.setActive(false); + this.#indicator.hide(); + + const selected = !view.state.selection.main.empty; + cursorFeedback(gesture.selectionMade && selected ? "confirm" : "end"); + gesture.touchSelection?.holdMenu(false); + this.#place(); + } +} + +function stopEvent(event) { + event.preventDefault(); + event.stopPropagation(); +} + +function getEditorView() { + const { editor, activeFile } = editorManager; + return activeFile?.type === "editor" ? editor : null; +} + +function getTouchSelectionController() { + return editorManager.activePane?.touchSelectionController; +} diff --git a/src/components/cursorController/indicator.js b/src/components/cursorController/indicator.js new file mode 100644 index 0000000000..07c009220c --- /dev/null +++ b/src/components/cursorController/indicator.js @@ -0,0 +1,75 @@ +const BADGE_GAP = 10; +const BADGE_EDGE = 8; +const HIDE_DELAY_MS = 150; + +/** + * A caret and a `line:column` label drawn over the editor while the cursor + * knob is held. The canvas editor hides its own caret while the soft + * keyboard is closed, so the knob draws one to stay visible either way. + */ +export default class CursorIndicator { + #element = ; + #caret = ; + #hideTimer = 0; + #active = false; + + show(view, selecting) { + clearTimeout(this.#hideTimer); + this.#active = true; + if (!this.#element.isConnected) { + document.body.append(this.#caret, this.#element); + } + const { state } = view; + const { main } = state.selection; + const line = state.doc.lineAt(main.head); + const column = main.head - line.from + 1; + const selected = main.to - main.from; + this.#element.textContent = selected + ? `${line.number}:${column} · ${selected}` + : `${line.number}:${column}`; + this.#element.dataset.selecting = String(selecting); + view.requestMeasure({ + key: "cursor-controller-badge", + read: () => ({ + coords: view.coordsAtPos(main.head, main.assoc < 0 ? -1 : 1), + width: this.#element.offsetWidth, + height: this.#element.offsetHeight, + }), + write: (measured) => this.#place(measured), + }); + } + + hide() { + this.#active = false; + this.#setVisible(false); + clearTimeout(this.#hideTimer); + this.#hideTimer = setTimeout(() => { + this.#element.remove(); + this.#caret.remove(); + }, HIDE_DELAY_MS); + } + + #place({ coords, width, height }) { + if (!this.#active) return; + if (!coords) { + this.#setVisible(false); + return; + } + const centerX = (coords.left + coords.right) / 2; + const left = Math.min( + Math.max(centerX - width / 2, BADGE_EDGE), + innerWidth - width - BADGE_EDGE, + ); + const above = coords.top - BADGE_GAP - height; + const top = above >= BADGE_EDGE ? above : coords.bottom + BADGE_GAP; + this.#element.style.transform = `translate(${left}px, ${top}px)`; + this.#caret.style.height = `${coords.bottom - coords.top}px`; + this.#caret.style.transform = `translate(${coords.left}px, ${coords.top}px)`; + this.#setVisible(true); + } + + #setVisible(visible) { + this.#element.classList.toggle("visible", visible); + this.#caret.classList.toggle("visible", visible); + } +} diff --git a/src/components/cursorController/knob.js b/src/components/cursorController/knob.js new file mode 100644 index 0000000000..55181dacf9 --- /dev/null +++ b/src/components/cursorController/knob.js @@ -0,0 +1,100 @@ +import quickTools from "components/quickTools"; + +const MAX_LEAN_PX = 3; +const LEAN_REACH_PX = 20; +const KNOB_RADIUS_PX = 10; +const BUMP_MS = 220; + +/** + * The cursor knob dot, centered on the seam between the Quick Tools + * rows. It floats over the rows, so the Quick Tools layout is untouched. + */ +export default class CursorKnob { + #element = ; + #footer = null; + #center = null; + #bumpTimer = 0; + + get element() { + return this.#element; + } + + mount($footer) { + this.#footer = $footer; + root.appendOuter(this.#element); + } + + place(visible) { + this.#apply(visible ? this.#measure() : null); + } + + lean(clientX, clientY) { + if (!this.#center) return; + const dx = reach(clientX - this.#center.x); + const dy = reach(clientY - this.#center.y); + const { style } = this.#element; + style.setProperty("--lean-x", `${dx * MAX_LEAN_PX}px`); + style.setProperty("--lean-y", `${dy * MAX_LEAN_PX}px`); + } + + setActive(active) { + this.#element.classList.toggle("active", active); + if (active) { + const rect = this.#element.getBoundingClientRect(); + this.#center = { + x: rect.left + rect.width / 2, + y: rect.top + rect.height / 2, + }; + return; + } + this.#center = null; + this.#element.style.removeProperty("--lean-x"); + this.#element.style.removeProperty("--lean-y"); + this.setSelecting(false); + } + + setSelecting(selecting) { + this.#element.dataset.selecting = String(selecting); + } + + bump() { + const $knob = this.#element; + $knob.classList.remove("bump"); + void $knob.offsetWidth; + $knob.classList.add("bump"); + clearTimeout(this.#bumpTimer); + this.#bumpTimer = setTimeout(() => $knob.classList.remove("bump"), BUMP_MS); + } + + #measure() { + const $footer = this.#footer; + const rows = [quickTools.$row1, quickTools.$row2].filter(($row) => + $footer.contains($row), + ); + if (!rows.length) return null; + + const rootRect = root.getBoundingClientRect(); + const footerRect = $footer.getBoundingClientRect(); + const firstRect = rows[0].getBoundingClientRect(); + const y = + rows.length > 1 ? firstRect.bottom : firstRect.top + KNOB_RADIUS_PX; + return { + x: footerRect.left + footerRect.width / 2 - rootRect.left, + bottom: rootRect.bottom - y, + rows: rows.length, + }; + } + + #apply(layout) { + this.#element.classList.toggle("hidden", !layout); + if (!layout) return; + const { style, dataset } = this.#element; + style.left = `${layout.x}px`; + style.bottom = `${layout.bottom}px`; + dataset.rows = String(layout.rows); + } +} + +function reach(offset) { + return Math.max(-1, Math.min(1, offset / LEAN_REACH_PX)); +} diff --git a/src/components/cursorController/style.scss b/src/components/cursorController/style.scss new file mode 100644 index 0000000000..722be434cc --- /dev/null +++ b/src/components/cursorController/style.scss @@ -0,0 +1,184 @@ +.cursor-knob { + position: fixed; + left: 0; + bottom: 0; + width: 20px; + height: 20px; + margin: 0 0 -10px -10px; + border-radius: 50%; + z-index: 99; + touch-action: none; + user-select: none; + -webkit-user-select: none; + -webkit-tap-highlight-color: transparent; + background: rgba(127, 127, 127, 0.22); + transition: + opacity 180ms ease, + scale 220ms cubic-bezier(0.2, 0.9, 0.3, 1.3), + background-color 160ms ease, + box-shadow 160ms ease; + + &::before { + content: ""; + position: absolute; + top: 3px; + right: 3px; + bottom: 3px; + left: 3px; + border-radius: 50%; + background-color: var(--primary-text-color); + background-image: + linear-gradient(var(--primary-color) 0.5px, transparent 0.5px), + linear-gradient(90deg, var(--primary-color) 0.5px, transparent 0.5px); + background-size: 2.5px 2.5px; + background-position: center; + box-shadow: + 0 2px 4px var(--box-shadow-color), + 0 1px 1px rgba(0, 0, 0, 0.3); + transform: translate(var(--lean-x, 0), var(--lean-y, 0)) rotate(15deg) scale(var(--press, 1)); + transition: + transform 120ms ease, + border-radius 160ms ease; + } + + &::after { + content: ""; + position: absolute; + top: -8px; + right: -8px; + bottom: -8px; + left: -8px; + border-radius: 50%; + } + + &[data-rows="1"]::after { + top: 0; + } + + &.hidden { + opacity: 0; + scale: 0.4; + pointer-events: none; + transition: + opacity 140ms ease, + scale 140ms ease-in; + } + + &.active { + background: rgba(127, 127, 127, 0.36); + + &::before { + --press: 1.2; + transition: transform 40ms linear; + } + } + + &[data-selecting="true"] { + box-shadow: inset 0 0 0 1.5px var(--active-color); + + &::before { + border-radius: 2px; + } + } + + &.bump::before { + animation: cursor-knob-bump 220ms ease; + } +} + +@keyframes cursor-knob-bump { + + 0%, + 100% { + top: 3px; + bottom: 3px; + } + + 25% { + top: 1px; + bottom: 5px; + } + + 75% { + top: 5px; + bottom: 1px; + } +} + +.cursor-controller-badge { + position: fixed; + left: 0; + top: 0; + z-index: 1000; + pointer-events: none; + padding: 2px 8px; + border-radius: 10px; + font-size: 12px; + font-weight: 600; + line-height: 18px; + font-variant-numeric: tabular-nums; + white-space: nowrap; + color: var(--popup-text-color); + background: var(--popup-background-color); + border: 1px solid var(--active-color); + box-shadow: 0 2px 8px var(--box-shadow-color); + opacity: 0; + transition: opacity 120ms ease; + + &.visible { + opacity: 1; + } + + &[data-selecting="true"] { + color: var(--popup-background-color); + background: var(--active-color); + } +} + +.cursor-controller-caret { + position: fixed; + left: 0; + top: 0; + width: 2px; + margin-left: -1px; + z-index: 1000; + pointer-events: none; + border-radius: 1px; + background: var(--active-color); + opacity: 0; + transition: opacity 120ms ease; + + &.visible { + opacity: 1; + } +} + +@supports (background: color-mix(in srgb, red 50%, transparent)) { + .cursor-knob { + background: color-mix(in srgb, var(--active-color) 22%, transparent); + + &.active { + background: color-mix(in srgb, var(--active-color) 36%, transparent); + } + } + + .cursor-controller-badge { + border-color: color-mix(in srgb, var(--active-color) 45%, transparent); + } + + .cursor-controller-caret { + box-shadow: 0 0 6px 1px color-mix(in srgb, var(--active-color) 60%, transparent); + } +} + +@media (prefers-reduced-motion: reduce) { + + .cursor-knob, + .cursor-knob.hidden, + .cursor-knob::before, + .cursor-controller-badge, + .cursor-controller-caret { + transition: none !important; + animation: none !important; + } +} diff --git a/src/handlers/quickTools.js b/src/handlers/quickTools.js index 724ccbefa8..3210c00b6f 100644 --- a/src/handlers/quickTools.js +++ b/src/handlers/quickTools.js @@ -1298,7 +1298,7 @@ function getInput() { return input || editorManager.editor.contentDOM; } -function setQuicktoolsUsed() { +export function setQuicktoolsUsed() { clearTimeout(quickToolUsedTimeout); quickToolUsed = true; quickToolUsedTimeout = setTimeout(() => { diff --git a/src/handlers/quickToolsInit.js b/src/handlers/quickToolsInit.js index d408f0f715..4d645c494f 100644 --- a/src/handlers/quickToolsInit.js +++ b/src/handlers/quickToolsInit.js @@ -1,5 +1,6 @@ import { redoDepth, undoDepth } from "@codemirror/commands"; import { focusEditorIfEditable } from "cm/editorReadOnly"; +import CursorController from "components/cursorController"; import quickTools from "components/quickTools"; import { description } from "components/quickTools/items"; import { hideTooltip, showTooltip } from "components/tooltip"; @@ -172,6 +173,7 @@ export default function init() { }); root.append($footer); + new CursorController().mount($footer); if (appSettings.value.floatingButton) { root.appendOuter($toggler); } diff --git a/src/lang/ar-ye.json b/src/lang/ar-ye.json index eda2f7e23d..665ea75bca 100644 --- a/src/lang/ar-ye.json +++ b/src/lang/ar-ye.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/az-az.json b/src/lang/az-az.json index be5a5f71cd..b603c91519 100644 --- a/src/lang/az-az.json +++ b/src/lang/az-az.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/be-by.json b/src/lang/be-by.json index dd5fcdb871..f236cc5493 100644 --- a/src/lang/be-by.json +++ b/src/lang/be-by.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/bn-bd.json b/src/lang/bn-bd.json index 3cab84bfa8..ae2b8e47a1 100644 --- a/src/lang/bn-bd.json +++ b/src/lang/bn-bd.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/cs-cz.json b/src/lang/cs-cz.json index 7c604601bb..3732d19fbd 100644 --- a/src/lang/cs-cz.json +++ b/src/lang/cs-cz.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/de-de.json b/src/lang/de-de.json index 04e468ccef..c49c0faf9f 100644 --- a/src/lang/de-de.json +++ b/src/lang/de-de.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/en-us.json b/src/lang/en-us.json index 0bf2c9a648..070e702464 100644 --- a/src/lang/en-us.json +++ b/src/lang/en-us.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/es-sv.json b/src/lang/es-sv.json index b1a752fb8b..60ba172a57 100644 --- a/src/lang/es-sv.json +++ b/src/lang/es-sv.json @@ -932,5 +932,9 @@ "store network error": "No se pudo conectar con la tienda. Revisa tu conexión a internet e inténtalo de nuevo.", "store error code": "La solicitud a la tienda falló (código {code}). Inténtalo de nuevo.", "sponsor confirmation pending": "Tu patrocinio está registrado. La confirmación de la tienda sigue pendiente. Acode lo volverá a intentar cuando regreses a la aplicación o vuelvas a abrir la sección de Patrocinio. No se te cobrará de nuevo.", - "sponsor recovery failed": "No se pudo recuperar el patrocinio. Inténtalo de nuevo." + "sponsor recovery failed": "No se pudo recuperar el patrocinio. Inténtalo de nuevo.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/fr-fr.json b/src/lang/fr-fr.json index 8491266185..f6991ff8df 100644 --- a/src/lang/fr-fr.json +++ b/src/lang/fr-fr.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/he-il.json b/src/lang/he-il.json index 397432a21a..673bca14c1 100644 --- a/src/lang/he-il.json +++ b/src/lang/he-il.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/hi-in.json b/src/lang/hi-in.json index c42d690ec7..f5a4dfa15a 100644 --- a/src/lang/hi-in.json +++ b/src/lang/hi-in.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/hu-hu.json b/src/lang/hu-hu.json index 5cd5072958..5bb8282772 100644 --- a/src/lang/hu-hu.json +++ b/src/lang/hu-hu.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/id-id.json b/src/lang/id-id.json index 64c496c2d2..d9519209f5 100644 --- a/src/lang/id-id.json +++ b/src/lang/id-id.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/index.d.ts b/src/lang/index.d.ts index 5a9e316f1d..749f269771 100644 --- a/src/lang/index.d.ts +++ b/src/lang/index.d.ts @@ -936,6 +936,10 @@ declare type LangStrings = { "store error code": string; "sponsor confirmation pending": string; "sponsor recovery failed": string; + "cursor controller": string; + "cursor controller haptics": string; + "settings-info-app-cursor-controller": string; + "settings-info-app-cursor-controller-haptics": string; }; declare var strings: LangStrings; diff --git a/src/lang/ir-fa.json b/src/lang/ir-fa.json index 900f600852..5bdf99e707 100644 --- a/src/lang/ir-fa.json +++ b/src/lang/ir-fa.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/it-it.json b/src/lang/it-it.json index ba4afe554a..50dac92481 100644 --- a/src/lang/it-it.json +++ b/src/lang/it-it.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/ja-jp.json b/src/lang/ja-jp.json index 17b76161ac..dee0551880 100644 --- a/src/lang/ja-jp.json +++ b/src/lang/ja-jp.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/ko-kr.json b/src/lang/ko-kr.json index 9bbf394881..691c0dbadc 100644 --- a/src/lang/ko-kr.json +++ b/src/lang/ko-kr.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/ln-ln.json b/src/lang/ln-ln.json index 336ac7596b..f4cdd80180 100644 --- a/src/lang/ln-ln.json +++ b/src/lang/ln-ln.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/ml-in.json b/src/lang/ml-in.json index 554c7bc59f..62b1bf9e83 100644 --- a/src/lang/ml-in.json +++ b/src/lang/ml-in.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/mm-unicode.json b/src/lang/mm-unicode.json index 001df84dc1..3931cdd5ae 100644 --- a/src/lang/mm-unicode.json +++ b/src/lang/mm-unicode.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/mm-zawgyi.json b/src/lang/mm-zawgyi.json index 2ec79685a4..8305609019 100644 --- a/src/lang/mm-zawgyi.json +++ b/src/lang/mm-zawgyi.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/pl-pl.json b/src/lang/pl-pl.json index a79fb0369b..36e70703e0 100644 --- a/src/lang/pl-pl.json +++ b/src/lang/pl-pl.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/pt-br.json b/src/lang/pt-br.json index c4b66354c2..0d183b1d88 100644 --- a/src/lang/pt-br.json +++ b/src/lang/pt-br.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/pu-in.json b/src/lang/pu-in.json index c4d2eb269a..a83dc49528 100644 --- a/src/lang/pu-in.json +++ b/src/lang/pu-in.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/ru-ru.json b/src/lang/ru-ru.json index 8d4de5070d..8d1affc057 100644 --- a/src/lang/ru-ru.json +++ b/src/lang/ru-ru.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/tl-ph.json b/src/lang/tl-ph.json index f373f44e58..c7390a413d 100644 --- a/src/lang/tl-ph.json +++ b/src/lang/tl-ph.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/tr-tr.json b/src/lang/tr-tr.json index 9dcbe3fe09..a3ecf1de64 100644 --- a/src/lang/tr-tr.json +++ b/src/lang/tr-tr.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/uk-ua.json b/src/lang/uk-ua.json index f9d5e31530..e2f2469856 100644 --- a/src/lang/uk-ua.json +++ b/src/lang/uk-ua.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/uz-uz.json b/src/lang/uz-uz.json index 4844d21ef0..f4de3c31a9 100644 --- a/src/lang/uz-uz.json +++ b/src/lang/uz-uz.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/vi-vn.json b/src/lang/vi-vn.json index e7eb21db68..2315d0d628 100644 --- a/src/lang/vi-vn.json +++ b/src/lang/vi-vn.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/zh-cn.json b/src/lang/zh-cn.json index 1c29a9f169..d3e7c90680 100644 --- a/src/lang/zh-cn.json +++ b/src/lang/zh-cn.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/zh-hant.json b/src/lang/zh-hant.json index 9e34a551fa..eac50c076a 100644 --- a/src/lang/zh-hant.json +++ b/src/lang/zh-hant.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lang/zh-tw.json b/src/lang/zh-tw.json index 809feef4b2..e367b55ce7 100644 --- a/src/lang/zh-tw.json +++ b/src/lang/zh-tw.json @@ -932,5 +932,9 @@ "store network error": "The store could not connect. Check your internet connection and try again.", "store error code": "The store request failed (code {code}). Please try again.", "sponsor confirmation pending": "Your sponsorship is registered. Store confirmation is still pending. Acode will retry when you return to the app or reopen Sponsorship. You will not be charged again.", - "sponsor recovery failed": "Unable to recover sponsorship. Please try again." + "sponsor recovery failed": "Unable to recover sponsorship. Please try again.", + "cursor controller": "Cursor controller", + "cursor controller haptics": "Cursor controller haptics", + "settings-info-app-cursor-controller": "Show a pointing-stick knob between the Quick Tools rows. Drag it to move the caret exactly with your finger; hold it still first to select text.", + "settings-info-app-cursor-controller-haptics": "Feel a tick each time the cursor controller moves the caret. Also follows Vibrate on tap and the system touch feedback setting." } diff --git a/src/lib/settings.js b/src/lib/settings.js index 49274e8951..3b7a4435eb 100644 --- a/src/lib/settings.js +++ b/src/lib/settings.js @@ -152,6 +152,8 @@ class Settings { openFileListPos: this.OPEN_FILE_LIST_POS_HEADER, quickTools: 2, quickToolsTriggerMode: this.QUICKTOOLS_TRIGGER_MODE_TOUCH, + cursorController: true, + cursorControllerHaptics: true, appFont: "", editorFont: "Roboto Mono", vibrateOnTap: true, diff --git a/src/settings/appSettings.js b/src/settings/appSettings.js index 619cd2dd05..f962095615 100644 --- a/src/settings/appSettings.js +++ b/src/settings/appSettings.js @@ -203,6 +203,20 @@ export default function otherSettings() { info: strings["settings-info-app-quick-tools-trigger-mode"], category: categories.quickTools, }, + { + key: "cursorController", + text: strings["cursor controller"], + checkbox: values.cursorController, + info: strings["settings-info-app-cursor-controller"], + category: categories.quickTools, + }, + { + key: "cursorControllerHaptics", + text: strings["cursor controller haptics"], + checkbox: values.cursorControllerHaptics, + info: strings["settings-info-app-cursor-controller-haptics"], + category: categories.quickTools, + }, { key: "quickToolsSettings", text: strings["shortcut buttons"], diff --git a/tests/unit/cursorControllerCaretDrag.test.ts b/tests/unit/cursorControllerCaretDrag.test.ts new file mode 100644 index 0000000000..85298d37be --- /dev/null +++ b/tests/unit/cursorControllerCaretDrag.test.ts @@ -0,0 +1,192 @@ +import { + EditorSelection, + EditorState, + type SelectionRange, + type TransactionSpec, +} from "@codemirror/state"; +import CaretDrag from "cm/cursorController/caretDrag"; +import { describe, expect, it } from "vitest"; + +const CHAR_WIDTH = 8; +const LINE_HEIGHT = 20; + +describe("CaretDrag", () => { + it("puts the caret under the finger's horizontal offset", () => { + const view = createView("abcdef", 1); + const drag = new CaretDrag(view); + drag.update(CHAR_WIDTH * 2, 0); + expect(head(view)).toBe(3); + drag.update(-CHAR_WIDTH, 0); + expect(head(view)).toBe(0); + }); + + it("moves one line per line height of travel, with no speed-up", () => { + const view = createView("abc\ndef\nghi", 1); + const drag = new CaretDrag(view); + drag.update(0, LINE_HEIGHT * 0.4); + expect(head(view)).toBe(1); + drag.update(0, LINE_HEIGHT); + expect(head(view)).toBe(5); + drag.update(0, LINE_HEIGHT * 2); + expect(head(view)).toBe(9); + }); + + it("keeps the finger's column across short lines", () => { + const view = createView("abcdef\nab\nabcdef", 5); + const drag = new CaretDrag(view); + drag.update(0, LINE_HEIGHT); + expect(head(view)).toBe(9); + drag.update(0, LINE_HEIGHT * 2); + expect(head(view)).toBe(15); + }); + + it("extends from a fixed anchor while selecting", () => { + const view = createView("abcdef", 1); + const drag = new CaretDrag(view); + drag.extend = true; + drag.update(CHAR_WIDTH * 3, 0); + const { anchor, head: selectionHead } = view.state.selection.main; + expect([anchor, selectionHead]).toEqual([1, 4]); + }); + + it("reports the document edge and responds at once on the way back", () => { + const view = createView("abc\ndef", 1); + const drag = new CaretDrag(view); + expect(drag.update(0, -LINE_HEIGHT * 3).blocked).toBe(true); + expect(head(view)).toBe(1); + + drag.update(0, -LINE_HEIGHT * 2); + expect(head(view)).toBe(5); + }); + + it("reports a row change only when the caret changes rows", () => { + const view = createView("abc\ndef", 1); + const drag = new CaretDrag(view); + expect(drag.update(0, -LINE_HEIGHT).changedRow).toBe(false); + expect(drag.update(8, -LINE_HEIGHT).changedRow).toBe(false); + expect(drag.update(8, 0).changedRow).toBe(true); + }); + + it("moves between soft-wrapped rows without a false edge", () => { + const view = createWrappedView("abcd efghijkl mnopqrst", [5, 14], 13); + const drag = new CaretDrag(view); + expect(drag.update(0, -LINE_HEIGHT).blocked).toBe(false); + expect(head(view)).toBe(5); + expect(view.state.selection.main.assoc).toBe(-1); + + drag.update(0, 0); + expect(head(view)).toBe(13); + }); + + it("keeps a selection anchor inside a shrunken document", () => { + const view = createView("abc\ndef", 6); + const drag = new CaretDrag(view); + drag.extend = true; + view.state = EditorState.create({ + doc: "ab", + selection: EditorSelection.cursor(1), + }); + expect(() => drag.update(CHAR_WIDTH, 0)).not.toThrow(); + expect(view.state.selection.main.anchor).toBeLessThanOrEqual(2); + }); +}); + +function head(view: ReturnType) { + return view.state.selection.main.head; +} + +function createView(doc: string, cursor: number) { + const view = { + state: EditorState.create({ + doc, + selection: EditorSelection.cursor(cursor), + }), + scrollDOM: { scrollLeft: 0 }, + defaultLineHeight: LINE_HEIGHT, + dispatch(spec: TransactionSpec) { + view.state = view.state.update(spec).state; + }, + coordsAtPos(pos: number) { + const line = view.state.doc.lineAt(pos); + const left = (pos - line.from) * CHAR_WIDTH; + const top = (line.number - 1) * LINE_HEIGHT; + return { left, right: left, top, bottom: top + LINE_HEIGHT }; + }, + posAtCoords({ x, y }: { x: number; y: number }) { + const { doc: text } = view.state; + const number = Math.min( + text.lines, + Math.max(1, Math.floor(y / LINE_HEIGHT) + 1), + ); + const line = text.line(number); + const column = Math.min( + line.length, + Math.max(0, Math.round(x / CHAR_WIDTH)), + ); + return line.from + column; + }, + moveVertically(start: SelectionRange, forward: boolean) { + const { doc: text } = view.state; + const line = text.lineAt(start.head); + const number = line.number + (forward ? 1 : -1); + if (number < 1) return EditorSelection.cursor(0); + if (number > text.lines) return EditorSelection.cursor(text.length); + const target = text.line(number); + return EditorSelection.cursor( + Math.min(target.to, target.from + start.head - line.from), + ); + }, + }; + return view; +} + +function createWrappedView(text: string, breaks: number[], cursor: number) { + const starts = [0, ...breaks]; + const ends = [...breaks, text.length]; + const last = starts.length - 1; + const rowOf = (pos: number, side: number) => { + for (let row = 0; row <= last; row++) { + const atEnd = pos === ends[row] && (side < 0 || row === last); + const inside = pos >= starts[row] && pos < ends[row]; + if (atEnd || (inside && !(pos === starts[row] && side < 0 && row > 0))) + return row; + } + return last; + }; + const cursorInRow = (row: number, column: number) => { + const pos = starts[row] + Math.min(column, ends[row] - starts[row]); + return EditorSelection.cursor( + pos, + pos === ends[row] && row < last ? -1 : 0, + ); + }; + const view = { + state: EditorState.create({ + doc: text, + selection: EditorSelection.cursor(cursor), + }), + scrollDOM: { scrollLeft: 0 }, + defaultLineHeight: LINE_HEIGHT, + dispatch(spec: TransactionSpec) { + view.state = view.state.update(spec).state; + }, + coordsAtPos(pos: number, side = 1) { + const row = rowOf(pos, side); + const left = (pos - starts[row]) * CHAR_WIDTH; + const top = row * LINE_HEIGHT; + return { left, right: left, top, bottom: top + LINE_HEIGHT }; + }, + posAtCoords({ x, y }: { x: number; y: number }) { + const row = Math.min(last, Math.max(0, Math.floor(y / LINE_HEIGHT))); + return cursorInRow(row, Math.max(0, Math.round(x / CHAR_WIDTH))).head; + }, + moveVertically(start: SelectionRange, forward: boolean) { + const row = rowOf(start.head, start.assoc < 0 ? -1 : 1); + const next = row + (forward ? 1 : -1); + if (next < 0) return EditorSelection.cursor(0); + if (next > last) return EditorSelection.cursor(text.length); + return cursorInRow(next, start.head - starts[row]); + }, + }; + return view; +} diff --git a/tests/unit/quickToolsAdapterUi.test.js b/tests/unit/quickToolsAdapterUi.test.js index 8b156ff8f8..07e883857e 100644 --- a/tests/unit/quickToolsAdapterUi.test.js +++ b/tests/unit/quickToolsAdapterUi.test.js @@ -53,6 +53,11 @@ vi.mock("lib/settings", () => ({ })); vi.mock("lib/editorFile", () => ({ syncQuickToolsVisibility: vi.fn() })); vi.mock("components/quickTools/items", () => ({ description: {} })); +vi.mock("components/cursorController", () => ({ + default: class { + mount() {} + }, +})); vi.mock("components/tooltip", () => ({ hideTooltip: vi.fn(), showTooltip: vi.fn(), From 79edd2a43b1c56a8e7e270abdd1649904e5e959d Mon Sep 17 00:00:00 2001 From: Ajit Kumar Date: Thu, 3 Dec 2026 21:00:37 +0530 Subject: [PATCH 2/2] fix(cursor-controller): apply pending movement on release A pointerup cancelled the queued animation frame before its offsets reached the caret, so quick drags could leave the caret short or unmoved. Apply the pending step on release before cleaning up. Co-Authored-By: Claude Opus 5.5 --- src/components/cursorController/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/components/cursorController/index.js b/src/components/cursorController/index.js index bef996619b..47b47b39af 100644 --- a/src/components/cursorController/index.js +++ b/src/components/cursorController/index.js @@ -188,7 +188,13 @@ export default class CursorController { }; #onEnd = (event) => { - if (event.pointerId === this.#gesture?.pointerId) this.#finish(); + const gesture = this.#gesture; + if (event.pointerId !== gesture?.pointerId) return; + if (event.type === "pointerup" && gesture.dirty && !gesture.holdTimer) { + gesture.dirty = false; + this.#step(gesture); + } + this.#finish(); }; #finish() {