Skip to content

Commit

Permalink
Text editor cursor selection drawing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronsaldo committed Nov 30, 2023
1 parent 7c6fc50 commit a64d596
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
97 changes: 94 additions & 3 deletions package-sources/Morphic.Core/AbstractTextAreaMorph.sysmel
Expand Up @@ -65,6 +65,55 @@ public class AbstractTextAreaMorph superclass: BorderedMorph; definition: {
}.

event modifiers hasShift ifTrue: {
switch: event symbol withCases: #{
KeySymbol Backspace : {
model deletePrevious.
event wasHandled: true.
self changed
}.
KeySymbol Delete : {
model deleteNext.
event wasHandled: true.
self changed
}.
KeySymbol Return : {
model insertString: "\n".
event wasHandled: true.
self changed
}.
KeySymbol Left : {
model expandCursorLeft.
event wasHandled: true.
self changed
}.
KeySymbol Right : {
model expandCursorRight.
event wasHandled: true.
self changed
}.
KeySymbol Down : {
model expandCursorDown.
event wasHandled: true.
self changed
}.
KeySymbol Up : {
model expandCursorUp.
event wasHandled: true.
self changed
}.
KeySymbol Home : {
model expandCursorToStartOfLine.
event wasHandled: true.
self changed
}.
KeySymbol End : {
model expandCursorToEndOfLine.
event wasHandled: true.
self changed
}.
_: {}
}.

return: void
}.

Expand Down Expand Up @@ -140,6 +189,45 @@ public class AbstractTextAreaMorph superclass: BorderedMorph; definition: {

let fontFace := self fontFaceToUse.
let textColor := self colorToUse.
let fontWidth := fontFace width.
let fontHeight := fontFace height.
let textBuffer := model textBuffer.

let minXPosition := self localBounds min x.
let maxXPosition := self localBounds max x.

let cursorColor := Color cyan.
let cursorBackgroundColor := Color cyan.

model cursors do: {:(TextEditorCursor)each :: Void |
let startIndex := each startIndex.
let endIndex := each endIndex.
startIndex = endIndex ifTrue: {
return: void.
}.

let minIndex := startIndex min: endIndex.
let maxIndex := startIndex max: endIndex.

let startLine mutable := 0sz.
let startColumn mutable := 0sz.
let endLine mutable := 0sz.
let endColumn mutable := 0sz.

textBuffer forIndex: minIndex computeLine: startLine andColumn: startColumn.
textBuffer forIndex: maxIndex computeLine: endLine andColumn: endColumn.

let startPosition := (startColumn asFloat32*fontWidth) @ (startLine asFloat32*fontHeight).
let endPosition := (endColumn asFloat32*fontWidth) @ ((endLine + 1sz) asFloat32*fontHeight).

startLine = endLine ifTrue: {
nodes add: (builder rectangle: (RectangleF32 min: startPosition max: endPosition) color: cursorBackgroundColor)
} ifFalse: {
nodes add: (builder rectangle: (RectangleF32 min: startPosition max: maxXPosition@(startPosition y + fontHeight)) color: cursorBackgroundColor).
nodes add: (builder rectangle: (RectangleF32 min: minXPosition@(startPosition y + fontHeight) max: maxXPosition @ (endPosition y- fontHeight)) color: cursorBackgroundColor).
nodes add: (builder rectangle: (RectangleF32 min: minXPosition@(endPosition y - fontHeight) max: endPosition) color: cursorBackgroundColor)
}.
}.

model textBuffer stringSpansDo: {:(String)string :(Size)startIndex :(Size)endIndex :: Void |
nodes add: (builder string: string from: startIndex until: endIndex
Expand All @@ -148,11 +236,14 @@ public class AbstractTextAreaMorph superclass: BorderedMorph; definition: {
)
}.

let cursorExtent := 1f32 @ fontFace height.
let cursorColor := Color cyan.
let cursorExtent := 1f32 @ fontHeight.

model cursors do: {:(TextEditorCursor)each :: Void |
let cursorPosition := (each startColumn asFloat32*fontFace width)@(each startLine asFloat32*fontFace height).
let line mutable := 0sz.
let column mutable := 0sz.
textBuffer forIndex: each endIndex computeLine: line andColumn: column.

let cursorPosition := (column asFloat32*fontFace width)@(line asFloat32*fontHeight).
nodes add: (builder rectangle: (cursorPosition extent: cursorExtent) color: cursorColor)
}.

Expand Down
3 changes: 3 additions & 0 deletions package-sources/Morphic.Core/TextEditorCursor.sysmel
Expand Up @@ -12,6 +12,9 @@ public final class TextEditorCursor definition: {
public method endIndex => Size
:= textBuffer computeIndexForLine: endLine andColumn: endColumn.

public method isSelectionRange => Boolean
:= self startIndex ~= self endIndex.

public method insertString: (string: String) ::=> Void := {
let startIndex := self startIndex.
let endIndex := self endIndex.
Expand Down
36 changes: 36 additions & 0 deletions package-sources/Morphic.Core/TextEditorModel.sysmel
Expand Up @@ -58,6 +58,42 @@ public class TextEditorModel definition: {
}
}.

public method expandCursorUp => Void := {
cursors do: {:(TextEditorCursor)eachCursor :: Void |
eachCursor expandUp.
}
}.

public method expandCursorDown => Void := {
cursors do: {:(TextEditorCursor)eachCursor :: Void |
eachCursor expandDown.
}
}.

public method expandCursorLeft => Void := {
cursors do: {:(TextEditorCursor)eachCursor :: Void |
eachCursor expandLeft.
}
}.

public method expandCursorRight ::=> Void := {
cursors do: {:(TextEditorCursor)eachCursor :: Void |
eachCursor expandRight.
}
}.

public method expandCursorToStartOfLine ::=> Void := {
cursors do: {:(TextEditorCursor)eachCursor :: Void |
eachCursor expandToStartOfLine.
}
}.

public method expandCursorToEndOfLine ::=> Void := {
cursors do: {:(TextEditorCursor)eachCursor :: Void |
eachCursor expandToEndOfLine.
}
}.

public method moveCursorUp => Void := {
cursors do: {:(TextEditorCursor)eachCursor :: Void |
eachCursor moveUp.
Expand Down

0 comments on commit a64d596

Please sign in to comment.