Files
sml-projects/fcore/text-scroll.sml

46 lines
1.5 KiB
Standard ML
Raw Normal View History

structure TextScroll =
struct
structure TC = TextConstants
(* Preqreuisite: move buffer to cursorIdx *)
fun getScrollColumn (buffer, cursorIdx, windowWidth, prevScrollColumn) =
let
val startOfLine = Cursor.vi0 (buffer, cursorIdx)
val newColumn = cursorIdx - startOfLine
val howManyColumnsCanWeFit =
if windowWidth >= TC.textLineWidth then TC.textLineCount
else windowWidth div TC.xSpace
val howManyColumnsCanWeFit = howManyColumnsCanWeFit - 1
in
if newColumn < prevScrollColumn then
(* we are moving the cursor backwards
* so make sure that newColumn is on the left side *)
newColumn
else if newColumn > prevScrollColumn + howManyColumnsCanWeFit then
(* we are scrolling forwards *)
newColumn - howManyColumnsCanWeFit
else
(* we can display the current column without moving the scroll column
* so we do that *)
prevScrollColumn
end
fun getStartLine (prevLineNumber, cursorLine, windowHeight) =
if cursorLine <= prevLineNumber then
(* if cursorLine is prior or same as prevLineNumber,
* then use cursorLine as scroll-line-start *)
cursorLine
else
(* cursorLine > prevLineNumber *)
let
val howManyLinesWeCanFit = windowHeight div TC.ySpace
in
if prevLineNumber + howManyLinesWeCanFit >= cursorLine then
prevLineNumber
else
let val lineDifference = cursorLine - prevLineNumber
in prevLineNumber + lineDifference
end
end
end