opencode-scroll-mode 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -1
  3. package/package.json +2 -1
  4. package/tui.ts +110 -10
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 qufeiyan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -7,7 +7,7 @@ OpenCode TUI 的「对话滚动模式」插件:一键切换编辑 / 滚动两
7
7
  - `ctrl+[`(默认,可用 `toggleKey` 配置)在「编辑模式」与「滚动模式」之间切换。
8
8
  - 编辑模式:输入框保留原生 readline 键位。
9
9
  - 滚动模式:导航键位移动 transcript 中的行 / 字符游标;只有游标到达视口边缘时才让宿主滚动,并启用 vim 键位与搜索。
10
- - 滚动模式会**锁定输入框**:进入时让编辑器失焦、隐藏编辑光标,并全局吞掉按键,避免滚动时误把字符打进 prompt;退出时恢复焦点、光标与输入。对话框(搜索、选择等)获得焦点时自动放行。
10
+ - 滚动模式会**锁定输入框**:进入时让编辑器失焦、隐藏编辑光标,并全局吞掉按键,避免滚动时误把字符打进 prompt;退出时恢复焦点、光标与输入。当其他组件(命令面板、搜索、选择等对话框)获得焦点时,滚动键位会**整体挂起**,把键盘交给对话框,避免在面板里打字时后台游标乱跳。
11
11
  - 滚动全部委托给 OpenCode 内置的 `session.*` 命令,不自行计算偏移,避免与 transcript 分页冲突。
12
12
  - 状态存于内存:每次 TUI 启动重置,插件热重载时保留。
13
13
 
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "opencode-scroll-mode",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
+ "license": "MIT",
4
5
  "description": "OpenCode TUI scroll mode plugin — toggle edit/scroll states with vim keys, message search, and line-precise visual selection/copy",
5
6
  "type": "module",
6
7
  "exports": {
package/tui.ts CHANGED
@@ -496,6 +496,12 @@ export default Plugin.define({
496
496
  cursorCol: 0,
497
497
  },
498
498
  })
499
+ // True while a widget other than the composer owns focus (the command
500
+ // palette, search, selects, …). While true the scroll keymap is suspended
501
+ // so typing in that dialog doesn't move the background cursor.
502
+ const [focusState, setFocusState] = context.storage.memory("scroll-mode.focus", {
503
+ initial: { foreign: false },
504
+ })
499
505
 
500
506
  // ---- editor lock while scroll mode is on -------------------------------
501
507
  // Scroll mode owns the keyboard: the composer must not receive text, or
@@ -515,6 +521,7 @@ export default Plugin.define({
515
521
  let savedFocus: any = null
516
522
  let detachGuard: (() => void) | undefined
517
523
  let detachCursorGuard: (() => void) | undefined
524
+ let detachFocusGuard: (() => void) | undefined
518
525
 
519
526
  const focusedRenderable = (): any => {
520
527
  try {
@@ -576,6 +583,9 @@ export default Plugin.define({
576
583
  const exitScroll = () => {
577
584
  stopPoll()
578
585
  clearLineSelection()
586
+ setFocusState((draft: any) => {
587
+ if (draft.foreign) draft.foreign = false
588
+ })
579
589
  const target = savedFocus
580
590
  savedFocus = null
581
591
  restoreEditorCursor(target)
@@ -594,11 +604,66 @@ export default Plugin.define({
594
604
  }
595
605
  }
596
606
 
607
+ // Suspend everything scroll mode paints, without leaving scroll mode.
608
+ //
609
+ // The user-visible bug: while a dialog (command palette, prompt, select)
610
+ // owns focus, the transcript highlight could still be re-resolved against
611
+ // the live layout and repaint a different line, which reads as "the
612
+ // background cursor jumps". Exiting scroll mode entirely is not an option:
613
+ // exitScroll() calls focus() on the saved composer and would steal focus
614
+ // back from the dialog. So on dialog focus we tear down the visual layer
615
+ // and stop the poll, and on focus return we rebuild it.
616
+ //
617
+ // This also covers our own dialogs (search prompt, selects): typing there
618
+ // no longer has any chance of moving the transcript cursor.
619
+ const applyFocusState = (foreign: boolean) => {
620
+ if (!state.active) return
621
+ const changed = focusState.foreign !== foreign
622
+ if (changed) {
623
+ setFocusState((draft: any) => {
624
+ draft.foreign = foreign
625
+ })
626
+ }
627
+ if (!changed) return
628
+ if (foreign) {
629
+ // Dialog took focus: no poll, no selection, no cursor bar.
630
+ stopPoll()
631
+ lastKey = null
632
+ barPos = null
633
+ clearLineSelection()
634
+ try {
635
+ renderer?.requestRender?.()
636
+ } catch {
637
+ /* ignore */
638
+ }
639
+ } else {
640
+ // Dialog closed: rebuild the highlight from the current layout.
641
+ lastKey = null
642
+ startPoll()
643
+ }
644
+ }
645
+
646
+ // Imperative focus check, read by each command's run() at execution time.
647
+ // The layer's reactive suspension (focusState.foreign) does not reliably
648
+ // re-evaluate on every host, so we do not trust a stored flag here: the
649
+ // command itself re-reads the live focus the moment it fires. Returns true
650
+ // when a dialog (or any widget other than the saved composer) owns focus.
651
+ const dialogHasFocus = (): boolean => {
652
+ if (!state.active) return false
653
+ const focused = focusedRenderable()
654
+ return !!focused && focused !== savedFocus
655
+ }
656
+
597
657
  const onKey = (key: any) => {
598
658
  if (!state.active) return
599
659
  const focused = focusedRenderable()
600
- // A dialog or other widget has taken focus: let it receive input.
601
- if (focused && focused !== savedFocus) return
660
+ // A dialog or other widget has taken focus: suspend the scroll keymap and
661
+ // let it receive input. Sync here too (not only via focused_renderable)
662
+ // so the suspension is in place before the next keystroke reaches the
663
+ // dispatcher, even if the host doesn't emit the focus event.
664
+ const foreign = !!focused && focused !== savedFocus
665
+ applyFocusState(foreign)
666
+ if (foreign) return
602
667
  if (focused) hideEditorCursor(focused)
603
668
  try {
604
669
  key.preventDefault()
@@ -622,6 +687,16 @@ export default Plugin.define({
622
687
  }
623
688
  }
624
689
 
690
+ // Track whether a widget other than the composer owns focus. While scroll
691
+ // mode is active and a dialog (palette, search, select) has focus, the
692
+ // scroll keymap is suspended so the dialog receives every key and the
693
+ // background cursor doesn't jump while typing.
694
+ const onFocusChange = (current: any) => {
695
+ if (!state.active) return
696
+ const foreign = !!current && current !== savedFocus
697
+ applyFocusState(foreign)
698
+ }
699
+
625
700
  const toast = (message: string, variant?: "info" | "success" | "warning" | "error") => {
626
701
  try {
627
702
  context.ui.toast.show(variant ? { message, variant } : { message })
@@ -1446,7 +1521,9 @@ export default Plugin.define({
1446
1521
 
1447
1522
  const drawCursorBar = (buffer: any) => {
1448
1523
  try {
1449
- if (!state.active || !options.highlight) return
1524
+ // While a dialog owns focus the whole scroll-mode overlay is suspended
1525
+ // (see applyFocusState): no bar, no selection, no poll.
1526
+ if (!state.active || !options.highlight || focusState.foreign) return
1450
1527
  refreshBar()
1451
1528
  if (!barPos || !barRgba) return
1452
1529
  if (barPos.y < 0) return
@@ -1830,9 +1907,11 @@ export default Plugin.define({
1830
1907
 
1831
1908
  keymap.layer(() => {
1832
1909
  const active = state.active
1910
+ const suspended = focusState.foreign
1833
1911
 
1834
- const commands: any[] = [
1835
- {
1912
+ const commands: any[] = []
1913
+ if (!suspended) {
1914
+ commands.push({
1836
1915
  id: "scroll-mode.toggle",
1837
1916
  title: "Toggle scroll mode",
1838
1917
  group: "Scroll mode",
@@ -1858,10 +1937,10 @@ export default Plugin.define({
1858
1937
  toast(`Scroll mode: editor disabled · ctrl+u / ctrl+d scroll, ${options.toggleKey} back to edit`)
1859
1938
  }
1860
1939
  },
1861
- },
1862
- ]
1940
+ })
1941
+ }
1863
1942
 
1864
- if (active) {
1943
+ if (active && !suspended) {
1865
1944
  // Half-page scrolling. The cursor keeps its screen row while the text
1866
1945
  // scrolls under it, in both normal and visual mode.
1867
1946
  commands.push(
@@ -2132,11 +2211,23 @@ export default Plugin.define({
2132
2211
  }
2133
2212
  }
2134
2213
 
2214
+ // Wrap every command's run() so it re-reads live focus at execution time.
2215
+ // If a dialog owns focus we return false (continue keyboard dispatch) so
2216
+ // the key reaches the dialog untouched, and the scroll action never runs.
2217
+ // This bypasses the layer's reactive suspension, which is unreliable.
2218
+ const guarded = commands.map((c: any) => ({
2219
+ ...c,
2220
+ run: (...args: any[]) => {
2221
+ if (dialogHasFocus()) return false
2222
+ return c.run(...args)
2223
+ },
2224
+ }))
2225
+
2135
2226
  return {
2136
2227
  mode: "global",
2137
2228
  priority: 100,
2138
- commands,
2139
- bindings: commands.map((c) => c.id),
2229
+ commands: guarded,
2230
+ bindings: guarded.map((c) => c.id),
2140
2231
  }
2141
2232
  })
2142
2233
 
@@ -2169,6 +2260,14 @@ export default Plugin.define({
2169
2260
  /* ignore */
2170
2261
  }
2171
2262
  }
2263
+ renderer.on("focused_renderable", onFocusChange)
2264
+ detachFocusGuard = () => {
2265
+ try {
2266
+ renderer.off("focused_renderable", onFocusChange)
2267
+ } catch {
2268
+ /* ignore */
2269
+ }
2270
+ }
2172
2271
  }
2173
2272
  } catch {
2174
2273
  /* ignore */
@@ -2184,6 +2283,7 @@ export default Plugin.define({
2184
2283
  clearTimeout(restoreIfActive)
2185
2284
  detachGuard?.()
2186
2285
  detachCursorGuard?.()
2286
+ detachFocusGuard?.()
2187
2287
  try {
2188
2288
  renderer?.removePostProcessFn?.(drawCursorBar)
2189
2289
  } catch {