From 4904b7df7f0793654b0bc04c350cd73368db0d0f Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Thu, 26 Feb 2026 21:53:54 -0700 Subject: [PATCH] ctrl-d and ctrl-u for half scrolling --- src/cg/06-textEditor.cg | 43 +++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/cg/06-textEditor.cg b/src/cg/06-textEditor.cg index 7238a7e..215b632 100644 --- a/src/cg/06-textEditor.cg +++ b/src/cg/06-textEditor.cg @@ -1,6 +1,13 @@ textEditor = name \ # defaults = {}; # c = { ...defaults, ...config }; + scale = 2; + charH = 12; + charW = 5; + lineGap = 1; + charGap = 2; + lineH = charH * scale + lineGap; + buffersKey = "textEditorBuffers"; # load from staging buffers if it exists there. if not, load from source @@ -116,19 +123,9 @@ textEditor = name \ newState = clampCursor state.{ cursorCol = state.cursorCol + 1 }; { state = newState, emit = [] }; - # todo: scrollHalfDown = state \ (); - # todo: scrollHalfUp = state \ (); - { view = ctx \ - scale = 2; - charH = 12; - charW = 5; - lineGap = 1; - charGap = 2; - lineH = charH * scale + lineGap; - autoScroll = state \ cursorY = state.cursorRow * lineH; newScrollY = (cursorY < state.scrollY @@ -143,11 +140,21 @@ textEditor = name \ | False \ (cursorX + charW * scale > state.scrollX + ctx.w | True \ cursorX + charW * scale - ctx.w | False \ state.scrollX)); - state.{ scrollY = newScrollY, cursorY = cursorY, scrollX = newScrollX, cursorX = cursorX }; + state.{ scrollY = newScrollY, scrollX = newScrollX }; withScroll = result \ { state = autoScroll result.state, emit = result.emit }; + scrollHalfUp = state ctx \ ( + diff = floor ((ctx.h / 2) / lineH); + newRow = state.cursorRow - diff; + withScroll { state = clampCursor state.{ cursorRow = newRow }, emit = [] }); + + scrollHalfDown = state ctx \ ( + diff = floor ((ctx.h / 2) / lineH); + newRow = state.cursorRow + diff; + withScroll { state = clampCursor state.{ cursorRow = newRow }, emit = [] }); + ui.stateful { focusable = True, autoFocus = True, @@ -198,6 +205,9 @@ textEditor = name \ | Insert \ insertChar "i" state | Normal \ insertMode state) + | Key { key = "d", ctrl = True } \ scrollHalfDown state ctx + | Key { key = "u", ctrl = True } \ scrollHalfUp state ctx + | Key { key = "u" } \ (state.mode | Insert \ insertChar "u" state | Normal \ undo state) @@ -218,9 +228,6 @@ textEditor = name \ | Insert \ insertChar "A" state | Normal \ apply state) - | Key { key = "d", ctrl = True } \ scrollHalfDown state - | Key { key = "u", ctrl = True } \ scrollHalfUp state - | Key { key = "Escape" } \ escape state | Key { key = "Backspace" } \ (state.mode @@ -239,10 +246,12 @@ textEditor = name \ | _ \ { state = state, emit = [] }, view = state emit \ - _ = debug! "here" state; + cursorX = state.cursorCol * charW * scale + charGap * state.cursorCol; + cursorY = state.cursorRow * lineH; + cursor = ui.positioned { - x = state.cursorX, - y = state.cursorY, + x = cursorX, + y = cursorY, child = ui.rect { w = charW * scale, h = charH * scale, color = "rgba(255,255,255,0.5)" } };