From ae28d79b46b8d20113faafcae30e4ab5cd93ffb5 Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 11 Feb 2020 12:59:36 +0000 Subject: [PATCH] Scroll so editor cursor is always visible This took a bit to figure out. Also, this is the only point in this project where I use lenses. --- package.yaml | 1 + src/Forest/Client/NodeEditor.hs | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/package.yaml b/package.yaml index 63b9343..459cc1a 100644 --- a/package.yaml +++ b/package.yaml @@ -18,6 +18,7 @@ dependencies: - async - brick - containers +- microlens - optparse-applicative - text - text-zipper diff --git a/src/Forest/Client/NodeEditor.hs b/src/Forest/Client/NodeEditor.hs index 202b2c6..aefa448 100644 --- a/src/Forest/Client/NodeEditor.hs +++ b/src/Forest/Client/NodeEditor.hs @@ -15,6 +15,7 @@ import Brick.Widgets.Edit import qualified Data.Text as T import Data.Text.Zipper import qualified Graphics.Vty as Vty +import Lens.Micro import Forest.Client.ResourceName @@ -52,8 +53,13 @@ handleNodeEditorEvent event ne = do pure ne{neEditor = newEditor} renderNodeEditor :: NodeEditor -> Widget ResourceName -renderNodeEditor ne = vLimit height $ renderEditor renderFunc True $ neEditor ne +renderNodeEditor ne = makeVisible $ vLimit height $ renderEditor renderFunc True ed where + ed = neEditor ne + height = length $ getCurrentText ne renderFunc :: [T.Text] -> Widget ResourceName renderFunc = vBox . map (\t -> if T.null t then txt " " else txt t) + + (row, col) = cursorPosition $ ed ^. editContentsL + makeVisible = visibleRegion (Location (col, row)) (1, 1)